Completed
Push — path-pattern ( 070cec...188849 )
by grégoire
04:18 queued 12s
created

SchemaAwareCommand::configureOptionals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
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\Inflector;
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\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
/**
21
 * SchemaAwareCommand
22
 *
23
 * Base class for generator commands.
24
 *
25
 * @abstract
26
 * @package   Cli
27
 * @copyright 2014 - 2015 Grégoire HUBERT
28
 * @author    Grégoire HUBERT
29
 * @license   X11 {@link http://opensource.org/licenses/mit-license.php}
30
 * @see       PommAwareCommand
31
 */
32
abstract class SchemaAwareCommand extends SessionAwareCommand
33
{
34
    protected $schema;
35
    protected $prefix_dir;
36
    protected $prefix_ns;
37
    protected $pathFile;
38
    protected $namespace;
39
    protected $flexible_container;
40
41
    /**
42
     * configure
43
     *
44
     * @see PommAwareCommand
45
     */
46
    protected function configureRequiredArguments()
47
    {
48
        parent::configureRequiredArguments()
49
            ->addOption(
50
                'prefix-dir',
51
                'd',
52
                InputOption::VALUE_REQUIRED,
53
                'Indicate a directory prefix.',
54
                '.'
55
            )
56
            ->addOption(
57
                'prefix-ns',
58
                'a',
59
                InputOption::VALUE_REQUIRED,
60
                'Indicate a namespace prefix.',
61
                ''
62
            )
63
        ;
64
65
        return $this;
66
    }
67
68
    /**
69
     * configureOptionals
70
     *
71
     * @see PommAwareCommand
72
     */
73
    protected function configureOptionals()
74
    {
75
        parent::configureOptionals()
76
            ->addArgument(
77
                'schema',
78
                InputArgument::OPTIONAL,
79
                'Schema of the relation.',
80
                'public'
81
            )
82
            ->addOption(
83
                'flexible-container',
84
                null,
85
                InputOption::VALUE_REQUIRED,
86
                'Use an alternative flexible entity container',
87
                'PommProject\ModelManager\Model\FlexibleEntity'
88
            )
89
        ;
90
91
        return $this;
92
    }
93
    /**
94
     * execute
95
     *
96
     * @see Command
97
     */
98
    protected function execute(InputInterface $input, OutputInterface $output)
99
    {
100
        parent::execute($input, $output);
101
        $this->schema   = $input->getArgument('schema');
102
103
        if (!$this->schema) {
104
            $this->schema = 'public';
105
        }
106
107
        $this->prefix_dir = $input->getOption('prefix-dir');
108
        $this->prefix_ns  = $input->getOption('prefix-ns');
109
        $this->flexible_container = $input->getOption('flexible-container');
110
    }
111
112
    /**
113
     * getPathFile
114
     *
115
     * Create path file from parameters and namespace.
116
     *
117
     * @access protected
118
     * @param  string $config_name
119
     * @param  string $file_suffix
120
     * @param  string $extra_dir
121
     * @param  string $file_name
122
     * @param  bool   $format_psr4
123
     * @return string
124
     */
125
    protected function getPathFile(
126
        $config_name,
0 ignored issues
show
Unused Code introduced by
The parameter $config_name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
        $file_name,
128
        $file_suffix = '',
129
        $extra_dir = '',
130
        $format_psr4 = null,
131
        $path_pattern = '{session}/{schema}Schema'
132
    )
133
    {
134
        $format_psr4 = $format_psr4 === null ? false : (bool) $format_psr4;
135
        $prefix_ns = "";
136
137
        if (!$format_psr4) {
138
            $prefix_ns = str_replace('\\', '/', trim($this->prefix_ns, '\\'));
139
        }
140
141
        $elements =
142
            [
143
                rtrim($this->prefix_dir, '/'),
144
                $prefix_ns,
145
                $this->expandPath($path_pattern),
146
                $extra_dir,
147
                sprintf("%s%s.php", Inflector::studlyCaps($file_name), $file_suffix)
148
            ];
149
150
        return join('/', array_filter($elements, function ($val) {
151
            return $val != null;
152
        }));
153
    }
154
155
    /**
156
     * expandPath
157
     *
158
     * Expand path pattern with the context values
159
     *
160
     * @param   string $pattern
161
     * @return  string
162
     */
163
    protected function expandPath($pattern)
164
    {
165
        return trim(
166
            strtr(
167
                $pattern,
168
                [
169
                    '{session}' => Inflector::studlyCaps($this->config_name),
170
                    '{schema}'  => Inflector::studlyCaps($this->schema),
171
                ]),
172
        '/');
173
    }
174
175
    /**
176
     * getNamespace
177
     *
178
     * Create namespace from parameters.
179
     *
180
     * @access protected
181
     * @param  string $config_name
182
     * @param  string $extra_ns
183
     * @return string
184
     */
185
    protected function getNamespace($config_name, $extra_ns = '')
186
    {
187
        $elements =
188
            [
189
                $this->prefix_ns,
190
                Inflector::studlyCaps($config_name),
191
                Inflector::studlyCaps(sprintf("%s_schema", $this->schema)),
192
                $extra_ns
193
            ];
194
195
        return join('\\', array_filter($elements, function ($val) {
196
            return $val != null;
197
        }));
198
    }
199
200
    /**
201
     * fetchSchemaOid
202
     *
203
     * Get the schema Oid from database.
204
     *
205
     * @access protected
206
     * @return int $oid
207
     * @throws CliException
208
     */
209
    protected function fetchSchemaOid()
210
    {
211
        $schema_oid = $this
212
            ->getSession()
213
            ->getInspector()
214
            ->getSchemaOid($this->schema)
215
            ;
216
217
        if ($schema_oid === null) {
218
            throw new CliException(
219
                sprintf(
220
                    "Could not find schema '%s'.",
221
                    $this->schema
222
                )
223
            );
224
        }
225
226
        return $schema_oid;
227
    }
228
}
229