container = $container; if (is_null($container)) { $this->initializeContainer($config); } } /** * Initialize the container */ protected function initializeContainer($config) { if (!empty($config)) { $this->container = $this->buildContainer($config); $this->container->compile(); } else { throw new \Exception(sprintf('Bitpay::initializeContainer(): Missing config parameter.')); } } /** * Build the container of services and parameters */ protected function buildContainer($config) { if (!empty($config)) { $container = new ContainerBuilder(new ParameterBag($this->getParameters())); $this->prepareContainer($container); $this->getContainerLoader($container)->load($config); return $container; } else { throw new \Exception(sprintf('Bitpay::buildContainer(): Missing config parameter.')); } } protected function getParameters() { return array( 'bitpay.root_dir' => realpath(__DIR__.'/..'), ); } /** */ private function prepareContainer(ContainerInterface $container) { if (!empty($container)) { foreach ($this->getDefaultExtensions() as $ext) { $container->registerExtension($ext); $container->loadFromExtension($ext->getAlias()); } } else { throw new \Exception(sprintf('Bitpay::prepareContainer(): Missing container parameter.')); } } /** * @param ContainerInterface $container * @return LoaderInterface */ private function getContainerLoader(ContainerInterface $container) { if (!empty($container)) { $locator = new FileLocator(); $resolver = new LoaderResolver( array( new ArrayLoader($container), new YamlFileLoader($container, $locator), ) ); return new DelegatingLoader($resolver); } else { throw new \Exception(sprintf('Bitpay::getContainerLoader(): Missing container parameter.')); } } /** * Returns an array of the default extensions * * @return array */ private function getDefaultExtensions() { return array( new BitpayExtension(), ); } /** * @return ContainerInterface */ public function getContainer() { return $this->container; } /** * @return mixed */ public function get($service) { if (!empty($service)) { return $this->container->get($service); } else { return null; } } }