/Classes/Core/ConfigurationManager.php

https://github.com/mneuhaus/FLOW3-Admin · PHP · 179 lines · 85 code · 31 blank · 63 comment · 11 complexity · 6476a182cb3a355df818ab5735adb00d MD5 · raw file

  1. <?php
  2. namespace Admin\Core;
  3. /* *
  4. * This script belongs to the FLOW3 framework. *
  5. * *
  6. * It is free software; you can redistribute it and/or modify it under *
  7. * the terms of the GNU Lesser General Public License as published by the *
  8. * Free Software Foundation, either version 3 of the License, or (at your *
  9. * option) any later version. *
  10. * *
  11. * This script is distributed in the hope that it will be useful, but *
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
  13. * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
  14. * General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU Lesser General Public *
  17. * License along with the script. *
  18. * If not, see http://www.gnu.org/licenses/lgpl.html *
  19. * *
  20. * The TYPO3 project - inspiring people to share! *
  21. * */
  22. use Doctrine\ORM\Mapping as ORM;
  23. use TYPO3\FLOW3\Annotations as FLOW3;
  24. /**
  25. *
  26. * @version $Id: ForViewHelper.php 3346 2009-10-22 17:26:10Z k-fish $
  27. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
  28. * @api
  29. * @FLOW3\Scope("singleton")
  30. */
  31. class ConfigurationManager{
  32. /**
  33. * @var TYPO3\FLOW3\Cache\CacheManager
  34. * @FLOW3\Inject
  35. */
  36. protected $cacheManager;
  37. /**
  38. * @var array
  39. */
  40. protected $configurationProviders;
  41. /**
  42. * @var \TYPO3\FLOW3\Object\ObjectManagerInterface
  43. * @api
  44. * @author Marc Neuhaus <apocalip@gmail.com>
  45. * @FLOW3\Inject
  46. */
  47. protected $objectManager;
  48. /**
  49. * @var \TYPO3\FLOW3\Package\PackageManagerInterface
  50. * @author Marc Neuhaus <apocalip@gmail.com>
  51. * @FLOW3\Inject
  52. */
  53. protected $packageManager;
  54. /**
  55. * @var \TYPO3\FLOW3\Reflection\ReflectionService
  56. * @api
  57. * @author Marc Neuhaus <apocalip@gmail.com>
  58. * @FLOW3\Inject
  59. */
  60. protected $reflectionService;
  61. /**
  62. * @var array
  63. */
  64. protected $settings;
  65. /**
  66. * @var array
  67. */
  68. protected $runtimeCache = array();
  69. public function __construct(\Admin\Core\Helper $helper){
  70. $this->settings = $helper->getSettings();
  71. }
  72. public function getClassConfiguration($class){
  73. $implementations = class_implements("\\" . ltrim($class, "\\"));
  74. if(in_array("Doctrine\ORM\Proxy\Proxy", $implementations))
  75. $class = get_parent_class("\\" . ltrim($class, "\\"));
  76. $this->class = $class;
  77. if(isset($this->settings["ConfigurationProvider"]) && !isset($this->runtimeCache[$class])){
  78. $configuration = array();
  79. $configurationProviders = $this->settings["ConfigurationProvider"];
  80. foreach($configurationProviders as $configurationProviderClass){
  81. $configurationProvider = $this->objectManager->get($configurationProviderClass);
  82. $configurationProvider->setSettings($this->settings);
  83. $configuration = $this->merge($configuration, $configurationProvider->get($class));
  84. }
  85. $this->runtimeCache[$class] = $configuration;
  86. }
  87. return $this->runtimeCache[$class];
  88. }
  89. public function merge($configuration, $override){
  90. foreach ($override as $annotation => $objects) {
  91. if($annotation == "properties"){
  92. if(!isset($configuration[$annotation]))
  93. $configuration[$annotation] = array();
  94. $configuration[$annotation] = $this->merge($configuration[$annotation], $objects);
  95. }else{
  96. foreach ($objects as $key => $object) {
  97. try{if(isset($object->multiple) && $object->multiple){
  98. $configuration[$annotation][] = $object;
  99. }else{
  100. $configuration[$annotation][$key] = $object;
  101. }
  102. }catch(\TYPO3\FLOW3\Error\Exception $e){}
  103. }
  104. }
  105. }
  106. return $configuration;
  107. }
  108. /**
  109. * returns classes that are taged with all of the specified tags
  110. *
  111. * @param string $tags
  112. * @return void
  113. * @author Marc Neuhaus
  114. */
  115. public function getClassesAnnotatedWith($tags){
  116. $cache = $this->cacheManager->getCache('Admin_ImplementationCache');
  117. $identifier = "ClassesTaggedWith-".implode("_",$tags);
  118. if(!$cache->has($identifier)){
  119. $classes = array();
  120. $activePackages = $this->packageManager->getActivePackages();
  121. foreach($activePackages as $packageName => $package) {
  122. if(substr($packageName, 0, 8) === "Doctrine") continue;
  123. foreach($package->getClassFiles() as $class => $file) {
  124. $annotations = $this->getClassConfiguration($class);
  125. $tagged = true;
  126. foreach($tags as $tag){
  127. if(!isset($annotations[$tag])) $tagged = false;
  128. }
  129. if($tagged)
  130. $classes[$class] = $packageName;
  131. }
  132. }
  133. $cache->set($identifier,$classes);
  134. }elseif(isset($this->runtimeCache[$identifier])){
  135. $classes = $this->runtimeCache[$identifier];
  136. }else{
  137. $this->runtimeCache[$identifier] = $classes = $cache->get($identifier);
  138. }
  139. return $classes;
  140. }
  141. public function setConfigurationProviders($configurationProviders){
  142. $this->configurationProviders = $configurationProviders;
  143. }
  144. public function setSettings($settings){
  145. $this->settings = $settings;
  146. }
  147. }
  148. ?>