/** * 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); }
/** * 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->share('JApplicationAdministrator', function (Container $container) { $app = new \JApplicationAdministrator(null, null, null, $container); // The session service provider needs JFactory::$application, set it if still null if (JFactory::$application === null) { JFactory::$application = $app; } $app->setDispatcher($container->get('Joomla\\Event\\DispatcherInterface')); $app->setLogger(JLog::createDelegatedLogger()); $app->setSession($container->get('Joomla\\Session\\SessionInterface')); return $app; }, true); $container->share('JApplicationSite', function (Container $container) { $app = new \JApplicationSite(null, null, null, $container); // The session service provider needs JFactory::$application, set it if still null if (JFactory::$application === null) { JFactory::$application = $app; } $app->setDispatcher($container->get('Joomla\\Event\\DispatcherInterface')); $app->setLogger(JLog::createDelegatedLogger()); $app->setSession($container->get('Joomla\\Session\\SessionInterface')); return $app; }, true); }
/** * Execute the middleware. Don't call this method directly; it is used by the `Application` internally. * * @internal * * @param ServerRequestInterface $request The request object * @param ResponseInterface $response The response object * @param callable $next The next middleware handler * * @return ResponseInterface */ public function handle(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { $attributes = $request->getAttributes(); if (!isset($attributes['command'])) { try { /** @var RepositoryInterface $repository */ $repository = $this->container->get('Repository')->forEntity(Page::class); /** @var Page[] $pages */ $pages = $repository->getAll(); $router = new Router(); foreach ($pages as $page) { $router->get($this->expandUrl($page->url, $page), function () use($page) { return $page; }); } $path = preg_replace('~^/.*?index.php/?~', '', $request->getUri()->getPath()); $route = $router->parseRoute($path); $page = $route['controller'](); $vars = $route['vars']; $command = new DisplayPageCommand($page->id, $vars, $request, $response->getBody(), $this->container); $request = $request->withAttribute('command', $command); // @todo Emit afterRouting event } catch (InvalidArgumentException $e) { // Do nothing } } return $next($request, $response); }
/** * 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->share('cck', function ($container) { return new CCKEngine($container->get('app'), $container->get('event.dispatcher'), $container); }); \JForm::addFieldPath(__DIR__ . '/Fields'); \JForm::addFormPath(__DIR__ . '/Resource/Form'); }
/** * @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.'); }
/** * @testdox Scalar resources can be extended */ public function testExtendScalar() { $container = new Container(); $container->set('foo', 'bar'); $this->assertEquals('bar', $container->get('foo')); $container->extend('foo', function ($originalResult, Container $c) { return $originalResult . 'baz'; }); $this->assertEquals('barbaz', $container->get('foo')); }
/** * 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); }
/** * Add the configuration from the environment to a container * * @param Container $container The container * @param string $alias An optional alias, defaults to 'config' * * @return void */ public function register(Container $container, $alias = 'config') { $file = '.env'; if ($container->has('ConfigFileName')) { $file = $container->get('ConfigFileName'); } $dotenv = new Dotenv($container->get('ConfigDirectory'), $file); $dotenv->overload(); $container->set($alias, new Registry($_ENV), true); }
/** * 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\\Database\\DatabaseDriver', function () use($container) { $config = $container->get('config'); $options = array('driver' => $config->get('dbtype'), 'host' => $config->get('host'), 'user' => $config->get('user'), 'password' => $config->get('password'), 'database' => $config->get('db'), 'prefix' => $config->get('dbprefix')); $db = DatabaseDriver::getInstance($options); $db->setDebug($config->get('debug', false)); return $db; }, true, true); // Alias the database $container->alias('db', 'Joomla\\Database\\DatabaseDriver'); JFactory::$database = $container->get('db'); }
/** * Gets a Github object. * * @param Container $c A DI container. * * @return Github * * @since 2.0 */ public function getGithub(Container $c) { /* @var $config Registry */ $config = $c->get('config'); /* @var $input Joomla\Input\Input */ $input = $c->get('input'); $options = new Registry(); $options->set('headers.Accept', 'application/vnd.github.html+json'); $options->set('api.username', $input->get('username', $config->get('api.username'))); $options->set('api.password', $input->get('password', $config->get('api.password'))); $options->set('api.url', $config->get('api.url')); $github = new Github($options); return $github; }
/** * @param Container $container The container * * @return \Joomla\Service\CommandBus */ public function createCommandBus(Container $container) { // Construct the command handler middleware $middleware = []; if ($container->has('CommandBusMiddleware')) { $middleware = (array) $container->get('CommandBusMiddleware'); } if ($container->has('extension_factory')) { $middleware[] = new ExtensionQueryMiddleware($container->get('extension_factory')); } $builder = new CommandBusBuilder($container->get('EventDispatcher')); $middleware = array_merge($middleware, $builder->getMiddleware()); $builder->setMiddleware($middleware); return $builder->getCommandBus(); }
/** * Get a AbstractTrackerController object for a given name. * * @param string $name The controller name (excluding prefix) for which to fetch and instance. * * @return AbstractTrackerController * * @since 1.0 * @throws \RuntimeException */ protected function fetchController($name) { // Derive the controller class name. $class = $this->controllerPrefix . ucfirst($name); // Check for the requested controller. if (!class_exists($class)) { throw new \RuntimeException(sprintf('Controller %s not found', $name)); } // Instantiate the controller. $controller = new $class($this->input, $this->container->get('app')); if ($controller instanceof ContainerAwareInterface) { $controller->setContainer($this->container); } return $controller; }
/** * Tests the get method for passing the * Joomla\DI\Container instance to the callback. * * @return void * * @since 1.0 */ public function testGetPassesContainerInstanceNotShared() { $this->fixture->set('foo', function ($c) { return $c; }, false); $this->assertSame($this->fixture, $this->fixture->get('foo')); }
/** * @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'"); }
/** * Set the last visited time for a newly logged in user * * @param integer $id The user ID to update * * @return void * * @since 1.0 */ public function setLastVisitTime($id) { /* @type \Joomla\Database\DatabaseDriver $db */ $db = $this->container->get('db'); $date = new Date(); $db->setQuery($db->getQuery(true)->update($db->quoteName('#__users'))->set($db->quoteName('lastvisitDate') . '=' . $db->quote($date->format($db->getDateFormat())))->where($db->quoteName('id') . '=' . (int) $id))->execute(); }
/** * 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); }
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); }
/** * @testdox Loading a file */ public function testLoadFile() { $container = new Container(); $loader = new YamlLoader($container); $loader->loadFromFile(dirname(__DIR__) . '/data/services.yml'); $this->assertEquals('called', $container->get('foo')); }
/** * Get relation types. * * @return array * * @since 1.0 */ public function getRelTypes() { static $relTypes = array(); if (!$relTypes) { $db = $this->container->get('db'); $relTypes = $db->setQuery($db->getQuery(true)->from($db->quoteName('#__issues_relations_types'))->select($db->quoteName('id', 'value'))->select($db->quoteName('name', 'text')))->loadObjectList(); } return $relTypes; }
/** * Get a Mustache object. * * @param Container $c A DI container. * * @return \Mustache_Engine * * @since 2.0 */ public function getMustache(Container $c) { $config = $c->get('config'); $mustache = new \Mustache_Engine(array('loader' => new \Mustache_Loader_FilesystemLoader($config->get('mustache.views', __DIR__ . '/../templates'), array('extension' => $config->get('mustache.ext', '.md'))))); $mustache->addHelper('number', array('1f' => function ($value) { return sprintf('%.1f', $value); })); return $mustache; }
/** * 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'); }
/** * Renders a changelog array. * * @param array $log The changelog details. * * @return void * * @since 1.2 */ private function decorateLog(&$log) { /** @var \Mustache_Engine $mustache */ $mustache = $this->container->get('mustache'); $view = $mustache->loadTemplate('release_notes'); foreach ($log as &$data) { // Note that Mustache does not like looping over associative arrays. $data->notes = $view->render($data); } }
/** * 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) { $server = new ApiServer($this->element, $this->uri, $this->option); $container->set('api.server', $server); $server->register(); // Listener $dispatcher = $container->get('event.dispatcher'); /** @var $dispatcher \JEventDispatcher */ $dispatcher->attach(new ApiListener($this->element, $dispatcher)); }
/** * @testdox The modified command bus has an execute method that takes a Query as a parameter */ public function testTheCommandBusHasAnExecuteMethodThatTakesAQueryAsAParameter() { $this->expectOutputString(sprintf("LOG: Starting %1\$s\nLOG: Ending %1\$s\n", "Joomla\\Tests\\Unit\\Service\\Stubs\\SimpleQuery")); $container = new Container(); $container->set('EventDispatcher', $this->getMockBuilder(DispatcherInterface::class)->getMock()); $container->set('CommandBusMiddleware', [new LoggingMiddleware(new Logger())]); $container->registerServiceProvider(new CommandBusServiceProvider()); $commandBus = $container->get('CommandBus'); $this->assertEquals('XSome contentY', $commandBus->handle(new SimpleQuery('Some content'))); }
/** * 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); }
/** * 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)); return $db; }, true, true); // Alias the database $container->alias('db', 'Joomla\\Database\\DatabaseDriver'); }
/** * {@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 Container Returns the container to support chaining. * * @since 1.0 * @throws \RuntimeException */ public function register(Container $container) { return $container->set('BabDev\\Transifex\\Transifex', function () use($container) { $options = new Registry(); /* @var \JTracker\Application $app */ $app = $container->get('app'); $options->set('api.username', $app->get('transifex.username')); $options->set('api.password', $app->get('transifex.password')); // Instantiate Transifex return new Transifex($options); })->alias('transifex', 'BabDev\\Transifex\\Transifex'); }
/** * 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')]); }); }
/** * Render database information. * * @return string HTML markup for database debug * * @since 1.0 */ protected function renderDatabase() { $debug = array(); $dbLog = $this->getLog('db'); if (!$dbLog) { return ''; } $tableFormat = new TableFormat(); $sqlFormat = new SqlFormat(); $dbDebugger = new DatabaseDebugger($this->container->get('db')); $debug[] = sprintf(g11n4t('One database query', '%d database queries', count($dbLog)), count($dbLog)); $prefix = $dbDebugger->getPrefix(); foreach ($dbLog as $i => $entry) { $explain = $dbDebugger->getExplain($entry->sql); $debug[] = '<pre class="dbQuery">' . $sqlFormat->highlightQuery($entry->sql, $prefix) . '</pre>'; if (isset($entry->times) && is_array($entry->times)) { $debug[] = sprintf('Query Time: %.3f ms', ($entry->times[1] - $entry->times[0]) * 1000) . '<br />'; } // Tabs headers $debug[] = '<ul class="nav nav-tabs">'; if ($explain) { $debug[] = '<li><a data-toggle="tab" href="#queryExplain-' . $i . '">Explain</a></li>'; } if (isset($entry->trace) && is_array($entry->trace)) { $debug[] = '<li><a data-toggle="tab" href="#queryTrace-' . $i . '">Trace</a></li>'; } if (isset($entry->profile) && is_array($entry->profile)) { $debug[] = '<li><a data-toggle="tab" href="#queryProfile-' . $i . '">Profile</a></li>'; } $debug[] = '</ul>'; // Tabs contents $debug[] = '<div class="tab-content">'; if ($explain) { $debug[] = '<div id="queryExplain-' . $i . '" class="tab-pane">'; $debug[] = $explain; $debug[] = '</div>'; } if (isset($entry->trace) && is_array($entry->trace)) { $debug[] = '<div id="queryTrace-' . $i . '" class="tab-pane">'; $debug[] = $tableFormat->fromTrace($entry->trace); $debug[] = '</div>'; } if (isset($entry->profile) && is_array($entry->profile)) { $debug[] = '<div id="queryProfile-' . $i . '" class="tab-pane">'; $debug[] = $tableFormat->fromArray($entry->profile); $debug[] = '</div>'; } $debug[] = '</div>'; } return implode("\n", $debug); }