/** * @param $targetName * @return Target|null */ protected function getTarget($targetName) { $targetName = Standard::normalizeName($targetName); if (!array_key_exists($targetName, $this->targets)) { return null; } return $this->targets[$targetName]; }
private function configureTarget($targetName) { $this->targetName = $targetName ? Standard::normalizeName($targetName) : $this->getName(); if (!$this->targetName) { $this->logger->critical('Interface ' . $this->getName() . ': entities target not configured'); throw new ApiServerException(sprintf('Interface "%s" error: entities target not configured', $this->getName())); } }
/** * {@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 __construct(ApiServerConnection $apiConnection) { $this->apiConnection = $apiConnection; $this->roles = []; $this->roles[] = 'ROLE_INTERFACE_' . strtoupper($this->apiConnection->getInterfaceName()); foreach ($this->apiConnection->getApiClient()->getAllowedInterfaces() as $allowedInterface) { $this->roles[] = 'ROLE_' . strtoupper(Standard::normalizeName($allowedInterface)) . '_CLIENT'; } if ($this->apiConnection->getApiUser()) { $this->roles[] = 'ROLE_USER'; } }
public function testNormalizeName() { // name -> name $this->assertEquals('name', Standard::normalizeName('name'), 'name -> name'); // Name -> name $this->assertEquals('name', Standard::normalizeName('Name'), 'Name -> name'); // some_name -> some_name $this->assertEquals('some_name', Standard::normalizeName('some_name'), 'some_name -> some_name'); // someName -> some_name $this->assertEquals('some_name', Standard::normalizeName('someName'), 'someName -> some_name'); // SomeName -> some_name $this->assertEquals('some_name', Standard::normalizeName('SomeName'), 'SomeName -> some_name'); // Some_Name -> some_name $this->assertEquals('some__name', Standard::normalizeName('Some_Name'), 'Some_Name -> some__name'); }
/** * {@inheritdoc} */ public function load(array $configs, ContainerBuilder $container) { $env = $container->getParameter("kernel.environment"); $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); $securityConfig = array_key_exists('security', $config) ? $config['security'] : null; $profilingLevel = $env == 'prod' ? Profiler::NO_PROFILING : ($config['debug'] ? Profiler::DEBUG_PROFILING : Profiler::LOG_PROFILING); $container->setDefinition('leoza_api_client.profiler', $this->getProfilerDefinition($profilingLevel)); $container->setDefinition('leoza_api_client.session', $this->getSessionDefinition()); foreach ($config['servers'] as $serverName => $serverConfig) { $serverName = Standard::normalizeName($serverName); $container->setDefinition('leoza_api_client.for.' . $serverName, $this->getApiClientDefinition($serverName, $serverConfig['url'], $serverConfig['token'], $serverConfig['timeout'])); } $container->setDefinition('leoza_api_client.security_manager', $this->getSecurityManagerDefinition($securityConfig)); $container->setDefinition('leoza_api_client.data_collector', $this->getDataCollectorDefinition()); $container->setDefinition('leoza_api_client.security.user_provider', $this->getSecurityUserProviderDefinition()); $container->setDefinition('leoza_api_client.security.authentication_provider', $this->getSecurityAuthenticationProviderDefinition()); $container->setDefinition('leoza_api_client.security.authentication_listener', $this->getSecurityAuthenticationListenerDefinition()); }
protected function execute(InputInterface $input, OutputInterface $output) { $container = $this->getContainer(); $sourceName = Standard::normalizeName($input->getArgument('source')); $localClassName = Standard::normalizeClassName($input->getArgument('localClassName')); $serviceName = 'leoza.entity_generator.' . $sourceName; if (!$sourceName || !$container->has($serviceName)) { $output->writeln(sprintf('Entities source "%s" is not configured', $sourceName)); return; } $generator = $container->get($serviceName); /* @var $generator EntityGenerator */ $output->write('Generating... '); if ($localClassName) { $generator->generate($localClassName); } else { $generator->generateAll(); } $output->writeln('done.'); }
/** * {@inheritdoc} */ public function load(array $configs, ContainerBuilder $container) { // getting config $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); // handling config $apiClientAuthService = $config['auth_service']; $defaultInterfaceName = $config['default_interface']; $apiServerEntitiesSource = $config['source']; $interfacesConfig = []; $interfacesHosts = []; foreach ($config['interfaces'] as $interfaceName => $interface) { $interfaceName = Standard::normalizeName($interfaceName); $interfacesConfig[$interfaceName] = ['controllersNamespace' => $interface['controllers_namespace'], 'defaultAction' => $interface['default_action'], 'defaultController' => $interface['default_controller'], 'showExceptions' => $interface['exceptions'], 'entitiesTarget' => $interface['target'], 'userAuthService' => $interface['user_auth_service'], 'tokenLiveTime' => $interface['user_token_live_time'], 'tokenRenewTime' => $interface['user_token_renew_time'], 'tokenSecret' => $interface['user_token_secret'] ? $interface['user_token_secret'] : $container->getParameter('secret')]; foreach ($interface['hosts'] as $host) { $interfacesHosts[strtolower($host)] = $interfaceName; } } // Setting definitions $container->addDefinitions(['leoza_api_server.request_parser' => $this->getRequestParserEventListenerDefinition($interfacesHosts, $defaultInterfaceName), 'leoza_api_server.entity_builder' => $this->getEntityBuilderEventListenerDefinition(), 'leoza_api_server.response_builder' => $this->getResponseBuilderEventListenerDefinition(), 'leoza_api_server.exceptions_catcher' => $this->getExceptionsCatcherEventListenerDefinition(), 'leoza_api_server.interface_factory' => $this->getApiServerInterfaceFactoryDefinition($apiServerEntitiesSource, $interfacesConfig), 'leoza_api_server.security.user_provider' => $this->getApiServerSecurityUserProviderDefinition($apiClientAuthService), 'leoza_api_server.security.authentication_provider' => $this->getApiServerSecurityAuthenticationProviderDefinition(), 'leoza_api_server.security.authentication_listener' => $this->getApiServerSecurityAuthenticationListenerDefinition()]); }
protected function loadClass() { // Checking is class exist if (!class_exists($this->className)) { throw $this->getClassException('Unable to find class "%s"'); } // Checking parent if (!is_a($this->className, Entity::class, true)) { throw $this->getClassException('Class "%s" must be an instance of Entity'); } // Creating the reflection class try { $this->reflectionClass = new \ReflectionClass($this->className); } catch (\Exception $e) { throw $this->getClassException('Unable to create reflection class for "%s"', $e); } // Reading Entity annotation try { $entityAnnotation = $this->reader->getClassAnnotation($this->reflectionClass, Annotation\Entity::class); } catch (\Exception $e) { throw $this->getClassException('Unable to read Entity annotation for the reflection class of "%s"', $e); } // If there is no annotation if ($entityAnnotation === null) { throw $this->getClassException('Class "%s" has no "@Entity" annotation'); } /* @var $entityAnnotation Annotation\Entity */ // Preparing and validating targets if (!is_array($entityAnnotation->value)) { throw $this->getClassException('Invalid entity targets in "%s"'); } // Load export targets $this->usedTargets = []; foreach ($entityAnnotation->value as $exportTarget) { if (!$exportTarget instanceof Annotation\ExportTarget) { throw $this->getClassException('Invalid entity targets in "%s"'); } /* @var $exportTarget Annotation\ExportTarget */ if (!$exportTarget->name) { throw $this->getClassException('Invalid entity targets in "%s": empty name'); } $targetName = Standard::normalizeName($exportTarget->name); if (!array_key_exists($targetName, $this->targets)) { if (!$exportTarget->namespace) { throw $this->getClassException('Invalid entity targets in "%s": empty namespace'); } $this->targets[$targetName] = new Target($exportTarget->export()); } if (!in_array($targetName, $this->usedTargets)) { $this->usedTargets[] = $targetName; } } }