| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace N98\Magento\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Magento\Backend\Model\UrlInterface as BackendUrlInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Magento\Framework\UrlInterface as FrontendUrlInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Magento\Store\Api\Data\StoreInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Magento\Store\Model\Store; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use N98\Util\Console\Helper\ParameterHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use N98\Util\Exec; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use N98\Util\OperatingSystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class OpenBrowserCommand extends AbstractMagentoCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             ->setName('open-browser') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             ->addArgument('store', InputArgument::OPTIONAL, 'Store code or ID') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             ->setDescription('Open current project in browser <comment>(experimental)</comment>') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param InputInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @throws RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return int|void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->detectMagento($output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         if (!$this->initMagento()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         /** @var $parameter ParameterHelper */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $parameter = $this->getHelper('parameter'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $store = $parameter->askStore($input, $output, 'store', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if ($store->getId() == Store::DEFAULT_STORE_ID) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $url = $this->getBackendStoreUrl($store); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $url = $this->getFrontendStoreUrl($store); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $output->writeln('Opening URL <comment>' . $url . '</comment> in browser'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $opener = $this->resolveOpenerCommand($output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         Exec::run(escapeshellcmd($opener . ' ' . $url)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param StoreInterface $store | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     private function getBackendStoreUrl(StoreInterface $store) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $baseConfig = $this->getHelper('magento')->getBaseConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if (!isset($baseConfig['backend']['frontName'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             throw new RuntimeException('frontName for admin area could not be found.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $adminFrontName = $baseConfig['backend']['frontName']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return rtrim($store->getBaseUrl(BackendUrlInterface::URL_TYPE_WEB), '/') . '/' . $adminFrontName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param StoreInterface $store | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     private function getFrontendStoreUrl(StoreInterface $store) | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         return $store->getBaseUrl(FrontendUrlInterface::URL_TYPE_LINK) . '?___store=' . $store->getCode(); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     private function resolveOpenerCommand(OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $opener = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         if (OperatingSystem::isMacOs()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $opener = 'open'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } elseif (OperatingSystem::isWindows()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $opener = 'start'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             // Linux | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             if (exec('which xdg-open')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 $opener = 'xdg-open'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } elseif (exec('which gnome-open')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $opener = 'gnome-open'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } elseif (exec('which kde-open')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 $opener = 'kde-open'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         if (empty($opener)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             throw new RuntimeException('No opener command like xdg-open, gnome-open, kde-open was found.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $message = sprintf('open command is "%s"', $opener); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $output->writeln( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 '<debug>' . $message . '</debug>' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return $opener; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  |