Passed
Push — feature/lg ( 1bf2ef...8f3d7e )
by Richard
03:59
created

GeneratorPublishCommand::fillLicense()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace PWWEB\Artomator\Commands\Publish;
4
5
use InfyOm\Generator\Utils\FileUtil;
6
use Symfony\Component\Console\Input\InputOption;
7
8
class GeneratorPublishCommand extends PublishBaseCommand
9
{
10
    /**
11
     * ALL REFERENCES TO (get_template\(\')([a-z_\.]+)(', 'laravel-generator'\))
12
     * REPLACED WITH: get_artomator_template('$2')
13
     */
14
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'artomator:publish';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Publishes & init api routes, base controller, base test cases traits.';
28
29
    /**
30
     * Execute the command.
31
     *
32
     * @return void
33
     */
34
    public function handle()
35
    {
36
        $this->publishTestCases();
37
        $this->publishBaseController();
38
        $repositoryPattern = config('infyom.laravel_generator.options.repository_pattern', true);
39
        if ($repositoryPattern) {
40
            $this->publishBaseRepository();
41
        }
42
        if ($this->option('localized')) {
43
            $this->publishLocaleFiles();
44
        }
45
    }
46
47
    /**
48
     * Replaces dynamic variables of template.
49
     * THIS IS A NEW FUNCTION ADDED.
50
     *
51
     * @param string $templateData
52
     *
53
     * @return string
54
     */
55
    private function fillLicense($templateData)
56
    {
57
        $replacements = [
58
            '$LICENSE_PACKAGE$' => config('pwweb.artomator.license.package', 'boo'),
59
            '$LICENSE_AUTHORS$' => license_authors(config('pwweb.artomator.license.authors')),
60
            '$LICENSE_COPYRIGHT$' => config('pwweb.artomator.license.copyright'),
61
            '$LICENSE$' => config('pwweb.artomator.license.license'),
62
        ];
63
        foreach ($replacements as $key => $replacement) {
64
            $templateData = str_replace($key, $replacement, $templateData);
65
        }
66
67
        return $templateData;
68
    }
69
70
    /**
71
     * Replaces dynamic variables of template.
72
     *
73
     * @param string $templateData
74
     *
75
     * @return string
76
     */
77
    protected function fillTemplate($templateData)
78
    {
79
        $apiVersion = config('infyom.laravel_generator.api_version', 'v1');
80
        $apiPrefix = config('infyom.laravel_generator.api_prefix', 'api');
81
82
        $templateData = str_replace('$API_VERSION$', $apiVersion, $templateData);
83
        $templateData = str_replace('$API_PREFIX$', $apiPrefix, $templateData);
84
        $appNamespace = $this->getLaravel()->getNamespace();
85
        $appNamespace = substr($appNamespace, 0, strlen($appNamespace) - 1);
86
        $templateData = str_replace('$NAMESPACE_APP$', $appNamespace, $templateData);
87
88
        // return $templateData;
89
        // ADDED THE FOLLOWING LINE:
90
        return $this->fillLicense($templateData);
91
    }
92
93
    private function publishTestCases()
94
    {
95
        $testsPath = config('infyom.laravel_generator.path.tests', base_path('tests/'));
96
        $testsNameSpace = config('infyom.laravel_generator.namespace.tests', 'Tests');
97
        $createdAtField = config('infyom.laravel_generator.timestamps.created_at', 'created_at');
98
        $updatedAtField = config('infyom.laravel_generator.timestamps.updated_at', 'updated_at');
99
100
        $templateData = get_artomator_template('test.api_test_trait');
101
102
        $templateData = str_replace('$NAMESPACE_TESTS$', $testsNameSpace, $templateData);
103
        $templateData = str_replace('$TIMESTAMPS$', "['$createdAtField', '$updatedAtField']", $templateData);
104
105
        // ADDED THE FOLLOWING LINE:
106
        $templateData = $this->fillLicense($templateData);
107
108
        $fileName = 'ApiTestTrait.php';
109
110
        if (file_exists($testsPath.$fileName) && ! $this->confirmOverwrite($fileName)) {
111
            return;
112
        }
113
114
        FileUtil::createFile($testsPath, $fileName, $templateData);
115
        $this->info('ApiTestTrait created');
116
117
        $testAPIsPath = config('infyom.laravel_generator.path.api_test', base_path('tests/APIs/'));
118
        if (! file_exists($testAPIsPath)) {
119
            FileUtil::createDirectoryIfNotExist($testAPIsPath);
120
            $this->info('APIs Tests directory created');
121
        }
122
123
        $testRepositoriesPath = config('infyom.laravel_generator.path.repository_test', base_path('tests/Repositories/'));
124
        if (! file_exists($testRepositoriesPath)) {
125
            FileUtil::createDirectoryIfNotExist($testRepositoriesPath);
126
            $this->info('Repositories Tests directory created');
127
        }
128
    }
129
130
    private function publishBaseController()
131
    {
132
        $this->info('Boo!');
133
        $templateData = get_artomator_template('app_base_controller');
134
135
        $templateData = $this->fillTemplate($templateData);
136
137
        $controllerPath = app_path('Http/Controllers/');
138
139
        $fileName = 'AppBaseController.php';
140
141
        if (file_exists($controllerPath.$fileName) && ! $this->confirmOverwrite($fileName)) {
142
            return;
143
        }
144
145
        FileUtil::createFile($controllerPath, $fileName, $templateData);
146
147
        $this->info('AppBaseController created');
148
    }
149
150
    private function publishBaseRepository()
151
    {
152
        $templateData = get_artomator_template('base_repository');
153
154
        $templateData = $this->fillTemplate($templateData);
155
156
        $repositoryPath = app_path('Repositories/');
157
158
        FileUtil::createDirectoryIfNotExist($repositoryPath);
159
160
        $fileName = 'BaseRepository.php';
161
162
        if (file_exists($repositoryPath.$fileName) && ! $this->confirmOverwrite($fileName)) {
163
            return;
164
        }
165
166
        FileUtil::createFile($repositoryPath, $fileName, $templateData);
167
168
        $this->info('BaseRepository created');
169
    }
170
171
    private function publishLocaleFiles()
172
    {
173
        $localesDir = __DIR__.'/../../../locale/';
174
175
        $this->publishDirectory($localesDir, resource_path('lang'), 'lang', true);
176
177
        $this->comment('Locale files published');
178
    }
179
180
    /**
181
     * Get the console command options.
182
     *
183
     * @return array
184
     */
185
    public function getOptions()
186
    {
187
        return [
188
            ['localized', null, InputOption::VALUE_NONE, 'Localize files.'],
189
        ];
190
    }
191
192
    /**
193
     * Get the console command arguments.
194
     *
195
     * @return array
196
     */
197
    protected function getArguments()
198
    {
199
        return [];
200
    }
201
}
202