/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return Container Returns itself to support chaining. */ public function register(Container $container) { // Application $container->alias('app', 'JApplicationCms')->share('JApplicationCms', array('JFactory', 'getApplication')); // Document $container->alias('document', 'JDocumentHtml')->share('JDocumentHtml', array('JFactory', 'getDocument')); // User $container->alias('user', 'JUser')->share('JUser', \JFactory::getUser()); // Input $container->alias('input', 'JInput')->share('JInput', \JFactory::getApplication()->input); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return void * * @since 1.0 */ public function register(Container $container) { $container->alias('Stats\\Application', 'Joomla\\Application\\AbstractApplication')->alias('Joomla\\Application\\AbstractWebApplication', 'Joomla\\Application\\AbstractApplication')->share('Joomla\\Application\\AbstractApplication', function (Container $container) { $application = new Application($container->get('Joomla\\Input\\Input'), $container->get('config')); // Inject extra services $application->setRouter($container->get('Stats\\Router')); return $application; }, true); $container->share('Joomla\\Input\\Input', function () { return new Input($_REQUEST); }, true); $container->share('Stats\\Router', function (Container $container) { $router = (new Router($container->get('Joomla\\Input\\Input')))->setContainer($container)->setControllerPrefix('Stats\\Controllers\\')->setDefaultController('DisplayController')->addMap('/submit', 'SubmitController')->addMap('/:source', 'DisplayController'); return $router; }, true); $container->share('Stats\\Controllers\\DisplayControllerGet', function (Container $container) { $controller = new DisplayControllerGet($container->get('Stats\\Views\\Stats\\StatsJsonView')); $controller->setApplication($container->get('Joomla\\Application\\AbstractApplication')); $controller->setInput($container->get('Joomla\\Input\\Input')); return $controller; }, true); $container->share('Stats\\Controllers\\SubmitControllerCreate', function (Container $container) { $controller = new SubmitControllerCreate($container->get('Stats\\Models\\StatsModel')); $controller->setApplication($container->get('Joomla\\Application\\AbstractApplication')); $controller->setInput($container->get('Joomla\\Input\\Input')); return $controller; }, true); $container->share('Stats\\Models\\StatsModel', function (Container $container) { return new StatsModel($container->get('Joomla\\Database\\DatabaseDriver')); }, true); $container->share('Stats\\Views\\Stats\\StatsJsonView', function (Container $container) { return new StatsJsonView($container->get('Stats\\Models\\StatsModel')); }, true); }
public function register(Container $container) { $container->alias("db", "Joomla\\Database\\DatabaseDriver")->share("Joomla\\Database\\DatabaseDriver", function () use($container) { $config = $container->get("config"); return DatabaseDriver::getInstance((array) $config["database"]); }, true); }
/** * @param Container $container The DI container * @param string $alias An optional alias * * @return void */ public function register(Container $container, $alias = null) { $container->set('CommandBus', [$this, 'createCommandBus'], true, true); if ($alias) { $container->alias($alias, 'CommandBus'); } }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return void * * @since 1.0 */ public function register(Container $container) { // Register the web processor $container->share('monolog.processor.web', function () { return new WebProcessor(); }); // Register the main application handler $container->share('monolog.handler.application', function (Container $container) { /** @var \Joomla\Registry\Registry $config */ $config = $container->get('config'); $level = strtoupper($config->get('log.application', $config->get('log.level', 'error'))); return new StreamHandler(APPROOT . '/logs/app.log', constant('\\Monolog\\Logger::' . $level)); }); // Register the database handler $container->share('monolog.handler.database', function (Container $container) { /** @var \Joomla\Registry\Registry $config */ $config = $container->get('config'); // If database debugging is enabled then force the logger's error level to DEBUG, otherwise use the level defined in the app config $level = $config->get('database.debug', false) ? 'DEBUG' : strtoupper($config->get('log.database', $config->get('log.level', 'error'))); return new StreamHandler(APPROOT . '/logs/database.log', constant('\\Monolog\\Logger::' . $level)); }); // Register the main Logger $container->alias('monolog', 'Monolog\\Logger')->alias('monolog.logger.application', 'Monolog\\Logger')->alias('Psr\\Log\\LoggerInterface', 'Monolog\\Logger')->share('Monolog\\Logger', function (Container $container) { return new Logger('MauticDashboard', [$container->get('monolog.handler.application')], [$container->get('monolog.processor.web')]); }); // Register the database Logger $container->share('monolog.logger.database', function (Container $container) { return new Logger('MauticDashboard', [$container->get('monolog.handler.database')], [$container->get('monolog.processor.web')]); }); }
/** * @testdox Container can manage an alias for a resource from an arbitrary Interop compatible container */ public function testDecorateArbitraryInteropContainerAlias() { $container = new Container(new \ArbitraryInteropContainer()); $container->alias('foo', 'aic_foo'); $this->assertTrue($container->has('foo'), "Container does not know alias 'foo'"); $this->assertEquals('aic_foo_content', $container->get('foo'), "Container does not return the correct value for alias 'foo'"); }
/** * Add the plugin factory to a container * * @param Container $container The container * @param string $alias An optional alias * * @return void */ public function register(Container $container, $alias = null) { $container->set($this->key, [$this, 'createExtensionFactory'], true, true); if (!empty($alias)) { $container->alias($alias, $this->key); } }
/** * Registers a RepositoryFactory with the container * * @param Container $container The DI container * @param string $alias An optional alias * * @return void */ public function register(Container $container, $alias = null) { $container->set('Repository', [$this, 'createRepositoryFactory'], true, true); if (!empty($alias)) { $container->alias($alias, 'Repository'); } }
/** * @param Container $container The DI container * @param string $alias An optional alias * * @return void */ public function register(Container $container, $alias = null) { $container->set('Session', [$this, 'createSession'], true, true); if ($alias) { $container->alias($alias, 'Session'); } }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return void * * @since 1.0 */ public function register(Container $container) { $container->alias('db', 'Joomla\\Database\\DatabaseDriver')->share('Joomla\\Database\\DatabaseDriver', function (Container $container) { $config = $container->get('config'); return DatabaseDriver::getInstance((array) $config['database']); }, true); }
/** * Add the dispatcher to a container * * @param Container $container The container * @param string $alias An optional alias * * @return void */ public function register(Container $container, $alias = null) { $container->set($this->key, [$this, 'createDispatcher'], true, true); if (!empty($alias)) { $container->alias($alias, $this->key); } }
/** * @param Container $container The DI container * @param string $alias An optional alias * * @return void */ public function register(Container $container, $alias = null) { $container->set('Request', [$this, 'createRequest'], true, true); if ($alias) { $container->alias($alias, 'Request'); } }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return Container Returns itself to support chaining. */ public function register(Container $container) { $name = $this->name; // Component $container->alias('component', ucfirst($name) . 'Component')->share(ucfirst($name) . 'Component', $this->component); // ControllerResolver $resolverClass = '\\Windwalker\\Controller\\Resolver\\ControllerResolver'; $container->alias('controller.resolver', $resolverClass)->share($resolverClass, function ($container) use($resolverClass) { return new $resolverClass($container->get('app'), $container); }); // Asset Helper $container->extend('helper.asset', function ($asset, $container) use($name) { $asset->resetPaths(); $asset = clone $asset; return $asset->setName('com_' . strtolower($name))->setContainer($container); }); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return Container Returns itself to support chaining. * * @since 1.0 * @throws \RuntimeException */ public function register(Container $container) { $container->set('App\\Debug\\TrackerDebugger', function () use($container) { return new TrackerDebugger($container); }, true, true); // Alias the object $container->alias('debugger', 'App\\Debug\\TrackerDebugger'); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return Container Returns itself to support chaining. * * @since 1.0 */ public function register(Container $container) { $operators = array('copy', 'convert'); foreach ($operators as $operator) { $class = '\\CodeGenerator\\FileOperator\\' . ucfirst($operator) . 'Operator'; $container->alias('operator.' . $operator, $class)->buildSharedObject($class); } }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return Container Returns itself to support chaining. * * @since 1.0 */ public function register(Container $container) { $container->set('Joomla\\Database\\DatabaseDriver', function () { return (new DatabaseDriver())->create($this->test, 'mysqli'); }, true, true); // Alias the database $container->alias('db', 'Joomla\\Database\\DatabaseDriver'); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return Container Returns the container to support chaining. * * @since 1.0 * @throws \RuntimeException */ public function register(Container $container) { $container->share('JTracker\\Github\\Github', function () use($container) { // Call the Github factory's getInstance method and inject the application; it handles the rest of the configuration return GithubFactory::getInstance($container->get('app')); }, true); // Alias the object $container->alias('gitHub', 'JTracker\\Github\\Github'); }
/** * @testdox Both the original key and the alias return the same resource */ public function testResolveAliasSameAsKey() { $container = new Container(); $container->set('foo', function () { return new \StdClass(); }, true, true); $container->alias('bar', 'foo'); $this->assertSame($container->get('foo'), $container->get('bar'), 'When retrieving an alias of a class, both the original and the alias should return the same object instance.'); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return void * * @since 1.0 */ public function register(Container $container) { $container->alias('db', DatabaseDriver::class)->share(DatabaseDriver::class, function (Container $container) { $config = $container->get('config'); $db = DatabaseDriver::getInstance((array) $config->get('database')); $db->setDebug($config->get('database.debug')); $db->setLogger($container->get('monolog.logger.database')); return $db; }, true); }
/** * {@inheritdoc} */ public function register(Container $container) { $container->set('Joomla\\Database\\DatabaseDriver', function () use($container) { $config = $container->get('config'); $options = array('driver' => $config->get('database.driver'), 'host' => $config->get('database.host'), 'user' => $config->get('database.user'), 'password' => $config->get('database.password'), 'database' => $config->get('database.name'), 'prefix' => $config->get('database.prefix')); $db = DatabaseDriver::getInstance($options); $db->setDebug($config->get('debug.database', false)); return $db; }, true, true); // Alias the database $container->alias('db', 'Joomla\\Database\\DatabaseDriver'); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return Container Returns itself to support chaining. * * @since 1.0 */ public function register(Container $container) { $container->set('Joomla\\Database\\DatabaseDriver', function () use($container) { $app = $container->get('app'); $options = array('driver' => $app->get('database.driver'), 'host' => $app->get('database.host'), 'user' => $app->get('database.user'), 'password' => $app->get('database.password'), 'database' => $app->get('database.name'), 'prefix' => $app->get('database.prefix')); $db = DatabaseDriver::getInstance($options); $db->setDebug($app->get('debug.database', false)); $logger = new Logger('JTracker-Database'); $logger->pushHandler(new StreamHandler($app->get('debug.log-path', JPATH_ROOT) . '/database.log', Logger::ERROR)); $logger->pushProcessor(new WebProcessor()); $db->setLogger($logger); return $db; }, true, true); // Alias the database $container->alias('db', 'Joomla\\Database\\DatabaseDriver'); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return void * * @since 1.0 */ public function register(Container $container) { $container->set('Joomla\\Renderer\\RendererInterface', function (Container $container) { /* @type \Joomla\Registry\Registry $config */ $config = $container->get('config'); // Instantiate the renderer object $renderer = new TwigRenderer($config->get('template')); // Add our Twig extension $renderer->getRenderer()->addExtension(new TwigExtension($this->app)); // Set the Lexer object $renderer->getRenderer()->setLexer(new \Twig_Lexer($renderer->getRenderer(), ['delimiters' => ['tag_comment' => ['{#', '#}'], 'tag_block' => ['{%', '%}'], 'tag_variable' => ['{{', '}}']]])); return $renderer; }, true, true); // Alias the renderer $container->alias('renderer', 'Joomla\\Renderer\\RendererInterface'); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return void * * @since 4.0 */ public function register(Container $container) { $container->alias('db', 'JDatabaseDriver')->alias('Joomla\\Database\\DatabaseInterface', 'JDatabaseDriver')->share('JDatabaseDriver', function (Container $container) { $conf = \JFactory::getConfig(); $options = array('driver' => $conf->get('dbtype'), 'host' => $conf->get('host'), 'user' => $conf->get('user'), 'password' => $conf->get('password'), 'database' => $conf->get('db'), 'prefix' => $conf->get('dbprefix')); try { $db = \JDatabaseDriver::getInstance($options); } catch (\RuntimeException $e) { if (!headers_sent()) { header('HTTP/1.1 500 Internal Server Error'); } jexit('Database Error: ' . $e->getMessage()); } $db->setDebug((bool) $conf->get('debug', false)); return $db; }, true); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return Container Returns itself to support chaining. * * @since 1.0 * @throws \RuntimeException */ public function register(Container $container) { $container->share('Monolog\\Logger', function () use($container) { // Instantiate the object $logger = new Logger('JTracker'); if (LoggerProvider::$fileName) { // Log to a file $logger->pushHandler(new StreamHandler($container->get('debugger')->getLogPath('root') . '/' . LoggerProvider::$fileName, Logger::INFO)); } elseif ('1' != LoggerProvider::$quiet) { // Log to screen $logger->pushHandler(new StreamHandler('php://stdout')); } else { $logger = new NullLogger(); } return $logger; }, true); // Alias the object $container->alias('logger', 'Monolog\\Logger'); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return void * * @since 1.0 */ public function register(Container $container) { $container->alias(Application::class, JoomlaApplication\AbstractApplication::class)->alias(JoomlaApplication\AbstractWebApplication::class, JoomlaApplication\AbstractApplication::class)->share(JoomlaApplication\AbstractApplication::class, function (Container $container) { $application = new Application($container->get(Input::class), $container->get('config')); // Inject extra services $application->setLogger($container->get('monolog.logger.application')); $application->setRouter($container->get(Router::class)); return $application; }, true); $container->share(Input::class, function () { return new Input($_REQUEST); }, true); $container->share(Router::class, function (Container $container) { $router = (new Router($container->get('Joomla\\Input\\Input')))->setContainer($container)->setControllerPrefix('Stats\\Controllers\\')->setDefaultController('DisplayController')->addMap('/submit', 'SubmitController')->addMap('/:source', 'DisplayController'); return $router; }, true); $container->share(DisplayControllerGet::class, function (Container $container) { $controller = new DisplayControllerGet($container->get(StatsJsonView::class)); $controller->setApplication($container->get('Joomla\\Application\\AbstractApplication')); $controller->setInput($container->get('Joomla\\Input\\Input')); return $controller; }, true); $container->share(SubmitControllerCreate::class, function (Container $container) { $controller = new SubmitControllerCreate($container->get('Stats\\Models\\StatsModel')); $controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); $controller->setInput($container->get(Input::class)); return $controller; }, true); $container->share(SubmitControllerGet::class, function (Container $container) { $controller = new SubmitControllerGet(); $controller->setApplication($container->get(JoomlaApplication\AbstractApplication::class)); $controller->setInput($container->get(Input::class)); return $controller; }, true); $container->share(StatsModel::class, function (Container $container) { return new StatsModel($container->get(DatabaseDriver::class)); }, true); $container->share(StatsJsonView::class, function (Container $container) { return new StatsJsonView($container->get(StatsModel::class)); }, true); }
/** * {@inheritdoc} */ public function register(Container $container) { $container->alias('renderer', 'Joomla\\Renderer\\RendererInterface')->set('Joomla\\Renderer\\RendererInterface', function (Container $container) { /* @type \Joomla\Registry\Registry $config */ $config = $container->get('config'); // Setup the path $templateConfig = $config->get('template'); $templateConfig->path = JPATH_TEMPLATES; // Instantiate the renderer object $renderer = new TwigRenderer($config->get('template')); // Add our Twig extension $renderer->getRenderer()->addExtension(new StatsExtension($container->get('app'))); // Add the debug extension if enabled if ($config->get('template.debug')) { $renderer->getRenderer()->addExtension(new \Twig_Extension_Debug()); } // Set the Lexer object $renderer->getRenderer()->setLexer(new \Twig_Lexer($renderer->getRenderer(), ['delimiters' => ['tag_comment' => ['{#', '#}'], 'tag_block' => ['{%', '%}'], 'tag_variable' => ['{{', '}}']]])); return $renderer; }, true, true); }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return void * * @since 4.0 */ public function register(Container $container) { $container->alias('session', 'Joomla\\Session\\SessionInterface')->alias('JSession', 'Joomla\\Session\\SessionInterface')->alias('Joomla\\Session\\Session', 'Joomla\\Session\\SessionInterface')->share('Joomla\\Session\\SessionInterface', function (Container $container) { $app = JFactory::getApplication(); // Generate a session name. $name = JApplicationHelper::getHash($app->get('session_name', get_class($app))); // Calculate the session lifetime. $lifetime = $app->get('lifetime') ? $app->get('lifetime') * 60 : 900; // Initialize the options for the Session object. $options = array('name' => $name, 'expire' => $lifetime); // Set up the storage handler $handler = new FilesystemHandler(JPATH_INSTALLATION . '/sessions'); $input = $app->input; $storage = new JoomlaStorage($input, $handler); $dispatcher = $container->get('Joomla\\Event\\DispatcherInterface'); $dispatcher->addListener('onAfterSessionStart', array($app, 'afterSessionStart')); $session = new JSession($storage, $dispatcher, $options); $session->addValidator(new AddressValidator($input, $session)); $session->addValidator(new ForwardedValidator($input, $session)); return $session; }, true); }
/** * Get delegator object to dispatch controller task. * * You can override it in component controller folders, otherwise Windwalker will use the default delegator. * * @param array $config Controller config. * * @return ControllerDelegator Delegator object. * * @throws \LogicException */ public function getDelegator($config) { $defaultDelegator = '\\Windwalker\\Controller\\Resolver\\ControllerDelegator'; $key = $config['prefix'] . '.' . $config['name'] . '.controller.delegator'; try { // Find from container $delegator = $this->container->get($key); } catch (\InvalidArgumentException $e) { // Find from component. $class = ucfirst($config['prefix']) . 'Controller' . ucfirst($config['name']) . 'Delegator'; if (class_exists($class)) { if (!is_subclass_of($class, $defaultDelegator)) { throw new \LogicException(sprintf('%s should extends from %', $class, $defaultDelegator)); } $delegator = new $class(); } else { // Find from windwalker $delegator = new $defaultDelegator(); } $this->container->alias($key, $class)->share($class, $delegator); } return $delegator; }
/** * Registers the service provider with a DI container. * * @param Container $container The DI container. * * @return void * * @since 1.0 */ public function register(Container $container) { $container->share('Joomla\\Renderer\\RendererInterface', function (Container $container) { /* @type \Joomla\Registry\Registry $config */ $config = $container->get('config'); // Instantiate the renderer object $rendererConfig = array_merge((array) $config->get('template'), ['path' => JPATH_TEMPLATES]); // If the cache isn't false, then it should be a file path relative to the app root $rendererConfig['cache'] = $rendererConfig['cache'] === false ? $rendererConfig['cache'] : JPATH_ROOT . '/' . $rendererConfig['cache']; // Instantiate the renderer object $renderer = new TwigRenderer($rendererConfig); // Add our Twig extension $renderer->getRenderer()->addExtension(new TwigExtension($this->app)); // Add the debug extension if enabled if ($config->get('template.debug')) { $renderer->getRenderer()->addExtension(new \Twig_Extension_Debug()); } // Set the Lexer object $renderer->getRenderer()->setLexer(new \Twig_Lexer($renderer->getRenderer(), ['delimiters' => ['tag_comment' => ['{#', '#}'], 'tag_block' => ['{%', '%}'], 'tag_variable' => ['{{', '}}']]])); return $renderer; }, true); // Alias the renderer $container->alias('renderer', 'Joomla\\Renderer\\RendererInterface'); }
/** * Test the alias method. * * @return void * * @since 1.0 */ public function testAlias() { $this->fixture->alias('foo', 'bar'); $aliases = $this->readAttribute($this->fixture, 'aliases'); $this->assertEquals(array('foo' => 'bar'), $aliases, 'When setting an alias, it should be set in the $aliases Container property.'); }