Skip to content

Commit

Permalink
Merge branch 'feature/cache_clear'
Browse files Browse the repository at this point in the history
* feature/cache_clear:
  ReadMe updatet with oxrun-light.
  small stuff
  Now describes what oxrun-light is and has cache-clear added.
  Make cache:clear runable without to load oxid bootstrap.php
  • Loading branch information
TumTum committed Sep 7, 2021
2 parents 48fe43e + a59392d commit 06f50f8
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 59 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [v5.1.0] 2021-09-07
### Changed
- `oxrun-light` is now officially documented.
- Command cache:clear works without bootstrap.php so can now used in `oxrun-light`.

## [v5.0.1] 2021-09-06
### Fixed
Expand Down
7 changes: 5 additions & 2 deletions README.md
Expand Up @@ -19,11 +19,14 @@ Oxrun provides a cli toolset for the OXID eShop Community Edition.
- PHP >=7.1 is required.
- OXID eShop >= CE v6.5 is required.

After installation manually clear the cache via `rm -rf source/tmp/*` to make all oxrun commands available.
After installation manually clear the cache via `./vendor/bin/oxrun-light cache:clear` to make all oxrun commands available.

# Usage

In your Installation Direction `./vendor/bin/oe-console`
`./vendor/bin/oe-console` is standard call.

* `./vendor/bin/oxrun-light` is a light version of tools that are not need an active OXID eSale database connection.
* `./vendor/bin/oxrun` is a alias from `oe-console`

---

Expand Down
29 changes: 28 additions & 1 deletion src/Oxrun/Application/oxrun-light.php
Expand Up @@ -9,7 +9,34 @@

declare(strict_types=1);

$application = new \Symfony\Component\Console\Application('oxrun-light', '0.2');
namespace Oxrun\Application;

/**
* Class OxrunLight
* @package Oxrun\Application
*/
class OxrunLight extends \Symfony\Component\Console\Application
{
/**
* @inheritDoc
*/
public function getHelp()
{
$version = parent::getHelp();
return <<<TAG
$version
-----------------------------------------------------------------------
<comment> Is a light console line tool for OXID eSale.
These commands don't need an active OXID eSale database connection.</comment>
-----------------------------------------------------------------------
TAG;
}

}

$application = new OxrunLight('oxrun-light', '0.3');
$application->add(new \Oxrun\Command\Cache\ClearCommand());
$application->add(new \Oxrun\Command\Misc\RegisterCommand());
$application->add(new \Oxrun\Command\Misc\PhpstormMetadataCommand());
$application->add(new \Oxrun\Command\Misc\GenerateDocumentationCommand());
Expand Down
89 changes: 77 additions & 12 deletions src/Oxrun/Command/Cache/ClearCommand.php
Expand Up @@ -2,12 +2,13 @@

namespace Oxrun\Command\Cache;

use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Facts\Facts;
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 Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Webmozart\PathUtil\Path;

/**
* Class ClearCommand
Expand All @@ -16,7 +17,39 @@
class ClearCommand extends Command
{

// use NoNeedDatabase;
/**
* @var Facts
*/
private $facts;

/**
* @var ?\OxidEsales\Eshop\Core\Cache\Generic\Cache
*/
private $genericCache = null;

/**
* @var ?\OxidEsales\Eshop\Core\Cache\DynamicContent\ContentCache
*/
private $dynamicContentCache = null;

/**
* ClearCommand constructor.
* @param Facts|null $facts
* @param \OxidEsales\Eshop\Core\Cache\Generic\Cache|null $genericCache
* @param \OxidEsales\Eshop\Core\Cache\DynamicContent\ContentCache|null $dynamicContentCache
*/
public function __construct(
Facts $facts = null,
$genericCache = null,
$dynamicContentCache = null
) {
$this->facts = $facts ?? new Facts();
$this->genericCache = $genericCache;
$this->dynamicContentCache = $dynamicContentCache;

parent::__construct();
}


/**
* Configures the current command.
Expand Down Expand Up @@ -62,11 +95,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function getCompileDir()
{
$oxidPath = OX_BASE_PATH;
$configfile = $oxidPath . DIRECTORY_SEPARATOR . 'config.inc.php';
$sourcePath = (new \OxidEsales\Facts\Facts())->getSourcePath();
$configfile = Path::join($sourcePath, 'config.inc.php');

if ($oxidPath && file_exists($configfile)) {
$oxConfigFile = new \OxConfigFile($configfile);
if ($sourcePath && file_exists($configfile)) {
$oxConfigFile = new \OxidEsales\Eshop\Core\ConfigFile($configfile);
return $oxConfigFile->getVar('sCompileDir');
}

Expand Down Expand Up @@ -123,7 +156,11 @@ protected function checkSameOwner($compileDir)
if ($current_owner != $owner) {
global $argv;
$owner = posix_getpwuid($owner);
throw new \Exception("Please run command as `${owner['name']}` user." . PHP_EOL . " sudo -u ${owner['name']} " . join(' ', $argv));
throw new \Exception(
"Please run command as `${owner['name']}` user." . PHP_EOL .
" sudo -u ${owner['name']} " .
join(' ', $argv)
);
}
}

Expand All @@ -132,23 +169,51 @@ protected function checkSameOwner($compileDir)
*/
protected function enterpriseCache(OutputInterface $output)
{
if (class_exists('\OxidEsales\Facts\Facts') == false) {
if ($this->facts->isEnterprise() == false) {
return;
}

if ((Registry::get(\OxidEsales\Facts\Facts::class))->isEnterprise() == false) {
if ($this->getApplication() instanceof \Oxrun\Application\OxrunLight) {
$output->writeln(
'<comment>[Info] The enterprise cache could not be cleared. ' .
'Goes only via the command `oe-console cache:clear`.</comment>',
OutputInterface::VERBOSITY_NORMAL
);
return;
}

try {
Registry::get('OxidEsales\Eshop\Core\Cache\Generic\Cache')->flush();
$this->getGenericCache()->flush();
$output->writeln('<info>Generic\Cache is cleared</info>');

Registry::get('OxidEsales\Eshop\Core\Cache\DynamicContent\ContentCache')->reset(true);
$this->getDynamicContentCache()->reset(true);
$output->writeln('<info>DynamicContent\Cache is cleared</info>');

} catch (\Exception $e) {
$output->writeln('<error>Only enterprise cache could\'t be cleared: '.$e->getMessage().'</error>');
$output->writeln('<error>Only enterprise cache could\'t be cleared: ' . $e->getMessage() . '</error>');
}
}

/**
* @return \OxidEsales\Eshop\Core\Cache\Generic\Cache
*/
private function getGenericCache()
{
if ($this->genericCache === null) {
$this->genericCache = new \OxidEsales\Eshop\Core\Cache\Generic\Cache();
}
return $this->genericCache;
}

/**
* @return \OxidEsales\Eshop\Core\Cache\DynamicContent\ContentCache
*/
private function getDynamicContentCache()
{
if ($this->dynamicContentCache === null) {
$this->dynamicContentCache = new \OxidEsales\Eshop\Core\Cache\DynamicContent\ContentCache();
}

return $this->dynamicContentCache;
}
}
67 changes: 24 additions & 43 deletions tests/Oxrun/Command/Cache/ClearCommandTest.php
Expand Up @@ -8,10 +8,12 @@
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\Console\Tester\CommandTester;

/**
* Class ClearCommandTest
* @package Oxrun\Command\Cache
*/
class ClearCommandTest extends TestCase
{
use ProphecyTrait;

public function testExecute()
{
$app = new Application();
Expand Down Expand Up @@ -62,14 +64,12 @@ public function testDontClearCompileFolderIfIsNotSameOwner()

public function testItClearCacheOnEnterpriseEdtion()
{
[$facts, $genericCache, $dynamicContentCache] = $this->mockEEClasses();

$app = new Application();
$app->add(new ClearCommand());
$app->add(new ClearCommand($facts, $genericCache, $dynamicContentCache));
$command = $app->find('cache:clear');

if ((new \OxidEsales\Facts\Facts)->isEnterprise() == false) {
$this->mockEEGenericCacheAndDynamicContentClass();
}


$commandTester = new CommandTester($command);
$commandTester->execute(
Expand All @@ -86,13 +86,15 @@ public function testItClearCacheOnEnterpriseEdtion()

public function testCatchExcetionByEE()
{
[$facts, $genericCache, $dynamicContentCache] = $this->mockEEClasses();

$app = new Application();
$app->add(new ClearCommand());
$app->add(new ClearCommand($facts, $genericCache, $dynamicContentCache));
$command = $app->find('cache:clear');

list($facts, $genericCache) = $this->mockEEGenericCacheClass();
$genericCache->flush()->willThrow(new \Exception('PHPUnit Tests'));

$genericCache
->method('flush')
->will($this->returnCallback(function () { throw new \Exception('PHPUnit Tests'); }));

$commandTester = new CommandTester($command);
$commandTester->execute(
Expand All @@ -106,40 +108,19 @@ public function testCatchExcetionByEE()
$this->assertStringContainsString('Only enterprise cache could', $display);
}

protected function tearDown(): void
{
Registry::set(\OxidEsales\Facts\Facts::class, null);
Registry::set('\OxidEsales\Eshop\Core\Cache\Generic\Cache', null);
Registry::set('\OxidEsales\Eshop\Core\Cache\DynamicContent\ContentCache', null);
}


/**
* @return array
*/
protected function mockEEGenericCacheClass()
{
$facts = $this->prophesize(\OxidEsales\Facts\Facts::class);
$facts->isEnterprise()->willReturn(true);
$genericCache = $this->prophesize(GenericCache::class);

Registry::set(\OxidEsales\Facts\Facts::class, $facts->reveal());
Registry::set('OxidEsales\Eshop\Core\Cache\Generic\Cache', $genericCache->reveal());

return [$facts, $genericCache];
}

/**
* @return array
*/
protected function mockEEGenericCacheAndDynamicContentClass()
protected function mockEEClasses(): array
{
list($facts, $genericCache) = $this->mockEEGenericCacheClass();

$dynamicContentCache = $this->prophesize(DynamicContentCache::class);
Registry::set('OxidEsales\Eshop\Core\Cache\DynamicContent\ContentCache', $dynamicContentCache->reveal());

return [$facts, $genericCache, $dynamicContentCache];
$facts = $this->createMock(\OxidEsales\Facts\Facts::class);
$facts
->method('isEnterprise')
->willReturn($this->returnValue(true));

return [
$facts,
$this->createMock(GenericCache::class),
$this->createMock(DynamicContentCache::class),
];
}
}

Expand Down
1 change: 0 additions & 1 deletion tests/Oxrun/Command/Misc/RegisterCommandTest.php
Expand Up @@ -17,7 +17,6 @@
/**
* Class RegisterCommandTest
* @package Oxrun\Command\Misc
* @group active
*/
class RegisterCommandTest extends TestCase
{
Expand Down

0 comments on commit 06f50f8

Please sign in to comment.