BaseApplication::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * Copyright (c) 2015 Juan José Torroglosa Ramón
4
 *
5
 * This file is part of the Cliphar package.
6
 *
7
 * For the full copyright and license information, please view
8
 * the LICENSE file that was distributed with this source code.
9
 */
10
11
namespace Cliphar;
12
13
use Cliphar\Command\CommandFactory;
14
use Cliphar\Container\ContainerFactory;
15
use Cliphar\Exception\InvalidCommandException;
16
use Cliphar\Exception\InvalidServiceProviderException;
17
use Cliphar\InputDefinition\Exception\InputDefinitionParsingException;
18
use Cliphar\InputDefinition\InputDefinitionParser;
19
use Cliphar\ServiceProvider;
20
use Cliphar\Symfony\SymfonyConsoleApplication;
21
use Exception;
22
use Interop\Container\ContainerInterface;
23
use Symfony\Component\Console\Command\Command;
24
use Symfony\Component\Console\Input\ArgvInput;
25
use Symfony\Component\Console\Output\ConsoleOutput;
26
27
/**
28
 * Class BaseApplication
29
 */
30
abstract class BaseApplication
31
{
32
    private static $instance;
33
34
    /**
35
     * @var ContainerInterface
36
     */
37
    protected $container;
38
39
    /**
40
     * @var Binder
41
     */
42
    protected $binder;
43
44
    /**
45
     * @var SymfonyConsoleApplication
46
     */
47
    private $symfonyApplication;
48
49
    /**
50
     * @var Command[]
51
     */
52
    private $commands;
53
54
    /**
55
     * @return static
56
     */
57
    public static function getInstance()
58
    {
59
        if (self::$instance === null) {
60
            self::$instance = new static();
61
        }
62
63
        return self::$instance;
64
    }
65
66
    protected final function __construct()
67
    {
68
        $factory = new ContainerFactory();
69
        $this->container = $factory->createLaravelContainer();
70
        $this->binder = $this->container->get('Cliphar\Binder');
71
        $this->symfonyApplication = new SymfonyConsoleApplication($this->getName(), $this->getVersion(), $this->binder);
72
        $this->symfonyApplication->setAutoExit(false);
73
        $this->commandFactory = $this->container->get('Cliphar\Command\CommandFactory');
0 ignored issues
show
Bug introduced by
The property commandFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
74
        $this->input = new ArgvInput();
0 ignored issues
show
Bug introduced by
The property input does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
75
        $this->output = new ConsoleOutput();
0 ignored issues
show
Bug introduced by
The property output does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
76
        $this->symfonyApplication->registerIO($this->input, $this->output);
77
        $this->commands = array();
78
    }
79
80
    /**
81
     * Runs the application
82
     * @throws \Exception
83
     */
84
    public final function run()
85
    {
86
        $this->registerServices();
87
        $this->registerCommands();
88
        return $this->symfonyApplication->run($this->input, $this->output);
89
    }
90
91
    /**
92
     * @param $name
93
     * @param $definition
94
     * @param $callable
95
     */
96
    public function addCommand($name, $definition, $callable)
97
    {
98
        try {
99
            $command = $this->commandFactory->createCommand($name, $definition, $callable);
100
            $this->commands[$command->getName()] = $command;
101
        } catch (InputDefinitionParsingException $e) {
102
            $this->symfonyApplication->renderException(
103
                $e,
104
                $this->container->get('Symfony\Component\Console\Output\OutputInterface')
105
            );
106
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method addCommand() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
107
        }
108
    }
109
110
    private function registerServices()
111
    {
112
        foreach ($this->getProviders() as $p) {
113
            /** @var ServiceProvider $provider */
114
            $provider = $this->resolveServiceProvider($p);
115
116
            $provider->register();
117
        }
118
    }
119
120
    private function registerCommands()
121
    {
122
        foreach ($this->getAllCommands() as $c) {
123
            /** @var Command $command */
124
            $command = $this->resolveCommand($c);
125
126
            $this->symfonyApplication->add($command);
127
        }
128
    }
129
130
    /**
131
     * @param string|ServiceProvider $provider
132
     * @return ServiceProvider
133
     * @throws InvalidServiceProviderException
134
     */
135
    private function resolveServiceProvider($provider)
136
    {
137
        if (is_string($provider)) {
138
            $provider = $this->container->get($provider);
139
        }
140
141
        if (! ($provider instanceof ServiceProvider)) {
142
            throw new InvalidServiceProviderException();
143
        }
144
145
        return $provider;
146
    }
147
148
    /**
149
     * @param $command
150
     * @return Command
151
     * @throws InvalidCommandException
152
     */
153
    private function resolveCommand($command)
154
    {
155
        if (is_string($command)) {
156
            $command = $this->container->get($command);
157
        }
158
159
        if (!($command instanceof Command)) {
160
            throw new InvalidCommandException();
161
        }
162
163
        return $command;
164
    }
165
166
    /**
167
     * @return string[]
168
     */
169
    protected function getAllCommands()
170
    {
171
        return array_merge($this->commands, $this->getCommands());
172
    }
173
174
    /**
175
     * @return string[]
176
     */
177
    abstract protected function getCommands();
178
179
    /**
180
     * @return string[]
181
     */
182
    abstract protected function getProviders();
183
184
    /**
185
     * @return string
186
     */
187
    abstract protected function getVersion();
188
189
    /**
190
     * @return string
191
     */
192
    abstract protected function getName();
193
}