/** * @param InputInterface $input * @param OutputInterface $output * @throws RuntimeException * @return int|void */ protected function execute(InputInterface $input, OutputInterface $output) { $opener = ''; if (OperatingSystem::isMacOs()) { $opener = 'open'; } elseif (OperatingSystem::isWindows()) { $opener = 'start'; } else { // Linux if (exec('which xde-open')) { $opener = 'xdg-open'; } elseif (exec('which gnome-open')) { $opener = 'gnome-open'; } elseif (exec('which kde-open')) { $opener = 'kde-open'; } } if (empty($opener)) { throw new RuntimeException('No opener command like xde-open, gnome-open, kde-open was found.'); } $this->detectMagento($output); if ($this->initMagento($output)) { $store = $this->getHelperSet()->get('parameter')->askStore($input, $output, 'store', true); if ($store->getId() == Store::DEFAULT_STORE_ID) { $url = $this->getBackendStoreUrl($store); } else { $url = $this->getFrontendStoreUrl($store); } $output->writeln('Opening URL <comment>' . $url . '</comment> in browser'); Exec::run(escapeshellcmd($opener . ' ' . $url)); } }
protected function execute(InputInterface $input, OutputInterface $output) { $this->detectMagento($output); if (!$this->initMagento()) { return; } // this command will not run on windows. if (OperatingSystem::isWindows()) { $output->writeln('<error>This command is not compatible with windows.</error>'); return; } $this->writeSection($output, 'Top log messages.'); $logPath = $this->findLogPath($input->getOption('log')); if ($logPath) { $process = new Process("cut -d ' ' -f 2- " . $logPath . " | sort | uniq -c | sort -n -k 1 | tail -" . $input->getArgument('top') . ""); $process->run(function ($type, $buffer) { if (Process::STATUS_READY) { $this->_result = $buffer; } }); $results = array_reverse(array_filter(explode("\n", $this->_result))); $i = 1; foreach ($results as $result) { $output->writeln('<comment>' . $i . '.</comment>' . " : " . $result); $i++; } } else { $output->writeln('<error>Could not find the path to the system.log</error>'); } }
protected function runSampleDataInstaller() { $installationArgs = $this->config->getArray('installation_args'); $processBuilder = new ProcessBuilder(['php', 'bin/magento', 'sampledata:deploy']); if (!OperatingSystem::isWindows()) { $processBuilder->setPrefix('/usr/bin/env'); } $process = $processBuilder->getProcess(); $process->setTimeout(86400); $process->start(); $process->wait(function ($type, $buffer) { $this->output->write($buffer, false); }); // @TODO Refactor code duplication if (!OperatingSystem::isWindows()) { $processBuilder->setPrefix('/usr/bin/env'); } $processBuilder = new ProcessBuilder(array('php', 'bin/magento', 'setup:upgrade')); $process = $processBuilder->getProcess(); $process->setTimeout(86400); $process->start(); $process->wait(function ($type, $buffer) { $this->output->write($buffer, false); }); }
/** * @param InputInterface $input * @param OutputInterface $output * @throws RuntimeException * @return int|void */ protected function execute(InputInterface $input, OutputInterface $output) { $opener = ''; if (OperatingSystem::isMacOs()) { $opener = 'open'; } elseif (OperatingSystem::isWindows()) { $opener = 'start'; } else { // Linux if (exec('which xde-open')) { $opener = 'xdg-open'; } elseif (exec('which gnome-open')) { $opener = 'gnome-open'; } elseif (exec('which kde-open')) { $opener = 'kde-open'; } } if (empty($opener)) { throw new RuntimeException('No opener command like xde-open, gnome-open, kde-open was found.'); } $this->detectMagento($output); if ($this->initMagento($output)) { $store = $this->getHelperSet()->get('parameter')->askStore($input, $output, 'store', true); if ($store->getId() == \Mage_Core_Model_App::ADMIN_STORE_ID) { $adminFrontName = (string) \Mage::getConfig()->getNode('admin/routers/adminhtml/args/frontName'); $url = rtrim($store->getBaseUrl(\Mage_Core_Model_Store::URL_TYPE_WEB), '/') . '/' . $adminFrontName; } else { $url = $store->getBaseUrl(\Mage_Core_Model_Store::URL_TYPE_LINK) . '?___store=' . $store->getCode(); } $output->writeln('Opening URL <comment>' . $url . '</comment> in browser'); Exec::run(escapeshellcmd($opener . ' ' . $url)); } }
/** * @param OutputInterface $output * @return string */ private function resolveOpenerCommand(OutputInterface $output) { $opener = ''; if (OperatingSystem::isMacOs()) { $opener = 'open'; } elseif (OperatingSystem::isWindows()) { $opener = 'start'; } else { // Linux if (exec('which xdg-open')) { $opener = 'xdg-open'; } elseif (exec('which gnome-open')) { $opener = 'gnome-open'; } elseif (exec('which kde-open')) { $opener = 'kde-open'; } } if (empty($opener)) { throw new RuntimeException('No opener command like xdg-open, gnome-open, kde-open was found.'); } if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) { $message = sprintf('open command is "%s"', $opener); $output->writeln('<debug>' . $message . '</debug>'); } return $opener; }
/** * @return bool */ public function isEnabled() { if (OperatingSystem::isWindows()) { return false; } if ($this->getApplication()->isPharMode()) { $pharFile = $_SERVER['argv'][0]; return substr($pharFile, -5) == '.phar'; } return true; }
/** * @return void */ private function runMagentoCommand($command) { $processBuilder = new ProcessBuilder(['php', 'bin/magento', $command]); if (!OperatingSystem::isWindows()) { $processBuilder->setPrefix('/usr/bin/env'); } $process = $processBuilder->getProcess(); $process->setTimeout(86400); $process->start(); $process->wait(function ($type, $buffer) { $this->output->write('bin/magento > ' . $buffer, false); }); }
/** * @param array $scriptFolders * @param string $magentoRootFolder */ public function __construct(array $scriptFolders, $magentoRootFolder = null) { $this->_magentoRootFolder = $magentoRootFolder; if (OperatingSystem::isWindows()) { $this->_homeScriptFolder = OperatingSystem::getHomeDir() . '/n98-magerun2/scripts'; } else { $this->_homeScriptFolder = OperatingSystem::getHomeDir() . '/.n98-magerun2/scripts'; } $this->_scriptFolders = $scriptFolders; $this->_scriptFolders[] = $this->_homeScriptFolder; foreach ($this->_scriptFolders as $key => $scriptFolder) { if (!is_dir($scriptFolder)) { unset($this->_scriptFolders[$key]); } } if (count($this->_scriptFolders)) { $this->findScripts(); } }
/** * @return string * @throws \Exception */ protected function downloadComposer() { $this->output->writeln('<info>Could not find composer. Try to download it.</info>'); $io = new ConsoleIO($this->input, $this->output, $this->getCommand()->getHelperSet()); $rfs = new RemoteFilesystem($io); $composerInstaller = $rfs->getContents('getcomposer.org', 'https://getcomposer.org/installer', true); $tempComposerInstaller = $this->config['installationFolder'] . '/_composer_installer.php'; file_put_contents($tempComposerInstaller, $composerInstaller); if (OperatingSystem::isWindows()) { $installCommand = 'php ' . $tempComposerInstaller . ' --force'; } else { $installCommand = '/usr/bin/env php ' . $tempComposerInstaller . ' --force'; } $this->output->writeln('<comment>' . $installCommand . '</comment>'); exec($installCommand, $installationOutput, $returnStatus); unlink($tempComposerInstaller); $installationOutput = implode(PHP_EOL, $installationOutput); if ($returnStatus !== self::EXEC_STATUS_OK) { throw new \Exception('Installation failed.' . $installationOutput); } else { $this->output->writeln('<info>Successfully installed composer to Magento root</info>'); } return $this->config['installationFolder'] . '/composer.phar'; }
/** * @return bool */ public function isEnabled() { return function_exists('exec') && !OperatingSystem::isWindows(); }
/** * Check if there is a user config file. ~/.n98-magerun.yaml * * @param array $config * @param string $magentoRootFolder * * @return array */ public function loadUserConfig($config, $magentoRootFolder = null) { if ($this->_userConfig == null) { $this->_userConfig = array(); $homeDirectory = OperatingSystem::getHomeDir(); if (OperatingSystem::isWindows()) { $personalConfigFile = $homeDirectory . DIRECTORY_SEPARATOR . $this->_customConfigFilename; } else { $personalConfigFile = $homeDirectory . DIRECTORY_SEPARATOR . '.' . $this->_customConfigFilename; } if ($homeDirectory && file_exists($personalConfigFile)) { $userConfig = $this->applyVariables(\file_get_contents($personalConfigFile), $magentoRootFolder, null); $this->_userConfig = Yaml::parse($userConfig); return $config; } } $config = ArrayFunctions::mergeArrays($config, $this->_userConfig); return $config; }
/** * Check if there is a user config file. ~/.n98-magerun.yaml * * @param array $config * @param string $magentoRootFolder * * @return array */ public function loadUserConfig($config, $magentoRootFolder = null) { if ($this->_userConfig == null) { $this->_userConfig = array(); $homeDirectory = OperatingSystem::getHomeDir(); if (OperatingSystem::isWindows()) { $personalConfigFile = $homeDirectory . DIRECTORY_SEPARATOR . $this->_customConfigFilename; } else { $personalConfigFile = $homeDirectory . DIRECTORY_SEPARATOR . '.' . $this->_customConfigFilename; } if ($homeDirectory && file_exists($personalConfigFile)) { $userConfig = $this->applyVariables(\file_get_contents($personalConfigFile), $magentoRootFolder, null); $this->_userConfig = Yaml::parse($userConfig); if (OutputInterface::VERBOSITY_DEBUG <= $this->_output->getVerbosity()) { $this->_output->writeln('<debug>Load user config <comment>' . $personalConfigFile . '</comment></debug>'); } return $config; } } $config = ArrayFunctions::mergeArrays($config, $this->_userConfig); return $config; }
/** * Load config from all installed bundles * * @param array $config * @param string $magentoRootFolder * * @return array */ public function loadPluginConfig(array $config, $magentoRootFolder) { if ($this->_pluginConfig == null) { $this->_pluginConfig = array(); $moduleBaseFolders = array(); $customFilename = $this->_customConfigFilename; $customName = pathinfo($customFilename, PATHINFO_FILENAME); if (OperatingSystem::isWindows()) { $config['plugin']['folders'][] = getenv('WINDIR') . '/' . $customName . '/modules'; $config['plugin']['folders'][] = OperatingSystem::getHomeDir() . '/' . $customName . '/modules'; } else { $config['plugin']['folders'][] = OperatingSystem::getHomeDir() . '/.' . $customName . '/modules'; } $config['plugin']['folders'][] = $magentoRootFolder . '/lib/' . $customName . '/modules'; foreach ($config['plugin']['folders'] as $folder) { if (is_dir($folder)) { $moduleBaseFolders[] = $folder; } } /** * Allow modules to be placed vendor folder if not in phar mode */ if (!$this->_isPharMode) { if (is_dir($this->getVendorDir())) { $finder = Finder::create(); $finder->files()->depth(2)->followLinks()->ignoreUnreadableDirs(true)->name($customFilename)->in($this->getVendorDir()); foreach ($finder as $file) { /* @var $file SplFileInfo */ $this->registerPluginConfigFile($magentoRootFolder, $file); } } } if (count($moduleBaseFolders) > 0) { // Glob plugin folders $finder = Finder::create(); $finder->files()->depth(1)->followLinks()->ignoreUnreadableDirs(true)->name($customFilename)->in($moduleBaseFolders); foreach ($finder as $file) { /* @var $file SplFileInfo */ $this->registerPluginConfigFile($magentoRootFolder, $file); } } } $config = ArrayFunctions::mergeArrays($config, $this->_pluginConfig); return $config; }
public function operatingSystemIsNotWindows() { $this->assert(!OperatingSystem::isWindows(), "operating system is windows"); }
/** * Search for magento root folder * * @param InputInterface $input * @param OutputInterface $output * @return void */ public function detectMagento(InputInterface $input = null, OutputInterface $output = null) { // do not detect magento twice if ($this->_magentoDetected) { return; } if ($this->getMagentoRootFolder() === null) { $this->_checkRootDirOption(); if (function_exists('exec')) { if (OperatingSystem::isWindows()) { $folder = exec('@echo %cd%'); // @TODO not currently tested!!! } else { $folder = exec('pwd'); } } else { $folder = getcwd(); } } else { $folder = $this->getMagentoRootFolder(); } $this->getHelperSet()->set(new MagentoHelper($input, $output), 'magento'); $magentoHelper = $this->getHelperSet()->get('magento'); /* @var $magentoHelper MagentoHelper */ if (!$this->_directRootDir) { $subFolders = $this->getDetectSubFolders(); } else { $subFolders = array(); } $this->_magentoDetected = $magentoHelper->detect($folder, $subFolders); $this->_magentoRootFolder = $magentoHelper->getRootFolder(); $this->_magentoEnterprise = $magentoHelper->isEnterpriseEdition(); $this->_magentoMajorVersion = $magentoHelper->getMajorVersion(); $this->_magerunStopFileFound = $magentoHelper->isMagerunStopFileFound(); $this->_magerunStopFileFolder = $magentoHelper->getMagerunStopFileFolder(); }
/** * @param InputInterface $input * @param OutputInterface $output * @throws \Exception * @return array */ protected function installMagento(InputInterface $input, OutputInterface $output) { $this->getApplication()->setAutoExit(false); $dialog = $this->getHelperSet()->get('dialog'); $defaults = $this->commandConfig['installation']['defaults']; $useDefaultConfigParams = $this->_parseBoolOption($input->getOption('useDefaultConfigParams')); $sessionSave = $useDefaultConfigParams ? $defaults['session_save'] : $dialog->ask($output, '<question>Please enter the session save:</question> <comment>[' . $defaults['session_save'] . ']</comment>: ', $defaults['session_save']); $adminFrontname = $useDefaultConfigParams ? $defaults['admin_frontname'] : $dialog->askAndValidate($output, '<question>Please enter the admin frontname:</question> <comment>[' . $defaults['admin_frontname'] . ']</comment> ', $this->notEmptyCallback, false, $defaults['admin_frontname']); $currency = $useDefaultConfigParams ? $defaults['currency'] : $dialog->askAndValidate($output, '<question>Please enter the default currency code:</question> <comment>[' . $defaults['currency'] . ']</comment>: ', $this->notEmptyCallback, false, $defaults['currency']); $locale = $useDefaultConfigParams ? $defaults['locale'] : $dialog->askAndValidate($output, '<question>Please enter the locale code:</question> <comment>[' . $defaults['locale'] . ']</comment>: ', $this->notEmptyCallback, false, $defaults['locale']); $timezone = $useDefaultConfigParams ? $defaults['timezone'] : $dialog->askAndValidate($output, '<question>Please enter the timezone:</question> <comment>[' . $defaults['timezone'] . ']</comment>: ', $this->notEmptyCallback, false, $defaults['timezone']); $adminUsername = $useDefaultConfigParams ? $defaults['admin_username'] : $dialog->askAndValidate($output, '<question>Please enter the admin username:</question> <comment>[' . $defaults['admin_username'] . ']</comment>: ', $this->notEmptyCallback, false, $defaults['admin_username']); $adminPassword = $useDefaultConfigParams ? $defaults['admin_password'] : $dialog->askAndValidate($output, '<question>Please enter the admin password:</question> <comment>[' . $defaults['admin_password'] . ']</comment>: ', $this->notEmptyCallback, false, $defaults['admin_password']); $adminFirstname = $useDefaultConfigParams ? $defaults['admin_firstname'] : $dialog->askAndValidate($output, '<question>Please enter the admin\'s firstname:</question> <comment>[' . $defaults['admin_firstname'] . ']</comment>: ', $this->notEmptyCallback, false, $defaults['admin_firstname']); $adminLastname = $useDefaultConfigParams ? $defaults['admin_lastname'] : $dialog->askAndValidate($output, '<question>Please enter the admin\'s lastname:</question> <comment>[' . $defaults['admin_lastname'] . ']</comment>: ', $this->notEmptyCallback, false, $defaults['admin_lastname']); $adminEmail = $useDefaultConfigParams ? $defaults['admin_email'] : $dialog->askAndValidate($output, '<question>Please enter the admin\'s email:</question> <comment>[' . $defaults['admin_email'] . ']</comment>: ', $this->notEmptyCallback, false, $defaults['admin_email']); $validateBaseUrl = function ($input) { if (!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $input)) { throw new \InvalidArgumentException('Please enter a valid URL'); } if (parse_url($input, \PHP_URL_HOST) == 'localhost') { throw new \InvalidArgumentException('localhost cause problems! Please use 127.0.0.1 or another hostname'); } return $input; }; $baseUrl = $input->getOption('baseUrl') !== null ? $input->getOption('baseUrl') : $dialog->askAndValidate($output, '<question>Please enter the base url:</question> ', $validateBaseUrl, false); $baseUrl = rtrim($baseUrl, '/') . '/'; // normalize baseUrl /** * Correct session save (common mistake) */ if ($sessionSave == 'file') { $sessionSave = 'files'; } /** * Try to create session folder */ $defaultSessionFolder = $this->config['installationFolder'] . DIRECTORY_SEPARATOR . 'var/session'; if ($sessionSave == 'files' && !is_dir($defaultSessionFolder)) { @mkdir($defaultSessionFolder); } $argv = array('license_agreement_accepted' => 'yes', 'locale' => $locale, 'timezone' => $timezone, 'db_host' => $this->config['db_host'], 'db_name' => $this->config['db_name'], 'db_user' => $this->config['db_user'], 'db_pass' => $this->config['db_pass'], 'url' => $baseUrl, 'use_rewrites' => 'yes', 'use_secure' => 'no', 'secure_base_url' => '', 'use_secure_admin' => 'no', 'admin_username' => $adminUsername, 'admin_lastname' => $adminLastname, 'admin_firstname' => $adminFirstname, 'admin_email' => $adminEmail, 'admin_password' => $adminPassword, 'session_save' => $sessionSave, 'admin_frontname' => $adminFrontname, 'backend_frontname' => $adminFrontname, 'default_currency' => $currency, 'skip_url_validation' => 'yes'); if ($useDefaultConfigParams) { if (strlen($defaults['encryption_key']) > 0) { $argv['encryption_key'] = $defaults['encryption_key']; } if (strlen($defaults['use_secure']) > 0) { $argv['use_secure'] = $defaults['use_secure']; $argv['secure_base_url'] = str_replace('http://', 'https://', $baseUrl); } if (strlen($defaults['use_rewrites']) > 0) { $argv['use_rewrites'] = $defaults['use_rewrites']; } } $installArgs = ''; foreach ($argv as $argName => $argValue) { $installArgs .= '--' . $argName . ' ' . escapeshellarg($argValue) . ' '; } $output->writeln('<info>Start installation process.</info>'); if (OperatingSystem::isWindows()) { $installCommand = 'php ' . $this->getInstallScriptPath() . ' ' . $installArgs; } else { $installCommand = '/usr/bin/env php ' . $this->getInstallScriptPath() . ' ' . $installArgs; } $output->writeln('<comment>' . $installCommand . '</comment>'); exec($installCommand, $installationOutput, $returnStatus); $installationOutput = implode(PHP_EOL, $installationOutput); if ($returnStatus !== self::EXEC_STATUS_OK) { throw new \Exception('Installation failed.' . $installationOutput); } else { $output->writeln('<info>Successfully installed Magento</info>'); $encryptionKey = trim(substr($installationOutput, strpos($installationOutput, ':') + 1)); $output->writeln('<comment>Encryption Key:</comment> <info>' . $encryptionKey . '</info>'); } $dialog = $this->getHelperSet()->get('dialog'); /** * Htaccess file */ if ($input->getOption('useDefaultConfigParams') == null || $input->getOption('replaceHtaccessFile') != null) { $replaceHtaccessFile = false; if ($this->_parseBoolOption($input->getOption('replaceHtaccessFile'))) { $replaceHtaccessFile = true; } elseif ($dialog->askConfirmation($output, '<question>Write BaseURL to .htaccess file?</question> <comment>[n]</comment>: ', false)) { $replaceHtaccessFile = true; } if ($replaceHtaccessFile) { $this->replaceHtaccessFile($baseUrl); } } \chdir($this->config['installationFolder']); $this->getApplication()->reinit(); $output->writeln('<info>Reindex all after installation</info>'); $this->getApplication()->run(new StringInput('index:reindex:all'), $output); $this->getApplication()->run(new StringInput('sys:check'), $output); $output->writeln('<info>Successfully installed magento</info>'); }
/** * @return array */ private function getUserConfigFilePaths() { $paths = array(); $homeDirectory = OperatingSystem::getHomeDir(); if (!strlen($homeDirectory)) { return $paths; } if (!is_dir($homeDirectory)) { throw new RuntimeException(sprintf("Home-directory '%s' is not a directory.", $homeDirectory)); } $basename = $this->customConfigFilename; if (OperatingSystem::isWindows()) { $paths[] = $homeDirectory . '/' . $basename; } $paths[] = $homeDirectory . '/.' . $basename; return $paths; }
/** * @param string $installArgs * * @throws Exception */ protected function _runInstaller($installArgs) { $installationOutput = null; $returnStatus = null; if (OperatingSystem::isWindows()) { $installCommand = 'php ' . $this->getInstallScriptPath() . ' setup:install ' . $installArgs; } else { $installCommand = '/usr/bin/env php ' . $this->getInstallScriptPath() . ' setup:install ' . $installArgs; } $this->output->writeln('<comment>' . $installCommand . '</comment>'); exec($installCommand, $installationOutput, $returnStatus); $installationOutput = implode(PHP_EOL, $installationOutput); if ($returnStatus !== self::EXEC_STATUS_OK) { throw new \RuntimeException('Installation failed.' . $installationOutput, 1); } else { $this->output->writeln('<info>Successfully installed Magento</info>'); $encryptionKey = trim(substr($installationOutput, strpos($installationOutput, ':') + 1)); $this->output->writeln('<comment>Encryption Key:</comment> <info>' . $encryptionKey . '</info>'); } }
/** * @return string */ private function getUserConfigFilePath() { $homeDirectory = OperatingSystem::getHomeDir(); if (!$homeDirectory) { throw new RuntimeException('Unable to get home-directory to obtain user-config-file.'); } if (!is_dir($homeDirectory)) { throw new RuntimeException(sprintf("Home-directory '%s' is not a directory.", $homeDirectory)); } $basename = $this->customConfigFilename; if (!OperatingSystem::isWindows()) { $basename = ".{$basename}"; } return $homeDirectory . '/' . $basename; }