vendor/metamodels/core/src/Attribute/AttributeFactory.php line 140

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of MetaModels/core.
  4.  *
  5.  * (c) 2012-2019 The MetaModels team.
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  *
  10.  * This project is provided in good faith and hope to be usable by anyone.
  11.  *
  12.  * @package    MetaModels/core
  13.  * @author     Christian Schiffler <c.schiffler@cyberspectrum.de>
  14.  * @author     Sven Baumann <baumann.sv@gmail.com>
  15.  * @copyright  2012-2019 The MetaModels team.
  16.  * @license    https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later
  17.  * @filesource
  18.  */
  19. namespace MetaModels\Attribute;
  20. use MetaModels\Attribute\Events\CollectMetaModelAttributeInformationEvent;
  21. use MetaModels\Attribute\Events\CreateAttributeEvent;
  22. use MetaModels\Attribute\Events\CreateAttributeFactoryEvent;
  23. use MetaModels\IMetaModel;
  24. use MetaModels\IMetaModelsServiceContainer;
  25. use MetaModels\MetaModelsEvents;
  26. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  27. /**
  28.  * This is the implementation of the Field factory to query instances of fields.
  29.  *
  30.  * Usually this is only used internally by {@link MetaModels\Factory}
  31.  */
  32. class AttributeFactory implements IAttributeFactory
  33. {
  34.     /**
  35.      * The service container.
  36.      *
  37.      * @var IMetaModelsServiceContainer
  38.      */
  39.     protected $serviceContainer;
  40.     /**
  41.      * The event dispatcher.
  42.      *
  43.      * @var EventDispatcherInterface
  44.      */
  45.     private $eventDispatcher;
  46.     /**
  47.      * The registered type factories.
  48.      *
  49.      * @var IAttributeTypeFactory[]
  50.      */
  51.     protected $typeFactories = array();
  52.     /**
  53.      * Create a new instance.
  54.      *
  55.      * @param EventDispatcherInterface $eventDispatcher The event dispatcher to use.
  56.      */
  57.     public function __construct(EventDispatcherInterface $eventDispatcher)
  58.     {
  59.         $this->eventDispatcher $eventDispatcher;
  60.     }
  61.     /**
  62.      * Set the service container.
  63.      *
  64.      * @param IMetaModelsServiceContainer $serviceContainer  The service container to use.
  65.      *
  66.      * @param bool                        $deprecationNotice Determine deprecated notice.
  67.      *
  68.      * @return AttributeFactory
  69.      *
  70.      * @deprecated The service container will get removed, use the symfony service container instead.
  71.      */
  72.     public function setServiceContainer(IMetaModelsServiceContainer $serviceContainer$deprecationNotice true)
  73.     {
  74.         if ($deprecationNotice) {
  75.             // @codingStandardsIgnoreStart
  76.             @trigger_error(
  77.                 '"' .__METHOD__ '" is deprecated and will get removed.',
  78.                 E_USER_DEPRECATED
  79.             );
  80.             // @codingStandardsIgnoreEnd
  81.         }
  82.         $this->serviceContainer $serviceContainer;
  83.         if ($this->eventDispatcher->hasListeners(MetaModelsEvents::ATTRIBUTE_FACTORY_CREATE)) {
  84.             // @codingStandardsIgnoreStart
  85.             @trigger_error(
  86.                 'Event "' .
  87.                 MetaModelsEvents::ATTRIBUTE_FACTORY_CREATE .
  88.                 '" is deprecated - register your attribute factories via the service container.',
  89.                 E_USER_DEPRECATED
  90.             );
  91.             // @codingStandardsIgnoreEnd
  92.             $this->eventDispatcher->dispatch(
  93.                 MetaModelsEvents::ATTRIBUTE_FACTORY_CREATE,
  94.                 new CreateAttributeFactoryEvent($this)
  95.             );
  96.         }
  97.         return $this;
  98.     }
  99.     /**
  100.      * Retrieve the service container.
  101.      *
  102.      * @return IMetaModelsServiceContainer
  103.      *
  104.      * @deprecated The service container will get removed, use the symfony service container instead.
  105.      */
  106.     public function getServiceContainer()
  107.     {
  108.         // @codingStandardsIgnoreStart
  109.         @trigger_error(
  110.             '"' .__METHOD__ '" is deprecated - use the services from the service container.',
  111.             E_USER_DEPRECATED
  112.         );
  113.         // @codingStandardsIgnoreEnd
  114.         return $this->serviceContainer;
  115.     }
  116.     /**
  117.      * Create an attribute instance from an information array.
  118.      *
  119.      * @param array      $information The attribute information.
  120.      *
  121.      * @param IMetaModel $metaModel   The MetaModel instance for which the attribute shall be created.
  122.      *
  123.      * @return IAttribute|null
  124.      */
  125.     public function createAttribute($information$metaModel)
  126.     {
  127.         $event = new CreateAttributeEvent($information$metaModel);
  128.         $this->eventDispatcher->dispatch(CreateAttributeEvent::NAME$event);
  129.         if ($event->getAttribute()) {
  130.             return $event->getAttribute();
  131.         }
  132.         $factory $this->getTypeFactory($information['type']);
  133.         if (!$factory) {
  134.             return null;
  135.         }
  136.         return $factory->createInstance($information$metaModel);
  137.     }
  138.     /**
  139.      * {@inheritdoc}
  140.      *
  141.      * @throws \RuntimeException When the type is already registered.
  142.      */
  143.     public function addTypeFactory(IAttributeTypeFactory $typeFactory)
  144.     {
  145.         $typeName $typeFactory->getTypeName();
  146.         if (isset($this->typeFactories[$typeName])) {
  147.             throw new \RuntimeException('Attribute type ' $typeName ' is already registered.');
  148.         }
  149.         $this->typeFactories[$typeName] = $typeFactory;
  150.         return $this;
  151.     }
  152.     /**
  153.      * {@inheritdoc}
  154.      */
  155.     public function getTypeFactory($typeFactory)
  156.     {
  157.         return isset($this->typeFactories[(string) $typeFactory]) ? $this->typeFactories[(string) $typeFactory] : null;
  158.     }
  159.     /**
  160.      * {@inheritdoc}
  161.      */
  162.     public function attributeTypeMatchesFlags($name$flags)
  163.     {
  164.         $factory $this->getTypeFactory($name);
  165.         // Shortcut, if all are valid, return all. :)
  166.         if ($flags === self::FLAG_ALL) {
  167.             return true;
  168.         }
  169.         return (($flags self::FLAG_INCLUDE_TRANSLATED) && $factory->isTranslatedType())
  170.             || (($flags self::FLAG_INCLUDE_SIMPLE) && $factory->isSimpleType())
  171.             || (($flags self::FLAG_INCLUDE_COMPLEX) && $factory->isComplexType());
  172.     }
  173.     /**
  174.      * {@inheritdoc}
  175.      */
  176.     public function getTypeNames($flags false)
  177.     {
  178.         if ($flags === false) {
  179.             $flags self::FLAG_ALL;
  180.         }
  181.         $result = array();
  182.         foreach (array_keys($this->typeFactories) as $name) {
  183.             if (!$this->attributeTypeMatchesFlags($name$flags)) {
  184.                 continue;
  185.             }
  186.             $result[] = $name;
  187.         }
  188.         return $result;
  189.     }
  190.     /**
  191.      * {@inheritdoc}
  192.      */
  193.     public function collectAttributeInformation(IMetaModel $metaModel)
  194.     {
  195.         $event = new CollectMetaModelAttributeInformationEvent($metaModel);
  196.         $this->eventDispatcher->dispatch($event::NAME$event);
  197.         return $event->getAttributeInformation();
  198.     }
  199.     /**
  200.      * {@inheritdoc}
  201.      */
  202.     public function createAttributesForMetaModel($metaModel)
  203.     {
  204.         $attributes = array();
  205.         foreach ($this->collectAttributeInformation($metaModel) as $information) {
  206.             $attribute $this->createAttribute($information$metaModel);
  207.             if ($attribute) {
  208.                 $attributes[] = $attribute;
  209.             }
  210.         }
  211.         return $attributes;
  212.     }
  213.     /**
  214.      * {@inheritdoc}
  215.      */
  216.     public function getIconForType($type)
  217.     {
  218.         return isset($this->typeFactories[(string) $type]) ? $this->typeFactories[(string) $type]->getTypeIcon() : null;
  219.     }
  220. }