/** * {@inheritdoc} */ public function load(array $configs, ContainerBuilder $container) { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); // sources $sources = []; if (array_key_exists('sources', $config) && is_array($config['sources'])) { foreach ($config['sources'] as $sourceName => $sourceNamespace) { $sourceName = Standard::normalizeName($sourceName); if (!$sourceName) { throw new \Exception('Empty source name'); } $sourceNamespace = Standard::normalizeNamespace($sourceNamespace); if (!$sourceNamespace) { throw new \Exception(sprintf('Empty source "%s" namespace', $sourceName)); } $sources[$sourceName] = $sourceNamespace; } } // targets $targets = []; if (array_key_exists('targets', $config) && is_array($config['targets'])) { foreach ($config['targets'] as $targetName => $targetData) { $targetName = Standard::normalizeName($targetName); if (!array_key_exists('namespace', $targetData)) { throw new \Exception(sprintf('Namespace of target "%s" is not set', $targetName)); } $targetData['namespace'] = Standard::normalizeNamespace($targetData['namespace']); if (!$targetData['namespace']) { throw new \Exception(sprintf('Empty target "%s" namespace', $targetName)); } $targetData['name'] = $targetName; $targets[] = ['name' => $targetName, 'namespace' => $targetData['namespace'], 'withAnnotations' => array_key_exists('annotations', $targetData) && $targetData['annotations'], 'useClassNotations' => array_key_exists('class_notations', $targetData) && $targetData['class_notations'], 'withInitFunction' => array_key_exists('init_function', $targetData) && $targetData['init_function']]; } } // Addind to container $container->setParameter('leoza.entities_sources', $sources); $container->setParameter('leoza.entities_targets', $targets); // Adding definitions $definitions = []; $definition = new Definition('Leoza\\EntitiesBundle\\Twig\\EntityGeneratorTwigExtension'); $definition->setPublic(false)->addTag('twig.extension'); $definitions[] = $definition; $srcDir = realpath($container->getParameter('kernel.root_dir') . '/../src'); foreach ($sources as $sourceName => $sourceNamespace) { /* @var $source array */ $definition = new Definition('Leoza\\EntitiesBundle\\EntityGenerator', [new Reference('logger'), $srcDir, new Reference('templating'), new Reference('annotation_reader'), $sourceNamespace, "%leoza.entities_targets%"]); $definition->addTag('monolog.logger', ['channel' => 'leoza']); $definitions['leoza.entity_generator.' . $sourceName] = $definition; $definition = new Definition('Leoza\\EntitiesBundle\\EntityManager', [new Reference('logger'), $sourceNamespace, "%leoza.entities_targets%"]); $definitions['leoza.entity_manager.' . $sourceName] = $definition; $definition->addTag('monolog.logger', ['channel' => 'leoza']); } $container->addDefinitions($definitions); }
public function testNormalizeNamespace() { // Namespace -> Namespace $this->assertEquals('Namespace', Standard::normalizeNamespace('Namespace'), 'Namespace -> Namespace'); // \Namespace -> Namespace $this->assertEquals('Namespace', Standard::normalizeNamespace('\\Namespace'), '\\Namespace -> Namespace'); // \Namespace\ -> Namespace $this->assertEquals('Namespace', Standard::normalizeNamespace('\\Namespace\\'), '\\Namespace\\ -> Namespace'); // \Namespace\Sub -> Namespace\Sub $this->assertEquals('Namespace\\Sub', Standard::normalizeNamespace('\\Namespace\\Sub'), '\\Namespace\\Sub -> Namespace\\Sub'); }
private function configure($controllersNamespace, $defaultController, $defaultAction, $requestAction) { // checking base controller namespace if (!$controllersNamespace) { $this->logger->critical('Action: interface namespace is not configured'); throw new ApiServerException(sprintf('Action of interface "%s" error: controllers namespace not configured', $this->getInterfaceName())); } // reset variables $path = []; $controller = null; $action = null; // parsing request action $requestAction = str_replace(['/', '\\'], ':', $requestAction); $requestAction = trim($requestAction, ':'); if ($requestAction) { $requestActionParts = explode(':', $requestAction); if (count($requestActionParts) < 2) { $controller = ucfirst($requestActionParts[0]); } else { $action = lcfirst(array_pop($requestActionParts)); $controller = ucfirst(array_pop($requestActionParts)); $path = $requestActionParts; } } if (!$controller) { $controller = ucfirst($defaultController); } if (!$action) { $action = lcfirst($defaultAction); } if (!$controller || !$action) { $this->logger->notice(sprintf('Action: unable to route action "%s"', $requestAction)); throw new ApiServerException(sprintf('Action of interface "%s" error: unable to route action "%s"', $this->getInterfaceName(), $requestAction)); } foreach ($path as &$subPath) { $subPath = ucfirst($subPath); } // calculating local class name $localClassName = $path; array_unshift($localClassName, 'Request'); array_push($localClassName, $controller); array_push($localClassName, ucfirst($action) . 'Data'); $this->localClassName = implode('\\', $localClassName); // calculating controller class name, method and symfony action route $controllerClassName = $path; array_unshift($controllerClassName, Standard::normalizeNamespace($controllersNamespace)); array_push($controllerClassName, $controller . 'Controller'); $controllerClassName = implode('\\', $controllerClassName); if (!class_exists($controllerClassName)) { $this->logger->notice(sprintf('Action: unable to route action "%s" because class "%s" not exists', $requestAction, $controllerClassName)); throw new ApiServerException(sprintf('Action of interface "%s" error: unable to find "%s" class', $this->getInterfaceName(), $controllerClassName)); } $controllerMethodName = $action . 'Action'; if (!method_exists($controllerClassName, $controllerMethodName)) { $this->logger->notice(sprintf('Action: unable to route action "%s" because class "%s" does not has method "%s"', $requestAction, $controllerClassName, $controllerMethodName)); throw new ApiServerException(sprintf('Action of interface "%s" error: controller "%s" has no method "%s"', $this->getInterfaceName(), $controllerClassName, $controllerMethodName)); } $this->actionRoute = $controllerClassName . '::' . $controllerMethodName; $this->logger->debug(sprintf('Action: route set to "%s"', $this->actionRoute)); }