Ejemplo n.º 1
0
 private function configure(InputInterface $input)
 {
     preg_match_all('/(\\-\\-skip\\-[a-zA-Z0-9\\.\\-\\/=\\"\']+)/', (string) $input, $matches);
     $stringInput = new StringInput(implode(' ', $matches[0]));
     $stringInput->bind($this->getDefinition());
     extract(Environment::getVars($stringInput));
     $app = new Generic($app_path, $lib_path, $env, $user, 'console', $this->cacheConfig);
     $config = $app->getConfig();
     // add helper sets
     if (isset($config['console']['helpers']) && is_array($config['console']['helpers'])) {
         $set_array = array();
         foreach ($config['console']['helpers'] as $name => $helper) {
             $helper = new $helper();
             $set_array[$name] = $helper->getHelper($app, $this);
         }
         $helperSet = new \Symfony\Component\Console\Helper\HelperSet($set_array);
         $this->setHelperSet($helperSet);
     }
     // add commands
     if (isset($config['console']['commands']) && is_array($config['console']['commands'])) {
         foreach ($config['console']['commands'] as $command) {
             $sets = array();
             if (!is_string($command)) {
                 if (!isset($command['class'])) {
                     throw new \Exception('Commands with extra configuration needs a `class` parameter. No class parameter found.');
                 }
                 $sets = isset($command['set']) ? $command['set'] : $sets;
                 if (!is_array($sets)) {
                     throw new \Exception('Commands with a `sets` parameter must be a hash array of paramerter => service, where parameter is the un-camelised key paramerter and service is the key identifier of the dependant service.');
                 }
                 $command = $command['class'];
             }
             $command = new $command();
             Utils::setParametersOn($command, $sets, $app);
             if ($command instanceof \Skip\ServiceContainerInterface) {
                 $command->setContainer($app);
             }
             $this->add($command);
         }
     }
     $this->app = $app;
 }
Ejemplo n.º 2
0
 public static function configureServices(\Pimple $app, array $config)
 {
     foreach ($config as $service_name => $service) {
         $setup = function ($service_name, $service) use($app) {
             $arguments = '';
             $deps = array();
             if (isset($service['dependencies']) && is_array($service['dependencies']) && count($service['dependencies']) > 0) {
                 $deps = $service['dependencies'];
             }
             $class = $service['class'];
             $sets = isset($service['set']) ? $service['set'] : array();
             if (!is_array($sets)) {
                 throw new \Exception('Service with a `sets` parameter must be a hash array of paramerter => service, where parameter is the un-camelised key and service is the key identifier of the dependant service.');
             }
             $service_closure = function (\Pimple $app) use($class, $deps, $sets, $service_name) {
                 // instantiate
                 // $object = eval( $string );
                 $class = new \ReflectionClass($class);
                 $arguments = array();
                 foreach ($deps as $key) {
                     $arguments[] = Utils::getArrayValue($app, $key);
                 }
                 $instance = $class->newInstanceArgs($arguments);
                 if ($instance instanceof \Skip\ServiceContainerInterface) {
                     $instance->setContainer($app);
                 }
                 // handle any sets
                 try {
                     Utils::setParametersOn($instance, $sets, $app);
                 } catch (\Exception $e) {
                     // make it clearer
                     throw new \Exception('Web Application initialisation of \'' . $service_name . '\' service: ' . $e->getMessage());
                 }
                 return $instance;
             };
             $type = isset($service['type']) ? $service['type'] : null;
             switch ($type) {
                 case 'protect':
                     $app[$service_name] = $app->protect($service_closure);
                     break;
                 case 'factory':
                     $app[$service_name] = $service_closure;
                     break;
                 case 'share':
                 default:
                     $app[$service_name] = $app->share($service_closure);
                     break;
             }
         };
         $setup($service_name, $service);
     }
 }