Skip to content

Instantly share code, notes, and snippets.

@GeeH
Created August 14, 2016 12:53
Show Gist options
  • Save GeeH/54a8939e64051114347eb8533c52be01 to your computer and use it in GitHub Desktop.
Save GeeH/54a8939e64051114347eb8533c52be01 to your computer and use it in GitHub Desktop.
<?php
namespace App;
use App\Action\HomePageAction;
use App\Action\HomePageFactory;
use App\Action\PingAction;
use Zend\Expressive\Router\FastRouteRouter;
use Zend\Expressive\Router\RouterInterface;
class ConfigProvider
{
function __invoke() : array
{
return [
'dependencies' => $this->getDependencyConfig(),
'routes' => $this->getRouteConfig(),
];
}
private function getDependencyConfig() : array
{
return [
'invokables' => [
RouterInterface::class => FastRouteRouter::class,
PingAction::class => PingAction::class,
],
];
}
private function getRouteConfig() : array
{
$routeConfig = [];
$actions = $this->getValidActions();
foreach ($actions as $action) {
$className = 'App\\Action\\' . $action;
$defined = defined("{$className}::ROUTE_NAME")
&& defined("{$className}::ROUTE_PATH")
&& defined("{$className}::ROUTE_METHODS");
if($defined) {
$routeConfig[] = $this->addRouteConfigForAction($className);
}
}
return $routeConfig;
}
/**
* @return array
*/
private function getValidActions() : array
{
$classes = [];
$actionList = glob('src/App/Action/*Action.php');
foreach ($actionList as $action) {
preg_match(
'/^src\/App\/Action\/(?\'class\'[A-Z][a-z]*Action).php$/',
$action,
$matches
);
if (isset($matches['class'])) {
$classes[] = $matches['class'];
}
}
return $classes;
}
private function addRouteConfigForAction(string $className) : array
{
return [
'name' => $className::ROUTE_NAME,
'path' => $className::ROUTE_PATH,
'middleware' => $className,
'allowed_methods' => $className::ROUTE_METHODS,
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment