| Total Complexity | 48 |
| Total Lines | 299 |
| 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 | |||
| 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 |
||
| 104 | { |
||
| 105 | return array_key_exists($name, $this->plugins); |
||
| 106 | } |
||
| 107 | |||
| 108 | private function addAutoload(): void |
||
| 109 | { |
||
| 110 | $baseDir = Toolkit::getBaseDir(); |
||
| 111 | $autoloadFile = $baseDir.'/vendor/autoload.php'; |
||
| 112 | |||
| 113 | // ignore if files is already loaded in phar file |
||
| 114 | if ( |
||
| 115 | is_file($autoloadFile) && |
||
| 116 | (false === strpos($autoloadFile, 'phar:///')) |
||
| 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 getCachePathPrefix() |
||
| 162 | { |
||
| 163 | // using argv command to differ each dotfiles executable file |
||
| 164 | global $argv; |
||
| 165 | $command = $argv[0]; |
||
| 166 | $cacheDir = getenv('DOTFILES_CACHE_DIR'); |
||
| 167 | |||
| 168 | return $cacheDir.DIRECTORY_SEPARATOR.crc32($command); |
||
| 169 | } |
||
| 170 | |||
| 171 | private function getConfiguration() |
||
| 172 | { |
||
| 173 | $configDir = getenv('DOTFILES_CONFIG_DIR'); |
||
| 174 | if (!is_dir($configDir)) { |
||
| 175 | return array(); |
||
| 176 | } |
||
| 177 | $cacheFile = $this->getCachePathPrefix().'/config.php'; |
||
| 178 | $cache = new ConfigCache($cacheFile, $this->debug); |
||
| 179 | if (!$cache->isFresh() || 'dev' === $this->env) { |
||
| 180 | $finder = Finder::create() |
||
| 181 | ->name('*.yaml') |
||
| 182 | ->name('*.yml') |
||
| 183 | ->in($configDir) |
||
| 184 | ; |
||
| 185 | $configs = array(); |
||
| 186 | $configFiles = array(); |
||
| 187 | /* @var SplFileInfo $file */ |
||
| 188 | foreach ($finder->files() as $file) { |
||
| 189 | $parsed = Yaml::parseFile($file->getRealPath()); |
||
| 190 | if (is_array($parsed)) { |
||
| 191 | $configs = array_merge_recursive($configs, $parsed); |
||
| 192 | } |
||
| 193 | $configFiles[] = new FileResource($file->getRealPath()); |
||
| 194 | } |
||
| 195 | $template = "<?php\n/* ParameterBag Cache File Generated at %s */\nreturn %s;\n"; |
||
| 196 | $time = new \DateTime(); |
||
| 197 | $contents = sprintf( |
||
| 198 | $template, |
||
| 199 | $time->format('Y-m-d H:i:s'), |
||
| 200 | var_export($configs, true) |
||
| 201 | ); |
||
| 202 | $cache->write($contents, $configFiles); |
||
| 203 | } |
||
| 204 | |||
| 205 | return include $cacheFile; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Load available plugins directory. |
||
| 210 | * |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | private function loadDirectoryFromAutoloader() |
||
| 214 | { |
||
| 215 | $spl = spl_autoload_functions(); |
||
| 216 | |||
| 217 | $dirs = array(); |
||
| 218 | foreach ($spl as $item) { |
||
| 219 | $object = $item[0]; |
||
| 220 | if (!$object instanceof ClassLoader) { |
||
| 221 | continue; |
||
| 222 | } |
||
| 223 | $temp = array_merge($object->getPrefixes(), $object->getPrefixesPsr4()); |
||
| 224 | foreach ($temp as $name => $dir) { |
||
| 225 | if (false === strpos($name, 'Dotfiles')) { |
||
| 226 | continue; |
||
| 227 | } |
||
| 228 | foreach ($dir as $num => $path) { |
||
| 229 | $path = realpath($path); |
||
| 230 | if ($path && is_dir($path) && !in_array($path, $dirs)) { |
||
| 231 | $dirs[] = $path; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | return $dirs; |
||
| 238 | } |
||
| 239 | |||
| 240 | private function loadDotEnv(): void |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Load available plugins. |
||
| 283 | */ |
||
| 284 | private function loadPlugins(): void |
||
| 302 | } |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | private function processCoreConfig(array $configs, ContainerBuilder $builder): void |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Register plugin. |
||
| 323 | * |
||
| 324 | * @param Plugin $plugin |
||
| 325 | */ |
||
| 326 | private function registerPlugin(Plugin $plugin): void |
||
| 333 | } |
||
| 334 | } |
||
| 335 |
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: