From e05031a7436bf733b274922b8c9cdbc8ecc1c53a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 28 Jul 2020 13:10:59 +0200 Subject: [PATCH] make timeline & paginator service private --- ChillMainBundle.php | 2 +- DependencyInjection/ChillMainExtension.php | 1 + .../TimelineCompilerClass.php | 12 ++--- Resources/config/services.yml | 8 ---- Resources/config/services/pagination.yml | 1 - Resources/config/services/timeline.yml | 7 +++ Timeline/TimelineBuilder.php | 47 ++++++++++++------- 7 files changed, 45 insertions(+), 33 deletions(-) rename DependencyInjection/{ => CompilerPass}/TimelineCompilerClass.php (83%) create mode 100644 Resources/config/services/timeline.yml diff --git a/ChillMainBundle.php b/ChillMainBundle.php index 2b4b4649b..7cd34037c 100644 --- a/ChillMainBundle.php +++ b/ChillMainBundle.php @@ -6,7 +6,7 @@ use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\DependencyInjection\ContainerBuilder; use Chill\MainBundle\DependencyInjection\SearchableServicesCompilerPass; use Chill\MainBundle\DependencyInjection\ConfigConsistencyCompilerPass; -use Chill\MainBundle\DependencyInjection\TimelineCompilerClass; +use Chill\MainBundle\DependencyInjection\CompilerPass\TimelineCompilerClass; use Chill\MainBundle\DependencyInjection\RoleProvidersCompilerPass; use Chill\MainBundle\DependencyInjection\CompilerPass\ExportsCompilerPass; use Chill\MainBundle\DependencyInjection\CompilerPass\WidgetsCompilerPass; diff --git a/DependencyInjection/ChillMainExtension.php b/DependencyInjection/ChillMainExtension.php index d33ef4eb9..92092d889 100644 --- a/DependencyInjection/ChillMainExtension.php +++ b/DependencyInjection/ChillMainExtension.php @@ -120,6 +120,7 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface, $loader->load('services/phonenumber.yml'); $loader->load('services/cache.yml'); $loader->load('services/templating.yml'); + $loader->load('services/timeline.yml'); $this->configureCruds($container, $config['cruds'], $loader); } diff --git a/DependencyInjection/TimelineCompilerClass.php b/DependencyInjection/CompilerPass/TimelineCompilerClass.php similarity index 83% rename from DependencyInjection/TimelineCompilerClass.php rename to DependencyInjection/CompilerPass/TimelineCompilerClass.php index 92aaaa003..d5b8efbde 100644 --- a/DependencyInjection/TimelineCompilerClass.php +++ b/DependencyInjection/CompilerPass/TimelineCompilerClass.php @@ -17,28 +17,28 @@ * along with this program. If not, see . */ -namespace Chill\MainBundle\DependencyInjection; +namespace Chill\MainBundle\DependencyInjection\CompilerPass; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; /** * Add services taggued with `name: chill.timeline` to * timeline_builder service definition * - * @author Julien Fastré */ class TimelineCompilerClass implements CompilerPassInterface { public function process(ContainerBuilder $container) { - if (!$container->hasDefinition('chill.main.timeline_builder')) { - throw new \LogicException('service chill.main.timeline_builder ' + if (!$container->hasDefinition('chill_main.timeline_builder')) { + throw new \LogicException('service chill_main.timeline_builder ' . 'is not defined.'); } $definition = $container->getDefinition( - 'chill.main.timeline_builder' + 'chill_main.timeline_builder' ); $taggedServices = $container->findTaggedServiceIds( @@ -56,7 +56,7 @@ class TimelineCompilerClass implements CompilerPassInterface $definition->addMethodCall( 'addProvider', - array($attributes["context"], $id) + array($attributes["context"], $id, new Reference($id)) ); } } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 9fdbd6a62..3f2c59506 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -40,14 +40,6 @@ services: chill.main.search_provider: class: Chill\MainBundle\Search\SearchProvider public: true - - chill.main.timeline_builder: - class: Chill\MainBundle\Timeline\TimelineBuilder - arguments: - - "@doctrine.orm.entity_manager" - public: true - calls: - - [ setContainer, ["@service_container"]] chill.main.validator.role_scope_scope_presence: class: Chill\MainBundle\Validation\Validator\RoleScopeScopePresence diff --git a/Resources/config/services/pagination.yml b/Resources/config/services/pagination.yml index 95ea5a387..ca9b50e4f 100644 --- a/Resources/config/services/pagination.yml +++ b/Resources/config/services/pagination.yml @@ -1,7 +1,6 @@ services: chill_main.paginator_factory: class: Chill\MainBundle\Pagination\PaginatorFactory - public: true # TODO sf4 check if service is public or if container is must be loaded via __constuct arguments: - "@request_stack" - "@router" diff --git a/Resources/config/services/timeline.yml b/Resources/config/services/timeline.yml new file mode 100644 index 000000000..241f6a6f1 --- /dev/null +++ b/Resources/config/services/timeline.yml @@ -0,0 +1,7 @@ +services: + chill_main.timeline_builder: + class: Chill\MainBundle\Timeline\TimelineBuilder + arguments: + - "@doctrine.orm.entity_manager" + calls: + - [ setContainer, ["@service_container"]] \ No newline at end of file diff --git a/Timeline/TimelineBuilder.php b/Timeline/TimelineBuilder.php index eb95d6c1c..1c5f364e2 100644 --- a/Timeline/TimelineBuilder.php +++ b/Timeline/TimelineBuilder.php @@ -40,17 +40,29 @@ class TimelineBuilder implements ContainerAwareInterface */ private $em; + /** + * Record provider + * + * This array has the structure `[ 'service id' => $service ]` + * + * @var TimelineProviderInterface[] + */ + private $providers = []; + + /** + * Record provider and their context + * + * This array has the structure `[ 'context' => [ 'service id' ] ]` + * + * @var array + */ + private $providersByContext = []; + public function __construct(EntityManagerInterface $em) { $this->em = $em; } - /** - * - * @var string references to providers services - */ - private $providers = array(); - /** * return an HTML string with timeline * @@ -108,9 +120,10 @@ class TimelineBuilder implements ContainerAwareInterface * @param string $context the context of the service * @param string $id the */ - public function addProvider($context, $id) + public function addProvider($context, $id, TimelineProviderInterface $provider) { - $this->providers[$context][] = $id; + $this->providersByContext[$context][] = $id; + $this->providers[$id] = $provider; } /** @@ -121,10 +134,16 @@ class TimelineBuilder implements ContainerAwareInterface */ public function getProvidersByContext($context) { - $providers = array(); + //throw an exception if no provider have been defined for this context + if (!array_key_exists($context, $this->providersByContext)) { + throw new \LogicException(sprintf('No builders have been defined for "%s"' + . ' context', $context)); + } + + $providers = []; - foreach($this->providers[$context] as $providerId) { - $providers[] = $this->container->get($providerId); + foreach($this->providersByContext[$context] as $providerId) { + $providers[] = $this->providers[$providerId]; } return $providers; @@ -144,12 +163,6 @@ class TimelineBuilder implements ContainerAwareInterface */ private function buildUnionQuery($context, array $args) { - //throw an exception if no provider have been defined for this context - if (!array_key_exists($context, $this->providers)) { - throw new \LogicException(sprintf('No builders have been defined for "%s"' - . ' context', $context)); - } - //append SELECT queries with UNION keyword between them $union = ''; foreach($this->getProvidersByContext($context) as $provider) {