-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Silex activator and route builder
- Loading branch information
Thibaud Fabre
committed
Feb 28, 2015
1 parent
4b69bb3
commit ffcebc7
Showing
2 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Aztech\Layers\Phinject; | ||
|
||
use Aztech\Phinject\Activator; | ||
use Aztech\Phinject\ConfigurationAware; | ||
use Aztech\Phinject\Container; | ||
use Aztech\Phinject\Util\ArrayResolver; | ||
use Silex\Application; | ||
|
||
class SilexActivator implements Activator, ConfigurationAware | ||
{ | ||
|
||
private $activatorKey; | ||
|
||
/** | ||
* @see \Aztech\Phinject\ConfigurationAware::setConfiguration() | ||
*/ | ||
public function setConfiguration(ArrayResolver $configurationNode) { | ||
$this->activatorKey = $configurationNode->resolve('key'); | ||
} | ||
|
||
/** | ||
* @see \Aztech\Phinject\Activator::createInstance() | ||
*/ | ||
public function createInstance(Container $container, ArrayResolver $serviceConfig, $serviceName) | ||
{ | ||
$nodeConfig = $serviceConfig->resolve($this->activatorKey); | ||
|
||
$silex = new Application($nodeConfig->extract()); | ||
|
||
foreach ($serviceConfig->resolveArray('providers', []) as $provider => $values) { | ||
$silex->register(new $provider(), $values->extract()); | ||
} | ||
|
||
$this->bindRoutes($container, $silex, $serviceConfig); | ||
|
||
return $silex; | ||
} | ||
|
||
private function bindRoutes(Container $container, Application $application, ArrayResolver $serviceConfig) | ||
{ | ||
$routeBuilder = new SilexRouteBuilder($application, $container); | ||
|
||
$routeBuilder->setDefaultRoute($serviceConfig->resolve('routes.default', '')); | ||
$routeBuilder->setErrorRoute($serviceConfig->resolve('routes.error', '')); | ||
|
||
$routeBuilder->bindRoutes(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
namespace Aztech\Layers\Phinject; | ||
|
||
use Aztech\Layers\Layer; | ||
use Aztech\Phinject\Injector; | ||
use Aztech\Phinject\ObjectContainer; | ||
use Aztech\Phinject\Util\ArrayResolver; | ||
use Silex\Application; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class SilexRouteBuilder | ||
{ | ||
|
||
private $application; | ||
|
||
private $container; | ||
|
||
private $hasDefaultController = false; | ||
|
||
private $defaultControllerKey = null; | ||
|
||
private $hasErrorController = false; | ||
|
||
private $errorControllerKey = null; | ||
|
||
public function __construct(Application $application, ObjectContainer $container) | ||
{ | ||
$this->application = $application; | ||
$this->container = $container; | ||
} | ||
|
||
public function setErrorRoute($errorControllerKey) | ||
{ | ||
$this->errorControllerKey = (string) $errorControllerKey; | ||
$this->hasErrorController = true; | ||
} | ||
|
||
public function setDefaultRoute($controllerKey, $disable = false) | ||
{ | ||
$this->defaultControllerKey = (string) $controllerKey; | ||
$this->hasDefaultController = ! empty($this->defaultControllerKey) && ! $disable; | ||
} | ||
|
||
public function bindRoutes() | ||
{ | ||
$config = $this->container->getGlobalConfig(); | ||
|
||
foreach ($config->resolve('classes') as $serviceName => $serviceConfig) { | ||
$this->bindRoute($serviceConfig, $serviceName); | ||
} | ||
|
||
if ($this->hasDefaultController) { | ||
$this->application->match('{url}', $this->deferExecution($this->defaultControllerKey)) | ||
->assert('url', '.+'); | ||
} | ||
|
||
if ($this->application['debug'] == false && $this->hasErrorController) { | ||
$this->application->error($this->getErrorHandler()); | ||
} | ||
} | ||
|
||
public function bindRoute(ArrayResolver $serviceConfig, $serviceName) | ||
{ | ||
$routePath = $serviceConfig->resolve('route.path', $serviceConfig->resolve('route')); | ||
$routeName = $serviceConfig->resolve('route.name', $serviceName); | ||
|
||
$methods = array_values($serviceConfig->resolveArray('route.methods', [ 'get' ])->extract()); | ||
$asserts = $serviceConfig->resolveArray('route.assert', [ ])->extract(); | ||
|
||
if (! $routePath || ! is_scalar($routePath)) { | ||
return; | ||
} | ||
|
||
foreach ($methods as $index => $method) { | ||
$route = $this->application->{$method}($routePath, $this->deferExecution($serviceName)); | ||
|
||
if ($routeName) { | ||
$routeName = $index > 0 ? $routeName . ':' . $method : $routeName; | ||
$route->bind($routeName); | ||
} | ||
|
||
foreach ($asserts as $param => $assert) { | ||
$route->assert($param, $assert); | ||
} | ||
} | ||
} | ||
|
||
private function deferExecution($serviceName) | ||
{ | ||
return function (Request $request) use ($serviceName) { | ||
$controller = $this->container->get($serviceName); | ||
|
||
return $controller($request); | ||
}; | ||
} | ||
|
||
private function getErrorHandler() | ||
{ | ||
$container = $this->container; | ||
$errorCallback = null; | ||
|
||
if ($this->hasErrorController) { | ||
$errorCallback = $this->errorControllerKey; | ||
} | ||
|
||
return function (\Exception $exception, $code) use ($container, $errorCallback) { | ||
if ($errorCallback != null) { | ||
$controller = $container->resolve($errorCallback); | ||
|
||
return $controller(Request::createFromGlobals()); | ||
} | ||
}; | ||
} | ||
} |