Completed
Pull Request — 2.0 (#39)
by Mikael
02:43
created

SchemaAwareCommand::configureRequiredArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 15
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
     * @param  string $dir_pattern
124
     * @return string
125
     */
126
    protected function getPathFile($config_name, $file_name, $file_suffix = '', $extra_dir = '', $format_psr4 = null,
127
        $dir_pattern = '')
128
    {
129
        $format_psr4 = $format_psr4 === null ? false : (bool) $format_psr4;
130
        $prefix_ns = "";
131
132
        if (!$format_psr4) {
133
            $prefix_ns = str_replace('\\', '/', trim($this->prefix_ns, '\\'));
134
        }
135
136
        $elements =
137
            [
138
                rtrim($this->prefix_dir, '/'),
139
                $prefix_ns,
140
                $this->generatePathForPattern($dir_pattern, $config_name),
141
                $extra_dir,
142
                sprintf("%s%s.php", Inflector::studlyCaps($file_name), $file_suffix)
143
            ];
144
145
        return join('/', array_filter($elements, function ($val) { return $val != null; }));
146
    }
147
148
    /**
149
     * getNamespace
150
     *
151
     * Create namespace from parameters.
152
     *
153
     * @access protected
154
     * @param  string $config_name
155
     * @param  string $extra_ns
156
     * @param  string $dir_pattern
157
     * @return string
158
     */
159
    protected function getNamespace($config_name, $extra_ns = '', $dir_pattern = "{Session}/{Schema}Schema")
160
    {
161
        $path_pattern = $this->generatePathForPattern($dir_pattern, $config_name);
162
163
        $elements =
164
            [
165
                $this->prefix_ns,
166
                str_replace('/', '\\',$path_pattern),
167
                $extra_ns
168
            ];
169
170
        return join('\\', array_filter($elements, function ($val) { return $val != null; }));
171
    }
172
173
    /**
174
     * fetchSchemaOid
175
     *
176
     * Get the schema Oid from database.
177
     *
178
     * @access protected
179
     * @return int $oid
180
     * @throws CliException
181
     */
182
    protected function fetchSchemaOid()
183
    {
184
        $schema_oid = $this
185
            ->getSession()
186
            ->getInspector()
187
            ->getSchemaOid($this->schema)
188
            ;
189
190
        if ($schema_oid === null) {
191
            throw new CliException(
192
                sprintf(
193
                    "Could not find schema '%s'.",
194
                    $this->schema
195
                )
196
            );
197
        }
198
199
        return $schema_oid;
200
    }
201
202
    /**
203
     * @param $dir_pattern
204
     * @param $config_name
205
     * @return string
206
     */
207
    protected function generatePathForPattern($dir_pattern, $config_name)
208
    {
209
        return strtr($dir_pattern,
210
            [
211
                '{Session}' => Inflector::studlyCaps($config_name),
212
                '{Schema}'  => Inflector::studlyCaps($this->schema),
213
            ]
214
        );
215
    }
216
}
217