Skip to content

Commit

Permalink
Add and use behavior ExpressionInjector
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg authored and yhabteab committed Oct 25, 2022
1 parent 72f1bed commit 12178a4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
53 changes: 53 additions & 0 deletions library/X509/Model/Behavior/ExpressionInjector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Icinga\Module\X509\Model\Behavior;

use ipl\Orm\Contract\QueryAwareBehavior;
use ipl\Orm\Contract\RewriteFilterBehavior;
use ipl\Orm\Query;
use ipl\Stdlib\Filter;

class ExpressionInjector implements RewriteFilterBehavior, QueryAwareBehavior
{
/** @var array */
protected $columns;

/** @var Query */
protected $query;

public function __construct(...$columns)
{
$this->columns = $columns;
}

public function setQuery(Query $query)
{
$this->query = $query;

return $this;
}

public function rewriteCondition(Filter\Condition $condition, $relation = null)
{
$columnName = $condition->metaData()->get('columnName');
if (in_array($columnName, $this->columns, true)) {
$relationPath = $condition->metaData()->get('relationPath');
if ($relationPath && $relationPath !== $this->query->getModel()->getTableAlias()) {
$subject = $this->query->getResolver()->resolveRelation($relationPath)->getTarget();
} else {
$subject = $this->query->getModel();
}

$expression = clone $subject->getColumns()[$columnName];
$expression->setColumns($this->query->getResolver()->qualifyColumns(
$this->query->getResolver()->requireAndResolveColumns(
$expression->getColumns(),
$subject
),
$subject
));

$condition->setColumn($this->query->getDb()->getQueryBuilder()->buildExpression($expression));
}
}
}
3 changes: 3 additions & 0 deletions library/X509/Model/X509Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Icinga\Module\X509\Model\Behavior\BoolCast;
use Icinga\Module\X509\Model\Behavior\DistinguishedEncodingRules;
use Icinga\Module\X509\Model\Behavior\ExpressionInjector;
use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behaviors;
use ipl\Orm\Model;
Expand Down Expand Up @@ -103,6 +104,8 @@ public function createBehaviors(Behaviors $behaviors)
'trusted',
'self_signed'
]));

$behaviors->add(new ExpressionInjector('duration', 'expires'));
}

public function createRelations(Relations $relations)
Expand Down

0 comments on commit 12178a4

Please sign in to comment.