/** * @param string $name service identifier * @param \Janeiro\Di\Container $container */ public function register($name, Container $container) { $session = new Session(hash('MD5', uniqid('', true))); if (isset($_SERVER[self::HEADER_SESSION_HASH])) { $hash = $_SERVER[self::HEADER_SESSION_HASH]; if (preg_match(self::MATCH_MD5, $hash)) { $session->setHash($hash)->start(); } } $container->set($name, $session); }
/** * @param string $name service identifier * @param \Janeiro\Di\Container $container */ public function register($name, Container $container) { $container->set($name, function () use($container, $name) { /** @var \Janeiro\Mvc\View $service */ $service = $container->resolve('\\Janeiro\\Mvc\\View'); foreach ($container['registry']->get('views') as $class => $suffixes) { $service->registerEngine($class, $suffixes); } return $service; }); }
/** * @param string $name service identifier * @param \Janeiro\Di\Container $container */ public function register($name, Container $container) { $container->set($name, function () use($container, $name) { $service = new Router(); $service->setNamespace($container['registry']->get('routing.namespace')); $service->setRoute(['module' => $container['registry']->get('routing.default.module'), 'controller' => $container['registry']->get('routing.default.controller'), 'action' => $container['registry']->get('routing.default.action')]); foreach ($container['registry']->get('routing.table') as $route) { foreach ($route as $pattern => $configuration) { $service->addRoute($pattern, $configuration); } } return $service; }); }
public function setup() { $container = new Container(); $container->set('view', function () use($container) { $view = new View($container); $view->registerEngine('Janeiro\\Mvc\\View\\Standard', '.php'); return $view; }, true)->set('router', function () use($container) { $router = new Router($container); $router->addRoute('/:module/:controller', ['module' => 1, 'controller' => 2, 'action' => 'index']); $router->addRoute('/:module/:controller/:action', ['module' => 1, 'controller' => 2, 'action' => 3]); $router->addRoute('/test/parameter/variable/:integer', ['module' => 'test', 'controller' => 'parameter', 'parameters' => ['variable' => 1]]); return $router; }, true); $this->dispatcher = new Dispatcher($container); $this->container = $container; }
/** * @param int $index * @return mixed */ private function resolveParameter($index) { $parameter = null; if ($this->container->has('registry')) { $key = sprintf('services.%s.parameters.%d', $this->name, $index); $parameter = $this->container->get('registry')->get($key, null); } return $parameter; }
/** * Create controller object * * @param \Janeiro\Request\AbstractRequest $request * @throws \Janeiro\Mvc\Exception * @return array */ private function getController(AbstractRequest $request) { if (false === ($target = $this->getControllerClassAndMethod())) { throw new Exception('Resource not found', Exception::HTTP_NOT_FOUND); } list($class, $method) = $target; $controllerReflector = $this->container->getFactory($class)->getReflection(); $methodAnnotation = $controllerReflector->getAnnotation('method', $method, $request->getMethod()); if (false === in_array($request->getMethod(), explode(',', $methodAnnotation))) { throw new Exception(sprintf('%s method not allowed', $request->getMethod()), Exception::HTTP_METHOD_NOT_ALLOWED); } $this->view->init($class, $method); return [$this->container->resolve($class, null), $method]; }
/** * Get instance of MVC view object * * @throws \Janeiro\Mvc\Exception * @return ViewInterface */ private function getViewObject() { $success = false; try { $renderer = $this->container->get($this->getView()); if ($renderer instanceof ViewInterface) { $success = true; } } catch (\Exception $e) { $renderer = null; } if (false === $success) { throw new Exception('Could not create view'); } return $renderer; }
public function testResolveWithAutoWiringParametersFromRegistry() { $config = ['services' => ['some_service' => ['class' => '\\Janeiro\\Di\\ServiceDummyWithParameters', 'parameters' => [0 => ['foo' => 'bar']]]]]; $container = new Container(new Registry($config)); $this->assertEquals('bar', $container->get('some_service')->getParameter('foo')); }
/** * @expectedException \Janeiro\Di\Exception * @expectedExceptionMessage Could not inject parameter #0 into "\Janeiro\Di\ServiceDummyWithParameters::__construct" */ public function testResolveWithAutoWiringException() { $this->container->resolve('\\Janeiro\\Di\\ServiceDummyWithParameters'); }