-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
668 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Colibri\Extension; | ||
|
||
use Colibri\Common\Callback; | ||
use Colibri\Core\Event\EntityLifecycleEvent; | ||
use Colibri\EventDispatcher\EventSubscriber; | ||
use Colibri\Exception\NotFoundException; | ||
use Colibri\Parameters\ParametersCollection; | ||
|
||
/** | ||
* Class AbstractExtension | ||
* @package Colibri\Extension | ||
*/ | ||
abstract class AbstractExtension implements ExtensionInterface, EventSubscriber | ||
{ | ||
|
||
/** | ||
* @var ParametersCollection | ||
*/ | ||
protected $configuration; | ||
|
||
/** | ||
* AbstractExtension constructor. | ||
* @param ParametersCollection $configuration | ||
* @throws NotFoundException | ||
*/ | ||
public function __construct(ParametersCollection $configuration) | ||
{ | ||
$extensionConfiguration = $configuration->path(sprintf('extensions.%s', $this->getNameNS())); | ||
|
||
if (null === $extensionConfiguration) { | ||
throw new NotFoundException(sprintf('Extension could not be loaded because configuration for "%s" does not exist', | ||
$this->getNameNS())); | ||
} | ||
|
||
$this->setConfiguration($extensionConfiguration); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function setConfiguration(ParametersCollection $collection) | ||
{ | ||
$this->configuration = $collection; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getConfiguration() | ||
{ | ||
return $this->configuration; | ||
} | ||
|
||
/** | ||
* @param EntityLifecycleEvent $event | ||
* @param callable $callback | ||
*/ | ||
protected function resolveEntities(EntityLifecycleEvent $event, callable $callback) | ||
{ | ||
$callback = new Callback($callback); | ||
|
||
foreach ($this->getConfiguration() as $entityClassName => $extensionConfiguration) { | ||
if ($event->getEntity() instanceof $entityClassName) { | ||
$entity = $event->getEntity(); | ||
$callback->call(...[$entity, $extensionConfiguration]); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param $variable | ||
* @return bool | ||
*/ | ||
protected static function isIterable($variable) | ||
{ | ||
if (version_compare(PHP_VERSION, 7.1) === -1) { | ||
return is_array($variable) || ($variable instanceof \Traversable); | ||
} else { | ||
return is_iterable($variable); | ||
} | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/Colibri/Extension/EventSubscriber/AbstractDataFilter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Colibri\Extension\EventSubscriber; | ||
|
||
use Colibri\Core\ORMEvents; | ||
use Colibri\Extension\AbstractExtension; | ||
|
||
/** | ||
* Class AbstractDataFilter | ||
* @package Colibri\Extension\EventSubscriber | ||
*/ | ||
abstract class AbstractDataFilter extends AbstractExtension | ||
{ | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getEvents() | ||
{ | ||
return [ORMEvents::beforePersist]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Colibri\Extension\EventSubscriber; | ||
|
||
use Colibri\Core\Entity\EntityInterface; | ||
use Colibri\Core\Event\EntityLifecycleEvent; | ||
use Colibri\Parameters\ParametersCollection; | ||
use Colibri\Filters\FilterInterface; | ||
|
||
/** | ||
* Class DataFilter | ||
* @package Colibri\Extension\EventSubscriber | ||
*/ | ||
class DataFilter extends AbstractDataFilter | ||
{ | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getNameNS() | ||
{ | ||
return 'dataFilter'; | ||
} | ||
|
||
/** | ||
* @param EntityLifecycleEvent $event | ||
*/ | ||
public function beforePersist(EntityLifecycleEvent $event) | ||
{ | ||
$this->resolveEntities($event, function (EntityInterface $entity, ParametersCollection $parameters) { | ||
/** @var ParametersCollection $configuration */ | ||
foreach ($parameters as $propertyName => $configuration) { | ||
if ($entity->hasProperty($propertyName) && $configuration->offsetExists('filters')) { | ||
$propertyData = $entity->getByProperty($propertyName); | ||
$propertyData = $this->applyFilters($propertyData, $configuration->offsetGet('filters')->toArray()); | ||
$entity->setByProperty($propertyName, $propertyData); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @param $propertyData | ||
* @param array $filterClasses | ||
* @return array|int|string | ||
*/ | ||
protected function applyFilters(&$propertyData, array $filterClasses) | ||
{ | ||
/** @var FilterInterface $filter */ | ||
foreach ($filterClasses as $filterClass => $filterClassArguments) { | ||
$filter = new $filterClass(...$filterClassArguments); | ||
$propertyData = $filter->apply($propertyData); | ||
} | ||
|
||
return $propertyData; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Colibri\Extension\EventSubscriber; | ||
|
||
use Colibri\Collection\Collection; | ||
use Colibri\Core\Event\EntityLifecycleEvent; | ||
use Colibri\Core\ORMEvents; | ||
use Colibri\Extension\AbstractExtension; | ||
use Colibri\Parameters\ParametersCollection; | ||
|
||
/** | ||
* Class ResourceLogger | ||
* @package Colibri\Extension\EventSubscriber | ||
*/ | ||
class ResourceLogger extends AbstractExtension | ||
{ | ||
|
||
protected $entitiesStates; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function __construct(ParametersCollection $configuration) | ||
{ | ||
parent::__construct($configuration); | ||
|
||
$this->entitiesStates = new Collection(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getEvents() | ||
{ | ||
return [ORMEvents::beforePersist, ORMEvents::afterPersist]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getNameNS() | ||
{ | ||
return 'resourceLogger'; | ||
} | ||
|
||
/** | ||
* @param EntityLifecycleEvent $event | ||
*/ | ||
public function beforePersist(EntityLifecycleEvent $event) | ||
{ | ||
$entity = $event->getEntity(); | ||
$states = $this->getEntitiesStates(); | ||
|
||
$states->set($entity->hashCode(), $entity); | ||
} | ||
|
||
/** | ||
* @param EntityLifecycleEvent $event | ||
*/ | ||
public function afterPersist(EntityLifecycleEvent $event) | ||
{ | ||
$entity = $event->getEntity(); | ||
$states = $this->getEntitiesStates(); | ||
|
||
if (!$states->has($entity->hashCode())) { | ||
|
||
} | ||
} | ||
|
||
/** | ||
* @return Collection | ||
*/ | ||
public function getEntitiesStates() | ||
{ | ||
return $this->entitiesStates; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/Colibri/Extension/EventSubscriber/ResourceLogger/ResourceModelInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Colibri\Extension\EventSubscriber\ResourceLogger; | ||
|
||
/** | ||
* Interface ResourceModelInterface | ||
* @package Colibri\Extension\EventSubscriber\ResourceLogger | ||
*/ | ||
interface ResourceModelInterface | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Colibri\Extension\EventSubscriber; | ||
|
||
use Behat\Transliterator\Transliterator; | ||
use Colibri\Core\Entity\EntityInterface; | ||
use Colibri\Core\Event\EntityLifecycleEvent; | ||
use Colibri\Core\ORMEvents; | ||
use Colibri\Extension\AbstractExtension; | ||
use Colibri\Parameters\ParametersCollection; | ||
|
||
/** | ||
* Class Sluggable | ||
* @package Colibri\Extension\EventSubscriber | ||
*/ | ||
class Sluggable extends AbstractExtension | ||
{ | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getEvents() | ||
{ | ||
return [ORMEvents::beforePersist]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getNameNS() | ||
{ | ||
return 'sluggable'; | ||
} | ||
|
||
/** | ||
* @param EntityInterface $entity | ||
* @param ParametersCollection $configuration | ||
* @return array | ||
*/ | ||
protected function getSlugForEntity(EntityInterface $entity, ParametersCollection $configuration) | ||
{ | ||
$parts = []; | ||
|
||
foreach ($configuration->offsetGet('properties') as $property) { | ||
if ($entity->hasProperty($property)) { | ||
$parts[] = Transliterator::urlize(Transliterator::transliterate($entity->getByProperty($property))); | ||
} | ||
} | ||
|
||
$string = implode($configuration->offsetGet('separator'), $parts); | ||
|
||
return sprintf('%s%s%s', $configuration->get('prefix'), $string, $configuration->get('suffix')); | ||
} | ||
|
||
/** | ||
* @param EntityLifecycleEvent $event | ||
*/ | ||
public function beforePersist(EntityLifecycleEvent $event) | ||
{ | ||
foreach ($this->getConfiguration() as $entityClassName => $propertyConfiguration) { | ||
if ($event->getEntity() instanceof $entityClassName) { | ||
$entity = $event->getEntity(); | ||
foreach ($propertyConfiguration as $propertyName => $configuration) { | ||
$entity->setByProperty($propertyName, $this->getSlugForEntity($entity, $configuration)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.