|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Sylius\RefundPlugin\Application; |
| 6 | + |
| 7 | +use ProxyManager\Proxy\VirtualProxyInterface; |
| 8 | +use PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer; |
| 9 | +use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; |
| 10 | +use Symfony\Component\Config\Loader\LoaderInterface; |
| 11 | +use Symfony\Component\Config\Resource\FileResource; |
| 12 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 13 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 14 | +use Symfony\Component\HttpKernel\Kernel as BaseKernel; |
| 15 | +use Symfony\Component\Routing\RouteCollectionBuilder; |
| 16 | + |
| 17 | +final class Kernel extends BaseKernel |
| 18 | +{ |
| 19 | + use MicroKernelTrait; |
| 20 | + |
| 21 | + private const CONFIG_EXTS = '.{php,xml,yaml,yml}'; |
| 22 | + |
| 23 | + private const IGNORED_SERVICES_DURING_CLEANUP = [ |
| 24 | + 'kernel', |
| 25 | + 'http_kernel', |
| 26 | + 'liip_imagine.mime_type_guesser', |
| 27 | + 'liip_imagine.extension_guesser', |
| 28 | + ]; |
| 29 | + |
| 30 | + public function getCacheDir(): string |
| 31 | + { |
| 32 | + return $this->getProjectDir() . '/var/cache/' . $this->environment; |
| 33 | + } |
| 34 | + |
| 35 | + public function getLogDir(): string |
| 36 | + { |
| 37 | + return $this->getProjectDir() . '/var/log'; |
| 38 | + } |
| 39 | + |
| 40 | + public function registerBundles(): iterable |
| 41 | + { |
| 42 | + $contents = require $this->getProjectDir() . '/config/bundles.php'; |
| 43 | + foreach ($contents as $class => $envs) { |
| 44 | + if (isset($envs['all']) || isset($envs[$this->environment])) { |
| 45 | + yield new $class(); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public function shutdown(): void |
| 51 | + { |
| 52 | + if (!$this->isTestEnvironment()) { |
| 53 | + parent::shutdown(); |
| 54 | + |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (false === $this->booted) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + $container = $this->getContainer(); |
| 63 | + |
| 64 | + parent::shutdown(); |
| 65 | + |
| 66 | + $this->cleanupContainer($container); |
| 67 | + } |
| 68 | + |
| 69 | + protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void |
| 70 | + { |
| 71 | + $container->addResource(new FileResource($this->getProjectDir() . '/config/bundles.php')); |
| 72 | + $container->setParameter('container.dumper.inline_class_loader', true); |
| 73 | + $confDir = $this->getProjectDir() . '/config'; |
| 74 | + |
| 75 | + $loader->load($confDir . '/{packages}/*' . self::CONFIG_EXTS, 'glob'); |
| 76 | + $loader->load($confDir . '/{packages}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, 'glob'); |
| 77 | + $loader->load($confDir . '/{services}' . self::CONFIG_EXTS, 'glob'); |
| 78 | + $loader->load($confDir . '/{services}_' . $this->environment . self::CONFIG_EXTS, 'glob'); |
| 79 | + } |
| 80 | + |
| 81 | + protected function configureRoutes(RouteCollectionBuilder $routes): void |
| 82 | + { |
| 83 | + $confDir = $this->getProjectDir() . '/config'; |
| 84 | + |
| 85 | + $routes->import($confDir . '/{routes}/*' . self::CONFIG_EXTS, '/', 'glob'); |
| 86 | + $routes->import($confDir . '/{routes}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, '/', 'glob'); |
| 87 | + $routes->import($confDir . '/{routes}' . self::CONFIG_EXTS, '/', 'glob'); |
| 88 | + } |
| 89 | + |
| 90 | + protected function getContainerBaseClass(): string |
| 91 | + { |
| 92 | + if ($this->isTestEnvironment()) { |
| 93 | + return MockerContainer::class; |
| 94 | + } |
| 95 | + |
| 96 | + return parent::getContainerBaseClass(); |
| 97 | + } |
| 98 | + |
| 99 | + private function isTestEnvironment(): bool |
| 100 | + { |
| 101 | + return 0 === strpos($this->getEnvironment(), 'test'); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Remove all container references from all loaded services |
| 106 | + */ |
| 107 | + private function cleanupContainer(ContainerInterface $container): void |
| 108 | + { |
| 109 | + $containerReflection = new \ReflectionObject($container); |
| 110 | + $containerServicesPropertyReflection = $containerReflection->getProperty('services'); |
| 111 | + $containerServicesPropertyReflection->setAccessible(true); |
| 112 | + |
| 113 | + $services = $containerServicesPropertyReflection->getValue($container) ?: []; |
| 114 | + foreach ($services as $serviceId => $service) { |
| 115 | + if (null === $service) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + if (in_array($serviceId, self::IGNORED_SERVICES_DURING_CLEANUP, true)) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + $serviceReflection = new \ReflectionObject($service); |
| 124 | + |
| 125 | + if ($serviceReflection->implementsInterface(VirtualProxyInterface::class)) { |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + $servicePropertiesReflections = $serviceReflection->getProperties(); |
| 130 | + $servicePropertiesDefaultValues = $serviceReflection->getDefaultProperties(); |
| 131 | + foreach ($servicePropertiesReflections as $servicePropertyReflection) { |
| 132 | + $defaultPropertyValue = null; |
| 133 | + if (isset($servicePropertiesDefaultValues[$servicePropertyReflection->getName()])) { |
| 134 | + $defaultPropertyValue = $servicePropertiesDefaultValues[$servicePropertyReflection->getName()]; |
| 135 | + } |
| 136 | + |
| 137 | + $servicePropertyReflection->setAccessible(true); |
| 138 | + $servicePropertyReflection->setValue($service, $defaultPropertyValue); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + $containerServicesPropertyReflection->setValue($container, null); |
| 143 | + } |
| 144 | +} |
0 commit comments