Change to use border-manager as checker registry.

This commit is contained in:
Benoît Burnichon
2015-03-17 16:55:55 +01:00
parent ed9bdd6683
commit 0ce4fd3192
5 changed files with 36 additions and 30 deletions

View File

@@ -13,7 +13,7 @@ namespace Alchemy\Phrasea\Border;
use Alchemy\Phrasea\Border\Checker\CheckerInterface; use Alchemy\Phrasea\Border\Checker\CheckerInterface;
use Alchemy\Phrasea\Border\Attribute\AttributeInterface; use Alchemy\Phrasea\Border\Attribute\AttributeInterface;
use Alchemy\Phrasea\Media\Subdef\OptionType\Boolean; use Alchemy\Phrasea\Exception\RuntimeException;
use Alchemy\Phrasea\Metadata\Tag\TfArchivedate; use Alchemy\Phrasea\Metadata\Tag\TfArchivedate;
use Alchemy\Phrasea\Metadata\Tag\TfQuarantine; use Alchemy\Phrasea\Metadata\Tag\TfQuarantine;
use Alchemy\Phrasea\Metadata\Tag\TfBasename; use Alchemy\Phrasea\Metadata\Tag\TfBasename;
@@ -143,7 +143,7 @@ class Manager
*/ */
public function registerChecker(CheckerInterface $checker) public function registerChecker(CheckerInterface $checker)
{ {
$this->checkers[] = $checker; $this->checkers[get_class($checker)] = $checker;
return $this; return $this;
} }
@@ -183,6 +183,22 @@ class Manager
return $this; return $this;
} }
/**
* Get checker instance from its class name.
*
* @param string $checkerName
* @return CheckerInterface
*/
public function getCheckerFromFQCN($checkerName)
{
$checkerName = trim($checkerName, '\\');
if (!isset($this->checkers[$checkerName])) {
throw new RuntimeException('Checker could not be found');
}
return $this->checkers[$checkerName];
}
/** /**
* Returns all the checkers registered * Returns all the checkers registered
* *
@@ -190,7 +206,7 @@ class Manager
*/ */
public function getCheckers() public function getCheckers()
{ {
return $this->checkers; return array_values($this->checkers);
} }
/** /**

View File

@@ -16,13 +16,13 @@ use Alchemy\Phrasea\Border\Attribute\Status;
use Alchemy\Phrasea\Border\Checker\CheckerInterface; use Alchemy\Phrasea\Border\Checker\CheckerInterface;
use Alchemy\Phrasea\Border\File; use Alchemy\Phrasea\Border\File;
use Alchemy\Phrasea\Border\Manager as BorderManager; use Alchemy\Phrasea\Border\Manager as BorderManager;
use Alchemy\Phrasea\Border\Manager;
use Alchemy\Phrasea\Cache\Cache as CacheInterface; use Alchemy\Phrasea\Cache\Cache as CacheInterface;
use Alchemy\Phrasea\Core\Event\ApiOAuth2EndEvent; use Alchemy\Phrasea\Core\Event\ApiOAuth2EndEvent;
use Alchemy\Phrasea\Core\Event\ApiOAuth2StartEvent; use Alchemy\Phrasea\Core\Event\ApiOAuth2StartEvent;
use Alchemy\Phrasea\Core\Event\PreAuthenticate; use Alchemy\Phrasea\Core\Event\PreAuthenticate;
use Alchemy\Phrasea\Core\Event\RecordEdit; use Alchemy\Phrasea\Core\Event\RecordEdit;
use Alchemy\Phrasea\Core\PhraseaEvents; use Alchemy\Phrasea\Core\PhraseaEvents;
use Alchemy\Phrasea\Exception\RuntimeException;
use Alchemy\Phrasea\Feed\Aggregate; use Alchemy\Phrasea\Feed\Aggregate;
use Alchemy\Phrasea\Feed\FeedInterface; use Alchemy\Phrasea\Feed\FeedInterface;
use Alchemy\Phrasea\Model\Entities\Basket; use Alchemy\Phrasea\Model\Entities\Basket;
@@ -812,20 +812,14 @@ class V1 implements ControllerProviderInterface
private function list_lazaret_file(Application $app, LazaretFile $file) private function list_lazaret_file(Application $app, LazaretFile $file)
{ {
/** @var CheckerInterface[] $checkers */ /** @var Manager $manager */
$checkers = $app['border-manager']->getCheckers(); $manager = $app['border-manager'];
/** @var TranslatorInterface $translator */ /** @var TranslatorInterface $translator */
$translator = $app['translator']; $translator = $app['translator'];
$checks = array_map(function (LazaretCheck $checker) use ($checkers, $translator) { $checks = array_map(function (LazaretCheck $checker) use ($manager, $translator) {
$checkerFQCN = $checker->getCheckClassname(); $checkerFQCN = $checker->getCheckClassname();
foreach ($checkers as $actualChecker) { return $manager->getCheckerFromFQCN($checkerFQCN)->getMessage($translator);
if (get_class($actualChecker) === $checkerFQCN) {
return $actualChecker->getMessage($translator);
}
}
throw new RuntimeException('Could not find checker');
}, iterator_to_array($file->getChecks())); }, iterator_to_array($file->getChecks()));
$usr_id = $user = null; $usr_id = $user = null;

View File

@@ -5,14 +5,15 @@ namespace Alchemy\Phrasea\Twig;
use Alchemy\Phrasea\Model\Entities\ElasticsearchRecord; use Alchemy\Phrasea\Model\Entities\ElasticsearchRecord;
use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Model\RecordInterface; use Alchemy\Phrasea\Model\RecordInterface;
use Silex\Application;
class PhraseanetExtension extends \Twig_Extension class PhraseanetExtension extends \Twig_Extension
{ {
/** @var Application */
private $app; private $app;
public function __construct( public function __construct(Application $app)
$app {
) {
$this->app = $app; $this->app = $app;
} }
@@ -37,7 +38,8 @@ class PhraseanetExtension extends \Twig_Extension
new \Twig_SimpleFunction('collection_logo', array($this, 'getCollectionLogo'), array( new \Twig_SimpleFunction('collection_logo', array($this, 'getCollectionLogo'), array(
'is_safe' => array('html') 'is_safe' => array('html')
)), )),
new \Twig_SimpleFunction('record_flags', array($this, 'getRecordFlags')) new \Twig_SimpleFunction('record_flags', array($this, 'getRecordFlags')),
new \Twig_SimpleFunction('border_checker_from_fqcn', array($this->app['border-manager'], 'getCheckerFromFQCN')),
); );
} }

View File

@@ -9,7 +9,7 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
use Alchemy\Phrasea\Border\Checker\CheckerInterface; use Alchemy\Phrasea\Border\Manager;
use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\Entities\User;
use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Translation\TranslatorInterface;
@@ -32,19 +32,13 @@ class eventsmanager_notify_uploadquarantine extends eventsmanager_notifyAbstract
*/ */
public function datas(array $data, $unread) public function datas(array $data, $unread)
{ {
/** @var CheckerInterface[] $checkers */ /** @var Manager $manager */
$checkers = $this->app['border-manager']->getCheckers(); $manager = $this->app['border-manager'];
/** @var TranslatorInterface $translator */ /** @var TranslatorInterface $translator */
$translator = $this->app['translator']; $translator = $this->app['translator'];
$reasons = array_map(function ($checkerFQCN) use ($checkers, $translator) { $reasons = array_map(function ($checkerFQCN) use ($manager, $translator) {
foreach ($checkers as $actualChecker) { return $manager->getCheckerFromFQCN($checkerFQCN)->getMessage($translator);
if (get_class($actualChecker) === $checkerFQCN) {
return $actualChecker->getMessage($translator);
}
}
throw new RuntimeException('Could not find checker');
}, $data['reasons']); }, $data['reasons']);
$filename = $data['filename']; $filename = $data['filename'];

View File

@@ -334,7 +334,7 @@
</div> </div>
<div class="caption"> <div class="caption">
{% for check in file.getChecks() %} {% for check in file.getChecks() %}
<p>{{ check.getMessage(app['translator']) }}</p> <p>{{ border_checker_from_fqcn(check.getCheckClassname()).getMessage(app['translator']) }}</p>
{% endfor %} {% endfor %}
</div> </div>
<div class="btn-group" style="text-align:center; padding:5px;"> <div class="btn-group" style="text-align:center; padding:5px;">