public function hydrate(Jarvis $app) { $app['doctrine.cache'] = function () { return new VoidCache(); }; $app['doctrine.annotation.driver'] = function () { return new AnnotationDriver(new AnnotationReader()); }; $app['entyMgr'] = function (Jarvis $app) : EntityManagerInterface { $settings = $app['doctrine.settings']; $cache = $app['doctrine.cache']; $config = Setup::createConfiguration($settings['debug'], $settings['proxies_dir'], $cache); $driver = $app['doctrine.annotation.driver']; if (isset($settings['entities_paths'])) { $driver->addPaths((array) $settings['entities_paths']); } AnnotationRegistry::registerLoader('class_exists'); $config->setMetadataDriverImpl($driver); $config->setAutoGenerateProxyClasses($settings['debug']); $config->setMetadataCacheImpl($cache); $config->setResultCacheImpl($cache); $config->setQueryCacheImpl($cache); $entyMgr = EntityManager::create($settings['dbal'], $config); if (isset($app['doctrine.orm.entyMgr.decorator']) && is_string($fqcn = $app['doctrine.orm.entyMgr.decorator']) && is_subclass_of($fqcn, EntityManagerDecorator::class)) { $entyMgr = new $fqcn($entyMgr); } $entyMgr->getEventManager()->addEventListener([Events::preRemove, Events::postRemove, Events::prePersist, Events::postPersist, Events::preUpdate, Events::postUpdate, Events::postLoad, Events::preFlush, Events::onFlush, Events::postFlush, Events::onClear], new EventListener($app)); $app->broadcast(DoctrineReadyEvent::READY_EVENT, new DoctrineReadyEvent($entyMgr)); return $entyMgr; }; $app['db_conn'] = function ($app) { $app['entyMgr']->getConnection(); }; $app->lock(['entyMgr', 'db_conn', 'doctrine.annotation.driver']); }
/** * {@inheritdoc} */ public function hydrate(Jarvis $app) { $app['annoReader'] = function ($app) { $cache = null; if (isset($app['doctrine.cache'])) { $cache = new DoctrineCache($app['doctrine.cache']); } return new Reader(new Parser(), $cache ?: new ArrayCache()); }; $app->lock('annoReader'); $app->addReceiver(JarvisEvents::CONTROLLER_EVENT, function (ControllerEvent $event) use($app) { $app->request->attributes->add((array) $event->arguments()); if (!is_array($event->callback())) { return; } list($controller, $action) = $event->callback(); $annotations = array_merge($app->annoReader->getClassAnnotations($controller)->toArray(), $app->annoReader->getMethodAnnotations($controller, $action)->toArray()); $handlers = $app->find(self::ANNO_SERVICE_HANDLER_BASE_ID . '*'); foreach ($annotations as $annotation) { foreach ($handlers as $handler) { if ($handler instanceof AnnotationHandlerInterface && $handler->supports($annotation)) { $handler->handle($annotation); } } } $arguments = []; $reflectionMethod = new \ReflectionMethod($controller, $action); foreach ($reflectionMethod->getParameters() as $reflectionParam) { if ($app->request->attributes->has($name = $reflectionParam->getName())) { $arguments[$name] = $app->request->attributes->get($name); } } $event->setArguments($arguments); }); }
protected function mountServices(Jarvis $app) { $app['app'] = function () use($app) : Jarvis { return $app; }; $app['request'] = function (Jarvis $app) : Request { $request = Request::createFromGlobals(); $session = $request->getSession(); if (null === $session) { $settings = $app['session.settings'] ?? []; $storageClassname = $settings['session.storage.classname'] ?? NativeSessionStorage::class; unset($settings['session.storage.classname']); $session = new Session(new $storageClassname($settings)); } $request->setSession($session); return $request; }; $app['session'] = function (Jarvis $app) : Session { return $app['request']->getSession(); }; $app['router'] = function () : Router { return new Router(); }; $app['callbackResolver'] = function (Jarvis $app) : CallbackResolver { return new CallbackResolver($app); }; $app->lock(['app', 'request', 'session', 'router', 'callbackResolver']); }
/** * {@inheritdoc} */ public function hydrate(Jarvis $container) { $container['twig'] = function ($container) { $config = array_merge(['auto-reload' => true, 'debug' => $container->debug, 'strict_variables' => true], $container->settings->get('twig', [])); if (!isset($config['templates_paths'])) { throw new \LogicException('Parameter `templates_paths` is missing to configure Twig.'); } $loader = new \Twig_Loader_Filesystem($config['templates_paths'], $config); return new \Twig_Environment($loader); }; $container->lock('twig'); }
/** * {@inheritdoc} */ public function hydrate(Jarvis $app) { $app['twig'] = function (Jarvis $app) : \Twig_Environment { $settings = array_merge(['auto_reload' => true, 'debug' => $app['debug'], 'strict_variables' => true], (array) ($app['twig.settings'] ?? [])); if (!isset($settings['templates_paths'])) { throw new \LogicException('Parameter `templates_paths` is missing to configure Twig.'); } $twig = new \Twig_Environment(new \Twig_Loader_Filesystem($settings['templates_paths']), $settings); $app->broadcast(TwigReadyEvent::READY_EVENT, new TwigReadyEvent($twig)); return $twig; }; $app->lock('twig'); }
/** * {@inheritdoc} */ public function hydrate(Jarvis $jarvis) { $doctrineConfig = $jarvis->settings->get('doctrine', []); if (empty($doctrineConfig)) { throw new \LogicException('You need to configure DoctrineORM Skill.'); } /* DBAL */ $jarvis['dbal_config'] = new \Doctrine\DBAL\Configuration(); $jarvis->lock('dbal_config'); $jarvis['dbal_connection'] = function ($jarvis) use($doctrineConfig) { return \Doctrine\DBAL\DriverManager::getConnection($doctrineConfig['db'], $jarvis->dbal_config); }; /* Doctrine ORM */ $jarvis['doctrine_orm.entity_path'] = [$doctrineConfig['orm']['entity_path']]; $jarvis['doctrine_orm.debug'] = $doctrineConfig['orm']['debug']; $jarvis['doctrine.orm_config'] = function ($jarvis) { return \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration($jarvis['doctrine_orm.entity_path'], $jarvis['doctrine_orm.debug']); }; $jarvis['doctrine_orm.entity.manager'] = function ($jarvis) use($doctrineConfig) { return \Doctrine\ORM\EntityManager::create($doctrineConfig['db'], $jarvis['doctrine.orm_config']); }; }
public function testAccessToLockedValueAsJarvisAttribute() { $app = new Jarvis(); $app['foo'] = 'bar'; $app->lock('foo'); $this->assertSame('bar', $app->foo); }