From e4ee4f74f2132ea66686e0520481a66373d649df Mon Sep 17 00:00:00 2001
From: Ivan Gavryshko <igavryshko@ebay.com>
Date: Thu, 7 May 2015 15:07:09 -0500
Subject: [PATCH] MAGETWO-36070: Move
 dev/tools/performance-toolkit/generate.php

- added command to CLI
- initial commit, need refactoring and cleanup
---
 .../fixtures/cart_price_rules.php             |   7 +-
 .../fixtures/catalog_price_rules.php          |   2 +-
 .../fixtures/categories.php                   |   4 +-
 .../fixtures/configs_apply.php                |   2 +-
 .../fixtures/configurable_products.php        |   2 +-
 .../fixtures/customers.php                    |   2 +-
 .../fixtures/simple_products.php              |   2 +-
 .../performance-toolkit/fixtures/stores.php   |   6 +-
 .../Magento/ToolkitFramework/Application.php  |  66 +++++++---
 .../Command/GenerateFixturesCommand.php       | 118 ++++++++++++++++++
 .../src/Magento/Setup/Console/CommandList.php |   1 +
 11 files changed, 177 insertions(+), 35 deletions(-)
 create mode 100644 setup/src/Magento/Setup/Console/Command/GenerateFixturesCommand.php

diff --git a/dev/tools/performance-toolkit/fixtures/cart_price_rules.php b/dev/tools/performance-toolkit/fixtures/cart_price_rules.php
index 8bb9287c3fa..03f39bac448 100644
--- a/dev/tools/performance-toolkit/fixtures/cart_price_rules.php
+++ b/dev/tools/performance-toolkit/fixtures/cart_price_rules.php
@@ -20,11 +20,8 @@ class CartPriceRulesFixture extends \Magento\ToolkitFramework\Fixture
     public function execute()
     {
         $this->application->resetObjectManager();
-        $cartPriceRulesCount = \Magento\ToolkitFramework\Config::getInstance()->getValue('cart_price_rules', 9);
-        $cartPriceRulesProductsFloor = \Magento\ToolkitFramework\Config::getInstance()->getValue(
-            'cart_price_rules_floor',
-            3
-        );
+        $cartPriceRulesCount = $this->application->getValue('cart_price_rules', 9);
+        $cartPriceRulesProductsFloor = $this->application->getValue('cart_price_rules_floor', 3);
 
         /** @var \Magento\Store\Model\StoreManager $storeManager */
         $storeManager = $this->application->getObjectManager()->create('Magento\Store\Model\StoreManager');
diff --git a/dev/tools/performance-toolkit/fixtures/catalog_price_rules.php b/dev/tools/performance-toolkit/fixtures/catalog_price_rules.php
index 8956c18ecaa..d6f9bc849be 100644
--- a/dev/tools/performance-toolkit/fixtures/catalog_price_rules.php
+++ b/dev/tools/performance-toolkit/fixtures/catalog_price_rules.php
@@ -19,7 +19,7 @@ class CatalogPriceRulesFixture extends \Magento\ToolkitFramework\Fixture
      */
     public function execute()
     {
-        $catalogPriceRulesCount = \Magento\ToolkitFramework\Config::getInstance()->getValue('catalog_price_rules', 3);
+        $catalogPriceRulesCount = $this->application->getValue('catalog_price_rules', 3);
         $this->application->resetObjectManager();
 
         /** @var \Magento\Store\Model\StoreManager $storeManager */
diff --git a/dev/tools/performance-toolkit/fixtures/categories.php b/dev/tools/performance-toolkit/fixtures/categories.php
index b6bc39d83f3..7e2222385d2 100644
--- a/dev/tools/performance-toolkit/fixtures/categories.php
+++ b/dev/tools/performance-toolkit/fixtures/categories.php
@@ -19,8 +19,8 @@ class CategoriesFixture extends \Magento\ToolkitFramework\Fixture
      */
     public function execute()
     {
-        $categoriesNumber = \Magento\ToolkitFramework\Config::getInstance()->getValue('categories', 18);
-        $maxNestingLevel = \Magento\ToolkitFramework\Config::getInstance()->getValue('categories_nesting_level', 3);
+        $categoriesNumber = $this->application->getValue('categories', 18);
+        $maxNestingLevel = $this->application->getValue('categories_nesting_level', 3);
         $this->application->resetObjectManager();
 
         /** @var \Magento\Store\Model\StoreManager $storeManager */
diff --git a/dev/tools/performance-toolkit/fixtures/configs_apply.php b/dev/tools/performance-toolkit/fixtures/configs_apply.php
index 99c62d1233a..a517c91a563 100644
--- a/dev/tools/performance-toolkit/fixtures/configs_apply.php
+++ b/dev/tools/performance-toolkit/fixtures/configs_apply.php
@@ -19,7 +19,7 @@ class ConfigsApplyFixture extends \Magento\ToolkitFramework\Fixture
      */
     public function execute()
     {
-        $configs = \Magento\ToolkitFramework\Config::getInstance()->getValue('configs', array());
+        $configs = $this->application->getValue('configs', array());
         $this->application->resetObjectManager();
 
         foreach ($configs['config'] as $config) {
diff --git a/dev/tools/performance-toolkit/fixtures/configurable_products.php b/dev/tools/performance-toolkit/fixtures/configurable_products.php
index a3aa8f8ba8d..7005c04b122 100644
--- a/dev/tools/performance-toolkit/fixtures/configurable_products.php
+++ b/dev/tools/performance-toolkit/fixtures/configurable_products.php
@@ -820,7 +820,7 @@ class ConfigurableProductsFixture extends \Magento\ToolkitFramework\Fixture
      */
     public function execute()
     {
-        $configurablesCount = \Magento\ToolkitFramework\Config::getInstance()->getValue('configurable_products', 90);
+        $configurablesCount = $this->application->getValue('configurable_products', 90);
         $this->application->resetObjectManager();
 
         /** @var \Magento\Store\Model\StoreManager $storeManager */
diff --git a/dev/tools/performance-toolkit/fixtures/customers.php b/dev/tools/performance-toolkit/fixtures/customers.php
index df42bc283a2..2ad72b2a70c 100644
--- a/dev/tools/performance-toolkit/fixtures/customers.php
+++ b/dev/tools/performance-toolkit/fixtures/customers.php
@@ -19,7 +19,7 @@ class CustomersFixture extends \Magento\ToolkitFramework\Fixture
      */
     public function execute()
     {
-        $customersNumber = \Magento\ToolkitFramework\Config::getInstance()->getValue('customers', 10);
+        $customersNumber = $this->application->getValue('customers', 10);
         $this->application->resetObjectManager();
 
         /** @var \Magento\Store\Model\StoreManager $storeManager */
diff --git a/dev/tools/performance-toolkit/fixtures/simple_products.php b/dev/tools/performance-toolkit/fixtures/simple_products.php
index 26d676455aa..a061f3d3007 100644
--- a/dev/tools/performance-toolkit/fixtures/simple_products.php
+++ b/dev/tools/performance-toolkit/fixtures/simple_products.php
@@ -19,7 +19,7 @@ class SimpleProductsFixture extends \Magento\ToolkitFramework\Fixture
      */
     public function execute()
     {
-        $simpleProductsCount = \Magento\ToolkitFramework\Config::getInstance()->getValue('simple_products', 180);
+        $simpleProductsCount = $this->application->getValue('simple_products', 180);
         $this->application->resetObjectManager();
 
         /** @var \Magento\Store\Model\StoreManager $storeManager */
diff --git a/dev/tools/performance-toolkit/fixtures/stores.php b/dev/tools/performance-toolkit/fixtures/stores.php
index ac56367cc27..d568706aba5 100644
--- a/dev/tools/performance-toolkit/fixtures/stores.php
+++ b/dev/tools/performance-toolkit/fixtures/stores.php
@@ -19,9 +19,9 @@ class StoresFixture extends \Magento\ToolkitFramework\Fixture
      */
     public function execute()
     {
-        $websitesCount = \Magento\ToolkitFramework\Config::getInstance()->getValue('websites', 2);
-        $storeGroupsCount = \Magento\ToolkitFramework\Config::getInstance()->getValue('store_groups', 3);
-        $storesCount = \Magento\ToolkitFramework\Config::getInstance()->getValue('store_views', 5);
+        $websitesCount = $this->application->getValue('websites', 2);
+        $storeGroupsCount = $this->application->getValue('store_groups', 3);
+        $storesCount = $this->application->getValue('store_views', 5);
         $this->application->resetObjectManager();
 
         /** @var \Magento\Store\Model\StoreManager $storeManager */
diff --git a/dev/tools/performance-toolkit/framework/Magento/ToolkitFramework/Application.php b/dev/tools/performance-toolkit/framework/Magento/ToolkitFramework/Application.php
index 75c1a83b236..892850a7137 100644
--- a/dev/tools/performance-toolkit/framework/Magento/ToolkitFramework/Application.php
+++ b/dev/tools/performance-toolkit/framework/Magento/ToolkitFramework/Application.php
@@ -10,6 +10,11 @@
 namespace Magento\ToolkitFramework;
 
 use Magento\Framework\App\Filesystem\DirectoryList;
+use Magento\Indexer\Console\Command\IndexerReindexCommand;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\ArrayInput;
+use Symfony\Component\Console\Output\OutputInterface;
+
 
 class Application
 {
@@ -35,11 +40,6 @@ class Application
      */
     protected $_application;
 
-    /**
-     * @var \Magento\Framework\Shell
-     */
-    protected $_shell;
-
     /**
      * @var \Magento\Framework\ObjectManagerInterface
      */
@@ -60,24 +60,25 @@ class Application
     protected $_paramLabels = [];
 
     /**
-     * @var string
+     * @var array
      */
-    protected $_applicationBaseDir;
+    protected $_initArguments;
 
     /**
+     * Configuration array
+     *
      * @var array
      */
-    protected $_initArguments;
+    protected $config = [];
 
     /**
-     * @param string $applicationBaseDir
-     * @param \Magento\Framework\Shell $shell
+     * Constructor
+     *
      */
-    public function __construct($applicationBaseDir, \Magento\Framework\Shell $shell, array $initArguments)
+    public function __construct(IndexerReindexCommand $reindexCommand, $initArguments = [])
     {
-        $this->_applicationBaseDir = $applicationBaseDir;
-        $this->_shell = $shell;
         $this->_initArguments = $initArguments;
+        $this->reindexCommand = $reindexCommand;
     }
 
     /**
@@ -119,15 +120,11 @@ class Application
 
     /**
      * Run reindex
-     *
-     * @return Application
      */
-    public function reindex()
+    public function reindex(OutputInterface $output)
     {
-        $this->_shell->execute(
-            'php -f ' . $this->_applicationBaseDir . '/bin/magento indexer:reindex --all'
-        );
-        return $this;
+        $input = new ArrayInput([]);
+        $this->reindexCommand->run($input, $output);
     }
 
     /**
@@ -205,4 +202,33 @@ class Application
         $this->bootstrap();
         return $this;
     }
+
+    /**
+     * Load config from file
+     *
+     * @param string $filename
+     * @throws \Exception
+     *
+     * @return void
+     */
+    public function loadConfig($filename)
+    {
+        if (!is_readable($filename)) {
+            throw new \Exception("Profile configuration file `{$filename}` is not readable or does not exists.");
+        }
+        $this->config = (new \Magento\Framework\Xml\Parser())->load($filename)->xmlToArray();
+    }
+
+    /**
+     * Get profile configuration value
+     *
+     * @param string $key
+     * @param null|mixed $default
+     *
+     * @return mixed
+     */
+    public function getValue($key, $default = null)
+    {
+        return isset($this->config['config']['profile'][$key]) ? $this->config['config']['profile'][$key] : $default;
+    }
 }
diff --git a/setup/src/Magento/Setup/Console/Command/GenerateFixturesCommand.php b/setup/src/Magento/Setup/Console/Command/GenerateFixturesCommand.php
new file mode 100644
index 00000000000..d6277e9fef3
--- /dev/null
+++ b/setup/src/Magento/Setup/Console/Command/GenerateFixturesCommand.php
@@ -0,0 +1,118 @@
+<?php
+/**
+ * Copyright © 2015 Magento. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+namespace Magento\Setup\Console\Command;
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Magento\ToolkitFramework\Config;
+use Magento\ToolkitFramework\Application;
+
+
+class GenerateFixturesCommand extends Command
+{
+    /**
+     * Profile argument
+     */
+    const PROFILE_ARGUMENT = 'profile';
+
+    /**
+     * @var Application
+     */
+    private $application;
+
+    /**
+     * @var Config
+     */
+    private $config;
+
+    /**
+     * @param Application $application
+     * @param Config $config
+     */
+    public function __construct(Application $application, Config $config)
+    {
+        $this->config = $config;
+        $this->application = $application;
+        parent::__construct();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function configure()
+    {
+        $this->setName('setup:perf:generate-fixtures')
+            ->setDescription('Generates fixtures')
+            ->setDefinition([
+                new InputArgument(
+                    self::PROFILE_ARGUMENT,
+                    InputArgument::REQUIRED,
+                    'Path to profile configuration file'
+                ),
+            ]);
+        parent::configure();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        try {
+            $totalStartTime = microtime(true);
+
+
+            $application = $this->application;
+            $application->bootstrap();
+            $application->loadFixtures();
+            $application->loadConfig($input->getArgument(self::PROFILE_ARGUMENT));
+
+            $output->writeln('<info>Generating profile with following params:</info>');
+
+            foreach ($application->getParamLabels() as $configKey => $label) {
+                $output->writeln('<info> |- ' . $label . ': ' . $application->getValue($configKey) . '</info>');
+            }
+
+            /** @var $config \Magento\Indexer\Model\Config */
+            $config = $application->getObjectManager()->get('Magento\Indexer\Model\Config');
+            $indexerListIds = $config->getIndexers();
+            /** @var $indexerRegistry \Magento\Indexer\Model\IndexerRegistry */
+            $indexerRegistry = $application->getObjectManager()->create('Magento\Indexer\Model\IndexerRegistry');
+            $indexersState = [];
+            foreach ($indexerListIds as $key => $indexerId) {
+                $indexer = $indexerRegistry->get($indexerId['indexer_id']);
+                $indexersState[$indexerId['indexer_id']] = $indexer->isScheduled();
+                $indexer->setScheduled(true);
+            }
+
+            foreach ($application->getFixtures() as $fixture) {
+                echo $fixture->getActionTitle() . '... ';
+                $startTime = microtime(true);
+                $fixture->execute();
+                $endTime = microtime(true);
+                $resultTime = $endTime - $startTime;
+                echo ' done in ' . gmdate('H:i:s', $resultTime) . PHP_EOL;
+            }
+
+            foreach ($indexerListIds as $indexerId) {
+                /** @var $indexer \Magento\Indexer\Model\Indexer */
+                $indexer = $indexerRegistry->get($indexerId['indexer_id']);
+                $indexer->setScheduled($indexersState[$indexerId['indexer_id']]);
+            }
+
+            $application->reindex($output);
+            $totalEndTime = microtime(true);
+            $totalResultTime = $totalEndTime - $totalStartTime;
+
+            $output->writeln('<info>Total execution time: ' . gmdate('H:i:s', $totalResultTime) . '</info>');
+        } catch (\Exception $e) {
+             $output->writeln('<error>' . $e->getMessage() . '</error>');
+        }
+    }
+}
diff --git a/setup/src/Magento/Setup/Console/CommandList.php b/setup/src/Magento/Setup/Console/CommandList.php
index 6ebcc5c9850..da9f1a45ff9 100644
--- a/setup/src/Magento/Setup/Console/CommandList.php
+++ b/setup/src/Magento/Setup/Console/CommandList.php
@@ -49,6 +49,7 @@ class CommandList
             'Magento\Setup\Console\Command\DeployStaticContentCommand',
             'Magento\Setup\Console\Command\DiCompileCommand',
             'Magento\Setup\Console\Command\DiCompileMultiTenantCommand',
+            'Magento\Setup\Console\Command\GenerateFixturesCommand',
             'Magento\Setup\Console\Command\I18nCollectPhrasesCommand',
             'Magento\Setup\Console\Command\I18nPackCommand',
             'Magento\Setup\Console\Command\InfoCurrencyListCommand',
-- 
GitLab