Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
6bddf29e
Commit
6bddf29e
authored
Dec 01, 2014
by
Dave Marshall
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add ViewListenerWrapper and tests
parent
86c02016
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
155 additions
and
24 deletions
+155
-24
src/Silex/Application.php
src/Silex/Application.php
+1
-3
src/Silex/ViewListenerWrapper.php
src/Silex/ViewListenerWrapper.php
+88
-0
tests/Silex/Tests/ApplicationTest.php
tests/Silex/Tests/ApplicationTest.php
+66
-21
No files found.
src/Silex/Application.php
View file @
6bddf29e
...
@@ -422,9 +422,7 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
...
@@ -422,9 +422,7 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
*/
*/
public
function
view
(
$callback
,
$priority
=
0
)
public
function
view
(
$callback
,
$priority
=
0
)
{
{
$this
->
on
(
KernelEvents
::
VIEW
,
function
(
GetResponseForControllerResultEvent
$event
)
use
(
$callback
)
{
$this
->
on
(
KernelEvents
::
VIEW
,
new
ViewListenerWrapper
(
$this
,
$callback
),
$priority
);
call_user_func
(
$callback
,
$event
);
},
$priority
);
}
}
/**
/**
...
...
src/Silex/ViewListenerWrapper.php
0 → 100644
View file @
6bddf29e
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Silex
;
use
Symfony\Component\HttpFoundation\Response
;
use
Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent
;
/**
* Wraps view listeners.
*
* @author Dave Marshall <dave@atst.io>
*/
class
ViewListenerWrapper
{
protected
$app
;
protected
$callback
;
/**
* Constructor.
*
* @param Application $app An Application instance
* @param callable $callback
*/
public
function
__construct
(
Application
$app
,
$callback
)
{
$this
->
app
=
$app
;
$this
->
callback
=
$callback
;
}
public
function
__invoke
(
GetResponseForControllerResultEvent
$event
)
{
$controllerResult
=
$event
->
getControllerResult
();
$this
->
callback
=
$this
->
app
[
'callback_resolver'
]
->
resolveCallback
(
$this
->
callback
);
if
(
!
$this
->
shouldRun
(
$controllerResult
))
{
return
;
}
$response
=
call_user_func
(
$this
->
callback
,
$controllerResult
,
$event
->
getRequest
());
if
(
$response
instanceof
Response
)
{
$event
->
setResponse
(
$response
);
}
else
{
$event
->
setControllerResult
(
$response
);
}
}
protected
function
shouldRun
(
$controllerResult
)
{
if
(
is_array
(
$this
->
callback
))
{
$callbackReflection
=
new
\ReflectionMethod
(
$this
->
callback
[
0
],
$this
->
callback
[
1
]);
}
elseif
(
is_object
(
$this
->
callback
)
&&
!
$this
->
callback
instanceof
\Closure
)
{
$callbackReflection
=
new
\ReflectionObject
(
$this
->
callback
);
$callbackReflection
=
$callbackReflection
->
getMethod
(
'__invoke'
);
}
else
{
$callbackReflection
=
new
\ReflectionFunction
(
$this
->
callback
);
}
if
(
$callbackReflection
->
getNumberOfParameters
()
>
0
)
{
$parameters
=
$callbackReflection
->
getParameters
();
$expectedControllerResult
=
$parameters
[
0
];
if
(
$expectedControllerResult
->
getClass
()
&&
(
!
is_object
(
$controllerResult
)
||
!
$expectedControllerResult
->
getClass
()
->
isInstance
(
$controllerResult
)))
{
return
false
;
}
if
(
$expectedControllerResult
->
isArray
()
&&
!
is_array
(
$controllerResult
))
{
return
false
;
}
if
(
method_exists
(
$expectedControllerResult
,
'isCallable'
)
&&
$expectedControllerResult
->
isCallable
()
&&
!
is_callable
(
$controllerResult
))
{
return
false
;
}
}
return
true
;
}
}
tests/Silex/Tests/ApplicationTest.php
View file @
6bddf29e
...
@@ -572,14 +572,25 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
...
@@ -572,14 +572,25 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
'ok'
,
$response
->
getContent
());
$this
->
assertEquals
(
'ok'
,
$response
->
getContent
());
}
}
public
function
testViewListener
()
public
function
testViewListener
WithPrimitive
()
{
{
$app
=
new
Application
();
$app
=
new
Application
();
$app
->
get
(
'/foo'
,
function
()
{
return
array
(
'ok'
);
});
$app
->
get
(
'/foo'
,
function
()
{
return
123
;
});
$app
->
view
(
function
(
$event
)
{
$app
->
view
(
function
(
$view
)
{
$view
=
$event
->
getControllerResult
();
return
new
Response
(
$view
);
});
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
$event
->
setResponse
(
new
Response
(
$view
[
0
]));
$this
->
assertEquals
(
'123'
,
$response
->
getContent
());
}
public
function
testViewListenerWithArrayTypeHint
()
{
$app
=
new
Application
();
$app
->
get
(
'/foo'
,
function
()
{
return
array
(
'ok'
);
});
$app
->
view
(
function
(
array
$view
)
{
return
new
Response
(
$view
[
0
]);
});
});
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
...
@@ -587,43 +598,77 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
...
@@ -587,43 +598,77 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this
->
assertEquals
(
'ok'
,
$response
->
getContent
());
$this
->
assertEquals
(
'ok'
,
$response
->
getContent
());
}
}
public
function
test
DefaultPriorityStringResponseTriggerViewListener
()
public
function
test
ViewListenerWithObjectTypeHint
()
{
{
$app
=
new
Application
();
$app
=
new
Application
();
$app
->
get
(
'/foo'
,
function
()
{
return
'not ok'
;
});
$app
->
get
(
'/foo'
,
function
()
{
return
(
object
)
array
(
'name'
=>
'world'
)
;
});
$app
->
view
(
function
(
$event
)
{
$app
->
view
(
function
(
\stdClass
$view
)
{
$event
->
setResponse
(
new
Response
(
'ok'
)
);
return
new
Response
(
'Hello '
.
$view
->
name
);
});
});
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
$this
->
assertEquals
(
'
ok
'
,
$response
->
getContent
());
$this
->
assertEquals
(
'
Hello world
'
,
$response
->
getContent
());
}
}
public
function
test
LowPriorityStringResponseDoesNotTriggerViewListener
()
public
function
test
ViewListenerWithCallableTypeHint
()
{
{
if
(
version_compare
(
phpversion
(),
'5.4.0'
,
'<'
))
{
$this
->
markTestSkipped
(
'Requires PHP >= 5.4.0'
);
}
$app
=
new
Application
();
$app
=
new
Application
();
$app
->
get
(
'/foo'
,
function
()
{
return
'ok'
;
});
$app
->
get
(
'/foo'
,
function
()
{
return
function
()
{
return
'world'
;
}
;
});
$app
->
view
(
function
(
$event
)
{
$app
->
view
(
function
(
callable
$view
)
{
$event
->
setResponse
(
new
Response
(
'not ok'
));
return
new
Response
(
'Hello '
.
$view
(
));
}
,
-
100
);
});
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
$this
->
assertEquals
(
'ok'
,
$response
->
getContent
());
$this
->
assertEquals
(
'Hello world'
,
$response
->
getContent
());
}
public
function
testViewListenersCanBeChained
()
{
$app
=
new
Application
();
$app
->
get
(
'/foo'
,
function
()
{
return
function
()
{
return
'world'
;
};
});
$app
->
view
(
function
(
callable
$view
)
{
return
(
object
)
array
(
'name'
=>
$view
());
});
$app
->
view
(
function
(
\stdClass
$view
)
{
return
array
(
'msg'
=>
'Hello '
.
$view
->
name
);
});
$app
->
view
(
function
(
array
$view
)
{
return
$view
[
'msg'
];
});
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
$this
->
assertEquals
(
'Hello world'
,
$response
->
getContent
());
}
}
public
function
test
ResponseInstanceResponseDoesNotTriggerViewListener
()
public
function
test
ViewListenersAreIgnoredIfNotSuitable
()
{
{
$app
=
new
Application
();
$app
=
new
Application
();
$app
->
get
(
'/foo'
,
function
()
{
return
new
Response
(
'ok'
);
});
$app
->
get
(
'/foo'
,
function
()
{
return
'Hello world'
;
});
$app
->
view
(
function
(
$event
)
{
$event
->
setResponse
(
new
Response
(
'not ok'
));
$app
->
view
(
function
(
callable
$view
)
{
throw
new
\Exception
(
"View listener was called"
);
});
$app
->
view
(
function
(
\stdClass
$view
)
{
throw
new
\Exception
(
"View listener was called"
);
});
$app
->
view
(
function
(
array
$view
)
{
throw
new
\Exception
(
"View listener was called"
);
});
});
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
$response
=
$app
->
handle
(
Request
::
create
(
'/foo'
));
$this
->
assertEquals
(
'
ok
'
,
$response
->
getContent
());
$this
->
assertEquals
(
'
Hello world
'
,
$response
->
getContent
());
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment