예제 #1
0
 public function init($configPath, ConfigurationInterface $configurationInterface, $configCachePath = null)
 {
     if (!is_dir($configPath)) {
         throw new \InvalidArgumentException("Config path must be a directory containing config file. Path given = " . $configPath);
     }
     $this->configPath = $configPath;
     $this->configCachePath = $configCachePath ?: $this->configPath . "/cache";
     $locator = new FileLocator([$this->configPath]);
     // read config.yml first
     $configResources = [];
     $yamlFiles = $locator->locate($this->configFilename, null, false);
     $rawData = [];
     foreach ($yamlFiles as $file) {
         $configResources[] = new FileResource(realpath($file));
         $config = Yaml::parse(file_get_contents($file));
         $rawData[] = $config;
     }
     $processor = new Processor();
     $this->configs = $processor->processConfiguration($configurationInterface, $rawData);
     if (!isset($this->configs['dir.config'])) {
         $this->configs['dir.config'] = $this->configPath;
     }
     $this->configDataProvider = new ArrayDataProvider($this->configs);
     // read container info
     $cacheFilePath = $this->configCachePath . "/container.php";
     $this->isDebugMode = $this->configDataProvider->getOptional('is_debug', ArrayDataProvider::BOOL_TYPE, true);
     $containerConfigCache = new ConfigCache($cacheFilePath, $this->isDebugMode);
     // refresh container if dirty
     if (!$containerConfigCache->isFresh()) {
         $builder = new ContainerBuilder();
         $builder->addCompilerPass(new SlimAppCompilerPass(static::class));
         $recursiveSetParameter = function (callable $recursiveCallback, ContainerBuilder $builder, array $configs, $prefix = 'app.') {
             foreach ($configs as $k => &$v) {
                 $builder->setParameter($prefix . "{$k}", $v);
                 if (is_array($v)) {
                     call_user_func($recursiveCallback, $recursiveCallback, $builder, $v, $prefix . "{$k}" . ".");
                 }
             }
         };
         call_user_func($recursiveSetParameter, $recursiveSetParameter, $builder, $this->configs);
         $loader = new YamlFileLoader($builder, $locator);
         $loader->load($this->serviceFilename);
         $builder->compile();
         $dumper = new PhpDumper($builder);
         $resources = $builder->getResources();
         $resources[] = new FileResource(__FILE__);
         $resources = array_merge($resources, $configResources);
         $containerConfigCache->write($dumper->dump(['class' => 'SlimAppCachedContainer', 'namespace' => __NAMESPACE__]), $resources);
         //mdebug("container dumped");
     }
     // create container instance
     /** @noinspection PhpIncludeInspection */
     require_once $cacheFilePath;
     /** @noinspection PhpUndefinedClassInspection */
     $this->container = new SlimAppCachedContainer();
     $this->container->get('app');
     // NOTE: loggers below will be overriden if running in console mode
     $logger = new LocalFileHandler($this->loggingPath, "%date%/%script%.log", $this->loggingLevel);
     $logger->install();
     $logger = new LocalErrorHandler($this->loggingPath, "%date%/%script%.error", $this->loggingLevel);
     $logger->install();
     //mdebug("SlimApp [%s] initialized", static::class);
 }
예제 #2
0
 protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
 {
     $name = $command->getName();
     $name = strtr($name, ":", ".");
     $logger = new LocalFileHandler($this->getLoggingPath(), "%date%/%script%.{$name}.log", $this->getLoggingLevel());
     $logger->install();
     $logger = new LocalErrorHandler($this->getLoggingPath(), "%date%/%script%.{$name}.error", $this->getLoggingLevel());
     $logger->install();
     return parent::doRunCommand($command, $input, $output);
 }