1+ <?php
2+
3+ namespace Modulus \Console \Commands ;
4+
5+ use Modulus \Console \ModulusCLI ;
6+ use AtlantisPHP \Console \Command ;
7+ use Modulus \Scaffolding \Template ;
8+ use Symfony \Component \Console \Input \InputInterface ;
9+ use Symfony \Component \Console \Output \OutputInterface ;
10+
11+ class CraftDirective extends Command
12+ {
13+ /**
14+ * The name and signature of the console command.
15+ *
16+ * @var string
17+ */
18+ protected $ signature = 'craft:directive {name} ' ;
19+
20+ /**
21+ * The full command description.
22+ *
23+ * @var string
24+ */
25+ protected $ help = 'This command allows you to create a Medusa directive ' ;
26+
27+ /**
28+ * The descriptions of the console commands.
29+ *
30+ * @var array
31+ */
32+ protected $ descriptions = [
33+ 'craft:directive ' => 'Create a new Medusa directive ' ,
34+ 'name ' => 'The name of the directive '
35+ ];
36+
37+ /**
38+ * @param InputInterface $input
39+ * @param OutputInterface $output
40+ *
41+ * @return void
42+ */
43+ protected function execute (InputInterface $ input , OutputInterface $ output )
44+ {
45+ $ name = $ input ->getArgument ('name ' );
46+
47+ if ($ this ->add ($ name )) {
48+ return $ output ->writeln ('<info>Directive " ' . $ name . '" has been successfuly created.</info> ' );
49+ }
50+
51+ return $ output ->writeln ('File " ' . $ name . '" already exists. ' );
52+ }
53+
54+ /**
55+ * Add new command
56+ *
57+ * @param string $name
58+ * @param string $class
59+ * @return boolean
60+ */
61+ private function add (string $ name ) : bool
62+ {
63+ $ appdir = ModulusCLI::$ appdir . 'app ' . DIRECTORY_SEPARATOR . 'Directives ' ;
64+ $ directive = $ appdir . DIRECTORY_SEPARATOR . $ name . '.php ' ;
65+ $ namespace = '' ;
66+
67+ if (substr_count ($ name , '/ ' ) > 0 ) {
68+ ModulusCLI::_dir (substr ($ directive , 0 , strrpos ($ directive , DIRECTORY_SEPARATOR )));
69+ $ namespace = substr ($ name , 0 , strrpos ($ name , DIRECTORY_SEPARATOR ));
70+ $ name = str_replace ($ namespace . DIRECTORY_SEPARATOR , '' , $ name );
71+
72+ $ namespace = '\\' . str_replace ('/ ' , '\\' , $ namespace );
73+ }
74+
75+ ModulusCLI::_dir ($ appdir );
76+
77+ $ content = Template::asset ('directive_template ' );
78+ $ content = str_replace ('{directive_name} ' , $ name , $ content );
79+ $ content = str_replace ('{directive_name__lower} ' , strtolower ($ name ), $ content );
80+ $ content = str_replace ('{namespace} ' , $ namespace , $ content );
81+
82+ if (file_exists ($ directive )) {
83+ return false ;
84+ }
85+ else {
86+ file_put_contents ($ directive , $ content );
87+ return true ;
88+ }
89+ }
90+
91+ }
0 commit comments