Completed
Push — master ( 952fde...7bae76 )
by Vladimir
11s
created

Application::setUseCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Console;
9
10
use allejo\stakx\Console\Command\BuildCommand;
11
use Symfony\Component\Console\Application as BaseApplication;
12
use Symfony\Component\Console\Input\ArgvInput;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\DependencyInjection\Container;
16
17
/**
18
 * The base application class for stakx.
19
 */
20
class Application extends BaseApplication
21
{
22
    /** @var bool */
23
    private $safeMode;
24
    /** @var bool */
25
    private $useCache;
26
    /** @var Container */
27
    private $container;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function run(InputInterface $input = null, OutputInterface $output = null)
33
    {
34
        $input = new ArgvInput();
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $input. This often makes code more readable.
Loading history...
35
        $this->handleApplicationFlags($input);
36
37
        $this->loadContainer([
38
            'parameters' => [
39
                'root_dir' => __DIR__ . '/../',
40
            ],
41
        ]);
42
43
        $output = $this->getContainer()->get('output');
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $output. This often makes code more readable.
Loading history...
44
45
        if (extension_loaded('xdebug') && !getenv('STAKX_DISABLE_XDEBUG_WARN'))
46
        {
47
            $output->writeln('<fg=black;bg=yellow>You are running Stakx with xdebug enabled. This has a major impact on runtime performance.</>');
48
        }
49
50
        return parent::run($input, $output);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function getDefaultCommands()
57
    {
58
        $commands = parent::getDefaultCommands();
59
60
        $commands[] = new BuildCommand();
61
62
        return $commands;
0 ignored issues
show
Best Practice introduced by
The expression return $commands; seems to be an array, but some of its elements' types (allejo\stakx\Console\Command\BuildCommand) are incompatible with the return type of the parent method Symfony\Component\Consol...ion::getDefaultCommands of type array<Symfony\Component\...le\Command\ListCommand>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
63
    }
64
65
    ///
66
    // Application Settings
67
    ///
68
69
    /**
70
     * Get whether or not the application is being run in safe mode.
71
     *
72
     * @return bool
73
     */
74
    public function inSafeMode()
75
    {
76
        return (bool)$this->safeMode;
77
    }
78
79
    /**
80
     * Set safe mode for the application.
81
     *
82
     * @param bool $safeMode
83
     */
84
    public function setSafeMode($safeMode)
85
    {
86
        $this->safeMode = $safeMode;
87
    }
88
89
    /**
90
     * Get whether or not to look for and use the application cache.
91
     *
92
     * @return bool
93
     */
94
    public function useCache()
95
    {
96
        return (bool)$this->useCache;
97
    }
98
99
    /**
100
     * Set whether or not to use an existing cache.
101
     *
102
     * @param bool $useCache
103
     */
104
    public function setUseCache($useCache)
105
    {
106
        $this->useCache = $useCache;
107
    }
108
109
    /**
110
     * Handle application wide flags.
111
     *
112
     * @param InputInterface $input
113
     */
114
    private function handleApplicationFlags(InputInterface $input)
115
    {
116
        $this->setUseCache($input->hasParameterOption('--use-cache'));
117
        $this->setSafeMode($input->hasParameterOption('--safe'));
118
    }
119
120
    ///
121
    // Container Settings
122
    ///
123
124
    /**
125
     * Get the Service container.
126
     *
127
     * @return Container
128
     */
129
    public function getContainer()
130
    {
131
        return $this->container;
132
    }
133
134
    /**
135
     * Load the cached application container or build a new one.
136
     *
137
     * @param array $containerOptions
138
     *
139
     * @throws \Exception
140
     */
141
    private function loadContainer(array $containerOptions)
142
    {
143
        $builder = new ContainerBuilder($containerOptions);
144
145
        require $builder->build();
146
147
        $this->container = new \ProjectServiceContainer();
148
    }
149
}
150