1+ <?php
2+
3+ namespace Etre \Shell \Console \Commands \Patch ;
4+
5+ use Etre \Shell \Helper \DirectoryHelper ;
6+ use Etre \Shell \Helper \PatchesHelper ;
7+ use N98 \Magento \Command \AbstractMagentoCommand ;
8+ use Symfony \Component \Console \Helper \ProgressBar ;
9+ use Symfony \Component \Console \Helper \Table as ConsoleTable ;
10+ use Symfony \Component \Console \Helper \TableCell as ConsoleTableCell ;
11+ use Symfony \Component \Console \Input \InputInterface ;
12+ use Symfony \Component \Console \Input \InputOption ;
13+ use Symfony \Component \Console \Output \OutputInterface ;
14+ use Symfony \Component \Console \Question \Question ;
15+
16+ class ListCommand extends AbstractMagentoCommand
17+ {
18+ protected $ patchTableHeaders = [
19+ "Installed " ,
20+ "Name " ,
21+ "Applied Magento Version " ,
22+ "Version " ,
23+ "" ,
24+ "Release Date " ,
25+ "" ,
26+ "State " ,
27+ ];
28+ /** @var DirectoryHelper $directoryHelper */
29+ protected $ directoryHelper ;
30+
31+ /** @var PatchesHelper $patchesHelper */
32+ protected $ patchesHelper ;
33+
34+ /**
35+ * PatchCommand constructor.
36+ * @param $patchHelper
37+ */
38+ public function __construct ($ name = null )
39+ {
40+ $ this ->directoryHelper = new DirectoryHelper ();
41+ $ this ->patchesHelper = new PatchesHelper ($ this ->directoryHelper );
42+
43+ parent ::__construct ($ name );
44+ }
45+
46+ public function configure ()
47+ {
48+ $ this
49+ ->setName ('etre:patch:list ' )
50+ ->setDescription ('List applied patches ' )
51+ ->addArgument ("id " , null , "Optional, Numerical ID of patch: <comment>XXXX</comment> in SUPEE-<comment>XXXX</comment> " )
52+ ->addArgument ("version " , null , "Optional, Patch version: <comment>Y</comment> in SUPEE-<comment>XXXX</comment> v<comment>Y</comment> " )
53+ ->addOption ('sort ' , null , InputOption::VALUE_OPTIONAL , "Sort by patch installation date: ASC | DESC " , "DESC " )
54+ ->addOption ('minimal ' , "m " , null , "List patches by name and version. " )
55+ ->addOption ('no-details ' , null , null , "Minimizes patch details shown " )
56+ ->setHelp ("This command lists your applied patches " );
57+ }
58+
59+ protected function execute (InputInterface $ input , OutputInterface $ output )
60+ {
61+ $ this ->detectMagento ($ output );
62+ if (!$ this ->initMagento ()) {
63+ return ;
64+ }
65+
66+ $ lookupPatchId = $ input ->getArgument ("id " );
67+ $ lookupPatchVersion = $ input ->getArgument ("version " );
68+
69+ $ this ->abortInvalidPatchId ($ output , $ lookupPatchId );
70+ $ this ->abortInvalidVersion ($ output , $ lookupPatchVersion );
71+
72+ $ patchData = $ this ->patchesHelper ->getPatches ($ input ->getOption ("sort " ), $ lookupPatchId , $ lookupPatchVersion );
73+ $ this ->abortOnEmptyPatches ($ output , $ patchData , $ lookupPatchId , $ lookupPatchVersion );
74+
75+ if ($ input ->getOption ("minimal " )):
76+ $ this ->outputResponseMinimal ($ output , $ patchData );
77+ endif ;
78+
79+ $ this ->executeNonMinimalResponse ($ input , $ output , $ patchData );
80+ $ output ->writeln ('' );
81+
82+ }
83+
84+ /**
85+ * @param OutputInterface $output
86+ * @param $lookupPatchId
87+ * @param $patchesHelper
88+ */
89+ protected function abortInvalidPatchId (OutputInterface $ output , $ lookupPatchId )
90+ {
91+ if ($ lookupPatchId && !$ this ->patchesHelper ->isValidPatchId ($ lookupPatchId )):
92+ $ this ->abortWithMessage ("Invalid patch Id provided. " , $ output );
93+ endif ;
94+ }
95+
96+ /**
97+ * @param OutputInterface $output
98+ */
99+ protected function abortWithMessage ($ message , OutputInterface $ output )
100+ {
101+ $ output ->writeln ("<comment> {$ message }</comment> " );
102+ exit ;
103+ }
104+
105+ /**
106+ * @param OutputInterface $output
107+ * @param $lookupPatchVersion
108+ * @param $patchesHelper
109+ */
110+ protected function abortInvalidVersion (OutputInterface $ output , $ lookupPatchVersion )
111+ {
112+ if ($ lookupPatchVersion && $ lookupPatchVersion && !$ this ->patchesHelper ->isValidVersion ($ lookupPatchVersion )):
113+ $ this ->abortWithMessage ("Invalid patch version provided. " , $ output );
114+ endif ;
115+ }
116+
117+ /**
118+ * @param OutputInterface $output
119+ * @param $patchData
120+ * @param $lookupPatchId
121+ * @param $lookupPatchVersion
122+ */
123+ protected function abortOnEmptyPatches (OutputInterface $ output , $ patchData , $ lookupPatchId , $ lookupPatchVersion )
124+ {
125+ if (empty ($ patchData )):
126+ if ($ lookupPatchId && $ lookupPatchVersion ):
127+ $ this ->abortWithMessage ("<comment>No patches found matching ID SUPEE- {$ lookupPatchId } v {$ lookupPatchVersion }</comment> " , $ output );
128+ elseif ($ lookupPatchId ):
129+ $ this ->abortWithMessage ("<comment>No patches found matching ID SUPEE- {$ lookupPatchId } at any version.</comment> " , $ output );
130+ else :
131+ $ this ->abortWithMessage ("<comment>No patches appear in applied.patches.list or this file could not be parsed.</comment> " , $ output );
132+ endif ;
133+ endif ;
134+ }
135+
136+ /**
137+ * @param OutputInterface $output
138+ * @param $patchData
139+ */
140+ protected function outputResponseMinimal (OutputInterface $ output , $ patchData )
141+ {
142+ foreach ($ patchData as $ patch ):
143+ $ this ->outputPatchTitle ($ output , $ patch );
144+ endforeach ;
145+ exit ;
146+ }
147+
148+ /**
149+ * @param OutputInterface $output
150+ * @param $patch
151+ */
152+ protected function outputPatchTitle (OutputInterface $ output , $ patch )
153+ {
154+ $ patchTitle = $ this ->getPatchTitle ($ patch );
155+ $ output ->writeln ("<info> {$ patchTitle }</info> " );
156+ }
157+
158+ /**
159+ * @param $patch
160+ * @return string
161+ */
162+ protected function getPatchTitle ($ patch )
163+ {
164+ $ patchName = $ patch ['headers ' ][1 ];
165+ $ patchVersion = $ patch ['headers ' ][3 ];
166+ $ patchState = "[ {$ patch ['headers ' ][7 ]}] " ;
167+ $ patchTitle = "{$ patchName } {$ patchVersion } {$ patchState }" ;
168+ return $ patchTitle ;
169+ }
170+
171+ /**
172+ * @param InputInterface $input
173+ * @param OutputInterface $output
174+ * @param $patchData
175+ * @param $progress
176+ */
177+ protected function executeNonMinimalResponse (InputInterface $ input , OutputInterface $ output , $ patchData )
178+ {
179+ $ progress = new ProgressBar ($ output , count ($ patchData ));
180+ $ output ->writeln ("" );
181+ $ progress ->setMessage ("% List Reviewed " );
182+ $ question = new Question ("Press any key to proceed to next patch. " );
183+ $ helper = $ this ->getHelper ('question ' );
184+
185+ foreach ($ patchData as $ patch ):
186+ $ this ->outputPatchTitle ($ output , $ patch );
187+ $ this ->outputPatchTable ($ input , $ output , $ patch );
188+ if (count ($ patchData ) > 1 ):
189+ $ progress ->advance ();
190+ $ this ->writeBlankLn ($ output );
191+ $ helper ->ask ($ input , $ output , $ question );
192+ endif ;
193+ $ output ->writeln ("" );
194+ endforeach ;
195+
196+ }
197+
198+ /**
199+ * @param InputInterface $input
200+ * @param OutputInterface $output
201+ * @param $patch
202+ */
203+ protected function outputPatchTable (InputInterface $ input , OutputInterface $ output , $ patch )
204+ {
205+ $ patchTable = new ConsoleTable ($ output );
206+ $ this ->setTableHeaders ($ patch , $ patchTable );
207+ if (!$ input ->getOption ('no-details ' )):
208+ foreach ($ patch ['details ' ] as $ patchDetail ):
209+ $ patchTable ->addRow ([new ConsoleTableCell ($ patchDetail , ['colspan ' => count ($ patch ['headers ' ])])]);
210+ endforeach ;
211+ endif ;
212+ $ patchTable ->setColumnWidths ([null , null , null , 1 ])->render ();
213+ }
214+
215+ /**
216+ * @param $patch
217+ * @param $patchTable
218+ */
219+ protected function setTableHeaders ($ patch , ConsoleTable $ patchTable )
220+ {
221+ $ patchTable ->setHeaders ($ this ->patchTableHeaders );
222+ $ patchTable ->addRow ($ patch ['headers ' ]);
223+ }
224+
225+ /**
226+ * @param OutputInterface $output
227+ */
228+ protected function writeBlankLn (OutputInterface $ output )
229+ {
230+ $ output ->writeln ("" );
231+ }
232+ }
0 commit comments