PommAwareCommand::getPomm()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
/*
3
 * This file is part of Pomm's Cli package.
4
 *
5
 * (c) 2014 - 2015 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\Cli\Command;
11
12
use PommProject\Cli\Exception\CliException;
13
use PommProject\Foundation\Pomm;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * PommAwareCommand
21
 *
22
 * Base command for all Pomm Cli commands.
23
 *
24
 * @package   Cli
25
 * @copyright 2014 - 2015 Grégoire HUBERT
26
 * @author    Grégoire HUBERT
27
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
28
 * @see       Command
29
 */
30
class PommAwareCommand extends Command
31
{
32
    private $pomm;
33
34
    protected $config_file;
35
36
    /**
37
     * execute
38
     *
39
     * Set pomm dependent variables.
40
     *
41
     * @see Command
42
     */
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        $this->config_file = $input->getOption('bootstrap-file');
46
    }
47
48
    /**
49
     * configureRequiredArguments
50
     *
51
     * In order to keep the same argument order for all commands, it is
52
     * necessary to be able to declare base required fields before subcommands.
53
     *
54
     * @access protected
55
     * @return PommAwareCommand $this
56
     */
57
    protected function configureRequiredArguments()
58
    {
59
        return $this;
60
    }
61
62
    /**
63
     * configureOptionals
64
     *
65
     * In order to keep the same argument order for all commands, it is
66
     * necessary to be able to declare base required fields before subcommands.
67
     *
68
     * @access protected
69
     * @return PommAwareCommand $this
70
     */
71
    protected function configureOptionals()
72
    {
73
        $this
74
            ->addOption(
75
                'bootstrap-file',
76
                '-b',
77
                InputArgument::OPTIONAL,
78
                'Complete path of the CLI bootstrap file.',
79
                sprintf("%s/.pomm_cli_bootstrap.php", getenv('PWD'))
80
            )
81
        ;
82
83
        return $this;
84
    }
85
86
    /**
87
     * configure
88
     *
89
     * @see Command
90
     */
91
    protected function configure()
92
    {
93
        $this
94
            ->configureRequiredArguments()
95
            ->configureOptionals()
96
            ;
97
    }
98
99
    /**
100
     * getPomm
101
     *
102
     * Return the Pomm instance.
103
     *
104
     * @access protected
105
     * @return Pomm
106
     * @throws CliException
107
     */
108
    protected function getPomm()
109
    {
110
        if ($this->pomm === null) {
111
            if (!file_exists($this->config_file)) {
112
                throw new CliException(sprintf("Could not load configuration '%s'.", $this->config_file));
113
            }
114
115
            $this->pomm = require $this->config_file;
116
117
            if (!$this->pomm instanceof Pomm) {
118
                throw new CliException(sprintf("Invalid configuration. Bootstrap file must return a Pomm instance."));
119
            }
120
        }
121
122
        return $this->pomm;
123
    }
124
125
    /**
126
     * setPomm
127
     *
128
     * When used with a framework, it is useful to get the Pomm instance from
129
     * the framework configuration mechanism.
130
     *
131
     * @access public
132
     * @param  Pomm     $pomm
133
     * @return PommAwareCommand
134
     */
135
    public function setPomm(Pomm $pomm)
136
    {
137
        $this->pomm = $pomm;
138
139
        return $this;
140
    }
141
}
142