1+ <?php
2+
3+ namespace CodeOfDigital \CacheRepository \Commands ;
4+
5+ class CriteriaCommand extends BaseCommand
6+ {
7+ /**
8+ * The name of the command
9+ *
10+ * @var string
11+ */
12+ protected $ signature = 'make:criteria {criteria} ' ;
13+
14+ /**
15+ * The description of command
16+ *
17+ * @var string
18+ */
19+ protected $ description = 'Create a new criteria ' ;
20+
21+ /**
22+ * Stub paths
23+ *
24+ * @var array|string[]
25+ */
26+ protected array $ stubs = [
27+ 'criteria ' => __DIR__ . '/stubs/criteria.stub '
28+ ];
29+
30+ /**
31+ * Name of criteria
32+ *
33+ * @var string
34+ */
35+ protected string $ criteriaName ;
36+
37+ public function __construct ()
38+ {
39+ parent ::__construct ();
40+ }
41+
42+ public function handle ()
43+ {
44+ $ criteria = $ this ->argument ('criteria ' );
45+ $ criteriaParts = explode ('\\' , $ criteria );
46+ $ this ->criteriaName = $ criteriaParts [array_key_last ($ criteriaParts )];
47+
48+ $ content = $ this ->fileManager ->get ($ this ->stubs ['criteria ' ]);
49+
50+ $ replacements = [
51+ '%namespace% ' => "{$ this ->appNamespace }Criteria \\{$ criteria }" ,
52+ '%criteriaName% ' => $ this ->criteriaName
53+ ];
54+
55+ $ content = str_replace (array_keys ($ replacements ), array_values ($ replacements ), $ content );
56+
57+ $ fileName = "{$ criteria }" ;
58+ $ fileDirectory = app ()->basePath () . "/App/Criteria " ;
59+ $ filePath = "{$ fileDirectory }/ {$ fileName }.php " ;
60+
61+ if (!$ this ->fileManager ->exists ($ fileDirectory ))
62+ $ this ->fileManager ->makeDirectory ($ fileDirectory , 0755 , true );
63+
64+ if ($ this ->laravel ->runningInConsole () && $ this ->fileManager ->exists ($ filePath )) {
65+ $ response = $ this ->ask ("The criteria [ {$ this ->criteriaName }] has already exists. Do you want to overwrite it? " , 'Yes ' );
66+
67+ if (!$ this ->isResponsePositive ($ response )) {
68+ $ this ->info ("The interface [ {$ this ->criteriaName }] will not be overwritten. " );
69+ return ;
70+ }
71+ }
72+
73+ $ this ->fileManager ->put ($ filePath , $ content );
74+
75+ $ this ->info ("The criteria [ {$ this ->criteriaName }] has been created. " );
76+ }
77+ }
0 commit comments