Skip to content

Commit

Permalink
Merge pull request #7494 from shadowhand/fix/cli-apcu-warning
Browse files Browse the repository at this point in the history
Do not allow cache clearing with APCu
  • Loading branch information
Ocramius committed Dec 5, 2018
2 parents 3cef55e + 1feac20 commit 3e0b70b
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 6 deletions.
6 changes: 6 additions & 0 deletions UPGRADE.md
@@ -1,5 +1,11 @@
# Upgrade to 3.0

## BC Break: Removed ability to clear cache via console with some cache drivers

The console commands `orm:clear-cache:metadata`, `orm:clear-cache:result`,
and `orm:clear-cache:query` cannot be used with the `ApcCache`, `ApcuCache`,
or `XcacheCache` because the memory is only available to the webserver process.

## BC Break: `orm:run-dql` command's `$depth` parameter removed

The `$depth` parameter has been removed, the dumping functionality
Expand Down
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ORM\Tools\Console\Command\ClearCache;

use Doctrine\Common\Cache\ApcCache;
use Doctrine\Common\Cache\ApcuCache;
use Doctrine\Common\Cache\XcacheCache;
use InvalidArgumentException;
use LogicException;
Expand Down Expand Up @@ -62,11 +63,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

if ($cacheDriver instanceof ApcCache) {
throw new LogicException('Cannot clear APC Cache from Console, its shared in the Webserver memory and not accessible from the CLI.');
throw new LogicException('Cannot clear APC Cache from Console, it is shared in the Webserver memory and not accessible from the CLI.');
}

if ($cacheDriver instanceof ApcuCache) {
throw new LogicException('Cannot clear APCu Cache from Console, it is shared in the Webserver memory and not accessible from the CLI.');
}

if ($cacheDriver instanceof XcacheCache) {
throw new LogicException('Cannot clear XCache Cache from Console, its shared in the Webserver memory and not accessible from the CLI.');
throw new LogicException('Cannot clear XCache Cache from Console, it is shared in the Webserver memory and not accessible from the CLI.');
}

$ui->comment('Clearing <info>all</info> Metadata cache entries');
Expand Down
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ORM\Tools\Console\Command\ClearCache;

use Doctrine\Common\Cache\ApcCache;
use Doctrine\Common\Cache\ApcuCache;
use Doctrine\Common\Cache\XcacheCache;
use InvalidArgumentException;
use LogicException;
Expand Down Expand Up @@ -62,10 +63,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

if ($cacheDriver instanceof ApcCache) {
throw new LogicException('Cannot clear APC Cache from Console, its shared in the Webserver memory and not accessible from the CLI.');
throw new LogicException('Cannot clear APC Cache from Console, it is shared in the Webserver memory and not accessible from the CLI.');
}

if ($cacheDriver instanceof ApcuCache) {
throw new LogicException('Cannot clear APCu Cache from Console, it is shared in the Webserver memory and not accessible from the CLI.');
}

if ($cacheDriver instanceof XcacheCache) {
throw new LogicException('Cannot clear XCache Cache from Console, its shared in the Webserver memory and not accessible from the CLI.');
throw new LogicException('Cannot clear XCache Cache from Console, it is shared in the Webserver memory and not accessible from the CLI.');
}

$ui->comment('Clearing <info>all</info> Query cache entries');
Expand Down
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ORM\Tools\Console\Command\ClearCache;

use Doctrine\Common\Cache\ApcCache;
use Doctrine\Common\Cache\ApcuCache;
use Doctrine\Common\Cache\XcacheCache;
use InvalidArgumentException;
use LogicException;
Expand Down Expand Up @@ -62,11 +63,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

if ($cacheDriver instanceof ApcCache) {
throw new LogicException('Cannot clear APC Cache from Console, its shared in the Webserver memory and not accessible from the CLI.');
throw new LogicException('Cannot clear APC Cache from Console, it is shared in the Webserver memory and not accessible from the CLI.');
}

if ($cacheDriver instanceof ApcuCache) {
throw new LogicException('Cannot clear APCu Cache from Console, it is shared in the Webserver memory and not accessible from the CLI.');
}

if ($cacheDriver instanceof XcacheCache) {
throw new LogicException('Cannot clear XCache Cache from Console, its shared in the Webserver memory and not accessible from the CLI.');
throw new LogicException('Cannot clear XCache Cache from Console, it is shared in the Webserver memory and not accessible from the CLI.');
}

$ui->comment('Clearing <info>all</info> Result cache entries');
Expand Down
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Tools\Console\Command;

use Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\Tests\OrmFunctionalTestCase;
use LogicException;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Tester\CommandTester;
use function sprintf;

class ClearCacheMetadataCommandTest extends OrmFunctionalTestCase
{
/** @var Application */
private $application;

/** @var MetadataCommand */
private $command;

protected function setUp() : void
{
parent::setUp();

$this->command = new MetadataCommand();

$this->application = new Application();
$this->application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($this->em)]));
$this->application->add($this->command);
}

public function dataInvalidCacheDrivers() : array
{
return [
'apc' => ['Doctrine\Common\Cache\ApcCache', 'APC Cache'],
'apcu' => ['Doctrine\Common\Cache\ApcuCache', 'APCu Cache'],
'xcache' => ['Doctrine\Common\Cache\XcacheCache', 'XCache Cache'],
];
}

/** @dataProvider dataInvalidCacheDrivers */
public function testCannotClearCacheWithInvalidDriver($driver, $name) : void
{
$this->em->getConfiguration()->setMetadataCacheImpl(new $driver());

$command = $this->application->find('orm:clear-cache:metadata');

$tester = new CommandTester($command);

$this->expectException(LogicException::class);
$this->expectExceptionMessage(sprintf('Cannot clear %s from Console', $name));

$tester->execute(
[
'command' => $command->getName(),
],
['decorated' => false]
);
}
}
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Tools\Console\Command;

use Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\Tests\OrmFunctionalTestCase;
use LogicException;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Tester\CommandTester;
use function sprintf;

class ClearCacheQueryCommandTest extends OrmFunctionalTestCase
{
/** @var Application */
private $application;

/** @var QueryCommand */
private $command;

protected function setUp() : void
{
parent::setUp();

$this->command = new QueryCommand();

$this->application = new Application();
$this->application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($this->em)]));
$this->application->add($this->command);
}

public function dataInvalidCacheDrivers() : array
{
return [
'apc' => ['Doctrine\Common\Cache\ApcCache', 'APC Cache'],
'apcu' => ['Doctrine\Common\Cache\ApcuCache', 'APCu Cache'],
'xcache' => ['Doctrine\Common\Cache\XcacheCache', 'XCache Cache'],
];
}

/** @dataProvider dataInvalidCacheDrivers */
public function testCannotClearCacheWithInvalidDriver($driver, $name) : void
{
$this->em->getConfiguration()->setQueryCacheImpl(new $driver());

$command = $this->application->find('orm:clear-cache:query');

$tester = new CommandTester($command);

$this->expectException(LogicException::class);
$this->expectExceptionMessage(sprintf('Cannot clear %s from Console', $name));

$tester->execute(
[
'command' => $command->getName(),
],
['decorated' => false]
);
}
}
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Tools\Console\Command;

use Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\Tests\OrmFunctionalTestCase;
use LogicException;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Tester\CommandTester;
use function sprintf;

class ClearCacheResultCommandTest extends OrmFunctionalTestCase
{
/** @var Application */
private $application;

/** @var ResultCommand */
private $command;

protected function setUp() : void
{
parent::setUp();

$this->command = new ResultCommand();

$this->application = new Application();
$this->application->setHelperSet(new HelperSet(['em' => new EntityManagerHelper($this->em)]));
$this->application->add($this->command);
}

public function dataInvalidCacheDrivers() : array
{
return [
'apc' => ['Doctrine\Common\Cache\ApcCache', 'APC Cache'],
'apcu' => ['Doctrine\Common\Cache\ApcuCache', 'APCu Cache'],
'xcache' => ['Doctrine\Common\Cache\XcacheCache', 'XCache Cache'],
];
}

/** @dataProvider dataInvalidCacheDrivers */
public function testCannotClearCacheWithInvalidDriver($driver, $name) : void
{
$this->em->getConfiguration()->setResultCacheImpl(new $driver());

$command = $this->application->find('orm:clear-cache:result');

$tester = new CommandTester($command);

$this->expectException(LogicException::class);
$this->expectExceptionMessage(sprintf('Cannot clear %s from Console', $name));

$tester->execute(
[
'command' => $command->getName(),
],
['decorated' => false]
);
}
}

0 comments on commit 3e0b70b

Please sign in to comment.