ConfigCacheCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 62
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
A execute() 0 10 1
A getFreshConfiguration() 0 13 3
A getCachedConfigPath() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Console;
13
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * Create a cache file for faster configuration loading.
19
 */
20
class ConfigCacheCommand extends BaseCommand
21
{
22
    /**
23
     * Configure the console command.
24
     *
25
     * @return void
26
     */
27
    protected function configure()
28
    {
29
        $this
30
            ->setName('config:cache')
31
            ->setDescription('Create a cache file for faster configuration loading');
32
    }
33
34
    /**
35
     * Execute the console command.
36
     *
37
     * @param InputInterface  $output
38
     * @param OutputInterface $output
39
     *
40
     * @return void
41
     */
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        $config = $this->getFreshConfiguration();
45
46
        file_put_contents(
47
            $this->getCachedConfigPath(), '<?php return '.var_export($config, true).';'.PHP_EOL
48
        );
49
50
        $output->writeln('Configuration cached successfully!');
51
    }
52
53
    /**
54
     * Boot a fresh copy of the application configuration.
55
     *
56
     * @return array
57
     */
58
    protected function getFreshConfiguration()
59
    {
60
        $config = [];
61
        foreach (glob(JITAMIN_DIR.'/config/*.php') as $file) {
62
            if (strrpos($file, '.default.php') !== false) {
63
                continue;
64
            }
65
            $section = str_replace('.php', '', basename($file));
66
            $config[$section] = require $file;
67
        }
68
69
        return $config;
70
    }
71
72
    /**
73
     * Get the path to the configuration cache file.
74
     *
75
     * @return string
76
     */
77
    public function getCachedConfigPath()
78
    {
79
        return __DIR__.'/../../bootstrap/cache/config.php';
80
    }
81
}
82