Skip to content
This repository has been archived by the owner on May 3, 2019. It is now read-only.

Commit

Permalink
TASK: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
johannessteu committed Apr 10, 2018
1 parent 4b8a352 commit 62cca88
Show file tree
Hide file tree
Showing 25 changed files with 1,617 additions and 0 deletions.
123 changes: 123 additions & 0 deletions Classes/Command/AppCommandController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
namespace Yeebase\Readiness\Command;

/**
* This file is part of the Yeebase.Readiness package.
*
* (c) 2018 yeebase media GmbH
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Error\Messages\Error;
use Neos\Error\Messages\Result;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Flow\Log\SystemLoggerInterface;
use Yeebase\Readiness\Service\ReadyTaskRunner;
use Yeebase\Readiness\Service\TestRunner;
use Yeebase\Readiness\Task\TaskInterface;
use Yeebase\Readiness\Test\TestInterface;

/**
* @Flow\Scope("singleton")
*/
class AppCommandController extends CommandController
{
/**
* @Flow\Inject
* @var SystemLoggerInterface
*/
protected $systemLogger;

/**
* @return Result
*/
protected function runReadyTasks(): Result
{
$taskRunner = new ReadyTaskRunner();
$taskRunner->onBeforeTask(function (TaskInterface $task) {
$this->output('Executing %s... ', [$task->getName()]);
});
$taskRunner->onTaskResult(function (TaskInterface $task, Result $result) {
if ($result->hasErrors()) {
$this->outputFormatted('<error>%s</error>', [$task->getErrorLabel()]);
} else if ($result->hasNotices()) {
$this->outputFormatted('<comment>%s</comment>', [$task->getNoticeLabel()]);
} else {
$this->outputFormatted('<success>%s</success>', [$task->getSuccessLabel()]);
}
});

try {
return $taskRunner->run();
} catch (\Exception $exception) {
$this->output->output('<error>%s</error>', [$exception->getMessage()]);
$this->systemLogger->logException($exception);
$result = new Result();
$result->addError(new Error('Chain failed'));
return $result;
}
}

/**
* @return Result
*/
protected function runTests(): Result
{
$testRunner = new TestRunner();

$testRunner->onBeforeTest(function (TestInterface $test) {
$this->output->output('Testing %s... ', [$test->getName()]);
});
$testRunner->onTestResult(function (TestInterface $test, Result $result) {
if ($result->hasErrors()) {
$this->output->outputLine('<error>%s</error>', [$test->getErrorLabel()]);
} else if ($result->hasNotices()) {
$this->output->outputLine('<commment>%s</commment>', [$test->getNoticeLabel()]);
} else {
$this->output->outputLine('<info>%s</info>', [$test->getSuccessLabel()]);
}
});

return $testRunner->run();
}

/**
* Checks the readiness of the application
*/
public function isReadyCommand()
{
$this->outputLine();
$this->outputLine('<info>Running tests...</info>');
$this->outputLine();
$testResult = $this->runTests();
$this->outputLine();
if (!$testResult->hasErrors()) {
$this->outputLine();
$this->outputLine('<info>All checks passed, executing ready tasks...</info>');
$this->outputLine();
$readyResult = $this->runReadyTasks();
$this->outputLine();

if (!$readyResult->hasErrors()) {
$this->outputLine('<success>Application is ready</success>');
$this->outputLine();
$this->outputLine();
$this->quit(0);
} else {
$this->outputLine('<error>Application did not pass all ready tasks and is not ready</error>');
$this->outputLine();
$this->outputLine();
$this->quit(1);
}
} else {
$this->outputLine();
$this->outputLine('<error>Application did not pass all checks and is not ready</error>');
$this->outputLine();
$this->quit(1);
}
}
}
67 changes: 67 additions & 0 deletions Classes/Eel/Helper/ChainHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
namespace Yeebase\Readiness\Eel\Helper;

/**
* This file is part of the Yeebase.XY package.
*
* (c) 2018 yeebase media GmbH
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Error\Messages\Error;
use Neos\Flow\Annotations as Flow;
use Neos\Eel\ProtectedContextAwareInterface;
use Yeebase\Readiness\Service\EelRuntime;

class ChainHelper implements ProtectedContextAwareInterface
{

/**
* @Flow\Inject
* @var EelRuntime
*/
protected $runtime;

/**
* @return bool
*/
public function isValid(): bool
{
$result = $this->runtime->getChainResult();
return !$result->hasErrors();
}

/**
* @return bool
*/
public function isInvalid(): bool
{
$result = $this->runtime->getChainResult();
return $result->hasErrors();
}

/**
* @return string
*/
public function getCombinedErrorMessages(): string
{
$result = $this->runtime->getChainResult();
$messages = array_map(function (Error $error) {
return $error->getMessage();
}, $result->getErrors());

return implode(PHP_EOL, $messages);
}

/**
* @param string $methodName
* @return boolean
*/
public function allowsCallOfMethod($methodName)
{
return $this->runtime->isTaskContext();
}
}
127 changes: 127 additions & 0 deletions Classes/Eel/Helper/LockHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
namespace Yeebase\Readiness\Eel\Helper;

/**
* This file is part of the Yeebase.XY package.
*
* (c) 2018 yeebase media GmbH
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Cache\Frontend\FrontendInterface;
use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cache\CacheManager;
use Yeebase\Readiness\Service\EelRuntime;

class LockHelper implements ProtectedContextAwareInterface
{
/**
* @Flow\InjectConfiguration("lockPrefix")
* @var string
*/
protected $lockPrefix;

/**
* @Flow\InjectConfiguration("defaultCacheName")
* @var string
*/
protected $defaultCacheName;

/**
* @var string
*/
protected $overrideCacheName;

/**
* @Flow\Inject
* @var CacheManager
*/
protected $cacheManager;

/**
* @Flow\Inject
* @var EelRuntime
*/
protected $runtime;

/**
* @param string $name
* @return string
*/
protected function getEntryIdentifier(string $name): string
{
return (!empty($this->lockPrefix) ? $this->lockPrefix . '_' : '') . $name;
}

/**
* @return FrontendInterface
*/
protected function getCache(): FrontendInterface
{
try {
$cache = $this->cacheManager->getCache($this->overrideCacheName ?? $this->defaultCacheName);
} finally {
$this->overrideCacheName = null;
}

return $cache;
}

/**
* @param string $cacheName
* @return $this
*/
public function withCache(string $cacheName)
{
$this->overrideCacheName = $cacheName;
return $this;
}

/**
* @param string $lockName
* @param string $value
*/
public function set(string $lockName, string $value = '1')
{
$this->getCache()->set($this->getEntryIdentifier($lockName), $value, [], 0);
}

/**
* @param string $lockName
*/
public function unset(string $lockName)
{
$this->getCache()->remove($this->getEntryIdentifier($lockName));
}

/**
* @param string $lockName
* @return bool
*/
public function isSet(string $lockName)
{
return $this->getCache()->has($this->getEntryIdentifier($lockName));
}

/**
* @param string $lockName
* @return bool
*/
public function isUnset(string $lockName)
{
return !$this->isSet($lockName);
}

/**
* @param string $methodName
* @return boolean
*/
public function allowsCallOfMethod($methodName)
{
return substr($methodName, 0, 2) === 'is' || $this->runtime->isTaskContext();
}
}
Loading

0 comments on commit 62cca88

Please sign in to comment.