diff --git a/composer.json b/composer.json index a711ca241..d06148471 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ "magerun" ], "require": { - "php": "~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0", + "php": "~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.3.0 || ~8.2.0 || ~8.4.0", "ext-json": "*", "ext-libxml": "*", "ext-pdo": "*", diff --git a/src/N98/Magento/Command/AbstractMagentoCommand.php b/src/N98/Magento/Command/AbstractMagentoCommand.php index 72307ab65..2c416b9bf 100644 --- a/src/N98/Magento/Command/AbstractMagentoCommand.php +++ b/src/N98/Magento/Command/AbstractMagentoCommand.php @@ -1,5 +1,7 @@ addFormatOption(); + } + } + /** * Initializes the command just after the input has been validated. * @@ -83,11 +109,50 @@ protected function initialize(InputInterface $input, OutputInterface $output) $this->checkDeprecatedAliases($input, $output); } + protected function execute(InputInterface $input, OutputInterface $output): int + { + if ($this instanceof CommandFormatable) { + $this->detectMagento($output, static::$detectMagentoSilent); + + $formatOption = $input->getOption(static::COMMAND_OPTION_FORMAT); + + if ($formatOption === null) { + $this->writeSection($output, $this->getSectionTitle($input, $output)); + } + + if (!$this->initMagento()) { + return Command::FAILURE; + } + + $data = $this->getListData($input, $output); + if ($formatOption === null && $data === []) { + if (static::$noResultMessage) { + $output->writeln(sprintf( + '%s', + static::$noResultMessage + )); + } else { + $output->writeln(sprintf( + 'No entry found for "%s" ', + $this->getSectionTitle($input, $output) + )); + } + } + + $this->getTableHelper() + ->setHeaders($this->getListHeader($input, $output)) + ->renderByFormat($output, $data, $input->getOption(self::COMMAND_OPTION_FORMAT)); + + return Command::SUCCESS; + } + + return Command::INVALID; + } + private function _initWebsites() { $this->_websiteCodeMap = []; - /** @var \Mage_Core_Model_Website[] $websites */ - $websites = Mage::app()->getWebsites(false); + $websites = Mage::app()->getWebsites(); foreach ($websites as $website) { $this->_websiteCodeMap[$website->getId()] = $website->getCode(); } @@ -638,7 +703,7 @@ protected function createSubCommandFactory( public function addFormatOption(): self { $this->addOption( - 'format', + self::COMMAND_OPTION_FORMAT, null, InputOption::VALUE_OPTIONAL, 'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']' diff --git a/src/N98/Magento/Command/Admin/User/AbstractAdminUserCommand.php b/src/N98/Magento/Command/Admin/User/AbstractAdminUserCommand.php index 1d09cf3ed..7f49c2f36 100644 --- a/src/N98/Magento/Command/Admin/User/AbstractAdminUserCommand.php +++ b/src/N98/Magento/Command/Admin/User/AbstractAdminUserCommand.php @@ -1,7 +1,13 @@ setName('admin:user:list') - ->setDescription('List admin users.') - ->addFormatOption() - ; + return ['id', 'username', 'email', 'status']; } /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int + * {@inheritDoc} */ - protected function execute(InputInterface $input, OutputInterface $output): int + public function getListData(InputInterface $input, OutputInterface $output): array { - $this->detectMagento($output, true); - if (!$this->initMagento()) { - return 0; + if (is_array($this->data)) { + return $this->data; } - /** @var \Mage_Admin_Model_User $userModel */ $userModel = $this->getUserModel(); $userList = $userModel->getCollection(); - $table = []; + $this->data = []; + /** @var Mage_Admin_Model_User $user */ foreach ($userList as $user) { - $table[] = [$user->getId(), $user->getUsername(), $user->getEmail(), $user->getIsActive() ? 'active' : 'inactive']; + $this->data[] = [ + $user->getId(), + $user->getUsername(), + $user->getEmail(), + $user->getIsActive() ? 'active' : 'inactive' + ]; } - $tableHelper = $this->getTableHelper(); - $tableHelper - ->setHeaders(['id', 'username', 'email', 'status']) - ->renderByFormat($output, $table, $input->getOption('format')); - return 0; + return $this->data; } } diff --git a/src/N98/Magento/Command/Cache/ListCommand.php b/src/N98/Magento/Command/Cache/ListCommand.php index cbae54ef4..5d28c17ff 100644 --- a/src/N98/Magento/Command/Cache/ListCommand.php +++ b/src/N98/Magento/Command/Cache/ListCommand.php @@ -1,7 +1,10 @@ setName('cache:list') - ->setDescription('Lists all magento caches') - ->addFormatOption() - ; + return 'Caches'; } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int + * {@inheritDoc} */ - protected function execute(InputInterface $input, OutputInterface $output): int + public function getListHeader(InputInterface $input, OutputInterface $output): array { - $this->detectMagento($output, true); - if (!$this->initMagento()) { - return 0; - } + return ['code', 'status']; + } - $cacheTypes = $this->_getCacheModel()->getTypes(); + /** + * {@inheritDoc} + */ + public function getListData(InputInterface $input, OutputInterface $output): array + { $table = []; + $cacheTypes = $this->_getCacheModel()->getTypes(); foreach ($cacheTypes as $cacheCode => $cacheInfo) { - $table[] = [$cacheCode, $cacheInfo['status'] ? 'enabled' : 'disabled']; + $table[] = [ + $cacheCode, + $cacheInfo['status'] ? 'enabled' : 'disabled' + ]; } - $tableHelper = $this->getTableHelper(); - $tableHelper - ->setHeaders(['code', 'status']) - ->renderByFormat($output, $table, $input->getOption('format')); - return 0; + return $table; } } diff --git a/src/N98/Magento/Command/Cms/Block/ListCommand.php b/src/N98/Magento/Command/Cms/Block/ListCommand.php index 529840d17..fdcb764f0 100644 --- a/src/N98/Magento/Command/Cms/Block/ListCommand.php +++ b/src/N98/Magento/Command/Cms/Block/ListCommand.php @@ -1,8 +1,11 @@ setName('cms:block:list') - ->setDescription('List all cms blocks') - ->addFormatOption() - ; + return 'CMS blocks'; } /** - * Get an instance of cms/block - * - * @return \Mage_Cms_Model_Block + * {@inheritDoc} */ - protected function _getBlockModel() + public function getListHeader(InputInterface $input, OutputInterface $output): array { - return $this->_getModel('cms/block'); + return ['block_id', 'identifier', 'title', 'is_active', 'store_ids']; } /** - * Execute the command - * - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int + * {@inheritDoc} */ - protected function execute(InputInterface $input, OutputInterface $output): int + public function getListData(InputInterface $input, OutputInterface $output): array { - $this->detectMagento($output, true); - if (!$this->initMagento()) { - return 0; - } - $cmsBlockCollection = $this->_getBlockModel()->getCollection()->addFieldToSelect('*'); - - /** @var \Mage_Cms_Model_Resource_Block $resourceModel */ $resourceModel = $this->_getBlockModel()->getResource(); $table = []; foreach ($cmsBlockCollection as $cmsBlock) { $storeIds = implode(',', $resourceModel->lookupStoreIds($cmsBlock->getId())); - $table[] = [$cmsBlock->getData('block_id'), $cmsBlock->getData('identifier'), $cmsBlock->getData('title'), $cmsBlock->getData('is_active') ? 'active' : 'inactive', $storeIds]; + $table[] = [ + $cmsBlock->getData('block_id'), + $cmsBlock->getData('identifier'), + $cmsBlock->getData('title'), + $cmsBlock->getData('is_active') ? 'active' : 'inactive', $storeIds + ]; } - $tableHelper = $this->getTableHelper(); - $tableHelper - ->setHeaders(['block_id', 'identifier', 'title', 'is_active', 'store_ids']) - ->renderByFormat($output, $table, $input->getOption('format')); - return 0; + return $table; + } + + /** + * Get an instance of cms/block + * + * @return \Mage_Cms_Model_Block + */ + protected function _getBlockModel() + { + return $this->_getModel('cms/block'); } } diff --git a/src/N98/Magento/Command/CommandFormatable.php b/src/N98/Magento/Command/CommandFormatable.php new file mode 100644 index 000000000..1d7fd5d52 --- /dev/null +++ b/src/N98/Magento/Command/CommandFormatable.php @@ -0,0 +1,38 @@ +setName('customer:list') - ->addArgument('search', InputArgument::OPTIONAL, 'Search query') - ->addFormatOption() - ->setDescription('Lists customers') - ; + $this->addArgument( + self::COMMAND_ARGUMENT_SEARCH, + InputArgument::OPTIONAL, + 'Search query' + ); + + parent::configure(); } /** - * {@inheritdoc} + * {@inheritDoc} */ - public function getHelp(): string + public function getSectionTitle(InputInterface $input, OutputInterface $output): string { - return <<detectMagento($output, true); - if (!$this->initMagento()) { - return 0; + return ['id', 'email', 'firstname', 'lastname', 'website']; + } + + /** + * {@inheritDoc} + * @throws Mage_Core_Exception + */ + public function getListData(InputInterface $input, OutputInterface $output): array + { + if (is_array($this->data)) { + return $this->data; } $config = $this->getCommandConfig(); @@ -52,27 +82,42 @@ protected function execute(InputInterface $input, OutputInterface $output): int $collection = $this->getCustomerCollection(); $collection->addAttributeToSelect(['entity_id', 'email', 'firstname', 'lastname', 'website_id']); - if ($input->getArgument('search')) { + $search = $input->getArgument(self::COMMAND_ARGUMENT_SEARCH); + if ($search) { $collection->addAttributeToFilter( - [['attribute' => 'email', 'like' => '%' . $input->getArgument('search') . '%'], ['attribute' => 'firstname', 'like' => '%' . $input->getArgument('search') . '%'], ['attribute' => 'lastname', 'like' => '%' . $input->getArgument('search') . '%']] + [ + ['attribute' => 'email', 'like' => '%' . $search . '%'], + ['attribute' => 'firstname', 'like' => '%' . $search . '%'], + ['attribute' => 'lastname', 'like' => '%' . $search . '%'] + ] ); } $collection->setPageSize($config['limit']); - $table = []; + $this->data = []; + /** @var Mage_Customer_Model_Customer $customer */ foreach ($collection as $customer) { - $table[] = [$customer->getId(), $customer->getEmail(), $customer->getFirstname(), $customer->getLastname(), $this->_getWebsiteCodeById($customer->getwebsiteId())]; + $this->data[] = [ + $customer->getId(), + $customer->getEmail(), + $customer->getFirstname(), + $customer->getLastname(), + $this->_getWebsiteCodeById($customer->getwebsiteId()) + ]; } - if (count($table) > 0) { - $tableHelper = $this->getTableHelper(); - $tableHelper - ->setHeaders(['id', 'email', 'firstname', 'lastname', 'website']) - ->renderByFormat($output, $table, $input->getOption('format')); - } else { - $output->writeln('No customers found'); - } - return 0; + return $this->data; + } + + /** + * {@inheritdoc} + */ + public function getHelp(): string + { + return <<setName('dev:email-template:usage') - ->setDescription('Display database transactional email template usage') - ->addFormatOption(); + return 'Transactional email templates'; } /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int + * {@inheritDoc} */ - protected function execute(InputInterface $input, OutputInterface $output): int + public function getListHeader(InputInterface $input, OutputInterface $output): array { - $this->detectMagento($output, true); - $this->initMagento(); - $templates = $this->findEmailTemplates(); - - if (!empty($templates)) { - $tableHelper = $this->getTableHelper(); - $tableHelper - ->setHeaders(['id', 'Name', 'Scope', 'Scope Id', 'Path']) - ->renderByFormat($output, $templates, $input->getOption('format')); - } else { - $output->writeln('No transactional email templates stored in the database.'); + return ['id', 'Name', 'Scope', 'Scope Id', 'Path']; + } + + /** + * {@inheritDoc} + * @throws Mage_Core_Exception + */ + public function getListData(InputInterface $input, OutputInterface $output): array + { + if (is_array($this->data)) { + return $this->data; } - return 0; + + $this->data = $this->findEmailTemplates(); + return $this->data; } - protected function findEmailTemplates() + /** + * @throws Mage_Core_Exception + */ + protected function findEmailTemplates(): array { /** @var Mage_Core_Model_Template[] $templates */ $templates = Mage::getModel('adminhtml/email_template')->getCollection(); @@ -58,8 +81,7 @@ protected function findEmailTemplates() $return = []; foreach ($templates as $template) { - - /** + /* * Some modules overload the template class so that the method getSystemConfigPathsWhereUsedCurrently * is not available, this is a workaround for that */ @@ -80,7 +102,7 @@ protected function findEmailTemplates() foreach ($configPaths as $configPath) { $return[] = [ - 'id' => $this->sanitizeEmailProperty((string) $template->getId()), + 'id' => $this->sanitizeEmailProperty((string)$template->getId()), 'Template Code' => $this->sanitizeEmailProperty($template->getTemplateCode()), 'Scope' => $this->sanitizeEmailProperty($configPath['scope']), 'Scope Id' => $this->sanitizeEmailProperty($configPath['scope_id']), @@ -97,7 +119,7 @@ protected function findEmailTemplates() * * @return string */ - private function sanitizeEmailProperty($input) + private function sanitizeEmailProperty(string $input): string { return trim($input); } diff --git a/src/N98/Magento/Command/Developer/Module/ListCommand.php b/src/N98/Magento/Command/Developer/Module/ListCommand.php index 5fc4850ac..3d385dba5 100644 --- a/src/N98/Magento/Command/Developer/Module/ListCommand.php +++ b/src/N98/Magento/Command/Developer/Module/ListCommand.php @@ -1,59 +1,105 @@ setName('dev:module:list') - ->addOption('codepool', null, InputOption::VALUE_OPTIONAL, 'Show modules in a specific codepool') - ->addOption('status', null, InputOption::VALUE_OPTIONAL, 'Show modules with a specific status') - ->addOption('vendor', null, InputOption::VALUE_OPTIONAL, 'Show modules of a specified vendor') - ->setAliases(['sys:modules:list'])// deprecated - ->setDescription('List all installed modules') - ->addFormatOption(); + ->addOption( + self::COMMAND_OPTION_COODPOOL, + null, + InputOption::VALUE_OPTIONAL, + 'Show modules in a specific codepool' + ) + ->addOption( + self::COMMAND_OPTION_STATUS, + null, + InputOption::VALUE_OPTIONAL, + 'Show modules with a specific status' + ) + ->addOption( + self::COMMAND_OPTION_VENDOR, + null, + InputOption::VALUE_OPTIONAL, + 'Show modules of a specified vendor' + ) + ->setAliases(['sys:modules:list']); // deprecated + + parent::configure(); } /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int + * {@inheritDoc} */ - protected function execute(InputInterface $input, OutputInterface $output): int + public function getSectionTitle(InputInterface $input, OutputInterface $output): string { - $this->detectMagento($output, true); + return 'Installed Modules'; + } - if ($input->getOption('format') === null) { - $this->writeSection($output, 'Magento Modules'); + /** + * {@inheritDoc} + */ + public function getListHeader(InputInterface $input, OutputInterface $output): array + { + return ['codePool', 'Name', 'Version', 'Status']; + } + + /** + * {@inheritDoc} + */ + public function getListData(InputInterface $input, OutputInterface $output): array + { + if (is_array($this->data)) { + return $this->data; } - $this->initMagento(); $modules = $this->filterModules($input); + $this->data = iterator_to_array($modules); - if (!count($modules)) { - $output->writeln('No modules match the specified criteria.'); - return 0; - } - - $table = $this->getTableHelper(); - $table - ->setHeaders(['codePool', 'Name', 'Version', 'Status']) - ->renderByFormat($output, iterator_to_array($modules), $input->getOption('format')); - return 0; + return $this->data; } /** @@ -61,12 +107,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int * * @return Modules */ - private function filterModules(InputInterface $input) + private function filterModules(InputInterface $input): Modules { $modules = new Modules(); - $modules = $modules->findInstalledModules() + return $modules + ->findInstalledModules() ->filterModules($input); - - return $modules; } } diff --git a/src/N98/Magento/Command/Developer/Module/Rewrite/ListCommand.php b/src/N98/Magento/Command/Developer/Module/Rewrite/ListCommand.php index 83a9ca791..fe16d0d33 100644 --- a/src/N98/Magento/Command/Developer/Module/Rewrite/ListCommand.php +++ b/src/N98/Magento/Command/Developer/Module/Rewrite/ListCommand.php @@ -1,63 +1,81 @@ setName('dev:module:rewrite:list') - ->setDescription('Lists all magento rewrites') - ->addFormatOption() - ; + return ['Type', 'Class', 'Rewrite']; } /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int + * {@inheritDoc} */ - protected function execute(InputInterface $input, OutputInterface $output): int + public function getListData(InputInterface $input, OutputInterface $output): array { - $this->detectMagento($output, true); - if (!$this->initMagento()) { - return 0; + if (is_array($this->data)) { + return $this->data; } $rewrites = array_merge($this->loadRewrites(), $this->loadAutoloaderRewrites()); - $table = []; + $this->data = []; foreach ($rewrites as $type => $data) { if ((is_countable($data) ? count($data) : 0) > 0) { foreach ($data as $class => $rewriteClass) { - $table[] = [$type, $class, implode(', ', $rewriteClass)]; + $this->data[] = [ + $type, + $class, + implode(', ', $rewriteClass) + ]; } } } - if (count($table) === 0 && $input->getOption('format') === null) { - $output->writeln('No rewrites were found.'); - } else { - if (count($table) == 0) { - $table = []; - } - - $tableHelper = $this->getTableHelper(); - $tableHelper - ->setHeaders(['Type', 'Class', 'Rewrite']) - ->setRows($table) - ->renderByFormat($output, $table, $input->getOption('format')); - } - return 0; + return $this->data; } } diff --git a/src/N98/Magento/Command/Developer/Theme/ListCommand.php b/src/N98/Magento/Command/Developer/Theme/ListCommand.php index 03f96a189..c3cb430ca 100644 --- a/src/N98/Magento/Command/Developer/Theme/ListCommand.php +++ b/src/N98/Magento/Command/Developer/Theme/ListCommand.php @@ -1,9 +1,12 @@ setName('dev:theme:list') - ->setDescription('Lists all available themes') - ->addFormatOption() - ; + return 'Themes'; } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int + * {@inheritDoc} */ - protected function execute(InputInterface $input, OutputInterface $output): int + public function getListHeader(InputInterface $input, OutputInterface $output): array { - $this->detectMagento($output); - if (!$this->initMagento()) { - return 0; - } + return ['label']; + } + /** + * {@inheritDoc} + */ + public function getListData(InputInterface $input, OutputInterface $output): array + { $packages = $this->getThemes(); $table = []; foreach ($packages as $package => $themes) { @@ -43,17 +56,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int } } - $tableHelper = $this->getTableHelper(); - $tableHelper - ->setHeaders(['Theme']) - ->renderByFormat($output, $table, $input->getOption('format')); - return 0; + return $table; } /** * @return array */ - protected function getThemes() + protected function getThemes(): array { return Mage::getModel('core/design_package')->getThemeList(); } diff --git a/src/N98/Magento/Command/Indexer/ListCommand.php b/src/N98/Magento/Command/Indexer/ListCommand.php index 58599e2bb..c528ab996 100644 --- a/src/N98/Magento/Command/Indexer/ListCommand.php +++ b/src/N98/Magento/Command/Indexer/ListCommand.php @@ -1,7 +1,10 @@ setName('index:list') - ->setDescription('Lists all magento indexes') - ->addFormatOption() - ; + return 'Indexes'; } /** - * {@inheritdoc} + * {@inheritDoc} */ - public function getHelp(): string + public function getListHeader(InputInterface $input, OutputInterface $output): array { - return <<detectMagento($output, true); - if (!$this->initMagento()) { - return 0; - } - $table = []; foreach ($this->getIndexerList() as $index) { - $table[] = [$index['code'], $index['status'], $index['last_runtime']]; + $table[] = [ + $index['code'], + $index['status'], + $index['last_runtime'] + ]; } - $tableHelper = $this->getTableHelper(); - $tableHelper - ->setHeaders(['code', 'status', 'time']) - ->renderByFormat($output, $table, $input->getOption('format')); - return 0; + return $table; + } + + /** + * {@inheritdoc} + */ + public function getHelp(): string + { + return <<commandConfig = $this->getCommandConfig(); $this->writeSection($output, 'Magento Installation'); diff --git a/src/N98/Magento/Command/Script/Repository/ListCommand.php b/src/N98/Magento/Command/Script/Repository/ListCommand.php index 0ed4eb797..699324ce7 100644 --- a/src/N98/Magento/Command/Script/Repository/ListCommand.php +++ b/src/N98/Magento/Command/Script/Repository/ListCommand.php @@ -1,7 +1,10 @@ setName('script:repo:list') - ->setDescription('Lists all scripts in repository') - ->addFormatOption() - ; + $table = []; + $files = $this->getScripts(); + if (count($files) > 0) { + foreach ($files as $file) { + $table[] = [ + substr($file['fileinfo']->getFilename(), 0, -strlen(self::MAGERUN_EXTENSION)), + $file['location'], + $file['description'] + ]; + } + } + + return $table; } /** @@ -39,33 +79,4 @@ public function getHelp(): string $ n98-magerun.phar script:repo:list HELP; } - - /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int - */ - protected function execute(InputInterface $input, OutputInterface $output): int - { - $files = $this->getScripts(); - if (count($files) > 0) { - $table = []; - foreach ($files as $file) { - $table[] = [substr($file['fileinfo']->getFilename(), 0, -strlen(self::MAGERUN_EXTENSION)), $file['location'], $file['description']]; - } - } else { - $table = []; - } - - if ($input->getOption('format') === null && count($table) === 0) { - $output->writeln('no script file found'); - } - - $tableHelper = $this->getTableHelper(); - $tableHelper - ->setHeaders(['Script', 'Location', 'Description']) - ->renderByFormat($output, $table, $input->getOption('format')); - return 0; - } } diff --git a/src/N98/Magento/Command/SelfUpdateCommand.php b/src/N98/Magento/Command/SelfUpdateCommand.php index 089c9ad95..fba514c55 100644 --- a/src/N98/Magento/Command/SelfUpdateCommand.php +++ b/src/N98/Magento/Command/SelfUpdateCommand.php @@ -59,12 +59,12 @@ public function getHelp(): string } /** - * @param \Symfony\Component\Console\Input\InputInterface $input - * @param \Symfony\Component\Console\Output\OutputInterface $output + * @param InputInterface $input + * @param OutputInterface $output * @return int * @throws \Exception */ - protected function execute(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output): int { $isDryRun = $input->getOption('dry-run'); @@ -165,7 +165,7 @@ protected function _exit($statusCode = 0) } /** - * @param \Symfony\Component\Console\Output\OutputInterface $output + * @param OutputInterface $output * @param string $remoteUrl * @param string $tempFilename */ @@ -261,7 +261,7 @@ private function replaceExistingPharFile($tempFilename, $localFilename) /** * Download changelog * - * @param \Symfony\Component\Console\Output\OutputInterface $output + * @param OutputInterface $output * @param bool $loadUnstable * @return string */ diff --git a/src/N98/Magento/Command/System/Cron/HistoryCommand.php b/src/N98/Magento/Command/System/Cron/HistoryCommand.php index 3415eef1a..f94fee03f 100644 --- a/src/N98/Magento/Command/System/Cron/HistoryCommand.php +++ b/src/N98/Magento/Command/System/Cron/HistoryCommand.php @@ -1,77 +1,107 @@ addOption( + self::COMMAND_OPTION_TIMEZONE, + null, + InputOption::VALUE_OPTIONAL, + 'Timezone to show finished at in' + ); + + parent::configure(); + } - protected function configure() + /** + * {@inheritDoc} + */ + public function getSectionTitle(InputInterface $input, OutputInterface $output): string { - $this - ->setName('sys:cron:history') - ->setDescription('Last executed cronjobs with status.') - ->addOption( - 'timezone', - null, - InputOption::VALUE_OPTIONAL, - 'Timezone to show finished at in' - ) - ->addFormatOption() - ; + return 'Last executed jobs'; } /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int + * {@inheritDoc} */ - protected function execute(InputInterface $input, OutputInterface $output): int + public function getListHeader(InputInterface $input, OutputInterface $output): array { - $this->detectMagento($output, true); + return ['Job', 'Status', 'Finished']; + } - if ($input->getOption('format') === null) { - $this->writeSection($output, 'Last executed jobs'); + /** + * {@inheritDoc} + * @throws Mage_Core_Model_Store_Exception|Mage_Core_Exception + */ + public function getListData(InputInterface $input, OutputInterface $output): array + { + if (is_array($this->data)) { + return $this->data; } - $this->initMagento(); - $timezone = $input->getOption('timezone') ?: Mage::app()->getStore()->getConfig('general/locale/timezone'); + $timezone = $input->getOption(self::COMMAND_OPTION_TIMEZONE) + ?: Mage::app()->getStore()->getConfig('general/locale/timezone'); - $output->writeln('Times shown in ' . $timezone . ''); + $output->writeln(sprintf('Times shown in %s', $timezone)); $date = Mage::getSingleton('core/date'); $offset = $date->calculateOffset($timezone); $collection = Mage::getModel('cron/schedule')->getCollection(); $collection ->addFieldToFilter('status', ['neq' => Mage_Cron_Model_Schedule::STATUS_PENDING]) - ->addOrder('finished_at', Varien_Data_Collection_Db::SORT_ORDER_DESC); + ->addOrder('finished_at'); - $table = []; + $this->data = []; + /** @var Mage_Cron_Model_Schedule $job */ foreach ($collection as $job) { - $table[] = [$job->getJobCode(), $job->getStatus(), $job->getFinishedAt() ? $date->gmtDate(null, $date->timestamp($job->getFinishedAt()) + $offset) : '']; + $this->data[] = [ + $job->getJobCode(), + $job->getStatus(), + $job->getFinishedAt() ? $date->gmtDate( + null, + $date->timestamp($job->getFinishedAt()) + $offset + ) : '' + ]; } - $tableHelper = $this->getTableHelper(); - $tableHelper - ->setHeaders(['Job', 'Status', 'Finished']) - ->renderByFormat($output, $table, $input->getOption('format')); - return 0; + return $this->data; } } diff --git a/src/N98/Magento/Command/System/Cron/ListCommand.php b/src/N98/Magento/Command/System/Cron/ListCommand.php index d92e0a9d8..c13a5f359 100644 --- a/src/N98/Magento/Command/System/Cron/ListCommand.php +++ b/src/N98/Magento/Command/System/Cron/ListCommand.php @@ -1,53 +1,60 @@ setName('sys:cron:list') - ->setDescription('Lists all cronjobs') - ->addFormatOption() - ; + return 'Cronjob List'; } /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int + * {@inheritDoc} */ - protected function execute(InputInterface $input, OutputInterface $output): int + public function getListHeader(InputInterface $input, OutputInterface $output): array { - $this->detectMagento($output, true); + return array_keys(current($this->getListData($input, $output))); + } - if ($input->getOption('format') === null) { - $this->writeSection($output, 'Cronjob List'); + /** + * {@inheritDoc} + */ + public function getListData(InputInterface $input, OutputInterface $output): array + { + if (is_array($this->data)) { + return $this->data; } - $this->initMagento(); - - $table = $this->getJobs(); - - $tableHelper = $this->getTableHelper(); - $tableHelper - ->setHeaders(array_keys(current($table))) - ->renderByFormat($output, $table, $input->getOption('format')); - return 0; + $this->data = $this->getJobs(); + return $this->data; } } diff --git a/src/N98/Magento/Command/System/Store/Config/BaseUrlListCommand.php b/src/N98/Magento/Command/System/Store/Config/BaseUrlListCommand.php index 4c92bbe85..077985c3e 100644 --- a/src/N98/Magento/Command/System/Store/Config/BaseUrlListCommand.php +++ b/src/N98/Magento/Command/System/Store/Config/BaseUrlListCommand.php @@ -1,54 +1,73 @@ setName('sys:store:config:base-url:list') - ->setDescription('Lists all base urls') - ->addFormatOption() - ; + return 'Magento Stores - Base URLs'; } /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int + * {@inheritDoc} */ - protected function execute(InputInterface $input, OutputInterface $output): int + public function getListHeader(InputInterface $input, OutputInterface $output): array { - $table = []; - $this->detectMagento($output, true); + return ['id', 'code', 'unsecure_baseurl', 'secure_baseurl']; + } - if (!$input->getOption('format')) { - $this->writeSection($output, 'Magento Stores - Base URLs'); + /** + * {@inheritDoc} + */ + public function getListData(InputInterface $input, OutputInterface $output): array + { + if (is_array($this->data)) { + return $this->data; } - $this->initMagento(); + $table = []; foreach (Mage::app()->getStores() as $store) { - $table[$store->getId()] = [$store->getId(), $store->getCode(), Mage::getStoreConfig('web/unsecure/base_url', $store), Mage::getStoreConfig('web/secure/base_url', $store)]; + $table[$store->getId()] = [ + $store->getId(), + $store->getCode(), + Mage::getStoreConfig('web/unsecure/base_url', $store), + Mage::getStoreConfig('web/secure/base_url', $store) + ]; } ksort($table); + $this->data = $table; - $tableHelper = $this->getTableHelper(); - $tableHelper - ->setHeaders(['id', 'code', 'unsecure_baseurl', 'secure_baseurl']) - ->renderByFormat($output, $table, $input->getOption('format')); - return 0; + return $this->data; } } diff --git a/src/N98/Magento/Command/System/Store/ListCommand.php b/src/N98/Magento/Command/System/Store/ListCommand.php index 14f5e507b..124f6c943 100644 --- a/src/N98/Magento/Command/System/Store/ListCommand.php +++ b/src/N98/Magento/Command/System/Store/ListCommand.php @@ -1,55 +1,66 @@ setName('sys:store:list') - ->setDescription('Lists all installed store-views') - ->addFormatOption() - ; + return 'Store views'; } /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int + * {@inheritDoc} */ - protected function execute(InputInterface $input, OutputInterface $output): int + public function getListHeader(InputInterface $input, OutputInterface $output): array { - $table = []; - $this->detectMagento($output, true); - $this->initMagento(); + return ['id', 'code']; + } + /** + * {@inheritDoc} + */ + public function getListData(InputInterface $input, OutputInterface $output): array + { + $table = []; foreach (Mage::app()->getStores() as $store) { - $table[$store->getId()] = [$store->getId(), $store->getCode()]; + $storeId = $store->getId(); + $table[$storeId] = [ + $storeId, + $store->getCode() + ]; } ksort($table); - $tableHelper = $this->getTableHelper(); - $tableHelper - ->setHeaders(['id', 'code']) - ->renderByFormat($output, $table, $input->getOption('format')); - return 0; + return $table; } } diff --git a/src/N98/Magento/Command/System/Website/ListCommand.php b/src/N98/Magento/Command/System/Website/ListCommand.php index 882f97c3c..8ed16cdd4 100644 --- a/src/N98/Magento/Command/System/Website/ListCommand.php +++ b/src/N98/Magento/Command/System/Website/ListCommand.php @@ -1,59 +1,66 @@ setName('sys:website:list') - ->setDescription('Lists all websites') - ->addFormatOption() - ; + return 'Magento Websites'; } /** - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int + * {@inheritDoc} */ - protected function execute(InputInterface $input, OutputInterface $output): int + public function getListHeader(InputInterface $input, OutputInterface $output): array { - $table = []; - $this->detectMagento($output, true); - - if ($input->getOption('format') === null) { - $this->writeSection($output, 'Magento Websites'); - } - $this->initMagento(); + return ['id', 'code']; + } - foreach (Mage::app()->getWebsites() as $store) { - $table[$store->getId()] = [$store->getId(), $store->getCode()]; + /** + * {@inheritDoc} + */ + public function getListData(InputInterface $input, OutputInterface $output): array + { + $table = []; + foreach (Mage::app()->getWebsites() as $website) { + $websiteId = $website->getId(); + $table[$websiteId] = [ + $websiteId, + $website->getCode() + ]; } ksort($table); - $tableHelper = $this->getTableHelper(); - $tableHelper - ->setHeaders(['id', 'code']) - ->renderByFormat($output, $table, $input->getOption('format')); - return 0; + return $table; } } diff --git a/src/N98/Magento/Modules.php b/src/N98/Magento/Modules.php index 7f1ca6537..642d47e08 100644 --- a/src/N98/Magento/Modules.php +++ b/src/N98/Magento/Modules.php @@ -1,16 +1,23 @@ trim($codePool), 'Name' => trim($moduleName), 'Version' => trim($version), 'Status' => StringTyped::formatActive($active)]; + $list[] = [ + 'codePool' => trim($codePool), + 'Name' => trim($moduleName), + 'Version' => trim($version), + 'Status' => StringTyped::formatActive($active) + ]; } return new Modules($list); @@ -59,20 +71,23 @@ public function findInstalledModules() * @param InputInterface $input * @return Modules */ - public function filterModules(InputInterface $input) + public function filterModules(InputInterface $input): Modules { $filtered = $this->list; - if ($input->getOption('codepool')) { - $filtered = ArrayFunctions::matrixFilterByValue($filtered, 'codePool', $input->getOption('codepool')); + $codepool = $input->getOption(ListCommand::COMMAND_OPTION_COODPOOL); + if ($codepool) { + $filtered = ArrayFunctions::matrixFilterByValue($filtered, 'codePool', $codepool); } - if ($input->getOption('status')) { - $filtered = ArrayFunctions::matrixFilterByValue($filtered, 'Status', $input->getOption('status')); + $status = $input->getOption(ListCommand::COMMAND_OPTION_STATUS); + if ($status) { + $filtered = ArrayFunctions::matrixFilterByValue($filtered, 'Status', $status); } - if ($input->getOption('vendor')) { - $filtered = ArrayFunctions::matrixFilterStartswith($filtered, 'Name', $input->getOption('vendor')); + $vendor = $input->getOption(ListCommand::COMMAND_OPTION_VENDOR); + if ($vendor) { + $filtered = ArrayFunctions::matrixFilterStartswith($filtered, 'Name', $vendor); } return new self($filtered); diff --git a/tests/N98/Magento/ModulesTest.php b/tests/N98/Magento/ModulesTest.php index 60072186d..7d89fb091 100644 --- a/tests/N98/Magento/ModulesTest.php +++ b/tests/N98/Magento/ModulesTest.php @@ -47,20 +47,52 @@ public function filteringCountAndIterating() /** * @test */ - public function findInstalledModulesAndFilterThem() + public function findInstalledModulesAndCount() { $this->getApplication()->initMagento(); - $modules = new Modules(); self::assertCount(0, $modules); + $total = count($modules->findInstalledModules()); self::assertGreaterThan(10, $total); + } + + /** + * @test + */ + public function findInstalledModulesAndFilterByCodepool() + { + $this->getApplication()->initMagento(); + $modules = new Modules(); + $total = count($modules->findInstalledModules()); + $filtered = $modules->filterModules($this->filter('codepool', 'core')); self::assertLessThan($total, count($filtered)); + } + + /** + * @test + */ + public function findInstalledModulesAndFilterByStatus() + { + $this->getApplication()->initMagento(); + $modules = new Modules(); + $total = count($modules->findInstalledModules()); + $filtered = $modules->filterModules($this->filter('status', 'active')); self::assertLessThan($total, count($filtered)); + } + + /** + * @test + */ + public function findInstalledModulesAndFilterByVendor() + { + $this->getApplication()->initMagento(); + $modules = new Modules(); + $total = count($modules->findInstalledModules()); $filtered = $modules->filterModules($this->filter('vendor', 'Mage_')); self::assertLessThan($total, count($filtered)); @@ -85,29 +117,17 @@ private function filter($option = null, $value = null) $options[$option] = $value; } - /** @var $input PHPUnit_Framework_MockObject_MockObject|ArrayInput */ + /** @var PHPUnit_Framework_MockObject_MockObject|ArrayInput $input */ $input = $this->getMockBuilder(ArrayInput::class) ->disableOriginalConstructor() ->setMethods(['getOption']) ->getMock(); - $i = 0; - foreach ($options as $opt => $val) { - $input - ->expects(self::at($i++)) - ->method('getOption') - ->with($opt) - ->willReturn($val); - - if (!$val) { - continue; - } - - $input->expects(self::at($i++)) - ->method('getOption') - ->with($opt) - ->willReturn($val); - } + $input + ->expects(self::any()) + ->method('getOption') + ->with($option) + ->willReturn($options[$option]); return $input; } diff --git a/tests/N98/Util/AutoloadHandlerTest.php b/tests/N98/Util/AutoloadHandlerTest.php index 2564958cc..d1fcf0fb9 100644 --- a/tests/N98/Util/AutoloadHandlerTest.php +++ b/tests/N98/Util/AutoloadHandlerTest.php @@ -106,7 +106,7 @@ public function changingCallback() $handler = $this->create(null, AutoloadHandler::NO_EXCEPTION); self::assertFalse($handler->__invoke('Test')); - self::assertObjectNotHasAttribute('count', $calls); + self::assertObjectNotHasProperty('count', $calls); $handler->setCallback($assertAble); self::assertTrue($handler->__invoke('Test'));