| Total Complexity | 48 |
| Total Lines | 308 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ApplicationFactory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ApplicationFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class ApplicationFactory |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var Container |
||
| 38 | */ |
||
| 39 | private $container; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | private $debug = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $env; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $envFiles = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Plugin[] |
||
| 58 | */ |
||
| 59 | private $plugins = array(); |
||
| 60 | |||
| 61 | public function __construct() |
||
| 62 | { |
||
| 63 | $files = array(__DIR__.'/Resources/default.env'); |
||
| 64 | |||
| 65 | // $PWD/.env always win |
||
| 66 | $cwd = getcwd(); |
||
| 67 | if (is_file($file = $cwd.'/.env.dist')) { |
||
| 68 | $files[] = $file; |
||
| 69 | } |
||
| 70 | if (is_file($file = $cwd.'/.env')) { |
||
| 71 | $files[] = $file; |
||
| 72 | } |
||
| 73 | |||
| 74 | $this->envFiles = $files; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @return $this |
||
| 79 | */ |
||
| 80 | public function boot(): self |
||
| 81 | { |
||
| 82 | $this->loadDotEnv(); |
||
| 83 | $this->addAutoload(); |
||
| 84 | $this->loadPlugins(); |
||
| 85 | $this->compileContainer(); |
||
| 86 | $this->ensureDir(); |
||
| 87 | |||
| 88 | return $this; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return Container |
||
| 93 | */ |
||
| 94 | public function getContainer(): Container |
||
| 95 | { |
||
| 96 | return $this->container; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $name |
||
| 101 | * |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function hasPlugin(string $name): bool |
||
| 105 | { |
||
| 106 | return array_key_exists($name, $this->plugins); |
||
| 107 | } |
||
| 108 | |||
| 109 | private function addAutoload(): void |
||
| 110 | { |
||
| 111 | $baseDir = getenv('DOTFILES_BACKUP_DIR'); |
||
| 112 | $autoloadFile = $baseDir.'/vendor/autoload.php'; |
||
| 113 | |||
| 114 | // ignore if files is already loaded in phar file |
||
| 115 | if ( |
||
| 116 | is_file($autoloadFile) |
||
| 117 | ) { |
||
| 118 | include_once $autoloadFile; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | private function compileContainer(): void |
||
| 123 | { |
||
| 124 | $configs = $this->getConfiguration(); |
||
| 125 | //$paramaterBag = new ParameterBag(); |
||
| 126 | $builder = new ContainerBuilder(); |
||
| 127 | $this->processCoreConfig($configs, $builder); |
||
| 128 | // processing core configuration |
||
| 129 | |||
| 130 | /* @var Plugin $plugin */ |
||
| 131 | foreach ($this->plugins as $name => $plugin) { |
||
| 132 | $pluginConfig = array_key_exists($name, $configs) ? $configs[$name] : array(); |
||
| 133 | $plugin->load($pluginConfig, $builder); |
||
| 134 | } |
||
| 135 | |||
| 136 | $cachePath = $this->getCachePathPrefix().'/container.php'; |
||
| 137 | $cache = new ConfigCache($cachePath, $this->debug); |
||
| 138 | if (!$cache->isFresh() || 'dev' == $this->env) { |
||
| 139 | $builder->addCompilerPass(new CommandPass()); |
||
| 140 | $builder->addCompilerPass(new ListenerPass()); |
||
| 141 | $builder->compile(true); |
||
| 142 | $dumper = new PhpDumper($builder); |
||
| 143 | $resources = $this->envFiles; |
||
| 144 | array_walk($resources, function (&$item): void { |
||
| 145 | $item = new FileResource($item); |
||
| 146 | }); |
||
| 147 | $resources = array_merge($resources, $builder->getResources()); |
||
| 148 | $cache->write($dumper->dump(array('class' => 'CachedContainer')), $resources); |
||
| 149 | } |
||
| 150 | if (!class_exists('CachedContainer')) { |
||
| 151 | include_once $cachePath; |
||
| 152 | } |
||
| 153 | $container = new \CachedContainer(); |
||
| 154 | |||
| 155 | $parameters = new Parameters(); |
||
| 156 | $parameters->setConfigs($container->getParameterBag()->all()); |
||
| 157 | $container->set('dotfiles.parameters', $parameters); |
||
| 158 | $this->container = $container; |
||
| 159 | } |
||
| 160 | |||
| 161 | private function ensureDir(): void |
||
| 162 | { |
||
| 163 | /* @var Parameters $parameters */ |
||
| 164 | $parameters = $this->container->get('dotfiles.parameters'); |
||
| 165 | Toolkit::ensureDir($parameters->get('dotfiles.install_dir')); |
||
| 166 | Toolkit::ensureDir($parameters->get('dotfiles.bin_dir')); |
||
| 167 | Toolkit::ensureDir($parameters->get('dotfiles.vendor_dir')); |
||
| 168 | } |
||
| 169 | |||
| 170 | private function getCachePathPrefix() |
||
| 178 | } |
||
| 179 | |||
| 180 | private function getConfiguration() |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Load available plugins directory. |
||
| 219 | * |
||
| 220 | * @return array |
||
| 221 | */ |
||
| 222 | private function loadDirectoryFromAutoloader() |
||
| 247 | } |
||
| 248 | |||
| 249 | private function loadDotEnv(): void |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Load available plugins. |
||
| 292 | */ |
||
| 293 | private function loadPlugins(): void |
||
| 311 | } |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | private function processCoreConfig(array $configs, ContainerBuilder $builder): void |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Register plugin. |
||
| 332 | * |
||
| 333 | * @param Plugin $plugin |
||
| 334 | */ |
||
| 335 | private function registerPlugin(Plugin $plugin): void |
||
| 342 | } |
||
| 343 | } |
||
| 344 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: