/** * Registers the service provider within a DI container. * * @param Container $container The DI container. * * @return void * * @since 2.0 */ public function register(Container $container) { $that = $this; $container->set('mustache', function ($c) use($that) { return $that->getMustache($c); }); }
/** * 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) { $model = $this->model; // QueryHelper $container->share('model.' . $this->name . '.helper.query', function ($container) use($model) { if ($model instanceof ListModel) { return $model->getQueryHelper(); } else { return new QueryHelper(); } }); // Filter $container->share('model.' . $this->name . '.filter', function ($container) use($model) { if ($model instanceof ListModel) { return $model->getFilterHelper(); } else { return new FilterHelper(); } })->alias('model.' . $this->name . '.helper.filter', 'model.' . $this->name . '.filter'); // Search $container->share('model.' . $this->name . '.search', function ($container) use($model) { if ($model instanceof ListModel) { return $model->getSearchHelper(); } else { return new SearchHelper(); } })->alias('model.' . $this->name . '.helper.search', 'model.' . $this->name . '.search'); }
/** * 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) { $app = $this->app; $container->set('app', function () use($app) { return $app; }, true, true); }
/** * 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) { // Global Config $container->share('joomla.config', array('JFactory', 'getConfig')); // Windwalker Config $container->share('windwalker.config', array($this, 'loadConfig')); // Database $this->share($container, 'db', 'JDatabaseDriver', array('JFactory', 'getDbo')); // Language $this->share($container, 'language', 'JLanguage', array('JFactory', 'getLanguage')); // Dispatcher $this->share($container, 'event.dispatcher', 'JEventDispatcher', array('JEventDispatcher', 'getInstance')); // Date $this->set($container, 'date', 'JDate', function () { return DateHelper::getDate(); }); // Global $container->set('SplPriorityQueue', function () { return new \SplPriorityQueue(); }); // Asset $container->share('helper.asset', function () { return new \Windwalker\Helper\AssetHelper(); }); // Detect deferent environment if (defined('WINDWALKER_CONSOLE')) { $container->registerServiceProvider(new CliProvider()); } else { $container->registerServiceProvider(new WebProvider()); } }
/** * Registers the service provider within a DI container. * * @param Container $container The DI container. * * @return void * * @since 1.2 */ public function register(Container $container) { $that = $this; $container->share('logger', function (Container $c) use($that) { return $that->getLogger($c); }, true); }
/** * Registers the service provider within a DI container. * * @param Container $container The DI container. * * @return void * * @since 1.2 */ public function register(Container $container) { $that = $this; $container->share('config', function ($c) use($that) { return $that->getConfig($c); }, true); }
/** * Instantiate the controller. * * @param \Joomla\DI\Container $container DI Container. * @param IOInterface $io The Controller object. */ public function __construct(JoomlaContainer $container = null, IOInterface $io = null) { $this->container = $container ?: $this->getContainer(); // Set provider $container->registerServiceProvider(new ServiceProvider()); parent::__construct($io); }
/** * 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); }
/** * {@inheritdoc} */ public function register(Container $container) { $config = $this->config; $container->share('config', function () use($config) { return $config; }); }
/** * Registers the service provider within a DI container. * * @param Container $container The DI container. * * @return void * * @since 1.1 */ public function register(Container $container) { $that = $this; $container->share('github', function (Container $c) use($that) { return $that->getGithub($c); }, true); }
/** * @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'); } }
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); }
/** * 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); }
/** * @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')); }
/** * 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'); }
/** * 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'); }
/** * @testdox The EventDispatcherServiceProvider adds an EventDispatcher to a container with an alias */ public function testEventDispatcherServiceProviderCreatesDispatcherWithAlias() { $container = new Container(); $container->set('extension_factory', $this->getMockBuilder(ExtensionFactoryInterface::class)->getMock()); $service = new EventDispatcherServiceProvider(); $service->register($container, 'unit'); $this->assertInstanceOf(DispatcherInterface::class, $container->get('unit')); }
/** * 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 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); } }
/** * @testdox getNewInstance() will always return a new instance, even if the resource was set to be shared */ public function testGetNewInstance() { $container = new Container(); $container->share('foo', function () { return new \stdClass(); }); $this->assertNotSame($container->getNewInstance('foo'), $container->getNewInstance('foo')); }
/** * @testdox The ExtensionFactoryServiceProvider adds an ExtensionFactory to a container with an alias */ public function testExtensionFactoryServiceProviderCreatesExtensionFactoryWithAlias() { $container = new Container(); $container->set('ConfigDirectory', __DIR__); $service = new ExtensionFactoryServiceProvider(); $service->register($container, 'unit'); $this->assertInstanceOf(ExtensionFactoryInterface::class, $container->get('unit')); }
/** * Return reward object. * * <code> * $rewardId = 1; * $projectId = 2; * * $this->prepareReward($container, $rewardId, $projectId); * $reward = $this->getReward($container, $rewardId, $projectId); * </code> * * @param Container $container * @param int $rewardId * @param int $projectId * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \UnexpectedValueException * @throws \OutOfBoundsException * * @return Reward */ protected function getReward($container, $rewardId, $projectId) { $rewardId = (int) abs($rewardId); $hash = StringHelper::generateMd5Hash(Constants::CONTAINER_REWARD, array($rewardId, $projectId)); if (!$container->exists($hash) and $rewardId > 0) { $this->prepareReward($container, $rewardId, $projectId); } return $container->get($hash); }
/** * Return profile object from Proof of Identity. * * <code> * $userId = 1; * * $this->prepareProfile($container, $userId); * $profile = $this->getProfile($container, $userId); * </code> * * @param Container $container * @param int $userId * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \UnexpectedValueException * @throws \OutOfBoundsException * * @return User */ protected function getProofProfile($container, $userId) { $userId = (int) abs($userId); $hash = StringHelper::generateMd5Hash(Constants::CONTAINER_PROOF_PROFILE, $userId); if (!$container->exists($hash) and $userId > 0) { $this->prepareProofProfile($container, $userId); } return $container->get($hash); }
/** * @testdox has() also resolves the alias if set. */ public function testExistsResolvesAlias() { $container = new Container(); $container->set('foo', function () { return new \stdClass(); }, true, true)->alias('bar', 'foo'); $this->assertTrue($container->has('foo'), "Original 'foo' was not resolved"); $this->assertTrue($container->has('bar'), "Alias 'bar' was not resolved"); }
public function createModel($name = null) { $name or $name = $this->getName(); $className = 'Framework\\Models\\' . ucfirst($name) . 'Model'; if (!class_exists($className)) { return false; } return $this->container->buildObject($className); }
/** * 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'); }
/** * Return project. * * <code> * $projectId = 1; * * $this->prepareProject($container, $projectId); * $project = $this->getProject($container, $projectId); * </code> * * @param Container $container * @param int $projectId * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \UnexpectedValueException * @throws \OutOfBoundsException * * @return Project */ protected function getProject($container, $projectId) { $projectId = (int) abs($projectId); $hash = StringHelper::generateMd5Hash(Constants::CONTAINER_PROJECT, $projectId); if (!$container->exists($hash) and $projectId > 0) { $this->prepareProject($container, $projectId); } return $container->get($hash); }
/** * Return user profile. * * <code> * $userId = 1; * * $this->prepareProfile($container, $params, $userId); * $profile = $this->getProfile($container, $params, $userId); * </code> * * @param Container $container * @param Registry $params * @param int $userId * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \UnexpectedValueException * @throws \OutOfBoundsException * * @return Project */ protected function getProfile($container, $params, $userId) { $userId = (int) abs($userId); $options = array('platform' => $params->get('integration_social_platform'), 'user_id' => $userId); $hash = StringHelper::generateMd5Hash(Constants::CONTAINER_PROFILE, $options); if (!$container->exists($hash) and $userId > 0) { $this->prepareProfile($container, $params, $userId); } return $container->get($hash); }
/** * 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->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); }