Generator::option()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yeelight\Generators;
4
5
use Illuminate\Console\DetectsApplicationNamespace;
6
use Illuminate\Filesystem\Filesystem;
7
use Illuminate\Support\Str;
8
9
/**
10
 * Class Generator
11
 *
12
 * @category Yeelight
13
 *
14
 * @package Yeelight\Generators
15
 *
16
 * @author Sheldon Lee <[email protected]>
17
 *
18
 * @license https://opensource.org/licenses/MIT MIT
19
 *
20
 * @link https://www.yeelight.com
21
 */
22
abstract class Generator
23
{
24
    use DetectsApplicationNamespace;
25
26
    /**
27
     * The filesystem instance.
28
     *
29
     * @var \Illuminate\Filesystem\Filesystem
30
     */
31
    protected $filesystem;
32
33
    /**
34
     * The array of options.
35
     *
36
     * @var array
37
     */
38
    protected $options;
39
40
    /**
41
     * The shortname of stub.
42
     *
43
     * @var string
44
     */
45
    protected $stub;
46
47
    /**
48
     * Create new instance of this class.
49
     *
50
     * @param array $options $options
51
     */
52
    public function __construct(array $options = [])
53
    {
54
        $this->filesystem = new Filesystem();
55
        $this->options = $options;
56
    }
57
58
    /**
59
     * Get the filesystem instance.
60
     *
61
     * @return \Illuminate\Filesystem\Filesystem
62
     */
63
    public function getFilesystem()
64
    {
65
        return $this->filesystem;
66
    }
67
68
    /**
69
     * Set the filesystem instance.
70
     *
71
     * @param \Illuminate\Filesystem\Filesystem $filesystem Filesystem
72
     *
73
     * @return $this
74
     */
75
    public function setFilesystem(Filesystem $filesystem)
76
    {
77
        $this->filesystem = $filesystem;
78
79
        return $this;
80
    }
81
82
    /**
83
     * Get stub template for generated file.
84
     *
85
     * @return string
86
     */
87
    public function getStub()
88
    {
89
        $path = config('repository.generator.stubsOverridePath', __DIR__);
90
91
        if (!file_exists($path.'/Stubs/'.$this->stub.'.stub')) {
92
            $path = __DIR__;
93
        }
94
95
        return (new Stub(
96
            $path . '/Stubs/' . $this->stub . '.stub',
97
            $this->getReplacements()
98
        ))->render();
99
    }
100
101
    /**
102
     * Get template replacements.
103
     *
104
     * @return array
105
     */
106
    public function getReplacements()
107
    {
108
        return [
109
            'class'          => $this->getClass(),
110
            'namespace'      => $this->getNamespace(),
111
            'root_namespace' => $this->getRootNamespace(),
112
        ];
113
    }
114
115
    /**
116
     * Get base path of destination file.
117
     *
118
     * @return string
119
     */
120
    public function getBasePath()
121
    {
122
        return base_path();
123
    }
124
125
    /**
126
     * Get destination path for generated file.
127
     *
128
     * @return string
129
     */
130
    public function getPath()
131
    {
132
        return $this->getBasePath().'/'.$this->getName().'.php';
133
    }
134
135
    /**
136
     * Get name input.
137
     *
138
     * @return string
139
     */
140
    public function getName()
141
    {
142
        $name = $this->name;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Yeelight\Generators\Generator. Since you implemented __get, consider adding a @property annotation.
Loading history...
143
        if (str_contains($this->name, '\\')) {
144
            $name = str_replace('\\', '/', $this->name);
145
        }
146
        if (str_contains($this->name, '/')) {
147
            $name = str_replace('/', '/', $this->name);
148
        }
149
150
        return Str::studly(
151
            str_replace(' ', '/', ucwords(str_replace('/', ' ', $name)))
152
        );
153
    }
154
155
    /**
156
     * Get name input.
157
     *
158
     * @return string
159
     */
160
    public function getSnakeName()
161
    {
162
        $name = $this->name;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Yeelight\Generators\Generator. Since you implemented __get, consider adding a @property annotation.
Loading history...
163
        if (str_contains($this->name, '\\')) {
164
            $name = str_replace('\\', '/', $this->name);
165
        }
166
        if (str_contains($this->name, '/')) {
167
            $name = str_replace('/', '/', $this->name);
168
        }
169
170
        return Str::plural(
171
            Str::snake(
172
                str_replace(' ', '/', ucwords(str_replace('/', ' ', $name)))
173
            )
174
        );
175
    }
176
177
    /**
178
     * Gets id name based on model.
179
     *
180
     * @return string
181
     */
182
    public function getIdName()
183
    {
184
        return str_singular($this->getSnakeName()).'_id';
185
    }
186
187
    /**
188
     * Get class name.
189
     *
190
     * @return string
191
     */
192
    public function getClass()
193
    {
194
        return Str::studly(class_basename($this->getName()));
195
    }
196
197
    /**
198
     * Get paths of namespace.
199
     *
200
     * @return array
201
     */
202
    public function getSegments()
203
    {
204
        return explode('/', $this->getName());
205
    }
206
207
    /**
208
     * Get root namespace.
209
     *
210
     * @return string
211
     */
212
    public function getRootNamespace()
213
    {
214
        return config(
215
            'repository.generator.rootNamespace',
216
            $this->getAppNamespace()
217
        );
218
    }
219
220
    /**
221
     * Get class-specific output paths.
222
     *
223
     * @param string $class className
224
     * @param bool $directoryPath directoryPath
225
     *
226
     * @return \Illuminate\Config\Repository|mixed
227
     */
228
    public function getConfigGeneratorClassPath($class, $directoryPath = false)
229
    {
230
        switch ($class) {
231
            case 'models' === $class:
232
                $path = config('repository.generator.paths.models', 'Entities');
233
                break;
234
            case 'repositories' === $class:
235
                $path = config(
236
                    'repository.generator.paths.repositories',
237
                    'Repositories'
238
                );
239
                break;
240
            case 'interfaces' === $class:
241
                $path = config('repository.generator.paths.interfaces', 'Repositories');
242
                break;
243
            case 'presenters' === $class:
244
                $path = config('repository.generator.paths.presenters', 'Presenters');
245
                break;
246
            case 'transformers' === $class:
247
                $path = config('repository.generator.paths.transformers', 'Transformers');
248
                break;
249
            case 'validators' === $class:
250
                $path = config('repository.generator.paths.validators', 'Validators');
251
                break;
252
            case 'controllers' === $class:
253
                $path = config('repository.generator.paths.controllers', 'Http\Controllers');
254
                break;
255
            case 'api_controllers' === $class:
256
                $path = config('repository.generator.paths.api_controllers', 'Http/Controllers/Api/Controllers');
257
                break;
258
            case 'requests' === $class:
259
                $path = config('repository.generator.paths.requests', 'Http/Requests');
260
                break;
261
            case 'provider' === $class:
262
                $path = config('repository.generator.paths.provider', 'RepositoryServiceProvider');
263
                break;
264
            case 'criteria' === $class:
265
                $path = config('repository.generator.paths.criteria', 'Criteria');
266
                break;
267
            case 'views' === $class:
268
                $path = config('repository.generator.paths.views', 'views');
269
                break;
270
            case 'lang' === $class:
271
                $path = config('repository.generator.paths.criteria', 'lang');
272
                break;
273
            default:
274
                $path = '';
275
        }
276
277
        if ($directoryPath) {
278
            $path = str_replace('\\', '/', $path);
279
        } else {
280
            $path = str_replace('/', '\\', $path);
281
        }
282
283
        return $path;
284
    }
285
286
    /**
287
     * GetPathConfigNode
288
     *
289
     * @return mixed
290
     */
291
    abstract public function getPathConfigNode();
292
293
    /**
294
     * Get class namespace.
295
     *
296
     * @return string
297
     */
298
    public function getNamespace()
299
    {
300
        $segments = $this->getSegments();
301
        array_pop($segments);
302
        $rootNamespace = $this->getRootNamespace();
303
        if ($rootNamespace == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $rootNamespace of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
304
            return;
305
        }
306
307
        return 'namespace ' .
308
            rtrim($rootNamespace . '\\' . implode($segments, '\\'), '\\') .
0 ignored issues
show
Bug introduced by
'\' of type string is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

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

308
            rtrim($rootNamespace . '\\' . implode($segments, /** @scrutinizer ignore-type */ '\\'), '\\') .
Loading history...
309
            ';';
310
    }
311
312
    /**
313
     * Setup some hook.
314
     *
315
     * @return void
316
     */
317
    public function setUp()
318
    {
319
        //
320
    }
321
322
    /**
323
     * Run the generator.
324
     *
325
     * @throws FileAlreadyExistsException
326
     *
327
     * @return int
328
     */
329
    public function run()
330
    {
331
        $this->setUp();
332
        if ($this->filesystem->exists($path = $this->getPath()) && !$this->force) {
0 ignored issues
show
Bug Best Practice introduced by
The property force does not exist on Yeelight\Generators\Generator. Since you implemented __get, consider adding a @property annotation.
Loading history...
333
            throw new FileAlreadyExistsException($path);
334
        }
335
        if (!$this->filesystem->isDirectory($dir = dirname($path))) {
336
            $this->filesystem->makeDirectory($dir, 0777, true, true);
337
        }
338
339
        return $this->filesystem->put($path, $this->getStub());
340
    }
341
342
    /**
343
     * Get options.
344
     *
345
     * @return array | string
346
     */
347
    public function getOptions()
348
    {
349
        return $this->options;
350
    }
351
352
    /**
353
     * Determinte whether the given key exist in options array.
354
     *
355
     * @param string $key Key
356
     *
357
     * @return bool
358
     */
359
    public function hasOption($key)
360
    {
361
        return array_key_exists($key, $this->options);
362
    }
363
364
    /**
365
     * Get value from options by given key.
366
     *
367
     * @param string $key key
368
     * @param string|null $default default
369
     *
370
     * @return string
371
     */
372
    public function getOption($key, $default = null)
373
    {
374
        if (!$this->hasOption($key)) {
375
            return $default;
376
        }
377
378
        return $this->options[$key] ?: $default;
379
    }
380
381
    /**
382
     * Helper method for "getOption".
383
     *
384
     * @param string $key key
385
     * @param string|null $default default
386
     *
387
     * @return string
388
     */
389
    public function option($key, $default = null)
390
    {
391
        return $this->getOption($key, $default);
392
    }
393
394
    /**
395
     * Handle call to __get method.
396
     *
397
     * @param string $key key
398
     *
399
     * @return string|mixed
400
     */
401
    public function __get($key)
402
    {
403
        if (property_exists($this, $key)) {
404
            return $this->{$key};
405
        }
406
407
        return $this->option($key);
408
    }
409
}
410