1+ <?php
2+
3+ namespace Console \Command ;
4+
5+ use Symfony \Component \Console \Command \Command ;
6+ use Symfony \Component \Console \Input \InputArgument ;
7+ use Symfony \Component \Console \Input \InputInterface ;
8+ use Symfony \Component \Console \Input \InputOption ;
9+ use Symfony \Component \Console \Output \OutputInterface ;
10+
11+ class GenerateCommand extends Command
12+ {
13+ private $ template = '<?php
14+
15+ namespace Command;
16+
17+ use Symfony\Component\Console\Command\Command;
18+ use Symfony\Component\Console\Input\InputArgument;
19+ use Symfony\Component\Console\Input\InputInterface;
20+ use Symfony\Component\Console\Input\InputOption;
21+ use Symfony\Component\Console\Output\OutputInterface;
22+
23+ /**
24+ * <class>
25+ */
26+ class <class> extends Command
27+ {
28+ /**
29+ * Configuration of command
30+ */
31+ protected function configure()
32+ {
33+ $this
34+ ->setName("<name>")
35+ ->setDescription("Command <name>")
36+ ;
37+ }
38+
39+ /**
40+ * Execute method of command
41+ *
42+ * @param InputInterface $input
43+ * @param OutputInterface $output
44+ *
45+ * @return int|null|void
46+ */
47+ protected function execute(InputInterface $input, OutputInterface $output)
48+ {
49+ $output->writeln(array("","<info>Execute</info>",""));
50+ }
51+ } ' ;
52+
53+ /**
54+ * Configuration of command
55+ */
56+ protected function configure ()
57+ {
58+ $ this
59+ ->setName ('generate ' )
60+ ->setDescription ('Generate skeleton class for new command ' )
61+ ->setHelp (<<<EOF
62+ The <info>generate</info> command create skeleton of new command class.
63+
64+ <info>php app/console generate</info>
65+ EOF
66+ );
67+ ;
68+ }
69+
70+ /**
71+ * Execute method of command
72+ *
73+ * @param InputInterface $input
74+ * @param OutputInterface $output
75+ *
76+ * @return int|null|void
77+ */
78+ protected function execute (InputInterface $ input , OutputInterface $ output )
79+ {
80+ $ output ->writeln (array (
81+ '' ,
82+ '<comment>Welcome to the command generator</comment> ' ,
83+ ''
84+ ));
85+ /* @var \Symfony\Component\Console\Helper\DialogHelper $dialog */
86+ $ dialog = $ this ->getHelperSet ()->get ('dialog ' );
87+ $ commandClass = $ dialog ->askAndValidate (
88+ $ output ,
89+ "<info>Please enter the name of the command class</info>: " ,
90+ function ($ answer ) {
91+ if ('Command ' !== substr ($ answer , -7 )) {
92+ throw new \RunTimeException (
93+ 'The name of the command should be suffixed with \'Command \''
94+ );
95+ }
96+ return $ answer ;
97+ },
98+ false ,
99+ 'DefaultCommand '
100+ );
101+
102+ //
103+ $ commandName = $ this ->colonize ($ commandClass );
104+
105+ $ path = $ this ->generateCommand ($ commandClass , $ commandName );
106+ $ output ->writeln (sprintf ('Generated new command class to "<info>%s</info>" ' , realpath ($ path )));
107+ }
108+
109+ /**
110+ * Generate command skeleton
111+ *
112+ * @param $commandClass
113+ * @param $commandName
114+ *
115+ * @return string
116+ *
117+ * @throws \Exception
118+ */
119+ protected function generateCommand ($ commandClass , $ commandName )
120+ {
121+ $ placeHolders = array (
122+ '<class> ' ,
123+ '<name> '
124+ );
125+ $ replacements = array (
126+ $ commandClass ,
127+ $ commandName
128+ );
129+ $ code = str_replace ($ placeHolders , $ replacements , $ this ->template );
130+ /* @var \Console\ApplicationExtra $app*/
131+ $ app = $ this ->getApplication ();
132+ $ dir = $ app ->getCommandsDir ();
133+ $ dir = rtrim ($ dir , '/ ' );
134+ $ path = $ dir . '/ ' .$ commandClass .'.php ' ;
135+
136+ if (!file_exists ($ dir )) {
137+ throw new \Exception (sprintf ('Commands directory "%s" does not exist. ' , $ dir ));
138+ }
139+
140+ file_put_contents ($ path , $ code );
141+
142+ return $ path ;
143+ }
144+
145+ /**
146+ * Colonize command name
147+ *
148+ * @param $word
149+ * @return string
150+ */
151+ protected function colonize ($ word )
152+ {
153+ $ word = str_replace ('Command ' , '' , $ word );
154+
155+ return strtolower (preg_replace ('/[^A-Z^a-z^0-9]+/ ' ,': ' ,
156+ preg_replace ('/([a-zd])([A-Z])/ ' ,'\1:\2 ' ,
157+ preg_replace ('/([A-Z]+)([A-Z][a-z])/ ' ,'\1:\2 ' ,$ word ))));
158+ }
159+ }
0 commit comments