| Total Complexity | 47 |
| Total Lines | 297 |
| 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 | if (is_file($file = getenv('HOME').'/.dotfiles_profile')) { |
||
| 66 | $files[] = $file; |
||
| 67 | } |
||
| 68 | |||
| 69 | // $PWD/.env always win |
||
| 70 | $cwd = getcwd(); |
||
| 71 | if (is_file($file = $cwd.'/.env.dist')) { |
||
| 72 | $files[] = $file; |
||
| 73 | } |
||
| 74 | if (is_file($file = $cwd.'/.env')) { |
||
| 75 | $files[] = $file; |
||
| 76 | } |
||
| 77 | |||
| 78 | $this->envFiles = $files; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return $this |
||
| 83 | */ |
||
| 84 | public function boot(): self |
||
| 85 | { |
||
| 86 | $this->loadDotEnv(); |
||
| 87 | $this->addAutoload(); |
||
| 88 | $this->loadPlugins(); |
||
| 89 | $this->compileContainer(); |
||
| 90 | |||
| 91 | return $this; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return Container |
||
| 96 | */ |
||
| 97 | public function getContainer(): Container |
||
| 98 | { |
||
| 99 | return $this->container; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param string $name |
||
| 104 | * |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | public function hasPlugin(string $name): bool |
||
| 108 | { |
||
| 109 | return array_key_exists($name, $this->plugins); |
||
| 110 | } |
||
| 111 | |||
| 112 | private function addAutoload(): void |
||
| 113 | { |
||
| 114 | $baseDir = Toolkit::getBaseDir(); |
||
| 115 | $autoloadFile = $baseDir.'/vendor/autoload.php'; |
||
| 116 | |||
| 117 | // ignore if files is already loaded in phar file |
||
| 118 | if ( |
||
| 119 | is_file($autoloadFile) && |
||
| 120 | (false === strpos($autoloadFile, 'phar:///')) |
||
| 121 | ) { |
||
| 122 | include_once $autoloadFile; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | private function compileContainer(): void |
||
| 127 | { |
||
| 128 | $configs = $this->getConfiguration(); |
||
| 129 | //$paramaterBag = new ParameterBag(); |
||
| 130 | $builder = new ContainerBuilder(); |
||
| 131 | $this->processCoreConfig($configs, $builder); |
||
| 132 | // processing core configuration |
||
| 133 | |||
| 134 | /* @var Plugin $plugin */ |
||
| 135 | foreach ($this->plugins as $name => $plugin) { |
||
| 136 | $pluginConfig = array_key_exists($name, $configs) ? $configs[$name] : array(); |
||
| 137 | $plugin->load($pluginConfig, $builder); |
||
| 138 | } |
||
| 139 | |||
| 140 | $cachePath = $this->getCachePathPrefix().'/container.php'; |
||
| 141 | $cache = new ConfigCache($cachePath, $this->debug); |
||
| 142 | if (!$cache->isFresh() || 'dev' == $this->env) { |
||
| 143 | $builder->addCompilerPass(new CommandPass()); |
||
| 144 | $builder->addCompilerPass(new ListenerPass()); |
||
| 145 | $builder->compile(true); |
||
| 146 | $dumper = new PhpDumper($builder); |
||
| 147 | $resources = $this->envFiles; |
||
| 148 | array_walk($resources, function (&$item): void { |
||
| 149 | $item = new FileResource($item); |
||
| 150 | }); |
||
| 151 | $resources = array_merge($resources, $builder->getResources()); |
||
| 152 | $cache->write($dumper->dump(array('class' => 'CachedContainer')), $resources); |
||
| 153 | } |
||
| 154 | if (!class_exists('CachedContainer')) { |
||
| 155 | include_once $cachePath; |
||
| 156 | } |
||
| 157 | $container = new \CachedContainer(); |
||
| 158 | |||
| 159 | $parameters = new Parameters(); |
||
| 160 | $parameters->setConfigs($container->getParameterBag()->all()); |
||
| 161 | $container->set('dotfiles.parameters', $parameters); |
||
| 162 | $this->container = $container; |
||
| 163 | } |
||
| 164 | |||
| 165 | private function getCachePathPrefix() |
||
| 166 | { |
||
| 167 | // using argv command to differ each dotfiles executable file |
||
| 168 | global $argv; |
||
| 169 | $command = $argv[0]; |
||
| 170 | $cacheDir = getenv('DOTFILES_CACHE_DIR'); |
||
| 171 | |||
| 172 | return $cacheDir.DIRECTORY_SEPARATOR.crc32($command); |
||
| 173 | } |
||
| 174 | |||
| 175 | private function getConfiguration() |
||
| 176 | { |
||
| 177 | $configDir = getenv('DOTFILES_CONFIG_DIR'); |
||
| 178 | if (!is_dir($configDir)) { |
||
| 179 | return array(); |
||
| 180 | } |
||
| 181 | $cacheFile = $this->getCachePathPrefix().'/config.php'; |
||
| 182 | $cache = new ConfigCache($cacheFile, $this->debug); |
||
| 183 | if (!$cache->isFresh() || 'dev' === $this->env) { |
||
| 184 | $finder = Finder::create() |
||
| 185 | ->name('*.yaml') |
||
| 186 | ->name('*.yml') |
||
| 187 | ->in($configDir) |
||
| 188 | ; |
||
| 189 | $configs = array(); |
||
| 190 | $configFiles = array(); |
||
| 191 | /* @var SplFileInfo $file */ |
||
| 192 | foreach ($finder->files() as $file) { |
||
| 193 | $parsed = Yaml::parseFile($file->getRealPath()); |
||
| 194 | if (is_array($parsed)) { |
||
| 195 | $configs = array_merge_recursive($configs, $parsed); |
||
| 196 | } |
||
| 197 | $configFiles[] = new FileResource($file->getRealPath()); |
||
| 198 | } |
||
| 199 | $template = "<?php\n/* ParameterBag Cache File Generated at %s */\nreturn %s;\n"; |
||
| 200 | $time = new \DateTime(); |
||
| 201 | $contents = sprintf( |
||
| 202 | $template, |
||
| 203 | $time->format('Y-m-d H:i:s'), |
||
| 204 | var_export($configs, true) |
||
| 205 | ); |
||
| 206 | $cache->write($contents, $configFiles); |
||
| 207 | } |
||
| 208 | |||
| 209 | return include $cacheFile; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Load available plugins directory. |
||
| 214 | * |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | private function loadDirectoryFromAutoloader() |
||
| 218 | { |
||
| 219 | $spl = spl_autoload_functions(); |
||
| 220 | |||
| 221 | $dirs = array(); |
||
| 222 | foreach ($spl as $item) { |
||
| 223 | $object = $item[0]; |
||
| 224 | if (!$object instanceof ClassLoader) { |
||
| 225 | continue; |
||
| 226 | } |
||
| 227 | $temp = array_merge($object->getPrefixes(), $object->getPrefixesPsr4()); |
||
| 228 | foreach ($temp as $name => $dir) { |
||
| 229 | if (false === strpos($name, 'Dotfiles')) { |
||
| 230 | continue; |
||
| 231 | } |
||
| 232 | foreach ($dir as $num => $path) { |
||
| 233 | $path = realpath($path); |
||
| 234 | if ($path && is_dir($path) && !in_array($path, $dirs)) { |
||
| 235 | $dirs[] = $path; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | return $dirs; |
||
| 242 | } |
||
| 243 | |||
| 244 | private function loadDotEnv(): void |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Load available plugins. |
||
| 281 | */ |
||
| 282 | private function loadPlugins(): void |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | private function processCoreConfig(array $configs, ContainerBuilder $builder): void |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Register plugin. |
||
| 321 | * |
||
| 322 | * @param Plugin $plugin |
||
| 323 | */ |
||
| 324 | private function registerPlugin(Plugin $plugin): void |
||
| 331 | } |
||
| 332 | } |
||
| 333 |
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: