Passed
Push — master ( 48aa9a...5ad1d2 )
by Richard
10:34
created

ViewServiceProviderGenerator::addBoot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 35
c 2
b 0
f 0
dl 0
loc 49
rs 9.36
cc 1
nc 1
nop 2
1
<?php
2
3
namespace PWWEB\Artomator\Generators;
4
5
use File;
6
use InfyOm\Generator\Generators\BaseGenerator;
7
use PWWEB\Artomator\Common\CommandData;
8
9
/**
10
 * Class ViewServiceProviderGenerator.
11
 */
12
class ViewServiceProviderGenerator extends BaseGenerator
13
{
14
    private $commandData;
15
16
    /**
17
     * ViewServiceProvider constructor.
18
     *
19
     * @param CommandData $commandData
20
     */
21
    public function __construct(CommandData $commandData)
22
    {
23
        $this->commandData = $commandData;
24
    }
25
26
    /**
27
     * Generate ViewServiceProvider.
28
     */
29
    public function generate()
30
    {
31
        $templateData = get_artomator_template('view_service_provider');
32
33
        $destination = $this->commandData->config->pathViewProvider;
34
35
        $fileName = basename($this->commandData->config->pathViewProvider);
36
37
        if (File::exists($destination)) {
38
            return;
39
        }
40
        File::copy($templateData, $destination);
41
42
        $this->commandData->commandComment($fileName.' published');
43
        $this->commandData->commandInfo($fileName);
44
    }
45
46
    /**
47
     * @param string      $views
48
     * @param string      $variableName
49
     * @param string      $columns
50
     * @param string      $tableName
51
     * @param string|null $modelName
52
     */
53
    public function addViewVariables($views, $variableName, $columns, $tableName, $modelName = null)
0 ignored issues
show
Unused Code introduced by
The parameter $variableName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

53
    public function addViewVariables($views, /** @scrutinizer ignore-unused */ $variableName, $columns, $tableName, $modelName = null)

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

Loading history...
54
    {
55
        if (! empty($modelName)) {
56
            $model = $modelName;
57
        } else {
58
            $model = model_name_from_table_name($tableName);
59
        }
60
61
        $this->commandData->addDynamicVariable('$COMPOSER_VIEWS$', $views);
62
        $this->commandData->addDynamicVariable('$COMPOSER_VIEW_MODEL$', $model);
63
        $this->commandData->addDynamicVariable('$COMPOSER_VIEW_COLUMNS$', $columns);
64
65
        $mainViewContent = $this->addViewComposer();
66
        $mainViewContent = $this->addNamespace($model, $mainViewContent);
67
        $mainViewContent = $this->addSelect($model, $views, $mainViewContent);
68
        $this->addCustomProvider();
69
70
        file_put_contents($this->commandData->config->pathViewProvider, $mainViewContent);
71
        $this->commandData->commandComment('View service provider file updated.');
72
    }
73
74
    public function addViewComposer()
75
    {
76
        $mainViewContent = file_get_contents($this->commandData->config->pathViewProvider);
77
        $newViewStatement = get_artomator_template('scaffold.view_composer');
78
        $newViewStatement = fill_template($this->commandData->dynamicVars, $newViewStatement);
79
80
        $newViewStatement = infy_nl(1).$newViewStatement;
81
        preg_match_all('/public function boot(.*)/', $mainViewContent, $matches);
82
83
        $totalMatches = count($matches[0]);
84
        $lastSeederStatement = $matches[0][$totalMatches - 1];
85
86
        $replacePosition = strpos($mainViewContent, $lastSeederStatement);
87
        $mainViewContent = substr_replace(
88
            $mainViewContent,
89
            $newViewStatement,
90
            $replacePosition + strlen($lastSeederStatement) + 6,
91
            0
92
        );
93
94
        return $mainViewContent;
95
    }
96
97
    public function addCustomProvider()
98
    {
99
        $configFile = base_path().'/config/app.php';
100
        $file = file_get_contents($configFile);
101
        $searchFor = 'Illuminate\View\ViewServiceProvider::class,';
102
        $customProviders = strpos($file, $searchFor);
103
104
        $isExist = strpos($file, "App\Providers\ViewServiceProvider::class");
105
        if ($customProviders && ! $isExist) {
106
            $newChanges = substr_replace(
107
                $file,
108
                infy_nl().infy_tab(8).'\App\Providers\ViewServiceProvider::class,',
109
                $customProviders + strlen($searchFor),
110
                0
111
            );
112
            file_put_contents($configFile, $newChanges);
113
        }
114
    }
115
116
    public function addNamespace($model, $mainViewContent)
117
    {
118
        $newModelStatement = 'use '.$this->commandData->config->nsInterface.'\\'.$model.'RepositoryInterface as '.$model.';';
119
        $isNameSpaceExist = strpos($mainViewContent, $newModelStatement);
120
        $newModelStatement = infy_nl().$newModelStatement;
121
        if (! $isNameSpaceExist) {
122
            preg_match_all('/namespace(.*)/', $mainViewContent, $matches);
123
            $totalMatches = count($matches[0]);
124
            $nameSpaceStatement = $matches[0][$totalMatches - 1];
125
            $replacePosition = strpos($mainViewContent, $nameSpaceStatement);
126
            $mainViewContent = substr_replace(
127
                $mainViewContent,
128
                $newModelStatement,
129
                $replacePosition + strlen($nameSpaceStatement),
130
                0
131
            );
132
            $mainViewContent = $this->addProperty($model, $mainViewContent);
133
            $mainViewContent = $this->addBoot($model, $mainViewContent);
134
        }
135
136
        return $mainViewContent;
137
    }
138
139
    public function addProperty($model, $mainViewContent)
140
    {
141
        $newPropertyStatement = "\n\n\t/**\n\t * The "
142
            .ucfirst($model)." repository.\n\t *\n\t * @var "
143
            .ucfirst($model)."\n\t */\n\tprivate \$"
144
            .lcfirst($model)."Repository;\n\n";
145
146
147
        preg_match_all('/private(.*)/', $mainViewContent, $matches);
148
        $totalMatches = count($matches[0]);
149
        $propertyStatement = $matches[0][$totalMatches - 1];
150
        $replacePosition = strpos($mainViewContent, $propertyStatement);
151
        $mainViewContent = substr_replace(
152
            $mainViewContent,
153
            $newPropertyStatement,
154
            $replacePosition + strlen($propertyStatement),
155
            0
156
        );
157
158
        return $mainViewContent;
159
    }
160
161
    public function addBoot($model, $mainViewContent)
162
    {
163
        $newBootStatement = ",\n\t\t"
164
            .ucfirst($model).' $'
165
            .lcfirst($model)."Repo\n\t";
166
167
        preg_match_all('/repo$/', $mainViewContent, $matches);
168
        $totalMatches = count($matches[0]);
169
        $bootStatement = $matches[0][$totalMatches - 1];
170
        $replacePosition = strpos($mainViewContent, $bootStatement);
171
        $mainViewContent = substr_replace(
172
            $mainViewContent,
173
            $newBootStatement,
174
            $replacePosition + strlen($bootStatement),
175
            0
176
        );
177
178
        $newBootStatement = "\n\t\t\$this->"
179
            .lcfirst($model).'Repository = $'
180
            .lcfirst($model).'Repo;';
181
182
        preg_match_all('/repo;$/', $mainViewContent, $matches);
183
        $totalMatches = count($matches[0]);
184
        $bootStatement = $matches[0][$totalMatches - 1];
185
        $replacePosition = strpos($mainViewContent, $bootStatement);
186
        $mainViewContent = substr_replace(
187
            $mainViewContent,
188
            $newBootStatement,
189
            $replacePosition + strlen($bootStatement),
190
            0
191
        );
192
193
        $newBootStatement = "\n\t * @param "
194
            .ucfirst($model).' $'
195
            .lcfirst($model).'Repo '
196
            .ucfirst($model).' repo';
197
198
        preg_match_all('/@parm(.*)/', $mainViewContent, $matches);
199
        $totalMatches = count($matches[0]);
200
        $bootStatement = $matches[0][$totalMatches - 1];
201
        $replacePosition = strpos($mainViewContent, $bootStatement);
202
        $mainViewContent = substr_replace(
203
            $mainViewContent,
204
            $newBootStatement,
205
            $replacePosition + strlen($bootStatement),
206
            0
207
        );
208
209
        return $mainViewContent;
210
    }
211
212
    public function addSelect($model, $view, $mainViewContent)
213
    {
214
        $newModelStatement = '\''.$model.'\' => [';
215
        $length = strlen($newModelStatement);
216
        $isNameSpaceExist = strpos($mainViewContent, $newModelStatement);
217
        $newModelStatement = infy_nl().$newModelStatement.infy_nl_tab().'\''.$view.'\','.infy_nl();
218
        if (! $isNameSpaceExist) {
219
            preg_match_all('/namespace(.*)/', $mainViewContent, $matches);
220
            $totalMatches = count($matches[0]);
221
            $nameSpaceStatement = $matches[0][$totalMatches - 1];
222
            $replacePosition = strpos($mainViewContent, $nameSpaceStatement);
223
            $mainViewContent = substr_replace(
224
                $mainViewContent,
225
                $newModelStatement.'],'.infy_nl(),
226
                $replacePosition + strlen($nameSpaceStatement),
227
                0
228
            );
229
        } else {
230
            $mainViewContent = substr_replace(
231
                $mainViewContent,
232
                $newModelStatement,
233
                $isNameSpaceExist,
234
                $length
235
            );
236
        }
237
238
        return $mainViewContent;
239
    }
240
}
241