Completed
Pull Request — master (#9)
by ANTHONIUS
02:53
created

ApplicationFactory::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the dotfiles project.
7
 *
8
 *     (c) Anthonius Munthi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Dotfiles\Core;
15
16
use Composer\Autoload\ClassLoader;
17
use Dotfiles\Core\DI\Compiler\DefaultPass;
18
use Dotfiles\Core\DI\Parameters;
19
use Dotfiles\Core\Util\Toolkit;
20
use Symfony\Component\Config\ConfigCache;
21
use Symfony\Component\Config\Definition\Processor;
22
use Symfony\Component\Config\FileLocator;
23
use Symfony\Component\Config\Resource\FileResource;
24
use Symfony\Component\DependencyInjection\Container;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Dotfiles\Core\Container. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
25
use Symfony\Component\DependencyInjection\ContainerBuilder;
26
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
27
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
28
use Symfony\Component\Dotenv\Dotenv;
29
use Symfony\Component\Finder\Finder;
30
use Symfony\Component\Finder\SplFileInfo;
31
use Symfony\Component\Yaml\Yaml;
32
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
104
    {
105
        return array_key_exists($name, $this->plugins);
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);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pluginConfig seems to be defined later in this foreach loop on line 117. Are you sure it is defined here?
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $dumper->dump(array('cla... => 'CachedContainer')) can also be of type array; however, parameter $content of Symfony\Component\Config...kerConfigCache::write() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
            $cache->write(/** @scrutinizer ignore-type */ $dumper->dump(array('class' => 'CachedContainer')), $resources);
Loading history...
136
        }
137
        if (!class_exists('CachedContainer')) {
138
            include_once $cachePath;
139
        }
140
        $container = new \CachedContainer();
0 ignored issues
show
Bug introduced by
The type CachedContainer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
162
    {
163
        /* @var Parameters $parameters */
164
        $parameters = $this->container->get('dotfiles.parameters');
165
        Toolkit::ensureDir($parameters->get('dotfiles.install_dir'));
166
        Toolkit::ensureDir($parameters->get('dotfiles.bin_dir'));
167
        Toolkit::ensureDir($parameters->get('dotfiles.vendor_dir'));
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
251
    {
252
        global $argv;
253
        // set temp dir based on OS
254
        putenv('DOTFILES_TEMP_DIR='.sys_get_temp_dir().'/dotfiles');
255
        $dryRun = in_array('--dry-run', $argv) ? true : false;
256
        putenv('DOTFILES_DRY_RUN='.$dryRun);
257
258
        $files = $this->envFiles;
259
        $env = new Dotenv();
260
        if (count($files) > 0) {
261
            call_user_func_array(array($env, 'load'), $files);
262
        }
263
264
        $dev = getenv('DOTFILES_ENV');
265
        if (
266
            'dev' !== $dev && is_file($file = getenv('HOME').'/.dotfiles_profile')) {
267
            $env->load($file);
268
        }
269
270
        $this->debug = (bool) getenv('DOTFILES_DEBUG');
271
        $this->env = getenv('DOTFILES_ENV');
272
273
        $homeDir = getenv('DOTFILES_HOME_DIR');
274
        $backupDir = getenv('DOTFILES_BACKUP_DIR');
275
        if (!getenv('DOTFILES_INSTALL_DIR')) {
276
            putenv('DOTFILES_INSTALL_DIR='.$homeDir.'/.dotfiles');
277
        }
278
279
        if (!getenv('DOTFILES_CONFIG_DIR')) {
280
            putenv('DOTFILES_CONFIG_DIR='.getenv('DOTFILES_BACKUP_DIR').'/config');
281
        }
282
283
        if (!getenv('DOTFILES_CACHE_DIR')) {
284
            putenv('DOTFILES_CACHE_DIR='.$backupDir.'/var/cache');
285
        }
286
        if (!getenv('DOTFILES_LOG_DIR')) {
287
            putenv('DOTFILES_LOG_DIR='.$backupDir.'/var/log');
288
        }
289
    }
290
291
    /**
292
     * Load available plugins.
293
     */
294
    private function loadPlugins(): void
295
    {
296
        $finder = Finder::create();
297
        $finder
298
            ->name('*Plugin.php')
299
        ;
300
        if (is_dir($dir = __DIR__.'/../Plugins')) {
301
            $finder->in(__DIR__.'/../Plugins');
302
        }
303
        $dirs = $this->loadDirectoryFromAutoloader();
304
        $finder->in($dirs);
305
        foreach ($finder->files() as $file) {
306
            $namespace = 'Dotfiles\\Plugins\\'.str_replace('Plugin.php', '', $file->getFileName());
307
            $className = $namespace.'\\'.str_replace('.php', '', $file->getFileName());
308
            if (class_exists($className)) {
309
                /* @var \Dotfiles\Core\Plugin $plugin */
310
                $plugin = new $className();
311
                $this->registerPlugin($plugin);
312
            }
313
        }
314
    }
315
316
    private function processCoreConfig(array $configs, ContainerBuilder $builder): void
317
    {
318
        $dotfileConfig = array_key_exists('dotfiles', $configs) ? $configs['dotfiles'] : array();
319
        $processor = new Processor();
320
        $parameters = $processor->processConfiguration(new Configuration(), $dotfileConfig);
321
        $parameters = array('dotfiles' => $parameters);
322
        Toolkit::flattenArray($parameters);
323
324
        $builder->getParameterBag()->add($parameters);
325
326
        $locator = new FileLocator(__DIR__.'/Resources/config');
327
        $loader = new YamlFileLoader($builder, $locator);
328
        $loader->load('services.yaml');
329
    }
330
331
    /**
332
     * Register plugin.
333
     *
334
     * @param Plugin $plugin
335
     */
336
    private function registerPlugin(Plugin $plugin): void
337
    {
338
        if ($this->hasPlugin($plugin->getName())) {
339
            return;
340
        }
341
342
        $this->plugins[$plugin->getName()] = $plugin;
343
    }
344
}
345