Skip to content

Commit

Permalink
MAGECLOUD-3258: Cache Warm-up, Using RegEx to Warm-up Multiple Pages (#2
Browse files Browse the repository at this point in the history
)
  • Loading branch information
oshmyheliuk authored and BaDos committed Jun 12, 2019
1 parent 0fe8439 commit 30d47bf
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
composer.lock
177 changes: 177 additions & 0 deletions Console/Command/ConfigShowEntityUrlsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CloudComponents\Console\Command;

use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Console\Cli;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\UrlFactory;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;

/**
* Returns list of category or cms-page urls for given stores
*/
class ConfigShowEntityUrlsCommand extends Command
{
/**
* Names of input arguments or options.
*/
const INPUT_OPTION_STORE_ID = 'store-id';
const INPUT_OPTION_ENTITY_TYPE = 'entity-type';

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var UrlFinderInterface
*/
private $urlFinder;

/**
* @var UrlFactory
*/
private $urlFactory;

/**
* @var State
*/
private $state;

/**
* @var array
*/
private $possibleEntities = [Rewrite::ENTITY_TYPE_CMS_PAGE, Rewrite::ENTITY_TYPE_CATEGORY];

/**
* @param StoreManagerInterface $storeManager
* @param UrlFinderInterface $urlFinder
* @param UrlFactory $urlFactory
* @param State $state
*/
public function __construct(
StoreManagerInterface $storeManager,
UrlFinderInterface $urlFinder,
UrlFactory $urlFactory,
State $state
) {
$this->storeManager = $storeManager;
$this->urlFinder = $urlFinder;
$this->urlFactory = $urlFactory;
$this->state = $state;

parent::__construct();
}

/**
* @inheritdoc
*/
protected function configure()
{
$this->setName('config:show:urls')
->setDescription(
'Returns urls for entity type and given store id or for all stores if store id isn\'t provided.'
);

$this->addOption(
self::INPUT_OPTION_STORE_ID,
null,
InputOption::VALUE_OPTIONAL,
'Store ID'
);
$this->addOption(
self::INPUT_OPTION_ENTITY_TYPE,
null,
InputOption::VALUE_REQUIRED,
'Entity type: ' . implode(',', $this->possibleEntities)
);

parent::configure();
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$this->setArea();
$entityType = $input->getOption(self::INPUT_OPTION_ENTITY_TYPE);
if (!in_array($entityType, $this->possibleEntities)) {
$output->write(sprintf(
'Wrong entity type "%s", possible values: %s',
$entityType,
implode(',', $this->possibleEntities)
));
return Cli::RETURN_FAILURE;
}

$storeId = $input->getOption(self::INPUT_OPTION_STORE_ID);

if ($storeId === null) {
$stores = $this->storeManager->getStores();
} else {
$stores = [$this->storeManager->getStore($storeId)];
}

$urls = $this->getPageUrls($stores, $entityType);

$output->write(json_encode(array_unique($urls)));
return Cli::RETURN_SUCCESS;
} catch (\Exception $e) {
$output->writeln($e->getMessage());
return Cli::RETURN_FAILURE;
}
}

/**
* @param StoreInterface[] $stores
* @param string $entityType
* @return array
*/
private function getPageUrls(array $stores, string $entityType): array
{
$urls = [];

foreach ($stores as $store) {
$url = $this->urlFactory->create()->setScope($store->getId());

$entities = $this->urlFinder->findAllByData([
UrlRewrite::STORE_ID => $store->getId(),
UrlRewrite::ENTITY_TYPE => $entityType
]);

foreach ($entities as $urlRewrite) {
$urls[] = $url->getUrl($urlRewrite->getRequestPath());
}
}

return $urls;
}

/**
* Sets area code.
*/
private function setArea()
{
try {
$this->state->setAreaCode(Area::AREA_GLOBAL);
} catch (LocalizedException $e) {
}
}
}
90 changes: 90 additions & 0 deletions Console/Command/ConfigShowStoreUrlCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CloudComponents\Console\Command;

use Magento\Framework\Console\Cli;
use Magento\Framework\UrlInterface;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Command for getting store ulr by store id.
*/
class ConfigShowStoreUrlCommand extends Command
{
/**
* Name of input argument.
*/
const INPUT_ARGUMENT_STORE_ID = 'store-id';

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param StoreManagerInterface $storeManager
*/
public function __construct(
StoreManagerInterface $storeManager
) {
parent::__construct();
$this->storeManager = $storeManager;
}

/**
* @inheritdoc
*/
protected function configure()
{
$this->setName('config:show:store-url')
->setDescription(
'Shows store base url for given id. Shows base url for all stores if id wasn\'t passed'
);

$this->addArgument(
self::INPUT_ARGUMENT_STORE_ID,
InputArgument::OPTIONAL,
'Store ID'
);

parent::configure();
}

/**
* Returns store url or all store urls if store id wasn't provided
*
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
/** @var Store $store */
$storeId = $input->getArgument(self::INPUT_ARGUMENT_STORE_ID);
if ($storeId !== null) {
$store = $this->storeManager->getStore($storeId);

$output->writeln($store->getBaseUrl(UrlInterface::URL_TYPE_LINK, $store->isUrlSecure()));
} else {
$urls = [];
foreach ($this->storeManager->getStores(true) as $store) {
$urls[$store->getId()] = $store->getBaseUrl(UrlInterface::URL_TYPE_LINK, $store->isUrlSecure());
}

$output->write(json_encode($urls, JSON_FORCE_OBJECT));
}

return Cli::RETURN_SUCCESS;
} catch (\Exception $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
return Cli::RETURN_FAILURE;
}
}
}
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "magento/magento-cloud-components",
"description": "Cloud Components Module for Magento 2.x",
"type": "magento2-module",
"version": "1.0.0",
"require": {
"php": "^7.0",
"ext-json": "*",
"magento/framework": "*",
"magento/module-store": "*",
"magento/module-url-rewrite": "*"
},
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
"Magento\\CloudComponents\\": ""
}
}
}
17 changes: 17 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandListInterface">
<arguments>
<argument name="commands" xsi:type="array">
<item name="configShowStoreUrlCommand" xsi:type="object">Magento\CloudComponents\Console\Command\ConfigShowStoreUrlCommand</item>
<item name="configShowEntityUrlsCommand" xsi:type="object">Magento\CloudComponents\Console\Command\ConfigShowEntityUrlsCommand</item>
</argument>
</arguments>
</type>
</config>
10 changes: 10 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magento_CloudComponents" setup_version="1.0.0" />
</config>
9 changes: 9 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use \Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_CloudComponents', __DIR__);

0 comments on commit 30d47bf

Please sign in to comment.