container-compiler.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\Config\ConfigCache;
  9. use Symfony\Component\DependencyInjection\ContainerBuilder;
  10. use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
  11. /**
  12. * This class is responsible for compiling the dependency injection container.
  13. */
  14. class Container_Compiler {
  15. /**
  16. * Compiles the dependency injection container.
  17. *
  18. * @param boolean $debug If false the container will only be re-compiled if it does not yet already exist.
  19. *
  20. * @throws \Exception If compiling the container fails.
  21. *
  22. * @return void
  23. */
  24. public static function compile( $debug ) {
  25. $file = __DIR__ . '/../../src/generated/container.php';
  26. $cache = new ConfigCache( $file, $debug );
  27. if ( ! $cache->isFresh() ) {
  28. $container_builder = new ContainerBuilder();
  29. $container_builder->addCompilerPass( new Loader_Pass() );
  30. $loader = new Custom_Loader( $container_builder );
  31. $loader->load( 'config/dependency-injection/services.php' );
  32. $container_builder->compile();
  33. $dumper = new PhpDumper( $container_builder );
  34. $code = $dumper->dump(
  35. [
  36. 'class' => 'Cached_Container',
  37. 'namespace' => 'Yoast\WP\Free\Generated',
  38. ]
  39. );
  40. $code = \str_replace( 'Symfony\\Component\\DependencyInjection', 'YoastSEO_Vendor\\Symfony\\Component\\DependencyInjection', $code );
  41. $code = \str_replace( 'Symfony\\\\Component\\\\DependencyInjection', 'YoastSEO_Vendor\\\\Symfony\\\\Component\\\\DependencyInjection', $code );
  42. $cache->write( $code, $container_builder->getResources() );
  43. }
  44. }
  45. }