本文整理汇总了PHP中Joomla\DI\Container::get方法的典型用法代码示例。如果您正苦于以下问题:PHP Container::get方法的具体用法?PHP Container::get怎么用?PHP Container::get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Joomla\DI\Container
的用法示例。
在下文中一共展示了Container::get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: register
/**
* 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);
}
示例2: register
/**
* 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);
}
示例3: handle
/**
* 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);
}
示例4: register
/**
* 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');
}
示例5: testResolveAliasSameAsKey
/**
* @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.');
}
示例6: testExtendScalar
/**
* @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'));
}
示例7: register
/**
* 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);
}
示例8: register
/**
* 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);
}
示例9: register
/**
* 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');
}
示例10: getGithub
/**
* 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;
}
示例11: createCommandBus
/**
* @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();
}
示例12: fetchController
/**
* 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;
}
示例13: testGetPassesContainerInstanceNotShared
/**
* 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'));
}
示例14: testDecorateArbitraryInteropContainerAlias
/**
* @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'");
}
示例15: setLastVisitTime
/**
* 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();
}