| Total Complexity | 49 |
| Total Lines | 320 |
| 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 | * @codeCoverageIgnore |
||
| 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 |
||
| 107 | } |
||
| 108 | |||
| 109 | protected function compileContainer(): void |
||
| 148 | } |
||
| 149 | |||
| 150 | protected function getCachePathPrefix() |
||
| 151 | { |
||
| 152 | // using argv command to differ each dotfiles executable file |
||
| 153 | global $argv; |
||
| 154 | $command = $argv[0]; |
||
| 155 | $cacheDir = getenv('DOTFILES_CACHE_DIR'); |
||
| 156 | $env = getenv('DOTFILES_ENV'); |
||
| 157 | |||
| 158 | return $cacheDir.DIRECTORY_SEPARATOR.$this->getContainerId().DIRECTORY_SEPARATOR.$env; |
||
| 159 | } |
||
| 160 | |||
| 161 | protected function getContainerId() |
||
| 162 | { |
||
| 163 | global $argv; |
||
| 164 | $command = $argv[0]; |
||
| 165 | |||
| 166 | return crc32($command); |
||
| 167 | } |
||
| 168 | |||
| 169 | private function addAutoload(): void |
||
| 170 | { |
||
| 171 | $baseDir = getenv('DOTFILES_BACKUP_DIR'); |
||
| 172 | $autoloadFile = $baseDir.'/vendor/autoload.php'; |
||
| 173 | |||
| 174 | // ignore if files is already loaded in phar file |
||
| 175 | if ( |
||
| 176 | is_file($autoloadFile) |
||
| 177 | ) { |
||
| 178 | include_once $autoloadFile; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | private function ensureDir(): void |
||
| 183 | { |
||
| 184 | /* @var Parameters $parameters */ |
||
| 185 | $parameters = $this->container->get('dotfiles.parameters'); |
||
| 186 | Toolkit::ensureDir($parameters->get('dotfiles.install_dir')); |
||
| 187 | Toolkit::ensureDir($parameters->get('dotfiles.bin_dir')); |
||
| 188 | Toolkit::ensureDir($parameters->get('dotfiles.vendor_dir')); |
||
| 189 | } |
||
| 190 | |||
| 191 | private function getConfiguration() |
||
| 192 | { |
||
| 193 | $configDir = getenv('DOTFILES_CONFIG_DIR'); |
||
| 194 | if (!is_dir($configDir)) { |
||
| 195 | return array(); |
||
| 196 | } |
||
| 197 | $cacheFile = $this->getCachePathPrefix().'/config.php'; |
||
| 198 | $cache = new ConfigCache($cacheFile, $this->debug); |
||
| 199 | if (!$cache->isFresh() || 'dev' === $this->env) { |
||
| 200 | $finder = Finder::create() |
||
| 201 | ->name('*.yaml') |
||
| 202 | ->name('*.yml') |
||
| 203 | ->in($configDir) |
||
| 204 | ; |
||
| 205 | $configs = array(); |
||
| 206 | $configFiles = array(); |
||
| 207 | /* @var SplFileInfo $file */ |
||
| 208 | foreach ($finder->files() as $file) { |
||
| 209 | $parsed = Yaml::parseFile($file->getRealPath()); |
||
| 210 | if (is_array($parsed)) { |
||
| 211 | $configs = array_merge_recursive($configs, $parsed); |
||
| 212 | } |
||
| 213 | $configFiles[] = new FileResource($file->getRealPath()); |
||
| 214 | } |
||
| 215 | $template = "<?php\n/* ParameterBag Cache File Generated at %s */\nreturn %s;\n"; |
||
| 216 | $time = new \DateTime(); |
||
| 217 | $contents = sprintf( |
||
| 218 | $template, |
||
| 219 | $time->format('Y-m-d H:i:s'), |
||
| 220 | var_export($configs, true) |
||
| 221 | ); |
||
| 222 | $cache->write($contents, $configFiles); |
||
| 223 | } |
||
| 224 | |||
| 225 | return include $cacheFile; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Load available plugins directory. |
||
| 230 | * |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | private function loadDirectoryFromAutoloader() |
||
| 234 | { |
||
| 235 | $spl = spl_autoload_functions(); |
||
| 236 | |||
| 237 | $dirs = array(); |
||
| 238 | foreach ($spl as $item) { |
||
| 239 | $object = $item[0]; |
||
| 240 | if (!$object instanceof ClassLoader) { |
||
| 241 | continue; |
||
| 242 | } |
||
| 243 | $temp = array_merge($object->getPrefixes(), $object->getPrefixesPsr4()); |
||
| 244 | foreach ($temp as $name => $dir) { |
||
| 245 | if (false === strpos($name, 'Dotfiles')) { |
||
| 246 | continue; |
||
| 247 | } |
||
| 248 | foreach ($dir as $num => $path) { |
||
| 249 | $path = realpath($path); |
||
| 250 | if ($path && is_dir($path) && !in_array($path, $dirs)) { |
||
| 251 | $dirs[] = $path; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | return $dirs; |
||
| 258 | } |
||
| 259 | |||
| 260 | private function loadDotEnv(): void |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Load available plugins. |
||
| 303 | */ |
||
| 304 | private function loadPlugins(): void |
||
| 322 | } |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | private function processCoreConfig(array $configs, ContainerBuilder $builder): void |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Register plugin. |
||
| 343 | * |
||
| 344 | * @param Plugin $plugin |
||
| 345 | */ |
||
| 346 | private function registerPlugin(Plugin $plugin): void |
||
| 355 |
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: