Skip to content

Commit 30d47bf

Browse files
oshmyheliukBaDos
authored andcommitted
MAGECLOUD-3258: Cache Warm-up, Using RegEx to Warm-up Multiple Pages (#2)
1 parent 0fe8439 commit 30d47bf

7 files changed

+324
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
composer.lock
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CloudComponents\Console\Command;
7+
8+
use Magento\Framework\App\Area;
9+
use Magento\Framework\App\State;
10+
use Magento\Framework\Console\Cli;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\UrlFactory;
13+
use Magento\Store\Api\Data\StoreInterface;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
use Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite;
16+
use Magento\UrlRewrite\Model\UrlFinderInterface;
17+
use Symfony\Component\Console\Command\Command;
18+
use Symfony\Component\Console\Input\InputInterface;
19+
use Symfony\Component\Console\Input\InputOption;
20+
use Symfony\Component\Console\Output\OutputInterface;
21+
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
22+
23+
/**
24+
* Returns list of category or cms-page urls for given stores
25+
*/
26+
class ConfigShowEntityUrlsCommand extends Command
27+
{
28+
/**
29+
* Names of input arguments or options.
30+
*/
31+
const INPUT_OPTION_STORE_ID = 'store-id';
32+
const INPUT_OPTION_ENTITY_TYPE = 'entity-type';
33+
34+
/**
35+
* @var StoreManagerInterface
36+
*/
37+
private $storeManager;
38+
39+
/**
40+
* @var UrlFinderInterface
41+
*/
42+
private $urlFinder;
43+
44+
/**
45+
* @var UrlFactory
46+
*/
47+
private $urlFactory;
48+
49+
/**
50+
* @var State
51+
*/
52+
private $state;
53+
54+
/**
55+
* @var array
56+
*/
57+
private $possibleEntities = [Rewrite::ENTITY_TYPE_CMS_PAGE, Rewrite::ENTITY_TYPE_CATEGORY];
58+
59+
/**
60+
* @param StoreManagerInterface $storeManager
61+
* @param UrlFinderInterface $urlFinder
62+
* @param UrlFactory $urlFactory
63+
* @param State $state
64+
*/
65+
public function __construct(
66+
StoreManagerInterface $storeManager,
67+
UrlFinderInterface $urlFinder,
68+
UrlFactory $urlFactory,
69+
State $state
70+
) {
71+
$this->storeManager = $storeManager;
72+
$this->urlFinder = $urlFinder;
73+
$this->urlFactory = $urlFactory;
74+
$this->state = $state;
75+
76+
parent::__construct();
77+
}
78+
79+
/**
80+
* @inheritdoc
81+
*/
82+
protected function configure()
83+
{
84+
$this->setName('config:show:urls')
85+
->setDescription(
86+
'Returns urls for entity type and given store id or for all stores if store id isn\'t provided.'
87+
);
88+
89+
$this->addOption(
90+
self::INPUT_OPTION_STORE_ID,
91+
null,
92+
InputOption::VALUE_OPTIONAL,
93+
'Store ID'
94+
);
95+
$this->addOption(
96+
self::INPUT_OPTION_ENTITY_TYPE,
97+
null,
98+
InputOption::VALUE_REQUIRED,
99+
'Entity type: ' . implode(',', $this->possibleEntities)
100+
);
101+
102+
parent::configure();
103+
}
104+
105+
/**
106+
* @param InputInterface $input
107+
* @param OutputInterface $output
108+
* @return int|null
109+
*/
110+
protected function execute(InputInterface $input, OutputInterface $output)
111+
{
112+
try {
113+
$this->setArea();
114+
$entityType = $input->getOption(self::INPUT_OPTION_ENTITY_TYPE);
115+
if (!in_array($entityType, $this->possibleEntities)) {
116+
$output->write(sprintf(
117+
'Wrong entity type "%s", possible values: %s',
118+
$entityType,
119+
implode(',', $this->possibleEntities)
120+
));
121+
return Cli::RETURN_FAILURE;
122+
}
123+
124+
$storeId = $input->getOption(self::INPUT_OPTION_STORE_ID);
125+
126+
if ($storeId === null) {
127+
$stores = $this->storeManager->getStores();
128+
} else {
129+
$stores = [$this->storeManager->getStore($storeId)];
130+
}
131+
132+
$urls = $this->getPageUrls($stores, $entityType);
133+
134+
$output->write(json_encode(array_unique($urls)));
135+
return Cli::RETURN_SUCCESS;
136+
} catch (\Exception $e) {
137+
$output->writeln($e->getMessage());
138+
return Cli::RETURN_FAILURE;
139+
}
140+
}
141+
142+
/**
143+
* @param StoreInterface[] $stores
144+
* @param string $entityType
145+
* @return array
146+
*/
147+
private function getPageUrls(array $stores, string $entityType): array
148+
{
149+
$urls = [];
150+
151+
foreach ($stores as $store) {
152+
$url = $this->urlFactory->create()->setScope($store->getId());
153+
154+
$entities = $this->urlFinder->findAllByData([
155+
UrlRewrite::STORE_ID => $store->getId(),
156+
UrlRewrite::ENTITY_TYPE => $entityType
157+
]);
158+
159+
foreach ($entities as $urlRewrite) {
160+
$urls[] = $url->getUrl($urlRewrite->getRequestPath());
161+
}
162+
}
163+
164+
return $urls;
165+
}
166+
167+
/**
168+
* Sets area code.
169+
*/
170+
private function setArea()
171+
{
172+
try {
173+
$this->state->setAreaCode(Area::AREA_GLOBAL);
174+
} catch (LocalizedException $e) {
175+
}
176+
}
177+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CloudComponents\Console\Command;
7+
8+
use Magento\Framework\Console\Cli;
9+
use Magento\Framework\UrlInterface;
10+
use Magento\Store\Model\Store;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
use Symfony\Component\Console\Command\Command;
13+
use Symfony\Component\Console\Input\InputArgument;
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
17+
/**
18+
* Command for getting store ulr by store id.
19+
*/
20+
class ConfigShowStoreUrlCommand extends Command
21+
{
22+
/**
23+
* Name of input argument.
24+
*/
25+
const INPUT_ARGUMENT_STORE_ID = 'store-id';
26+
27+
/**
28+
* @var StoreManagerInterface
29+
*/
30+
private $storeManager;
31+
32+
/**
33+
* @param StoreManagerInterface $storeManager
34+
*/
35+
public function __construct(
36+
StoreManagerInterface $storeManager
37+
) {
38+
parent::__construct();
39+
$this->storeManager = $storeManager;
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function configure()
46+
{
47+
$this->setName('config:show:store-url')
48+
->setDescription(
49+
'Shows store base url for given id. Shows base url for all stores if id wasn\'t passed'
50+
);
51+
52+
$this->addArgument(
53+
self::INPUT_ARGUMENT_STORE_ID,
54+
InputArgument::OPTIONAL,
55+
'Store ID'
56+
);
57+
58+
parent::configure();
59+
}
60+
61+
/**
62+
* Returns store url or all store urls if store id wasn't provided
63+
*
64+
* {@inheritdoc}
65+
*/
66+
protected function execute(InputInterface $input, OutputInterface $output)
67+
{
68+
try {
69+
/** @var Store $store */
70+
$storeId = $input->getArgument(self::INPUT_ARGUMENT_STORE_ID);
71+
if ($storeId !== null) {
72+
$store = $this->storeManager->getStore($storeId);
73+
74+
$output->writeln($store->getBaseUrl(UrlInterface::URL_TYPE_LINK, $store->isUrlSecure()));
75+
} else {
76+
$urls = [];
77+
foreach ($this->storeManager->getStores(true) as $store) {
78+
$urls[$store->getId()] = $store->getBaseUrl(UrlInterface::URL_TYPE_LINK, $store->isUrlSecure());
79+
}
80+
81+
$output->write(json_encode($urls, JSON_FORCE_OBJECT));
82+
}
83+
84+
return Cli::RETURN_SUCCESS;
85+
} catch (\Exception $e) {
86+
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
87+
return Cli::RETURN_FAILURE;
88+
}
89+
}
90+
}

composer.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "magento/magento-cloud-components",
3+
"description": "Cloud Components Module for Magento 2.x",
4+
"type": "magento2-module",
5+
"version": "1.0.0",
6+
"require": {
7+
"php": "^7.0",
8+
"ext-json": "*",
9+
"magento/framework": "*",
10+
"magento/module-store": "*",
11+
"magento/module-url-rewrite": "*"
12+
},
13+
"autoload": {
14+
"files": [ "registration.php" ],
15+
"psr-4": {
16+
"Magento\\CloudComponents\\": ""
17+
}
18+
}
19+
}

etc/di.xml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\Framework\Console\CommandListInterface">
10+
<arguments>
11+
<argument name="commands" xsi:type="array">
12+
<item name="configShowStoreUrlCommand" xsi:type="object">Magento\CloudComponents\Console\Command\ConfigShowStoreUrlCommand</item>
13+
<item name="configShowEntityUrlsCommand" xsi:type="object">Magento\CloudComponents\Console\Command\ConfigShowEntityUrlsCommand</item>
14+
</argument>
15+
</arguments>
16+
</type>
17+
</config>

etc/module.xml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_CloudComponents" setup_version="1.0.0" />
10+
</config>

registration.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use \Magento\Framework\Component\ComponentRegistrar;
8+
9+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_CloudComponents', __DIR__);

0 commit comments

Comments
 (0)