public function load($file_path, $values = array())
 {
     if (!file_exists($file_path) || false === ($data = file_get_contents($file_path))) {
         throw new RuntimeException(sprintf('Unable to read file: %s', $file_path));
     }
     $parser = new JsonParser();
     try {
         $data = Utils::toArray($parser->parse($this->doReplace($data, $values), true));
     } catch (\Exception $e) {
         throw new \Exception(sprintf('Unable to parse file %s: %s', $file_path, $e->getMessage()));
     }
     return $data;
 }
 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;
 }
Exemple #3
0
 protected function registerController($name, $controller, $mount = '')
 {
     if (!isset($controller['match'])) {
         throw new InvalidRouteConfigurationException(sprintf('Your route confugiration for %s is invalid. You need to provide a \'match\' key to match a url', $name));
     }
     if (!isset($controller['controller'])) {
         throw new InvalidRouteConfigurationException(sprintf('Your route confugiration for %s is invalid. You need to provide a \'controller\' key that maps to a function or class method', $name));
     }
     try {
         // create closure of controller class function
         $method = Utils::getClosure($controller['controller']);
     } catch (\Exception $e) {
         throw new InvalidRouteConfigurationException(sprintf('Your route configuration %s is invalid. %s', $name, $e->getMessage()));
     }
     $route = preg_replace(array('/\\/+/', '/\\/$/'), array('/', ''), $mount . $controller['match']);
     $route = preg_replace('/^\\//', '', $route);
     $route = $this->match($route, $method);
     // bind route to name
     $route->bind($name);
     // handle any defined request methods
     if (isset($controller['request_methods'])) {
         if (is_array($controller['request_methods'])) {
             $methods = implode('|', $controller['request_methods']);
         } else {
             $methods = $controller['request_methods'];
         }
         //@TODO: check format of methods string and that it is a string else throw InvalidRouteConfigurationException
         $route->method($methods);
     }
     // handle any defined converts
     if (isset($controller['convert'])) {
         if (!is_array($controller['convert'])) {
             throw new InvalidRouteConfigurationException(sprintf("Problem with your defined converts in route '%s' config. Please provide a key pair array e.g. \"param1\": \"\\Class::convertMethod\"", $name));
         }
         foreach ($controller['convert'] as $param => $convert_method) {
             try {
                 // create closure of controller class function
                 $method = Utils::getClosure($convert_method);
             } catch (\Exception $e) {
                 throw new InvalidRouteConfigurationException(sprintf('Your defined convert for param %s in route configuration %s is invalid. %s', $param, $name, $e->getMessage()));
             }
             $route->convert($param, $method);
         }
     }
     // handle any defined assertions
     if (isset($controller['assert'])) {
         if (!is_array($controller['assert'])) {
             throw new InvalidRouteConfigurationException(sprintf("Problem with your defined assertions in route '%s' config. Please provide a key pair array e.g. \"param1\": \"\\Class::assertMethod\"", $name));
         }
         foreach ($controller['assert'] as $param => $assertion) {
             $route->assert($param, $assertion);
         }
     }
     // handle any defined default values
     if (isset($controller['default_values'])) {
         if (!is_array($controller['default_values'])) {
             throw new InvalidRouteConfigurationException(sprintf("Problem with your defined default values in route '%s' config. Please provide a key pair array e.g. \"param1\": \"value\"", $name));
         }
         foreach ($controller['default_values'] as $param => $value) {
             $route->value($param, $value);
         }
     }
     if (isset($controller['response_headers']) && is_array($controller['response_headers'])) {
         $route->after(function (Request $request, Response $response) use($controller) {
             foreach ($controller['response_headers'] as $key => $value) {
                 $response->headers->set($key, $value);
             }
         });
     }
     $app = $this;
     // configure Route middleware
     if (isset($controller['middleware'])) {
         if (!is_array($controller['middleware'])) {
             throw new InvalidConfigurationException(sprintf('The defined middleware hooks configuration for route \'%s\' config is invalid. Please provide an array of functions to call.', $name));
         }
         foreach ($controller['middleware'] as $hook) {
             $route->{$hook}['type'](Utils::getClosure($hook['method'], function ($object) use($app) {
                 if ($object instanceof \Skip\ServiceContainerInterface) {
                     $object->setContainer($app);
                 }
             }));
         }
     }
 }
 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);
     }
 }
 public static function setParametersOn($object, $mappings, $hash_stack)
 {
     $reflect = new \ReflectionClass($object);
     $props = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC);
     foreach ($mappings as $param => $dep_key) {
         // check if $dep_key actually exists!
         // check if its a public parameter
         try {
             $prop = $reflect->getProperty($param);
         } catch (\ReflectionException $e) {
             $prop = null;
         }
         try {
             $magic_prop = $reflect->getMethod('__set');
         } catch (\ReflectionException $e) {
             $magic_prop = null;
         }
         if ($prop && $prop->isPublic() || $magic_prop && $magic_prop->isPublic()) {
             $object->{$param} = Utils::getArrayValue($hash_stack, $dep_key);
             continue;
         }
         // check if its a public set* or magic __call method
         $call = 'set' . ucwords(preg_replace('/_+/', ' ', strtolower($param)));
         $call = preg_replace('/\\s+/', '', $call);
         try {
             $method = $reflect->getMethod($call);
         } catch (\ReflectionException $e) {
             $method = null;
         }
         try {
             $magic_method = $reflect->getMethod('__call');
         } catch (\ReflectionException $e) {
             $magic_method = null;
         }
         if ($method && $method->isPublic() || $magic_method && $magic_method->isPublic()) {
             $object->{$call}(Utils::getArrayValue($hash_stack, $dep_key));
             continue;
         }
         // else throw error!
         throw new \Exception(sprintf('Unable to set param %s on object of type `%s`', $param, get_class($object)));
     }
 }
 protected function transformContext($context)
 {
     foreach ($this->context_transformers as $class) {
         $method = \Skip\Utils::getClosure($class . '::transform');
         $context = $method($this->app, $context);
     }
     return $context;
 }