PersistenceActionKeyProvider.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\config\impl;
12 
21 
22 /**
23  * PersistenceActionKeyProvider searches for action keys in the
24  * application storage.
25  *
26  * @author ingo herwig <ingo@wemove.com>
27  */
29 
30  private static $cacheKey = 'keys';
31 
32  private $entityType = null;
33  private $valueMap = [];
34  private $id = null;
35 
36  private $isLoadingKeys = false;
37 
38  /**
39  * Constructor.
40  */
41  public function __construct() {
42  ObjectFactory::getInstance('eventManager')->addListener(PersistenceEvent::NAME,
43  [$this, 'keyChanged']);
44  }
45 
46  /**
47  * Destructor.
48  */
49  public function __destruct() {
50  ObjectFactory::getInstance('eventManager')->removeListener(PersistenceEvent::NAME,
51  [$this, 'keyChanged']);
52  }
53 
54  /**
55  * Set the entity type to search in.
56  * @param $entityType String
57  */
58  public function setEntityType($entityType) {
59  $this->entityType = $entityType;
60  $this->id = null;
61  }
62 
63  /**
64  * Set the value map for the entity type.
65  * @param $valueMap Associative array that maps the keys 'resource', 'context', 'action', 'value'
66  * to value names of the entity type.
67  */
68  public function setValueMap($valueMap) {
69  $this->valueMap = $valueMap;
70  }
71 
72  /**
73  * @see ActionKeyProvider::containsKey()
74  */
75  public function containsKey($actionKey) {
76  return $this->getKeyValue($actionKey) !== null;
77  }
78 
79  /**
80  * @see ActionKeyProvider::getKeyValue()
81  */
82  public function getKeyValue($actionKey) {
83  if ($this->isLoadingKeys) {
84  return null;
85  }
86  $cache = ObjectFactory::getInstance('dynamicCache');
87  $cacheSection = $this->getId();
88  if (!$cache->exists($cacheSection, self::$cacheKey)) {
89  $keys = $this->getAllKeyValues();
90  $cache->put($cacheSection, self::$cacheKey, $keys);
91  }
92  else {
93  $keys = $cache->get($cacheSection, self::$cacheKey);
94  }
95  return isset($keys[$actionKey]) ? $keys[$actionKey] : null;
96  }
97 
98  /**
99  * @see ActionKeyProvider::getId()
100  */
101  public function getId() {
102  if ($this->id == null) {
103  $this->id = str_replace('\\', '.', __CLASS__).'.'.$this->entityType;
104  }
105  return $this->id;
106  }
107 
108  /**
109  * Get all key values from the storage
110  * @return Associative array with action keys as keys
111  */
112  protected function getAllKeyValues() {
113  $keys = [];
114  // add temporary permission to allow to read entitys
115  $this->isLoadingKeys = true;
116  $permissionManager = ObjectFactory::getInstance('permissionManager');
117  $tmpPerm = $permissionManager->addTempPermission($this->entityType, '', PersistenceAction::READ);
118  $persistenceFacade = ObjectFactory::getInstance('persistenceFacade');
119  $objects = $persistenceFacade->loadObjects($this->entityType, BuildDepth::SINGLE);
120  $permissionManager->removeTempPermission($tmpPerm);
121  $this->isLoadingKeys = false;
122  foreach ($objects as $object) {
123  $key = ActionKey::createKey(
124  $object->getValue($this->valueMap['resource']),
125  $object->getValue($this->valueMap['context']),
126  $object->getValue($this->valueMap['action'])
127  );
128  $keys[$key] = $object->getValue($this->valueMap['value']);
129  }
130  return $keys;
131  }
132 
133  /**
134  * Get a single key value from the storage
135  * @param $actionKey The action key
136  * @return String
137  */
138  protected function getSingleKeyValue($actionKey) {
139  $query = new ObjectQuery($this->entityType, __CLASS__.__METHOD__);
140  $tpl = $query->getObjectTemplate($this->entityType);
141  $actionKeyParams = ActionKey::parseKey($actionKey);
142  $tpl->setValue($this->valueMap['resource'], Criteria::asValue('=', $actionKeyParams['resource']));
143  $tpl->setValue($this->valueMap['context'], Criteria::asValue('=', $actionKeyParams['context']));
144  $tpl->setValue($this->valueMap['action'], Criteria::asValue('=', $actionKeyParams['action']));
145  $keys = $query->execute(BuildDepth::SINGLE);
146  if (sizeof($keys) > 0) {
147  return $keys[0]->getValue($this->valueMap['value']);
148  }
149  return null;
150  }
151 
152  /**
153  * Listen to PersistentEvent
154  * @param $event PersistentEvent instance
155  */
156  public function keyChanged(PersistenceEvent $event) {
157  $object = $event->getObject();
158  if ($object->getType() == $this->entityType) {
159  $cache = ObjectFactory::getInstance('dynamicCache');
160  $cache->clear($this->getId());
161  }
162  }
163 }
164 ?>
getObject()
Get the object involved.
PersistenceActionKeyProvider searches for action keys in the application storage.
static asValue($operator, $value)
Factory method for constructing a Criteria that may be used as value on a PersistentObject's attribut...
Definition: Criteria.php:58
Criteria defines a condition on a PersistentObject's attribute used to select specific instances.
Definition: Criteria.php:21
BuildDepth values are used to define the depth when loading object trees.
Definition: BuildDepth.php:19
static parseKey($actionKey)
Parse an action.
Definition: ActionKey.php:42
Implementations of ActionKeyProvider search for action keys.
PersistentEvent signals create/update/delete operations on a persistent entity.
static createKey($resource, $context, $action)
Create an action key from the given values.
Definition: ActionKey.php:33
setEntityType($entityType)
Set the entity type to search in.
setValueMap($valueMap)
Set the value map for the entity type.
static getInstance($name, $dynamicConfiguration=[])
An action key is a combination of a resource, context and action that is represented as a string.
Definition: ActionKey.php:22
getSingleKeyValue($actionKey)
Get a single key value from the storage.
PersistenceAction values are used to define actions on PersistentObject instances.
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...
keyChanged(PersistenceEvent $event)
Listen to PersistentEvent.
ObjectQuery implements a template based object query.