ModuleCreate::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
// +---------------------------------------------------------------------------+
3
// | This file is part of the Agavi package.                                   |
4
// | Copyright (c) 2005-2016 the Agavi Project.                                |
5
// |                                                                           |
6
// | For the full copyright and license information, please view the LICENSE   |
7
// | file that was distributed with this source code. You can also view the    |
8
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
9
// |   vi: set noexpandtab:                                                    |
10
// |   Local Variables:                                                        |
11
// |   indent-tabs-mode: t                                                     |
12
// |   End:                                                                    |
13
// +---------------------------------------------------------------------------+
14
/**
15
 * Create an empty module
16
 *
17
 * @author     Markus Lervik <[email protected]>
18
 * @copyright  Authors
19
 * @copyright  The Agavi Project
20
 *
21
 * @since      2.0.0
22
 **/
23
namespace Agavi\Build\Console\Command;
24
25
use Symfony\Component\Console\Exception\InvalidArgumentException;
26
use Symfony\Component\Console\Input\InputArgument;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
use Symfony\Component\Console\Question\Question;
31
use Symfony\Component\Yaml\Yaml;
32
33
class ModuleCreate extends AgaviCommand
34
{
35
36
    protected function configure()
37
    {
38
        $this->setName('agavi:module')
39
            ->setDescription('Create module')
40
            ->addOption('settings', null, InputOption::VALUE_REQUIRED, '.settings.yml to read configuration from')
41
            ->addArgument('module', InputArgument::OPTIONAL, 'The name of the module. If it\'s not provided, it will be asked for.');
42
    }
43
44
    public function execute(InputInterface $input, OutputInterface $output)
45
    {
46
47 View Code Duplication
        if ($input->hasOption('settings') && $input->getOption('settings') != null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
            $settingsFile = $input->getOption('settings');
49
        } else {
50
            $settingsFile = '.' . DIRECTORY_SEPARATOR . '.settings.yml';
51
        }
52
53
        if (!file_exists($settingsFile)) {
54
            throw new InvalidArgumentException(sprintf('Cannot find settings file "%s"', $settingsFile));
55
        }
56
57
        $helper = $this->getHelper('question');
58
59
        $settings = Yaml::parse(file_get_contents($settingsFile));
60
61
        if (!is_array($settings)) {
62
            throw new InvalidArgumentException(sprintf('Error parsing settings file "%s". Return value unexpected. Expected array, got %s', $settingsFile, gettype($settings)));
63
        }
64
65
        if (!isset($settings['project']['prefix'])) {
66
            throw new InvalidArgumentException(sprintf('No project prefix found in settings file "%s"', $settingsFile));
67
        }
68
        $projectLocation = (is_array($settings) && isset($settings['project']['location']) ? $settings['project']['location'] : '.');
69
70
        if ($input->hasArgument('module') && $input->getArgument('module') != null) {
71
            $module = $input->getArgument('module');
72
        } else {
73
            $question = new Question("Module name: ");
74
            $module = $helper->ask($input, $output, $question);
75
        }
76
77
        $module = ucfirst(TransformIdentifier::transform($module));
78
79
        if (file_exists($projectLocation . '/app/modules/' . $module)) {
80
            throw new InvalidArgumentException(sprintf('Module "%s" already exists in "%s"', $module, implode(DIRECTORY_SEPARATOR, [$projectLocation, 'app', 'modules', $module])));
81
        }
82
83
        $defaultParams = [
84
            'projectLocation' => $projectLocation,
85
            'projectName' => $settings['project']['name'],
86
            'projectPrefix' => $settings['project']['prefix'],
87
            'moduleName' => $module,
88
            'FQNS' => $settings['project']['namespace'],
89
            'NS' => substr($settings['project']['namespace'], 1, strlen($settings['project']['namespace']))
90
        ];
91
92
        @mkdir($projectLocation . '/app/modules/' . $module, 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
93
        @mkdir($projectLocation . '/app/modules/' . $module . '/cache', 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
94
        @mkdir($projectLocation . '/app/modules/' . $module . '/config', 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
95
        @mkdir($projectLocation . '/app/modules/' . $module . '/controllers', 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
96
        @mkdir($projectLocation . '/app/modules/' . $module . '/lib', 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
97
        @mkdir($projectLocation . '/app/modules/' . $module . '/validate', 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
98
        @mkdir($projectLocation . '/app/modules/' . $module . '/views', 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
99
        @mkdir($projectLocation . '/app/modules/' . $module . '/templates', 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
100
        @mkdir($projectLocation . '/app/modules/' . $module . '/models', 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
101
102
        $fc = new FileCopyHelper();
103
104
        // Config
105 View Code Duplication
        foreach (glob($this->getSourceDir() . '/build/templates/app/modules/config/*.xml.tmpl') as $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
            $fc->copy($file, $projectLocation . '/app/modules/' . $module . '/config/' . basename($file, '.tmpl'),
107
                function ($data, $params) {
108
                    return $this->moduleTokenReplacer($data, $params);
109
                }, $defaultParams);
110
        }
111
112
        // Lib
113
        @mkdir($projectLocation . '/app/modules/' . $module . '/lib/controller', 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
114 View Code Duplication
        foreach (glob($this->getSourceDir() . '/build/templates/app/modules/lib/controller/*.tmpl') as $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
            $fc->copy($file, $projectLocation . '/app/modules/' . $module . '/lib/controller/' . $settings['project']['prefix'] . $module . basename($file, '.tmpl'),
116
                function ($data, $params) {
117
                    return $this->moduleTokenReplacer($data, $params);
118
                }, $defaultParams);
119
        }
120
        @mkdir($projectLocation . '/app/modules/' . $module . '/lib/model', 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
121 View Code Duplication
        foreach (glob($this->getSourceDir() . '/build/templates/app/modules/lib/model/*.tmpl') as $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
            $fc->copy($file, $projectLocation . '/app/modules/' . $module . '/lib/model/' . $settings['project']['prefix'] . $module . basename($file, '.tmpl'),
123
                function ($data, $params) {
124
                    return $this->moduleTokenReplacer($data, $params);
125
                }, $defaultParams);
126
        }
127
        @mkdir($projectLocation . '/app/modules/' . $module . '/lib/view', 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
128 View Code Duplication
        foreach (glob($this->getSourceDir() . '/build/templates/app/modules/lib/view/*.tmpl') as $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
            $fc->copy($file, $projectLocation . '/app/modules/' . $module . '/lib/view/' . $settings['project']['prefix'] . $module . basename($file, '.tmpl'),
130
                function ($data, $params) {
131
                    return $this->moduleTokenReplacer($data, $params);
132
                }, $defaultParams);
133
        }
134
    }
135
136
    public function moduleTokenReplacer($data, $params)
137
    {
138
        return str_replace([
139
            '%%AGAVI_SOURCE_LOCATION%%',
140
            '%%PROJECT_LOCATION%%',
141
            '%%PROJECT_NAME%%',
142
            '%%PROJECT_PREFIX%%',
143
            '%%MODULE_NAME%%',
144
            '%%MODULE_CONTROLLER_PATH%%',
145
            '%%MODULE_VIEW_PATH%%',
146
            '%%MODULE_VIEW_NAME%%',
147
            '%%MODULE_CACHE_PATH%%',
148
            '%%MODULE_VALIDATE_PATH%%',
149
            '%%MODULE_TEMPLATES_DIRECTORY%%',
150
            '%%PROJECT_NAMESPACE%%',
151
            '%%FQNS%%'
152
        ], [
153
            $this->getSourceDir(),
154
            $params['projectLocation'],
155
            $params['projectName'],
156
            $params['projectPrefix'],
157
            $params['moduleName'],
158
            '%core.module_dir%/${moduleName}/controllers/${controllerName}Controller.class.php',
159
            '%core.module_dir%/${moduleName}/views/${viewName}View.class.php',
160
            '${controllerName}${viewName}',
161
            '%core.module_dir%/${moduleName}/cache/${controllerName}.xml',
162
            '%core.module_dir%/${moduleName}/validate/${controllerName}.xml',
163
            '%core.module_dir%/${module}/templates',
164
            $params['NS'],
165
            $params['FQNS']
166
        ], $data);
167
    }
168
}
169