@@ -50,6 +50,12 @@ class ControllerCommand extends Command
5050 /** @var Filesystem */
5151 protected $ _fileSystem ;
5252
53+ /** @var array Files created during execution */
54+ protected $ _createdFiles = [];
55+
56+ /** @var array Directories created during execution */
57+ protected $ _createdDirectories = [];
58+
5359 protected function configure (): void
5460 {
5561 $ this
@@ -68,6 +74,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6874 $ this ->_controllerType = $ input ->getArgument ('controllerType ' );
6975 $ this ->_template = $ input ->getOption ('template ' );
7076 $ this ->_fileSystem = new Filesystem ();
77+ $ this ->_createdFiles = [];
78+ $ this ->_createdDirectories = [];
7179
7280 if (!is_dir (_PS_MODULE_DIR_ . $ this ->_moduleName )) {
7381 $ output ->writeln ('<error>Module not exists</error> ' );
@@ -104,18 +112,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
104112
105113 $ defaultContent = str_replace ('{controllerClass} ' , $ controllerClass , $ defaultContent );
106114
115+ $ controllerPath = _PS_MODULE_DIR_ . $ this ->_moduleName . '/controllers/ ' . $ this ->_controllerType . '/ ' . strtolower ($ this ->_controllerName ) . '.php ' ;
116+
107117 try {
108- $ this ->_fileSystem ->dumpFile (
109- _PS_MODULE_DIR_ . $ this ->_moduleName . '/controllers/ ' . $ this ->_controllerType . '/ ' . strtolower ($ this ->_controllerName ) . '.php ' ,
110- $ defaultContent
111- );
118+ $ this ->_fileSystem ->dumpFile ($ controllerPath , $ defaultContent );
119+ $ this ->_createdFiles [] = $ this ->_getRelativePath ($ controllerPath );
112120 } catch (IOException $ e ) {
113121 $ output ->writeln ('<error>Unable to creat controller directories</error> ' );
114122
115123 return self ::RESPONSE_ERROR ;
116124 }
117125
118- $ output ->writeln ('<info>Controller ' . $ this ->_controllerName . ' created with sucess ' );
126+ $ output ->writeln ('<info>Controller ' . $ this ->_controllerName . ' created with success</info> ' );
127+ $ this ->_displayCreatedFiles ($ output );
119128
120129 return self ::RESPONSE_SUCCESS ;
121130 }
@@ -131,16 +140,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
131140 */
132141 protected function _createDirectories (): void
133142 {
134- if (!$ this ->_fileSystem ->exists (_PS_MODULE_DIR_ . $ this ->_moduleName . '/controllers/admin ' )) {
135- $ this ->_fileSystem ->mkdir (_PS_MODULE_DIR_ . $ this ->_moduleName . '/controllers/admin ' , 0775 );
143+ $ adminDir = _PS_MODULE_DIR_ . $ this ->_moduleName . '/controllers/admin ' ;
144+ if (!$ this ->_fileSystem ->exists ($ adminDir )) {
145+ $ this ->_fileSystem ->mkdir ($ adminDir , 0775 );
146+ $ this ->_createdDirectories [] = $ this ->_getRelativePath ($ adminDir );
136147 }
137148
138- if (!$ this ->_fileSystem ->exists (_PS_MODULE_DIR_ . $ this ->_moduleName . '/controllers/front ' )) {
139- $ this ->_fileSystem ->mkdir (_PS_MODULE_DIR_ . $ this ->_moduleName . '/controllers/front ' , 0775 );
149+ $ frontDir = _PS_MODULE_DIR_ . $ this ->_moduleName . '/controllers/front ' ;
150+ if (!$ this ->_fileSystem ->exists ($ frontDir )) {
151+ $ this ->_fileSystem ->mkdir ($ frontDir , 0775 );
152+ $ this ->_createdDirectories [] = $ this ->_getRelativePath ($ frontDir );
140153 }
141154
142- if (!$ this ->_fileSystem ->exists (_PS_MODULE_DIR_ . $ this ->_moduleName . '/views/templates/front ' )) {
143- $ this ->_fileSystem ->mkdir (_PS_MODULE_DIR_ . $ this ->_moduleName . '/views/templates/front ' , 0775 );
155+ $ templatesDir = _PS_MODULE_DIR_ . $ this ->_moduleName . '/views/templates/front ' ;
156+ if (!$ this ->_fileSystem ->exists ($ templatesDir )) {
157+ $ this ->_fileSystem ->mkdir ($ templatesDir , 0775 );
158+ $ this ->_createdDirectories [] = $ this ->_getRelativePath ($ templatesDir );
144159 }
145160
146161 /*$indexCommand = $this->getApplication()->find('dev:add-index-files');
@@ -220,9 +235,44 @@ protected function _generateTemplate(): void
220235<p>Controller template generated by PrestashopConsole to edit</p>
221236{/block}
222237 ' ;
223- $ this ->_fileSystem ->dumpFile (
224- _PS_MODULE_DIR_ . $ this ->_moduleName . '/views/templates/front/ ' . $ this ->_controllerName . '.tpl ' ,
225- $ defaultTemplateContent
226- );
238+ $ templatePath = _PS_MODULE_DIR_ . $ this ->_moduleName . '/views/templates/front/ ' . $ this ->_controllerName . '.tpl ' ;
239+ $ this ->_fileSystem ->dumpFile ($ templatePath , $ defaultTemplateContent );
240+ $ this ->_createdFiles [] = $ this ->_getRelativePath ($ templatePath );
241+ }
242+
243+ /**
244+ * Get relative path from PrestaShop root
245+ *
246+ * @param string $absolutePath
247+ * @return string
248+ */
249+ protected function _getRelativePath (string $ absolutePath ): string
250+ {
251+ return str_replace (_PS_ROOT_DIR_ . '/ ' , '' , $ absolutePath );
252+ }
253+
254+ /**
255+ * Display created files and directories
256+ *
257+ * @param OutputInterface $output
258+ * @return void
259+ */
260+ protected function _displayCreatedFiles (OutputInterface $ output ): void
261+ {
262+ if (!empty ($ this ->_createdDirectories )) {
263+ $ output ->writeln ('' );
264+ $ output ->writeln ('<comment>Created directories:</comment> ' );
265+ foreach ($ this ->_createdDirectories as $ dir ) {
266+ $ output ->writeln (' - ' . $ dir );
267+ }
268+ }
269+
270+ if (!empty ($ this ->_createdFiles )) {
271+ $ output ->writeln ('' );
272+ $ output ->writeln ('<comment>Created files:</comment> ' );
273+ foreach ($ this ->_createdFiles as $ file ) {
274+ $ output ->writeln (' - ' . $ file );
275+ }
276+ }
227277 }
228278}
0 commit comments