/** * Factory method to create an instance of a block given its name and the providing module instance. * Supports either Zikula\Core\BlockControllerInterface or * Zikula_Controller_AbstractBlock (to be removed). * * @todo at Core-2.0 remove BC support for Zikula_Controller_AbstractBlock * @todo remove `null` default value for $moduleBundle at Core-2.0 and check for null below * @param $blockClassName * @param AbstractModule|null $moduleBundle * @return \Zikula_Controller_AbstractBlock|BlockControllerInterface */ public function getInstance($blockClassName, AbstractModule $moduleBundle = null) { if (strpos($blockClassName, '.')) { // probably a service name if ($this->container->has($blockClassName)) { $service = $this->container->get($blockClassName); if ($service instanceof BlockControllerInterface) { return $service; } } } if (!class_exists($blockClassName)) { throw new \RuntimeException(sprintf('Classname %s does not exist.', $blockClassName)); } if (!is_subclass_of($blockClassName, 'Zikula\\Core\\BlockControllerInterface') && !is_subclass_of($blockClassName, 'Zikula_Controller_AbstractBlock')) { throw new \RuntimeException(sprintf('Block class %s must implement Zikula\\Core\\BlockControllerInterface or be a subclass of Zikula_Controller_AbstractBlock.', $blockClassName)); } $serviceNameHelper = new ServiceNameHelper(); if ($this->container->has($blockServiceName = $serviceNameHelper->generateServiceNameFromClassName($blockClassName))) { return $this->container->get($blockServiceName); } if (is_subclass_of($blockClassName, 'Zikula_Controller_AbstractBlock')) { $blockInstance = new $blockClassName($this->container, $moduleBundle); $blockInstance->init(); } elseif (is_subclass_of($blockClassName, 'Zikula\\Core\\Controller\\AbstractBlockController')) { if (null === $moduleBundle || !$moduleBundle instanceof AbstractModule) { throw new \LogicException('$moduleBundle must be instance of AbstractModule and not null.'); } $blockInstance = new $blockClassName($moduleBundle); } else { $blockInstance = new $blockClassName(); } if ($blockInstance instanceof ContainerAwareInterface) { $blockInstance->setContainer($this->container); } $this->container->set($blockServiceName, $blockInstance); return $blockInstance; }
/** * @dataProvider classNameProvider * @param $expected * @param $className */ public function testGenerateServiceNameFromClassName($className, $expected) { $this->assertEquals($expected, $this->helper->generateServiceNameFromClassName($className)); }