Commit a3a784b2 authored by Igor Wiedler's avatar Igor Wiedler

[mount] refactor mounting to not expose an __invoke method

parent 79d0dc45
...@@ -215,7 +215,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -215,7 +215,7 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
{ {
$this['dispatcher']->addListener(Events::onSilexError, function(GetResponseForErrorEvent $event) use ($callback) { $this['dispatcher']->addListener(Events::onSilexError, function(GetResponseForErrorEvent $event) use ($callback) {
$exception = $event->getException(); $exception = $event->getException();
$result = $callback->__invoke($exception); $result = $callback($exception);
if (null !== $result) { if (null !== $result) {
$event->setStringResponse($result); $event->setStringResponse($result);
...@@ -263,10 +263,22 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -263,10 +263,22 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
*/ */
public function mount($prefix, $app) public function mount($prefix, $app)
{ {
$mountHandler = function (Request $request, $prefix) use ($app) {
if (is_callable($app)) {
$app = $app();
}
foreach ($app['controllers']->all() as $controller) {
$controller->getRoute()->setPattern(rtrim($prefix, '/').$controller->getRoute()->getPattern());
}
return $app->handle($request);
};
$prefix = rtrim($prefix, '/'); $prefix = rtrim($prefix, '/');
$this $this
->match($prefix.'/{path}', $app) ->match($prefix.'/{path}', $mountHandler)
->assert('path', '.*') ->assert('path', '.*')
->value('prefix', $prefix); ->value('prefix', $prefix);
} }
...@@ -290,21 +302,6 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -290,21 +302,6 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
return $this['kernel']->handle($request, $type, $catch); return $this['kernel']->handle($request, $type, $catch);
} }
/**
* Handles a Request when this application has been mounted under a prefix.
*
* @param Request $request A Request instance
* @param string $path The path info (without the prefix)
*/
public function __invoke(Request $request, $prefix)
{
foreach ($this['controllers']->all() as $controller) {
$controller->getRoute()->setPattern(rtrim($prefix, '/').$controller->getRoute()->getPattern());
}
return $this->handle($request);
}
/** /**
* Handles onCoreRequest events. * Handles onCoreRequest events.
*/ */
......
...@@ -37,17 +37,14 @@ class LazyApplication ...@@ -37,17 +37,14 @@ class LazyApplication
} }
/** /**
* Handles a Request when this application has been mounted under a prefix. * Returns the application.
*
* @param Request $request A Request instance
* @param string $path The path info (without the prefix)
*/ */
public function __invoke(Request $request, $prefix) public function __invoke()
{ {
if (!$this->app) { if (!$this->app) {
$this->app = require $this->appPath; $this->app = require $this->appPath;
} }
return $this->app->__invoke($request, $prefix); return $this->app;
} }
} }
...@@ -57,15 +57,14 @@ class FunctionalTest extends \PHPUnit_Framework_TestCase ...@@ -57,15 +57,14 @@ class FunctionalTest extends \PHPUnit_Framework_TestCase
public function testLazyMount() public function testLazyMount()
{ {
$mounted = function (Request $request, $prefix) { $mountedFactory = function () {
$mounted = new Application(); $mounted = new Application();
$mounted->get('/{name}', function ($name) { return new Response($name); }); $mounted->get('/{name}', function ($name) { return new Response($name); });
return $mounted;
return $mounted($request, $prefix);
}; };
$app = new Application(); $app = new Application();
$app->mount('/hello', $mounted); $app->mount('/hello', $mountedFactory);
$response = $app->handle(Request::create('/hello/Silex')); $response = $app->handle(Request::create('/hello/Silex'));
$this->assertEquals('Silex', $response->getContent()); $this->assertEquals('Silex', $response->getContent());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment