Пример #1
0
 /**
  * Expands GLOB resource patterns.
  * 
  * @param  string $resource Resource name.
  * @param  string $type    Type of the resource.
  * @return array
  */
 public function expand($resource, $type)
 {
     list($moduleName, $subDir, $filePattern) = $this->parseResourceName($resource);
     $resourceLocation = $moduleName . ':' . $subDir . ':';
     // read from application dir
     $appDir = rtrim($this->_application->getApplicationDir(), DS) . DS . 'Resources' . DS;
     // if a module name was specified then point to a subfolder with the module name
     if (!empty($moduleName)) {
         // check for module existence in the first place
         if (!$this->_application->hasModule($moduleName)) {
             throw new ResourceNotFoundException('There is no module "' . $moduleName . '" registered, so cannot find its resource.');
         }
         $appDir = $appDir . $moduleName . DS;
     }
     $appDir = $this->buildResourcePath($appDir, $type, $subDir, '');
     $appFiles = FilesystemUtils::glob($appDir . $filePattern, FilesystemUtils::GLOB_ROOTFIRST | GLOB_BRACE);
     $resources = array();
     foreach ($appFiles as $file) {
         $resources[] = $resourceLocation . substr($file, mb_strlen($appDir));
     }
     // now take care of the module dir
     if ($moduleName) {
         $module = $this->_application->getModule($moduleName);
         $moduleDir = rtrim($module->getModuleDir(), DS) . DS . 'Resources' . DS;
         $moduleDir = $this->buildResourcePath($moduleDir, $type, $subDir, '');
         $moduleFiles = FilesystemUtils::glob($moduleDir . $filePattern, GLOB_BRACE);
         foreach ($moduleFiles as $file) {
             $resources[] = $resourceLocation . substr($file, mb_strlen($moduleDir));
         }
     }
     $resources = array_unique($resources);
     return ArrayUtils::sortPaths($resources, true);
 }
Пример #2
0
 /**
  * Constructor.
  *
  * @param Application $application Application for this router.
  * @param CacheInterface $cache Cache in which routes definitions will be stored.
  * @param boolean $cacheEnabled [optional] Whether or not the cache is enabled. Default: `true`.
  * @param string $host [optional] Host to use when generating full URL's. Default: `localhost`.
  * @param string $protocol [optional] Protocol to use when generating full URL's. Default: `http://`.
  * @param int $port [optional] Port to use when generating full URL's. Default: `80`.
  */
 public function __construct(AbstractApplication $application, CacheInterface $cache, $cacheEnabled = true, $host = 'localhost', $protocol = 'http://', $port = 80)
 {
     $this->cache = $cache;
     $this->cache->setEnabled($cacheEnabled);
     $this->setHost($host);
     $this->setProtocol($protocol);
     $this->setPort($port);
     // read all routes from cache and if there aren't any then read them from application modules and write to cache
     $router = $this;
     $this->routes = $this->cache->get('routes', 0, function () use($router, $application) {
         foreach ($application->getModules() as $module) {
             $router->readModuleRoutes($module);
         }
         return $router->getRoutes();
     });
 }
Пример #3
0
 /**
  * Configure the application.
  */
 public function configure()
 {
     parent::configure();
     // copy config values to the container
     $config = $this->getConfig();
     $this->container->setParameter('log_error_file', $config->get('log_error_file'));
 }
Пример #4
0
 /**
  * Executes a command by parsing all the input options and arguments and passing them to the command.
  * 
  * @param string $name Name of the command to call.
  * @param InputInterface $input Input.
  * @param OutputInterface $ouput Output.
  */
 public function exec($name, InputInterface $input, OutputInterface $output)
 {
     if (!isset($this->commands[$name])) {
         throw new NotFoundException('The command "' . $name . '" was not found in the list of registered commands.');
     }
     $commandInfo = $this->commands[$name];
     $commandClass = $commandInfo['class'];
     $consoleCommand = $commandInfo['command'];
     // instantiate the command with appropriate DI
     $command = new $commandClass($this->application->getContainer(), $input, $output, $consoleCommand->getHelperSet());
     // parse options
     $command->setOptions($input->getOptions());
     // parse arguments
     $arguments = array();
     foreach ($input->getArguments() as $key => $argument) {
         // if key is command then ignore it
         if ($key === 'command') {
             continue;
         }
         $arguments[] = $argument;
     }
     // call!
     call_user_func_array(array($command, 'execute'), $arguments);
 }
Пример #5
0
 /**
  * Runs the application.
  */
 public function run()
 {
     if ($this->container->getParameter('mode') !== Framework::MODE_CONSOLE) {
         throw new \RuntimeException('Genry can only be run in console!');
     }
     parent::run();
 }
Пример #6
0
 /**
  * This method is called by Splot Framework during the run phase, so right before it will
  * handle a request or a CLI command.
  *
  * This is a place where all services from all modules have been registered and the whole app
  * is fully bootstrapped and fully configured so you can add some global application-wide
  * logic or behavior here.
  */
 public function run()
 {
     parent::run();
 }
Пример #7
0
 /**
  * Runs the application in context of the given mode and using the given runner function.
  *
  * The last argument is a callback that will be invoked by this method and it should contain
  * execution logic specific to the given mode.
  *
  * Returns whatever the runner returns (which should be a result of running the application).
  * 
  * @param  AbstractApplication $application Application to be ran.
  * @param  int                 $mode        Mode in which the application should be ran.
  *                                          One of the `Framework::MODE_*` constants.
  * @param  callable            $runner      Closure that is responsible for actually running
  *                                          the application in appropriate mode.
  * @return mixed
  */
 protected function doRunApplication(AbstractApplication $application, $mode, $runner)
 {
     $timer = new Timer();
     $container = $application->getContainer();
     // container can now know in what mode its running
     $container->setParameter('mode', $mode);
     // run modules and the app
     foreach ($application->getModules() as $module) {
         $module->run();
     }
     $application->run();
     // run using the passed runner
     $result = call_user_func($runner);
     // log benchmark data
     $time = $timer->stop();
     $memory = $timer->getMemoryUsage();
     $container->get('splot.logger')->debug('Application run phase in mode "{mode}" finished in {time} ms and used {memory} memory.', array('mode' => self::modeName($mode), 'env' => $container->getParameter('env'), 'debug' => $container->getParameter('debug'), 'time' => $time, 'memory' => StringUtils::bytesToString($memory), '@stat' => 'splot.run.' . self::modeName($mode), '@time' => $time, '@memory' => $memory));
     return $result;
 }