Skip to content

Commit 39d89a1

Browse files
oshmyheliukshiftedreality
authored andcommitted
MAGECLOUD-1444: Refactor Flag Files (#123)
1 parent 3a8a3e6 commit 39d89a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+661
-1136
lines changed

src/App/Container.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111
use Magento\MagentoCloud\Command\ConfigDump;
1212
use Magento\MagentoCloud\Command\Prestart;
1313
use Magento\MagentoCloud\Command\PostDeploy;
14-
use Magento\MagentoCloud\Config\StageConfigInterface;
1514
use Magento\MagentoCloud\Config\ValidatorInterface;
1615
use Magento\MagentoCloud\Config\Validator as ConfigValidator;
17-
use Magento\MagentoCloud\Config\Stage\Build as BuildConfig;
18-
use Magento\MagentoCloud\Config\Stage\Deploy as DeployConfig;
1916
use Magento\MagentoCloud\DB\Data\ConnectionInterface;
2017
use Magento\MagentoCloud\DB\Data\ReadConnection;
2118
use Magento\MagentoCloud\Filesystem\DirectoryCopier;
2219
use Magento\MagentoCloud\Filesystem\DirectoryList;
20+
use Magento\MagentoCloud\Filesystem\Flag;
2321
use Magento\MagentoCloud\Process\ProcessInterface;
2422
use Magento\MagentoCloud\Process\ProcessComposite;
2523
use Magento\MagentoCloud\Process\Build as BuildProcess;
@@ -75,14 +73,12 @@ function () use ($directoryList) {
7573
);
7674
});
7775
$this->container->singleton(
78-
\Magento\MagentoCloud\Filesystem\FlagFilePool::class,
76+
Flag\Pool::class,
7977
function () {
80-
return new \Magento\MagentoCloud\Filesystem\FlagFilePool([
81-
$this->container->make(\Magento\MagentoCloud\Filesystem\FlagFile\RegenerateFlag::class),
82-
$this->container->make(\Magento\MagentoCloud\Filesystem\FlagFile\StaticContentDeployFlag::class),
83-
$this->container->make(
84-
\Magento\MagentoCloud\Filesystem\FlagFile\StaticContentDeployPendingFlag::class
85-
),
78+
return new Flag\Pool([
79+
Flag\Manager::FLAG_REGENERATE => 'var/.regenerate',
80+
Flag\Manager::FLAG_STATIC_CONTENT_DEPLOY_IN_BUILD => '.static_content_deploy',
81+
Flag\Manager::FLAG_STATIC_CONTENT_DEPLOY_PENDING => 'var/.static_content_deploy_pending',
8682
]);
8783
}
8884
);

src/Config/Environment.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
use Magento\MagentoCloud\Filesystem\DirectoryList;
99
use Magento\MagentoCloud\Filesystem\Driver\File;
10-
use Magento\MagentoCloud\Filesystem\FlagFile\StaticContentDeployFlag;
11-
use Magento\MagentoCloud\Filesystem\FlagFilePool;
10+
use Magento\MagentoCloud\Filesystem\Flag\Manager as FlagManager;
1211
use Psr\Log\LoggerInterface;
1312

1413
/**
@@ -47,9 +46,9 @@ class Environment
4746
private $directoryList;
4847

4948
/**
50-
* @var FlagFilePool
49+
* @var FlagManager
5150
*/
52-
private $flagFilePool;
51+
private $flagManager;
5352

5453
/**
5554
* @var array
@@ -60,18 +59,18 @@ class Environment
6059
* @param LoggerInterface $logger
6160
* @param File $file
6261
* @param DirectoryList $directoryList
63-
* @param FlagFilePool $flagFilePool
62+
* @param FlagManager $flagManager
6463
*/
6564
public function __construct(
6665
LoggerInterface $logger,
6766
File $file,
6867
DirectoryList $directoryList,
69-
FlagFilePool $flagFilePool
68+
FlagManager $flagManager
7069
) {
7170
$this->logger = $logger;
7271
$this->file = $file;
7372
$this->directoryList = $directoryList;
74-
$this->flagFilePool = $flagFilePool;
73+
$this->flagManager = $flagManager;
7574
}
7675

7776
/**
@@ -179,7 +178,7 @@ public function getWritableDirectories(): array
179178
*/
180179
public function isDeployStaticContent(): bool
181180
{
182-
return !$this->flagFilePool->getFlag(StaticContentDeployFlag::KEY)->exists();
181+
return !$this->flagManager->exists(FlagManager::FLAG_STATIC_CONTENT_DEPLOY_IN_BUILD);
183182
}
184183

185184
/**

src/Filesystem/Flag/Manager.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MagentoCloud\Filesystem\Flag;
7+
8+
use Magento\MagentoCloud\Filesystem\DirectoryList;
9+
use Magento\MagentoCloud\Filesystem\Driver\File;
10+
use Magento\MagentoCloud\Filesystem\FileSystemException;
11+
use Psr\Log\LoggerInterface;
12+
13+
/**
14+
* Performs operations with file flags.
15+
*/
16+
class Manager
17+
{
18+
/**
19+
* This flag is creating by magento for cleaning up generated/code, generated/metadata and var/cache directories
20+
* for subsequent regeneration of this content.
21+
*/
22+
const FLAG_REGENERATE = 'regenerate';
23+
24+
/**
25+
* Used to mark that static content deployment was performed on build phase.
26+
*/
27+
const FLAG_STATIC_CONTENT_DEPLOY_IN_BUILD = 'scd_in_build';
28+
29+
/**
30+
* Used for postponing static content deployment until prestart phase.
31+
*/
32+
const FLAG_STATIC_CONTENT_DEPLOY_PENDING = 'scd_pending';
33+
34+
/**
35+
* @var LoggerInterface
36+
*/
37+
private $logger;
38+
39+
/**
40+
* @var File
41+
*/
42+
private $file;
43+
44+
/**
45+
* @var DirectoryList
46+
*/
47+
private $directoryList;
48+
49+
/**
50+
* @var Pool
51+
*/
52+
private $flagPool;
53+
54+
/**
55+
* @param LoggerInterface $logger
56+
* @param File $file
57+
* @param Pool $flagPool
58+
* @param DirectoryList $directoryList
59+
*/
60+
public function __construct(
61+
LoggerInterface $logger,
62+
File $file,
63+
Pool $flagPool,
64+
DirectoryList $directoryList
65+
) {
66+
$this->logger = $logger;
67+
$this->file = $file;
68+
$this->flagPool = $flagPool;
69+
$this->directoryList = $directoryList;
70+
}
71+
72+
/**
73+
* Determines whether or not a flag exists.
74+
*
75+
* @param string $flagKey
76+
* @return bool
77+
* @throws \RuntimeException If flag with given key is not registered
78+
*/
79+
public function exists(string $flagKey): bool
80+
{
81+
$path = $this->directoryList->getMagentoRoot() . '/' . $this->getFlagPath($flagKey);
82+
83+
try {
84+
return $this->file->isExists($path);
85+
} catch (FileSystemException $e) {
86+
$this->logger->notice($e->getMessage());
87+
}
88+
89+
return false;
90+
}
91+
92+
/**
93+
* Sets a flag on the file system.
94+
*
95+
* @param string $flagKey
96+
* @return bool Returns false if file for required flag was not created, otherwise returns true
97+
* @throws \RuntimeException If flag with given key is not registered
98+
*/
99+
public function set(string $flagKey): bool
100+
{
101+
$flagPath = $this->getFlagPath($flagKey);
102+
$path = $this->directoryList->getMagentoRoot() . '/' . $flagPath;
103+
104+
try {
105+
if ($this->file->touch($path)) {
106+
$this->logger->info('Set flag: ' . $flagPath);
107+
return true;
108+
}
109+
} catch (FileSystemException $e) {
110+
$this->logger->notice($e->getMessage());
111+
}
112+
113+
return false;
114+
}
115+
116+
/**
117+
* Deletes a flag from the filesystem.
118+
*
119+
* @param string $flagKey
120+
* @return bool Returns true if file does not exist or was removed by this method
121+
* @throws \RuntimeException If flag with given key is not registered
122+
*/
123+
public function delete(string $flagKey): bool
124+
{
125+
$flagPath = $this->getFlagPath($flagKey);
126+
127+
if (!$this->exists($flagKey)) {
128+
$this->logger->info(sprintf('Flag %s has already been deleted.', $flagPath));
129+
return true;
130+
}
131+
132+
$path = $this->directoryList->getMagentoRoot() . '/' . $flagPath;
133+
134+
try {
135+
if ($this->file->deleteFile($path)) {
136+
$this->logger->info('Deleting flag: ' . $flagPath);
137+
return true;
138+
}
139+
} catch (FileSystemException $e) {
140+
$this->logger->notice($e->getMessage());
141+
}
142+
143+
return false;
144+
}
145+
146+
/**
147+
* Returns relative flag path by given flag key.
148+
*
149+
* @param string $flagKey
150+
* @return string
151+
* @throws \RuntimeException If flag with given key is not registered
152+
*/
153+
public function getFlagPath(string $flagKey): string
154+
{
155+
$flagPath = $this->flagPool->get($flagKey);
156+
157+
if (!$flagPath) {
158+
throw new \RuntimeException(sprintf('Flag with key %s is not registered in flagPool', $flagKey));
159+
}
160+
161+
return $flagPath;
162+
}
163+
}

src/Filesystem/Flag/Pool.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\MagentoCloud\Filesystem\Flag;
7+
8+
class Pool
9+
{
10+
/**
11+
* @var array
12+
*/
13+
private $flags;
14+
15+
/**
16+
* @param array $flags
17+
*/
18+
public function __construct(array $flags)
19+
{
20+
$this->flags = $flags;
21+
}
22+
23+
/**
24+
* Gets flag path by key, returns null if flag not exists.
25+
*
26+
* @param string $key
27+
* @return string|null
28+
*/
29+
public function get(string $key)
30+
{
31+
return $this->flags[$key] ?? null;
32+
}
33+
}

src/Filesystem/FlagFile/Base.php

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)