|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CoreShop\Bundle\CoreBundle\Migrations; |
| 6 | + |
| 7 | +use CoreShop\Bundle\FrontendBundle\CoreShopFrontendBundle; |
| 8 | +use Doctrine\DBAL\Schema\Schema; |
| 9 | +use Doctrine\Migrations\AbstractMigration; |
| 10 | +use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
| 11 | +use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
| 12 | + |
| 13 | +final class Version20241018060845 extends AbstractMigration implements ContainerAwareInterface |
| 14 | +{ |
| 15 | + use ContainerAwareTrait; |
| 16 | + |
| 17 | + public function getDescription(): string |
| 18 | + { |
| 19 | + return 'Enable the CoreShop FrontendBundle in bundles.php'; |
| 20 | + } |
| 21 | + |
| 22 | + public function up(Schema $schema): void |
| 23 | + { |
| 24 | + $this->enableFrontendBundle(); |
| 25 | + } |
| 26 | + |
| 27 | + public function down(Schema $schema): void |
| 28 | + { |
| 29 | + $this->disableFrontendBundle(); |
| 30 | + } |
| 31 | + |
| 32 | + private function enableFrontendBundle(): void |
| 33 | + { |
| 34 | + $file = $this->getConfigFile(); |
| 35 | + $registered = $this->load($file); |
| 36 | + |
| 37 | + $class = CoreShopFrontendBundle::class; |
| 38 | + $bundle = ltrim($class, '\\'); |
| 39 | + |
| 40 | + if (!isset($registered[$bundle])) { |
| 41 | + $registered[$bundle] = ['all' => true]; |
| 42 | + } |
| 43 | + |
| 44 | + $this->dump($file, $registered); |
| 45 | + } |
| 46 | + |
| 47 | + private function disableFrontendBundle(): void |
| 48 | + { |
| 49 | + $file = $this->getConfigFile(); |
| 50 | + $registered = $this->load($file); |
| 51 | + |
| 52 | + $class = CoreShopFrontendBundle::class; |
| 53 | + $bundle = ltrim($class, '\\'); |
| 54 | + |
| 55 | + if (isset($registered[$bundle])) { |
| 56 | + unset($registered[$bundle]); |
| 57 | + } |
| 58 | + |
| 59 | + $this->dump($file, $registered); |
| 60 | + } |
| 61 | + |
| 62 | + private function dump(string $file, array $bundles) |
| 63 | + { |
| 64 | + $contents = $this->buildContents($bundles); |
| 65 | + |
| 66 | + if (!is_dir(\dirname($file))) { |
| 67 | + if (!mkdir($concurrentDirectory = \dirname($file), 0777, true) && !is_dir($concurrentDirectory)) { |
| 68 | + throw new \RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory)); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + file_put_contents($file, $contents); |
| 73 | + |
| 74 | + if (\function_exists('opcache_invalidate')) { |
| 75 | + opcache_invalidate($file); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private function buildContents(array $bundles): string |
| 80 | + { |
| 81 | + $contents = "<?php\n\nreturn [\n"; |
| 82 | + foreach ($bundles as $class => $envs) { |
| 83 | + $contents .= " $class::class => ["; |
| 84 | + foreach ($envs as $env => $value) { |
| 85 | + $booleanValue = var_export($value, true); |
| 86 | + $contents .= "'$env' => $booleanValue, "; |
| 87 | + } |
| 88 | + $contents = substr($contents, 0, -2)."],\n"; |
| 89 | + } |
| 90 | + $contents .= "];\n"; |
| 91 | + |
| 92 | + return $contents; |
| 93 | + } |
| 94 | + |
| 95 | + private function load(string $file): array |
| 96 | + { |
| 97 | + $bundles = file_exists($file) ? (require_once $file) : []; |
| 98 | + if (!\is_array($bundles)) { |
| 99 | + $bundles = []; |
| 100 | + } |
| 101 | + |
| 102 | + return $bundles; |
| 103 | + } |
| 104 | + |
| 105 | + private function getConfigFile(): string |
| 106 | + { |
| 107 | + return $this->container->getParameter('kernel.project_dir').'/config/bundles.php'; |
| 108 | + } |
| 109 | +} |
0 commit comments