loader-pass.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Yoast SEO Plugin File.
  4. *
  5. * @package Yoast\YoastSEO\Dependency_Injection
  6. */
  7. namespace Yoast\WP\Free\Dependency_Injection;
  8. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  9. use Symfony\Component\DependencyInjection\ContainerBuilder;
  10. use Symfony\Component\DependencyInjection\Definition;
  11. use Symfony\Component\DependencyInjection\Reference;
  12. use Yoast\WP\Free\Conditionals\Conditional;
  13. use Yoast\WP\Free\Loader;
  14. use Yoast\WP\Free\WordPress\Initializer;
  15. use Yoast\WP\Free\WordPress\Integration;
  16. /**
  17. * A pass is a step in the compilation process of the container.
  18. *
  19. * This step will automatically ensure all classes implementing the Integration interface
  20. * are registered with the Loader class.
  21. */
  22. class Loader_Pass implements CompilerPassInterface {
  23. /**
  24. * Checks all definitions to ensure all classes implementing the Integration interface
  25. * are registered with the Loader class.
  26. *
  27. * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container The container.
  28. */
  29. public function process( ContainerBuilder $container ) {
  30. if ( ! $container->hasDefinition( Loader::class ) ) {
  31. return;
  32. }
  33. $loader_definition = $container->getDefinition( Loader::class );
  34. $loader_definition->setArgument( 0, new Reference( 'service_container' ) );
  35. $loader_definition->setPublic( true );
  36. $definitions = $container->getDefinitions();
  37. foreach ( $definitions as $definition ) {
  38. $this->process_definition( $definition, $loader_definition );
  39. }
  40. }
  41. /**
  42. * Processes a definition in the container.
  43. *
  44. * @param \Symfony\Component\DependencyInjection\Definition $definition The definition to process.
  45. * @param \Symfony\Component\DependencyInjection\Definition $loader_definition The loader definition.
  46. */
  47. private function process_definition( Definition $definition, Definition $loader_definition ) {
  48. $class = $definition->getClass();
  49. if ( \is_subclass_of( $class, Initializer::class ) ) {
  50. $loader_definition->addMethodCall( 'register_initializer', [ $class ] );
  51. $definition->setPublic( true );
  52. }
  53. if ( \is_subclass_of( $class, Integration::class ) ) {
  54. $loader_definition->addMethodCall( 'register_integration', [ $class ] );
  55. $definition->setPublic( true );
  56. }
  57. if ( \is_subclass_of( $class, Conditional::class ) ) {
  58. $definition->setPublic( true );
  59. }
  60. }
  61. }