/**
  * @param Configuration  $configuration
  * @param ServiceManager $serviceManager
  */
 public function __construct(Configuration $configuration, ServiceManager $serviceManager)
 {
     if (!$configuration->get('twig', null)) {
         return;
     }
     $cachePath = $configuration->getApplicationPath() . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
     $options = [];
     if ($configuration->get('cache.config.enabled', false)) {
         $options['cache'] = $cachePath;
     }
     $paths = $configuration->get('twig.paths', []);
     foreach ($paths as $offset => $path) {
         $paths[$offset] = dirname($configuration->getApplicationPath()) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . $path;
     }
     $loader = new \Twig_Loader_Filesystem($paths);
     $this->twig = new \Twig_Environment($loader, $options + $configuration->get('twig.options', []));
     if (isset($configuration->get('twig.options', [])['debug']) && $configuration->get('twig.options', [])['debug']) {
         $this->twig->addExtension(new \Twig_Extension_Debug());
     }
     foreach ($configuration->get('twig.helpers', []) as $functionName => $helperClass) {
         $func = new \Twig_SimpleFunction($functionName, function () use($helperClass, $serviceManager) {
             $instance = $serviceManager->load($helperClass);
             return call_user_func_array($instance, func_get_args());
         });
         $this->twig->addFunction($func);
     }
     foreach ($configuration->get('twig.filters', []) as $functionName => $helperClass) {
         $func = new \Twig_SimpleFilter($functionName, function () use($helperClass, $serviceManager) {
             $instance = $serviceManager->load($helperClass);
             return call_user_func_array($instance, func_get_args());
         });
         $this->twig->addFilter($func);
     }
 }
 /**
  * @return null
  */
 public function __invoke()
 {
     $requestMiddlewares = $this->configuration->get('middlewares', []);
     $routeMiddlewares = $this->configuration->get('routes', []);
     ksort($requestMiddlewares);
     $table = [];
     foreach ($requestMiddlewares as $priority => $className) {
         $table[] = ['Priority' => $priority, 'Class name' => $className, 'Route' => 'Any', 'When' => 'On Request'];
     }
     foreach ($routeMiddlewares as $routeName => $details) {
         if (!isset($details['middlewares']) || !is_array($details['middlewares'])) {
             continue;
         }
         foreach ($details['middlewares'] as $priority => $className) {
             $table[] = ['Priority' => $priority, 'Class name' => $className, 'Route' => $routeName, 'When' => 'Only on route match'];
         }
     }
     $this->climate->out('');
     $this->climate->yellowTable($table);
     $this->climate->out('');
 }
 /**
  * @return null
  */
 public function __invoke()
 {
     $services = $this->configuration->get('services', []);
     $table = [];
     foreach ($services as $providerClass) {
         /** @var ServiceProvider $provider */
         $provider = new $providerClass();
         $provides = $provider->provide();
         if (is_callable($provides)) {
             try {
                 $provides = $this->serviceManager->get(is_array($provider->alias()) ? $provider->alias()[0] : $provider->alias());
                 $provides = 'Factory => ' . get_class($provides);
             } catch (\Exception $e) {
                 $provides = 'Factory => (ERROR. Could not load by "Class")';
             }
         }
         $table[] = ['Class' => implode(',', is_array($provider->alias()) ? $provider->alias() : [$provider->alias()]), 'Provides' => $provides, 'Defined by' => $providerClass];
     }
     $this->climate->out('');
     $this->climate->yellowTable($table);
     $this->climate->out('');
 }
 /**
  * @param Configuration $configuration
  */
 public function __construct(Configuration $configuration)
 {
     $cacheOptions = $configuration->get('cache.application');
     if (!$cacheOptions['enabled']) {
         $this->enabled = false;
         return;
     }
     $options = !isset($cacheOptions['options']) ? [] : $cacheOptions['options'];
     if (!is_array($options)) {
         $options = [$options];
     }
     $driverClassName = 'Stash\\Driver\\' . $cacheOptions['driver'];
     $driver = new $driverClassName();
     $driver->setOptions($options);
     $this->pool = new Stash\Pool($driver);
 }
Beispiel #5
0
 /**
  * @param Configuration $configuration
  *
  * @return LeagueContainer
  */
 protected function prepareServiceManager(Configuration $configuration)
 {
     $container = $this->config->get('services', []);
     $serviceManager = new LeagueAdapter();
     foreach ($container as $className) {
         /** @var ServiceProvider $provider */
         $provider = new $className();
         if ($provider instanceof ServiceProvider) {
             throw new \InvalidArgumentException(sprintf('Provider %s must be ServiceProvider', $className));
         }
         $aliases = $provider->alias();
         if (!is_array($aliases)) {
             $aliases = [$aliases];
         }
         foreach ($aliases as $alias) {
             if ($provider->share()) {
                 $serviceManager->share($alias);
             }
             if ($provider instanceof ServiceManager\SetterInjectorProvider) {
                 $serviceManager->inflector($provider->inflector())->invokeMethod($provider->method(), [$alias]);
             }
             $provides = $provider->provide();
             if (!is_callable($provides)) {
                 $serviceManager->alias($alias, $provides);
                 continue;
             }
             $serviceManager->add($alias, function () use($provides, $serviceManager) {
                 return $serviceManager->call($provides);
             });
         }
     }
     // Add the ServiceManager and the Configuration
     $serviceManager->add(LeagueAdapter::class, $serviceManager);
     $serviceManager->add(ConfigurationContainer::class, $this->config);
     return $serviceManager;
 }
 /**
  * @param Configuration $config
  * @param Request       $request
  */
 public function __construct(Configuration $config, Request $request)
 {
     $this->request = $request;
     $container = $config->get('routes', []);
     if (!$container) {
         throw new \InvalidArgumentException('Routes must be defined in the configuration');
     }
     if ($config->get('cache.config.enabled', FALSE)) {
         $cachePath = $config->getApplicationPath() . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'router';
         if (file_exists($cachePath)) {
             $oldRouter = unserialize(file_get_contents($cachePath));
             foreach ($oldRouter as $prop => $value) {
                 $this->{$prop} = $value;
             }
             return;
         }
     }
     parent::__construct(new RouteCollection(new RouteFactory('Arhitect\\Router\\Adapter\\RouteAdapter')), new Generator());
     foreach ($container as $routeName => $details) {
         if (isset($details['middlewares'])) {
             if (is_array($details['middlewares'])) {
                 $this->middlewares[$routeName] = $details['middlewares'];
             }
         }
         if (isset($details['extends']) && FALSE != $details['extends']) {
             $path = [$routeName];
             $parent = isset($container[$details['extends']]) ? $container[$details['extends']] : NULL;
             $parentName = isset($container[$details['extends']]) ? $details['extends'] : NULL;
             while ($parent) {
                 $path[] = $parentName;
                 if (isset($parent['extends'])) {
                     $parentName = $parent['extends'];
                     $parent = $container[$parent['extends']];
                 } else {
                     $parent = NULL;
                 }
             }
             $call = [];
             $that = $this;
             foreach ($path as $offset => $item) {
                 if (!$call) {
                     $call[$item] = function ($router) use($that, $container, $item) {
                         /** @var Route $route */
                         $route = $router->add($item, $container[$item]['path']);
                         $container[$item]['path'] = $route->path;
                         $tmp = $that->addRoute($route->name, $container[$item]);
                         $router->offsetSet($route->name, $tmp);
                     };
                     continue;
                 }
                 if ($offset == count($path) - 1) {
                     break;
                 }
                 $call[$item] = function ($router) use($container, $item, $call, $offset, $path) {
                     $router->attach($item, $container[$item]['path'], $call[$path[$offset - 1]]);
                 };
             }
             $this->attach($path[$offset], $container[$path[$offset]]['path'], $call[$path[$offset - 1]]);
             continue;
         }
         if (!$this->offsetExists($routeName)) {
             $this->offsetSet($routeName, $this->addRoute($routeName, $details));
         }
     }
     if ($config->get('cache.config.enabled', FALSE)) {
         file_put_contents($cachePath, serialize($this));
     }
 }
Beispiel #7
0
 /**
  * @param Configuration $configuration
  * @param Request       $request
  */
 public function __construct(Configuration $configuration, Request $request)
 {
     $this->version = $configuration->get('twig.assets.version', time());
     $this->path = $configuration->get('twig.assets.path', $request->getBasePath());
 }