Skip to content

Commit a4ec8b6

Browse files
authored
MCLOUD-7015: The ece-tool should be able to handle the situation when the opcache.enable_cli is enabled. (#13)
1 parent a0dd530 commit a4ec8b6

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

scenario/deploy.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0"?>
22
<scenario xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:ece-tools:config/scenario.xsd">
3+
<step name="php-opcache-reset" type="Magento\MagentoCloud\Step\Deploy\PhpOpcacheReset" priority="50"/>
34
<step name="remove-deploy-failed-flag" type="Magento\MagentoCloud\Step\Deploy\RemoveDeployFailedFlag" priority="100"/>
45
<step name="pre-deploy" type="Magento\MagentoCloud\Step\Deploy\PreDeploy" priority="200">
56
<arguments>

src/Service/Php.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,24 @@ public function getVersion(): string
3131
{
3232
return $this->getConfiguration()['version'];
3333
}
34+
35+
/**
36+
* Checks if opcache is enabled for PHP CLI
37+
*
38+
* @return bool
39+
*/
40+
public function isOpcacheCliEnabled(): bool
41+
{
42+
return (bool)ini_get('opcache.enable_cli');
43+
}
44+
45+
/**
46+
* Resets the contents of the opcache
47+
*
48+
* @return bool
49+
*/
50+
public function resetOpcache(): bool
51+
{
52+
return opcache_reset();
53+
}
3454
}

src/Step/Deploy/PhpOpcacheReset.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Step\Deploy;
9+
10+
use Magento\MagentoCloud\Service\Php;
11+
use Magento\MagentoCloud\Step\StepInterface;
12+
use Psr\Log\LoggerInterface;
13+
14+
/**
15+
* Flushes the contents of the opcache if the PHP CLI opcache is enabled
16+
*/
17+
class PhpOpcacheReset implements StepInterface
18+
{
19+
/**
20+
* @var LoggerInterface
21+
*/
22+
private $logger;
23+
24+
/**
25+
* @var Php
26+
*/
27+
private $php;
28+
29+
/**
30+
* @param LoggerInterface $logger
31+
* @param Php $php
32+
*/
33+
public function __construct(
34+
LoggerInterface $logger,
35+
Php $php
36+
) {
37+
$this->logger = $logger;
38+
$this->php = $php;
39+
}
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function execute()
45+
{
46+
if ($this->php->isOpcacheCliEnabled()) {
47+
$this->logger->notice('Reset the contents of the opcache');
48+
$this->php->resetOpcache();
49+
}
50+
}
51+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Test\Unit\Step\Deploy;
9+
10+
use Magento\MagentoCloud\Service\Php;
11+
use Magento\MagentoCloud\Step\Deploy\PhpOpcacheReset;
12+
use PHPUnit\Framework\MockObject\MockObject;
13+
use PHPUnit\Framework\TestCase;
14+
use Psr\Log\LoggerInterface;
15+
16+
/**
17+
* @inheritdoc
18+
*/
19+
class PhpOpcacheResetTest extends TestCase
20+
{
21+
/**
22+
* @var PhpOpcacheReset
23+
*/
24+
private $step;
25+
26+
/**
27+
* @var LoggerInterface|MockObject
28+
*/
29+
private $loggerMock;
30+
31+
/**
32+
* @var Php|MockObject
33+
*/
34+
private $phpMock;
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
protected function setUp(): void
40+
{
41+
$this->loggerMock = $this->getMockForAbstractClass(LoggerInterface::class);
42+
$this->phpMock = $this->createMock(Php::class);
43+
44+
$this->step = new PhpOpcacheReset(
45+
$this->loggerMock,
46+
$this->phpMock
47+
);
48+
}
49+
50+
/**
51+
* Opcache Cli Enabled
52+
*/
53+
public function testExecuteOpcacheCliEnabled(): void
54+
{
55+
$this->phpMock->expects($this->once())
56+
->method('isOpcacheCliEnabled')
57+
->willReturn(true);
58+
$this->loggerMock->expects($this->once())
59+
->method('notice')
60+
->with('Reset the contents of the opcache');
61+
$this->phpMock->expects($this->once())
62+
->method('resetOpcache');
63+
$this->step->execute();
64+
}
65+
66+
/**
67+
* Opcache Cli No Enabled
68+
*/
69+
public function testExecuteOpcacheCliNoEnabled(): void
70+
{
71+
$this->phpMock->expects($this->once())
72+
->method('isOpcacheCliEnabled')
73+
->willReturn(false);
74+
$this->loggerMock->expects($this->never())
75+
->method('notice');
76+
$this->phpMock->expects($this->never())
77+
->method('resetOpcache');
78+
79+
$this->step->execute();
80+
}
81+
}

0 commit comments

Comments
 (0)