| Total Complexity | 48 |
| Total Lines | 310 |
| 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 |
||
| 33 | class ApplicationFactory |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var Container |
||
| 37 | */ |
||
| 38 | private $container; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | private $debug = false; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $env; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $envFiles = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var Plugin[] |
||
| 57 | */ |
||
| 58 | private $plugins = array(); |
||
| 59 | |||
| 60 | public function __construct() |
||
| 61 | { |
||
| 62 | $files = array(__DIR__.'/Resources/default.env'); |
||
| 63 | |||
| 64 | // $PWD/.env always win |
||
| 65 | $cwd = getcwd(); |
||
| 66 | if (is_file($file = $cwd.'/.env.dist')) { |
||
| 67 | $files[] = $file; |
||
| 68 | } |
||
| 69 | if (is_file($file = $cwd.'/.env')) { |
||
| 70 | $files[] = $file; |
||
| 71 | } |
||
| 72 | |||
| 73 | $this->envFiles = $files; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return $this |
||
| 78 | */ |
||
| 79 | public function boot(): self |
||
| 80 | { |
||
| 81 | $this->loadDotEnv(); |
||
| 82 | $this->addAutoload(); |
||
| 83 | $this->loadPlugins(); |
||
| 84 | $this->compileContainer(); |
||
| 85 | $this->ensureDir(); |
||
| 86 | |||
| 87 | return $this; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return Container |
||
| 92 | */ |
||
| 93 | public function getContainer(): Container |
||
| 94 | { |
||
| 95 | return $this->container; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param string $name |
||
| 100 | * |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | public function hasPlugin(string $name): bool |
||
| 106 | } |
||
| 107 | |||
| 108 | protected function compileContainer(): void |
||
| 109 | { |
||
| 110 | $configs = $this->getConfiguration(); |
||
| 111 | $builder = new ContainerBuilder(); |
||
| 112 | $this->processCoreConfig($configs, $builder); |
||
| 113 | // processing core configuration |
||
| 114 | |||
| 115 | /* @var Plugin $plugin */ |
||
| 116 | foreach ($this->plugins as $name => $plugin) { |
||
| 117 | $pluginConfig[$name] = array_key_exists($name, $configs) ? $configs[$name] : array(); |
||
| 118 | |||
| 119 | $plugin->load($pluginConfig, $builder); |
||
| 120 | } |
||
| 121 | |||
| 122 | $cachePath = $this->getCachePathPrefix().'/container.php'; |
||
| 123 | $cache = new ConfigCache($cachePath, $this->debug); |
||
| 124 | |||
| 125 | // always compile container in dev environment |
||
| 126 | if (!$cache->isFresh() || 'prod' !== $this->env) { |
||
| 127 | $builder->addCompilerPass(new DefaultPass()); |
||
| 128 | $builder->compile(true); |
||
| 129 | $dumper = new PhpDumper($builder); |
||
| 130 | $resources = $this->envFiles; |
||
| 131 | array_walk($resources, function (&$item): void { |
||
| 132 | $item = new FileResource($item); |
||
| 133 | }); |
||
| 134 | $resources = array_merge($resources, $builder->getResources()); |
||
| 135 | $cache->write($dumper->dump(array('class' => 'CachedContainer')), $resources); |
||
| 136 | } |
||
| 137 | if (!class_exists('CachedContainer')) { |
||
| 138 | include_once $cachePath; |
||
| 139 | } |
||
| 140 | $container = new \CachedContainer(); |
||
| 141 | |||
| 142 | $parameters = new Parameters(); |
||
| 143 | $parameters->setConfigs($container->getParameterBag()->all()); |
||
| 144 | $container->set('dotfiles.parameters', $parameters); |
||
| 145 | $this->container = $container; |
||
| 146 | } |
||
| 147 | |||
| 148 | private function addAutoload(): void |
||
| 149 | { |
||
| 150 | $baseDir = getenv('DOTFILES_BACKUP_DIR'); |
||
| 151 | $autoloadFile = $baseDir.'/vendor/autoload.php'; |
||
| 152 | |||
| 153 | // ignore if files is already loaded in phar file |
||
| 154 | if ( |
||
| 155 | is_file($autoloadFile) |
||
| 156 | ) { |
||
| 157 | include_once $autoloadFile; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | private function ensureDir(): void |
||
| 168 | } |
||
| 169 | |||
| 170 | private function getCachePathPrefix() |
||
| 171 | { |
||
| 172 | // using argv command to differ each dotfiles executable file |
||
| 173 | global $argv; |
||
| 174 | $command = $argv[0]; |
||
| 175 | $cacheDir = getenv('DOTFILES_CACHE_DIR'); |
||
| 176 | $env = getenv('DOTFILES_ENV'); |
||
| 177 | |||
| 178 | return $cacheDir.DIRECTORY_SEPARATOR.crc32($command).DIRECTORY_SEPARATOR.$env; |
||
| 179 | } |
||
| 180 | |||
| 181 | private function getConfiguration() |
||
| 182 | { |
||
| 183 | $configDir = getenv('DOTFILES_CONFIG_DIR'); |
||
| 184 | if (!is_dir($configDir)) { |
||
| 185 | return array(); |
||
| 186 | } |
||
| 187 | $cacheFile = $this->getCachePathPrefix().'/config.php'; |
||
| 188 | $cache = new ConfigCache($cacheFile, $this->debug); |
||
| 189 | if (!$cache->isFresh() || 'dev' === $this->env) { |
||
| 190 | $finder = Finder::create() |
||
| 191 | ->name('*.yaml') |
||
| 192 | ->name('*.yml') |
||
| 193 | ->in($configDir) |
||
| 194 | ; |
||
| 195 | $configs = array(); |
||
| 196 | $configFiles = array(); |
||
| 197 | /* @var SplFileInfo $file */ |
||
| 198 | foreach ($finder->files() as $file) { |
||
| 199 | $parsed = Yaml::parseFile($file->getRealPath()); |
||
| 200 | if (is_array($parsed)) { |
||
| 201 | $configs = array_merge_recursive($configs, $parsed); |
||
| 202 | } |
||
| 203 | $configFiles[] = new FileResource($file->getRealPath()); |
||
| 204 | } |
||
| 205 | $template = "<?php\n/* ParameterBag Cache File Generated at %s */\nreturn %s;\n"; |
||
| 206 | $time = new \DateTime(); |
||
| 207 | $contents = sprintf( |
||
| 208 | $template, |
||
| 209 | $time->format('Y-m-d H:i:s'), |
||
| 210 | var_export($configs, true) |
||
| 211 | ); |
||
| 212 | $cache->write($contents, $configFiles); |
||
| 213 | } |
||
| 214 | |||
| 215 | return include $cacheFile; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Load available plugins directory. |
||
| 220 | * |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | private function loadDirectoryFromAutoloader() |
||
| 224 | { |
||
| 225 | $spl = spl_autoload_functions(); |
||
| 226 | |||
| 227 | $dirs = array(); |
||
| 228 | foreach ($spl as $item) { |
||
| 229 | $object = $item[0]; |
||
| 230 | if (!$object instanceof ClassLoader) { |
||
| 231 | continue; |
||
| 232 | } |
||
| 233 | $temp = array_merge($object->getPrefixes(), $object->getPrefixesPsr4()); |
||
| 234 | foreach ($temp as $name => $dir) { |
||
| 235 | if (false === strpos($name, 'Dotfiles')) { |
||
| 236 | continue; |
||
| 237 | } |
||
| 238 | foreach ($dir as $num => $path) { |
||
| 239 | $path = realpath($path); |
||
| 240 | if ($path && is_dir($path) && !in_array($path, $dirs)) { |
||
| 241 | $dirs[] = $path; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | return $dirs; |
||
| 248 | } |
||
| 249 | |||
| 250 | private function loadDotEnv(): void |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Load available plugins. |
||
| 293 | */ |
||
| 294 | private function loadPlugins(): void |
||
| 312 | } |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | private function processCoreConfig(array $configs, ContainerBuilder $builder): void |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Register plugin. |
||
| 333 | * |
||
| 334 | * @param Plugin $plugin |
||
| 335 | */ |
||
| 336 | private function registerPlugin(Plugin $plugin): void |
||
| 343 | } |
||
| 344 | } |
||
| 345 |
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: