22
33namespace TickTackk \DeveloperTools \Cli \Command ;
44
5- use Closure ;
65use Symfony \Component \Console \Command \Command ;
76use Symfony \Component \Console \Helper \QuestionHelper ;
87use Symfony \Component \Console \Input \InputArgument ;
98use Symfony \Component \Console \Input \InputInterface ;
109use Symfony \Component \Console \Output \OutputInterface ;
1110use Symfony \Component \Console \Question \Question ;
12- use XF \Util \ File ;
13- use XF \AddOn \ AddOn ;
11+ use XF \Cli \ Command \ AddOnActionTrait ;
12+ use XF \Util \ File as FileUtil ;
1413const USE_FUNCTION_PLACEHOLDER = true ;
1514use function str_replace ;
1615
1716/**
18- * Class ClassExtension
19- *
20- * @package TickTackk\DeveloperTools\Cli\Command\AddOn
17+ * @version 1.4.0
2118 */
2219class ClassExtension extends Command
2320{
21+ use AddOnActionTrait;
22+
2423 protected function configure () : void
2524 {
2625 $ this
@@ -40,6 +39,8 @@ protected function configure() : void
4039 }
4140
4241 /**
42+ * @version 1.4.0
43+ *
4344 * @param InputInterface $input
4445 * @param OutputInterface $output
4546 *
@@ -55,15 +56,18 @@ protected function execute(InputInterface $input, OutputInterface $output) : ?in
5556 if (!$ addOnId )
5657 {
5758 $ question = new Question ('<question>Enter the ID for the add-on:</question> ' );
58- $ question ->setValidator ($ this ->getAddOnQuestionFieldValidator ('addon_id ' ));
5959 $ addOnId = $ helper ->ask ($ input , $ output , $ question );
6060 $ output ->writeln ('' );
6161 }
6262
63- $ addOnObj = new AddOn ($ addOnId , \XF ::app ()->addOnManager ());
63+ $ addOnObj = $ this ->checkEditableAddOn ($ addOnId , $ error );
64+ if (!$ addOnObj )
65+ {
66+ $ output ->writeln ('<error> ' . $ error . '</error> ' );
67+ return 1 ;
68+ }
6469
6570 $ jsonPath = $ addOnObj ->getJsonPath ();
66-
6771 if (!file_exists ($ jsonPath ))
6872 {
6973 $ output ->writeln (sprintf ('<error>The addon.json file must exist at %s.</error> ' , $ jsonPath ));
@@ -86,7 +90,7 @@ protected function execute(InputInterface $input, OutputInterface $output) : ?in
8690 $ toClassPath = $ addOnObj ->getAddOnDirectory () . DIRECTORY_SEPARATOR . $ fromClassPath ;
8791 $ outputPath = $ toClassPath . '.php ' ;
8892
89- File ::createDirectory (dirname ($ toClassPath ), false );
93+ FileUtil ::createDirectory (dirname ($ toClassPath ), false );
9094
9195 if (!file_exists ($ outputPath ))
9296 {
@@ -158,7 +162,7 @@ class {$className} extends XFCP_{$className}
158162}
159163TEMPLATE ;
160164
161- $ written = File ::writeFile ($ outputPath , $ template , false );
165+ $ written = FileUtil ::writeFile ($ outputPath , $ template , false );
162166 if ($ written )
163167 {
164168 $ output ->writeln ("Wrote class extension template to {$ outputPath }" );
@@ -206,28 +210,4 @@ protected function getCommonUseStatements() : string
206210TEMPLATE ;
207211
208212 }
209-
210- /**
211- * @param $key
212- *
213- * @return Closure
214- */
215- protected function getAddOnQuestionFieldValidator ($ key ) : callable
216- {
217- return function ($ value ) use ($ key )
218- {
219- $ addOn = \XF ::em ()->create ('XF:AddOn ' );
220-
221- $ valid = $ addOn ->set ($ key , $ value );
222- if (!$ valid )
223- {
224- $ errors = $ addOn ->getErrors ();
225- if (isset ($ errors [$ key ]))
226- {
227- throw new \InvalidArgumentException ($ errors [$ key ]);
228- }
229- }
230- return $ value ;
231- };
232- }
233213}
0 commit comments