/** * Set the controller's service container. * * Instantiates an empty view if the container has a view resolver. * * @param Container $services */ public function setServiceContainer(Container $services) { $this->services = $services; if ($this->services->has('Darya\\View\\Resolver')) { $this->template = $this->services->resolve('Darya\\View\\Resolver')->create(); } }
/** * Register an autoloader for application classes. * * @param Container $container */ public function register(Container $container) { $configuration = $container->resolve('Darya\\Foundation\\Configuration'); $basePath = $container->get('path'); $namespace = $configuration->get('project.namespace', 'Application'); // Map the configured namespace to the application directory $autoloader = new Autoloader($basePath, array($namespace => 'application')); $autoloader->register(); $container->register(array('Darya\\Common\\Autoloader' => $autoloader)); }
/** * Register an SQL database connection with the service container. * * @param Container $container */ public function register(Container $container) { $container->register(array('Darya\\Database\\Connection' => function ($container) { $config = $container->config; $factory = new Factory(); $connection = $factory->create($config['database.type'], array('hostname' => $config['database.hostname'], 'username' => $config['database.username'], 'password' => $config['database.password'], 'database' => $config['database.database'])); $connection->setEventDispatcher($container->event); return $connection; })); }
/** * Alias ChromePhp to Chrome and, if debugging is configured, enable * display_errors. * * @param Container $container */ public function register(Container $container) { if (class_exists('ChromePhp') && !class_exists('Chrome')) { class_alias('ChromePhp', 'Chrome'); } $configuration = $container->resolve('Darya\\Foundation\\Configuration'); if (!$configuration->get('debug')) { return; } ini_set('display_errors', 1); }
/** * Register a Smarty view resolver with the service container. * * @param Container $container */ public function register(Container $container) { $container->register(array('Darya\\Smarty\\ViewResolver' => function ($container) { $basePath = $container->get('path'); $realBasePath = realpath("{$basePath}/views/smarty"); $viewResolver = new ViewResolver('Darya\\Smarty\\View', $realBasePath); $viewResolver->shareConfig(array('base' => $realBasePath, 'cache' => '../../storage/cache', 'compile' => '../../storage/views')); $viewResolver->share(array('config' => $container->config)); return $viewResolver; }, 'Darya\\View\\Resolver' => 'Darya\\Smarty\\ViewResolver')); }
/** * Register a router with the service container. * * @param Container $container */ public function register(Container $container) { $container->register(array('Darya\\Routing\\Router' => function ($container) { $config = $container->config; $routes = $config['routes'] ?: array('/:controller/:action/:params' => null, '/:controller/:params' => null, '/:action/:params' => null, '/' => null); $projectNamespace = $config['project.namespace'] ?: 'Application'; $defaultNamespace = "{$projectNamespace}\\Controllers"; $router = new Router($routes, array('namespace' => $defaultNamespace)); $router->base($config['base_url']); $router->setServiceContainer($container); $router->setEventDispatcher($container->resolve('Darya\\Events\\Dispatchable')); return $router; })); }
/** * Magic method that redirects static calls to the facade's related service. * * @param string $method * @param array $parameters * @return mixed */ public static function __callStatic($method, $parameters) { $service = static::getServiceName(); if (!static::$serviceContainer) { throw new RuntimeException('Tried to use a facade without setting a service container'); } $instance = static::$serviceContainer->resolve($service); if (!is_object($instance)) { throw new RuntimeException('Facade resolved non-object from the service container'); } if (!method_exists($instance, $method)) { throw new RuntimeException('Call to non-existent method "' . $method . '" on facade instance'); } return static::$serviceContainer->call(array($instance, $method), $parameters); }
/** * Helper method for instantiating classes. * * Instantiates the given class if it isn't already an object. Uses the * service container if available. * * @param mixed $class * @param array $arguments [optional] * @return object */ protected function create($class, $arguments) { if (!is_object($class) && class_exists($class)) { if ($this->services) { $class = $this->services->create($class, $arguments); } else { $reflection = new ReflectionClass($class); $class = $reflection->newInstanceArgs($arguments); } } return $class; }
/** * Register a configuration object, and any of its service aliases and * providers, with the container. * * @param Container $container */ public function register(Container $container) { $container->register(array('Darya\\Foundation\\Configuration' => function (Application $application) { $basePath = $application->basePath(); // Load the application's configuration $configuration = new Configuration(array("{$basePath}/config/config.default.php", "{$basePath}/config/config.php")); return $configuration; })); $configuration = $container->resolve('Darya\\Foundation\\Configuration'); // Register the configured aliases foreach ($configuration['aliases'] as $alias => $service) { $container->alias($alias, $service); } // Register the configured service providers if ($container instanceof Application) { foreach ($configuration['services'] as $service) { if (class_exists($service) && is_subclass_of($service, 'Darya\\Service\\Contracts\\Provider')) { $container->provide($container->create($service)); } } } }
/** * Register a global HTTP request, response and session with the container. * * @param Container $container */ public function register(Container $container) { $container->register(array('Darya\\Http\\Request' => function ($container) { return Request::createFromGlobals($container->resolve('Darya\\Http\\Session')); }, 'Darya\\Http\\Response' => new Response(), 'Darya\\Http\\Session' => new Session\Php())); }
/** * Register a database storage implementation with the service container. * * @param Container $container */ public function register(Container $container) { $container->register(array('Darya\\Database\\Storage' => function ($container) { return new Storage($container->resolve('Darya\\Database\\Connection')); }, 'Darya\\Storage\\Readable' => 'Darya\\Database\\Storage', 'Darya\\Storage\\Modifiable' => 'Darya\\Database\\Storage', 'Darya\\Storage\\Searchable' => 'Darya\\Database\\Storage', 'Darya\\Storage\\Queryable' => 'Darya\\Database\\Storage', 'Darya\\Storage\\Aggregational' => 'Darya\\Database\\Storage')); }
/** * Register an event dispatcher with the container. * * @param Container $container */ public function register(Container $container) { $container->register(array('Darya\\Events\\Dispatcher' => new Dispatcher(), 'Darya\\Events\\Dispatchable' => 'Darya\\Events\\Dispatcher')); }