1+ <?php
2+
3+ namespace Etre \Shell \Console \Commands \Installer ;
4+
5+ use Etre \Shell \Exceptions \ExtensionMissingException ;
6+ use Etre \Shell \Exceptions \InvalidUrlException ;
7+ use Etre \Shell \Helper \DirectoryHelper ;
8+ use Etre \Shell \Helper \PatchesHelper ;
9+ use Symfony \Component \Console \Command \Command ;
10+ use Symfony \Component \Console \Input \InputInterface ;
11+ use Symfony \Component \Console \Input \InputOption ;
12+ use Symfony \Component \Console \Output \OutputInterface ;
13+
14+ class RecoverCommand extends Command
15+ {
16+ protected $ zendHttpConfig ;
17+ /** @var DirectoryHelper $directoryHelper */
18+ protected $ directoryHelper ;
19+
20+ /** @var PatchesHelper $patchesHelper */
21+ protected $ patchesHelper ;
22+ protected $ edition ;
23+ protected $ version ;
24+
25+ /**
26+ * PatchCommand constructor.
27+ * @param $patchHelper
28+ */
29+ public function __construct ($ name = null )
30+ {
31+ $ this ->zendHttpConfig = [
32+ 'adapter ' => 'Zend_Http_Client_Adapter_Socket ' ,
33+ 'ssltransport ' => 'tls ' ,
34+ ];
35+ $ this ->directoryHelper = new DirectoryHelper ();
36+ $ this ->patchesHelper = new PatchesHelper ($ this ->directoryHelper );
37+
38+ parent ::__construct ($ name );
39+ }
40+
41+ public function configure ()
42+ {
43+ $ this
44+ ->setName ('installer:recover ' )
45+ ->setDescription ("Missing your install.php? Download it quick and easy. " )
46+ ->addOption ("mage-edition " , "E " , InputOption::VALUE_OPTIONAL , "Override the Magento installer edition: <comment>Enterprise|Community</comment> " )
47+ ->addOption ("mage-version " , "X " , InputOption::VALUE_OPTIONAL , "Override the Magento installer version: <comment>1.9.3.0|1.9.2.4|1.9.2.3|...</comment> " )
48+ ->setHelp ("This command will download the install.php for your version of Magento or another version if specified. " );
49+ }
50+
51+ protected function execute (InputInterface $ input , OutputInterface $ output )
52+ {
53+ $ this ->writeBlankLn ($ output );
54+ $ options ['is_installed ' ] = false ;
55+ $ output ->writeln ("<info>Initiating installer</info> " );
56+ try {
57+ \Mage::app ();
58+ $ this ->initEdition ($ input );
59+ if (!$ this ->checkIsValidEdition ($ output )) return ;
60+
61+ $ this ->initVersion ($ input );
62+ if (!$ this ->checkIsValidVersion ($ output )) return ;
63+
64+ if (!extension_loaded ('curl ' )) {
65+ throw new ExtensionMissingException ("curl extension required. " );
66+ }
67+ $ output ->writeln ("Getting Magento installer for your version: <info> {$ this ->getVersion ()}</info> " );
68+ // Set the configuration parameters
69+ $ installerHttpLookupClient = new \Zend_Http_Client ("https://api.github.com/repos/OpenMage/magento-mirror/contents/install.php " , $ this ->zendHttpConfig );
70+ $ installerHttpLookupClient ->setParameterGet ("ref " , $ this ->getVersion ());
71+ $ installerHttpLookupClient ->request ();
72+ $ statusCode = $ installerHttpLookupClient ->getLastResponse ()->getStatus ();
73+ if ($ statusCode !== 200 ):
74+ $ statusString = $ installerHttpLookupClient ->getLastResponse ();
75+ throw new \Exception ("Attempting to download Magento installer: could not reach GitHub API " );
76+ endif ;
77+ $ mageVersionInstaller = json_decode ($ installerHttpLookupClient ->getLastResponse ()->getBody ());
78+
79+ $ installerDownloader = new \Zend_Http_Client ($ mageVersionInstaller ->download_url , $ this ->zendHttpConfig );
80+ $ installerDownloader ->request ();
81+ $ gitDownloadResponse = $ installerDownloader ->getLastResponse ();
82+ $ statusCode = $ gitDownloadResponse ->getStatus ();
83+ if ($ statusCode !== 200 ):
84+ $ statusString = $ gitDownloadResponse ->getHeaders ()['Status ' ];
85+ throw new InvalidUrlException ("The attempt to download {$ mageVersionInstaller ->download_url } returned {$ statusString }" );
86+ endif ;
87+ $ directoryHelper = $ this ->directoryHelper ;
88+ $ installerSavePath = \Mage::getBaseDir () . $ directoryHelper ::DS . "install.php " ;
89+ file_put_contents ($ installerSavePath ,$ gitDownloadResponse ->getBody ());
90+ } catch (\Zend_Db_Adapter_Exception $ e ) {
91+ $ this ->outputMissingExtensionMessage ($ output , $ e );
92+
93+ } catch (ExtensionMissingException $ e ) {
94+ $ this ->outputMissingExtensionMessage ($ output , $ e );
95+ } catch (InvalidUrlException $ e ) {
96+ $ output ->writeln ([
97+ "<info>Could not complete request:</info> " ,
98+ "\tError: \t<error> {$ e ->getMessage ()}</error> "
99+ ]);
100+ }catch (\Exception $ e ){
101+ $ output ->writeln ([
102+ "\tError: \t<error> {$ e ->getMessage ()}</error> "
103+ ]);
104+ }
105+ $ output ->writeln ("<info>Installer has been recovered and placed in your Magento root directory</info> " );
106+
107+ $ this ->writeBlankLn ($ output );
108+ }
109+
110+ /**
111+ * @param OutputInterface $output
112+ */
113+ protected function writeBlankLn (OutputInterface $ output )
114+ {
115+ $ output ->writeln ("" );
116+ }
117+
118+ /**
119+ * @param InputInterface $input
120+ */
121+ protected function initEdition (InputInterface $ input )
122+ {
123+ if ($ input ->getOption ("mage-edition " )):
124+ $ this ->setEdition ($ input ->getOption ("mage-edition " ));
125+ else :
126+ $ this ->setEdition (\Mage::getEdition ());
127+ endif ;
128+ }
129+
130+ /**
131+ * @param OutputInterface $output
132+ */
133+ protected function checkIsValidEdition ($ output )
134+ {
135+ $ edition = strtolower ($ this ->getEdition ());
136+
137+ if ($ edition !== "community " ):
138+ $ output ->writeln ("<comment> {$ edition } is not supported by this installer.</comment> " );
139+
140+ return false ;
141+ endif ;
142+ return true ;
143+ }
144+
145+ /**
146+ * @return mixed
147+ */
148+ public function getEdition ()
149+ {
150+ return $ this ->edition ;
151+ }
152+
153+ /**
154+ * @param mixed $edition
155+ */
156+ public function setEdition ($ edition )
157+ {
158+ $ this ->edition = $ edition ;
159+ }
160+
161+ /**
162+ * @param InputInterface $input
163+ */
164+ protected function initVersion (InputInterface $ input )
165+ {
166+ if ($ input ->getOption ("mage-version " )):
167+ $ this ->setVersion ($ input ->getOption ("mage-version " ));
168+ else :
169+ $ this ->setVersion (\Mage::getVersion ());
170+ endif ;
171+ }
172+
173+ /**
174+ * @param OutputInterface $output
175+ */
176+ protected function checkIsValidVersion ($ output )
177+ {
178+ $ version = $ this ->getVersion ();
179+ $ zendClient = new \Zend_Http_Client ("https://api.github.com/repos/OpenMage/magento-mirror/tags " , $ this ->zendHttpConfig );
180+ $ zendClient ->request ();
181+ $ gitResponse = $ zendClient ->getLastResponse ();
182+ $ statusCode = $ gitResponse ->getStatus ();
183+ if ($ statusCode !== 200 ) throw new InvalidUrlException ("Could not communicate with GitHub: \n\thttps://api.github.com/repos/OpenMage/magento-mirror/tags \n\t{$ gitResponse ->getBody ()}" );
184+ $ gitVersionResults = json_decode ($ gitResponse ->getBody ());
185+
186+ if (!$ this ->objArraySearch ($ gitVersionResults , "name " , $ version )):
187+ $ availableVersions = [];
188+ foreach ($ gitVersionResults as $ availableVersion ):
189+ $ availableVersions [] = $ availableVersion ->name ;
190+ endforeach ;
191+ $ availableVersions = implode (" | " , $ availableVersions );
192+ $ output ->writeln ([
193+ "<info>Version Response:</info> " ,
194+ "\t<comment> {$ version } is not supported by this installer.</comment> " ,
195+ "\t<comment>Available Versions:</comment> {$ availableVersions }" ,
196+ ]);
197+ return false ;
198+ endif ;
199+ return true ;
200+ }
201+
202+ /**
203+ * @return mixed
204+ */
205+ public function getVersion ()
206+ {
207+ return $ this ->version ;
208+ }
209+
210+ /**
211+ * @param mixed $version
212+ * @return SetupCommand
213+ */
214+ public function setVersion ($ version )
215+ {
216+ $ this ->version = $ version ;
217+ return $ this ;
218+ }
219+
220+ /**
221+ * @param OutputInterface $output
222+ * @param $e
223+ */
224+ protected function outputMissingExtensionMessage (OutputInterface $ output , $ e )
225+ {
226+ $ missingExtension = $ this ->getMissingExtension ($ e );
227+ $ missingExtensionUrl = $ this ->getExtensionUrl ($ missingExtension );
228+ $ output ->writeln ([
229+ "PHP Plugin missing: " ,
230+ "\tError: \t<error> {$ e ->getMessage ()}</error> " ,
231+ "\tHelper: \tTry executing <info>sudo [apt|apt-get|yum] install {$ missingExtension }</info> or visit: {$ missingExtensionUrl } for installation instructions. " ,
232+ "\t\t<info>Sudo may be needed</info> " ,
233+ ]);
234+ }
235+
236+ /**
237+ * @param $e
238+ * @return mixed
239+ */
240+ protected function getMissingExtension ($ e )
241+ {
242+ $ errorMessage = $ e ->getMessage ();
243+ $ extension = explode ("extension " , $ errorMessage )[0 ];
244+ $ extensionTidied = trim ($ extension );
245+ return "php- " . str_replace ('_ ' , '- ' , $ extensionTidied );
246+ }
247+
248+ /**
249+ * @param $missingExtension
250+ * @return string
251+ */
252+ protected function getExtensionUrl ($ missingExtension )
253+ {
254+ $ urlParam = str_replace ("php- " , "" , $ missingExtension );
255+ return "http://php.net/manual/en/ref. {$ urlParam }.php " ;
256+ }
257+
258+ public static function objArraySearch ($ array , $ index , $ value )
259+ {
260+ foreach ($ array as $ arrayInf ) {
261+ if ($ arrayInf ->{$ index } == $ value ) {
262+ return $ arrayInf ;
263+ }
264+ }
265+ return null ;
266+ }
267+
268+
269+ }
0 commit comments