/** * Test exists * * @return void * * @since 1.0 */ public function testExists() { $reflection = new \ReflectionProperty($this->fixture, 'dataStore'); $reflection->setAccessible(true); $reflection->setValue($this->fixture, array('foo' => 'bar')); $this->assertTrue($this->fixture->exists('foo'), 'When calling exists on an item that has been set in the container, it should return true.'); $this->assertFalse($this->fixture->exists('baz'), 'When calling exists on an item that has not been set in the container, it should return false.'); }
/** * 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); }
/** * 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 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); }
/** * Return number formatter. * * <code> * $fractionDigits = 2; * * $this->prepareNumberFormatter($container, $fractionDigits); * $numberFormatter = $this->getNumberFormatter($container, $fractionDigits); * </code> * * @param Container $container * @param int $fractionDigits * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \OutOfBoundsException * * @return \NumberFormatter */ protected function getNumberFormatter($container, $fractionDigits = 2) { $fractionDigits = (int) abs($fractionDigits); $numberHash = StringHelper::generateMd5Hash(Constants::CONTAINER_FORMATTER_NUMBER, $fractionDigits); if (!$container->exists($numberHash)) { $this->prepareNumberFormatter($container, $fractionDigits); $numberFormatter = $container->get($numberHash); } else { $numberFormatter = $container->get($numberHash); } return $numberFormatter; }
/** * Constructor. * * @param string $name The Component name. * @param \JInput $input The Input object. * @param \JApplicationCms $application The Application object. * @param Container $container The DI container. * * @throws \Exception */ public function __construct($name = null, $input = null, $application = null, $container = null) { $this->name = $name; // Guess component name. if (!$this->name) { $reflection = $this->getReflection(); $this->name = $reflection->getShortName(); $this->name = strtolower(str_replace('Component', '', $this->name)); if (!$this->name) { throw new \Exception('Component need name.'); } } $this->option = 'com_' . strtolower($this->name); $this->container = $container ?: Container::getInstance($this->option); $this->application = $application ?: $this->container->get('app'); $this->input = $input ?: $this->application->input; // Add a config but make it B/C if ($this->container->exists($this->option . '.config')) { $this->config = $this->container->get($this->option . '.config'); } $this->config = $this->config ?: new Registry(); $this->config->merge(\JComponentHelper::getParams($this->option)); $this->init(); }
/** * @testdox The database service provider is registered to the DI container * * @covers Stats\Providers\DatabaseServiceProvider::register */ public function testTheDatabaseServiceProviderIsRegisteredToTheContainer() { $container = new Container(); $container->registerServiceProvider(new DatabaseServiceProvider()); $this->assertTrue($container->exists('Joomla\\Database\\DatabaseDriver')); }
/** * Return money formatter. * * <code> * $this->prepareMoneyFormatter($container, $params); * $money = $this->getMoneyFormatter($container, $params); * </code> * * @param Container $container * @param Registry $params * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws \OutOfBoundsException * * @return Money */ protected function getMoneyFormatter($container, $params) { $currencyId = $params->get('project_currency'); $moneyHash = StringHelper::generateMd5Hash(Constants::CONTAINER_FORMATTER_MONEY, $currencyId); if (!$container->exists($moneyHash)) { $this->prepareMoneyFormatter($container, $params); $money = $container->get($moneyHash); } else { $money = $container->get($moneyHash); } return $money; }
/** * @testdox The application service provider is registered to the DI container * * @covers Stats\Providers\ApplicationServiceProvider::register */ public function testTheApplicationServiceProviderIsRegisteredToTheContainer() { $container = new Container(); $container->registerServiceProvider(new ApplicationServiceProvider()); $this->assertTrue($container->exists('Stats\\Application')); }
/** * @testdox The config service provider is registered to the DI container * * @covers Stats\Providers\ConfigServiceProvider::__construct * @covers Stats\Providers\ConfigServiceProvider::register */ public function testTheConfigServiceProviderIsRegisteredToTheContainer() { $container = new Container(); $container->registerServiceProvider(new ConfigServiceProvider(APPROOT . '/etc/config.dist.json')); $this->assertTrue($container->exists('config')); }
/** * Returns a reference to the global JApplicationCms object, only creating it if it doesn't already exist. * * This method must be invoked as: $web = JApplicationCms::getInstance(); * * @param string $name The name (optional) of the JApplicationCms class to instantiate. * @param string $prefix The class name prefix of the object. * @param Container $container An optional dependency injection container to inject into the application. * * @return JApplicationCms * * @since 3.2 * @throws RuntimeException */ public static function getInstance($name = null, $prefix = 'JApplication', Container $container = null) { if (empty(static::$instances[$name])) { // Create a JApplicationCms object. $classname = $prefix . ucfirst($name); if (!class_exists($classname)) { throw new RuntimeException(JText::sprintf('JLIB_APPLICATION_ERROR_APPLICATION_LOAD', $name), 500); } if ($container && $container->exists($classname)) { static::$instances[$name] = $container->get($classname); } else { // TODO - This creates an implicit hard requirement on the JApplicationCms constructor static::$instances[$name] = new $classname(null, null, null, $container); } } return static::$instances[$name]; }