| Total Complexity | 48 |
| Total Lines | 311 |
| 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() |
||
| 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 |
||
| 107 | } |
||
| 108 | |||
| 109 | protected function compileContainer(): void |
||
| 110 | { |
||
| 111 | $configs = $this->getConfiguration(); |
||
| 112 | $builder = new ContainerBuilder(); |
||
| 113 | $this->processCoreConfig($configs, $builder); |
||
| 114 | // processing core configuration |
||
| 115 | |||
| 116 | /* @var Plugin $plugin */ |
||
| 117 | foreach ($this->plugins as $name => $plugin) { |
||
| 118 | $pluginConfig[$name] = array_key_exists($name, $configs) ? $configs[$name] : array(); |
||
| 119 | |||
| 120 | $plugin->load($pluginConfig, $builder); |
||
| 121 | } |
||
| 122 | |||
| 123 | $cachePath = $this->getCachePathPrefix().'/container.php'; |
||
| 124 | $cache = new ConfigCache($cachePath, $this->debug); |
||
| 125 | |||
| 126 | // always compile container in dev environment |
||
| 127 | if (!$cache->isFresh() || 'dev' === $this->env) { |
||
| 128 | $builder->addCompilerPass(new CommandPass()); |
||
| 129 | $builder->addCompilerPass(new ListenerPass()); |
||
| 130 | $builder->compile(true); |
||
| 131 | $dumper = new PhpDumper($builder); |
||
| 132 | $resources = $this->envFiles; |
||
| 133 | array_walk($resources, function (&$item): void { |
||
| 134 | $item = new FileResource($item); |
||
| 135 | }); |
||
| 136 | $resources = array_merge($resources, $builder->getResources()); |
||
| 137 | $cache->write($dumper->dump(array('class' => 'CachedContainer')), $resources); |
||
| 138 | } |
||
| 139 | if (!class_exists('CachedContainer')) { |
||
| 140 | include_once $cachePath; |
||
| 141 | } |
||
| 142 | $container = new \CachedContainer(); |
||
| 143 | |||
| 144 | $parameters = new Parameters(); |
||
| 145 | $parameters->setConfigs($container->getParameterBag()->all()); |
||
| 146 | $container->set('dotfiles.parameters', $parameters); |
||
| 147 | $this->container = $container; |
||
| 148 | } |
||
| 149 | |||
| 150 | private function addAutoload(): void |
||
| 151 | { |
||
| 152 | $baseDir = getenv('DOTFILES_BACKUP_DIR'); |
||
| 153 | $autoloadFile = $baseDir.'/vendor/autoload.php'; |
||
| 154 | |||
| 155 | // ignore if files is already loaded in phar file |
||
| 156 | if ( |
||
| 157 | is_file($autoloadFile) |
||
| 158 | ) { |
||
| 159 | include_once $autoloadFile; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | private function ensureDir(): void |
||
| 164 | { |
||
| 165 | /* @var Parameters $parameters */ |
||
| 166 | $parameters = $this->container->get('dotfiles.parameters'); |
||
| 167 | Toolkit::ensureDir($parameters->get('dotfiles.install_dir')); |
||
| 168 | Toolkit::ensureDir($parameters->get('dotfiles.bin_dir')); |
||
| 169 | Toolkit::ensureDir($parameters->get('dotfiles.vendor_dir')); |
||
| 170 | } |
||
| 171 | |||
| 172 | private function getCachePathPrefix() |
||
| 173 | { |
||
| 174 | // using argv command to differ each dotfiles executable file |
||
| 175 | global $argv; |
||
| 176 | $command = $argv[0]; |
||
| 177 | $cacheDir = getenv('DOTFILES_CACHE_DIR'); |
||
| 178 | $env = getenv('DOTFILES_ENV'); |
||
| 179 | |||
| 180 | return $cacheDir.DIRECTORY_SEPARATOR.crc32($command).DIRECTORY_SEPARATOR.$env; |
||
| 181 | } |
||
| 182 | |||
| 183 | private function getConfiguration() |
||
| 184 | { |
||
| 185 | $configDir = getenv('DOTFILES_CONFIG_DIR'); |
||
| 186 | if (!is_dir($configDir)) { |
||
| 187 | return array(); |
||
| 188 | } |
||
| 189 | $cacheFile = $this->getCachePathPrefix().'/config.php'; |
||
| 190 | $cache = new ConfigCache($cacheFile, $this->debug); |
||
| 191 | if (!$cache->isFresh() || 'dev' === $this->env) { |
||
| 192 | $finder = Finder::create() |
||
| 193 | ->name('*.yaml') |
||
| 194 | ->name('*.yml') |
||
| 195 | ->in($configDir) |
||
| 196 | ; |
||
| 197 | $configs = array(); |
||
| 198 | $configFiles = array(); |
||
| 199 | /* @var SplFileInfo $file */ |
||
| 200 | foreach ($finder->files() as $file) { |
||
| 201 | $parsed = Yaml::parseFile($file->getRealPath()); |
||
| 202 | if (is_array($parsed)) { |
||
| 203 | $configs = array_merge_recursive($configs, $parsed); |
||
| 204 | } |
||
| 205 | $configFiles[] = new FileResource($file->getRealPath()); |
||
| 206 | } |
||
| 207 | $template = "<?php\n/* ParameterBag Cache File Generated at %s */\nreturn %s;\n"; |
||
| 208 | $time = new \DateTime(); |
||
| 209 | $contents = sprintf( |
||
| 210 | $template, |
||
| 211 | $time->format('Y-m-d H:i:s'), |
||
| 212 | var_export($configs, true) |
||
| 213 | ); |
||
| 214 | $cache->write($contents, $configFiles); |
||
| 215 | } |
||
| 216 | |||
| 217 | return include $cacheFile; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Load available plugins directory. |
||
| 222 | * |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | private function loadDirectoryFromAutoloader() |
||
| 226 | { |
||
| 227 | $spl = spl_autoload_functions(); |
||
| 228 | |||
| 229 | $dirs = array(); |
||
| 230 | foreach ($spl as $item) { |
||
| 231 | $object = $item[0]; |
||
| 232 | if (!$object instanceof ClassLoader) { |
||
| 233 | continue; |
||
| 234 | } |
||
| 235 | $temp = array_merge($object->getPrefixes(), $object->getPrefixesPsr4()); |
||
| 236 | foreach ($temp as $name => $dir) { |
||
| 237 | if (false === strpos($name, 'Dotfiles')) { |
||
| 238 | continue; |
||
| 239 | } |
||
| 240 | foreach ($dir as $num => $path) { |
||
| 241 | $path = realpath($path); |
||
| 242 | if ($path && is_dir($path) && !in_array($path, $dirs)) { |
||
| 243 | $dirs[] = $path; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | return $dirs; |
||
| 250 | } |
||
| 251 | |||
| 252 | private function loadDotEnv(): void |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Load available plugins. |
||
| 295 | */ |
||
| 296 | private function loadPlugins(): void |
||
| 314 | } |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 318 | private function processCoreConfig(array $configs, ContainerBuilder $builder): void |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Register plugin. |
||
| 335 | * |
||
| 336 | * @param Plugin $plugin |
||
| 337 | */ |
||
| 338 | private function registerPlugin(Plugin $plugin): void |
||
| 347 |
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: