/Classes/Controller/ApiController.php

https://github.com/sitegeist/Sitegeist.Monocle · PHP · 204 lines · 100 code · 24 blank · 80 comment · 3 complexity · 024a6dc8768304fd75e30f53d9dace71 MD5 · raw file

  1. <?php
  2. namespace Sitegeist\Monocle\Controller;
  3. /**
  4. * This file is part of the Sitegeist.Monocle package
  5. *
  6. * (c) 2016
  7. * Martin Ficzel <ficzel@sitegeist.de>
  8. * Wilhelm Behncke <behncke@sitegeist.de>
  9. *
  10. * This package is Open Source Software. For the full copyright and license
  11. * information, please view the LICENSE file which was distributed with this
  12. * source code.
  13. */
  14. use Neos\Flow\Annotations as Flow;
  15. use Neos\Flow\Mvc\Controller\ActionController;
  16. use Neos\Flow\ResourceManagement\ResourceManager;
  17. use Sitegeist\Monocle\Domain\Fusion\PrototypeRepository;
  18. use Sitegeist\Monocle\Fusion\FusionService;
  19. use Sitegeist\Monocle\Service\PackageKeyTrait;
  20. use Sitegeist\Monocle\Service\ConfigurationService;
  21. use Sitegeist\Monocle\Domain\PrototypeDetails\PrototypeDetailsFactory;
  22. /**
  23. * Class ApiController
  24. * @package Sitegeist\Monocle\Controller
  25. */
  26. class ApiController extends ActionController
  27. {
  28. use PackageKeyTrait;
  29. /**
  30. * @var array
  31. */
  32. protected $defaultViewObjectName = 'Neos\Flow\Mvc\View\JsonView';
  33. /**
  34. * @Flow\Inject
  35. * @var FusionService
  36. */
  37. protected $fusionService;
  38. /**
  39. * @Flow\Inject
  40. * @var ResourceManager
  41. */
  42. protected $resourceManager;
  43. /**
  44. * @var array
  45. * @Flow\InjectConfiguration("preview.additionalResources")
  46. */
  47. protected $additionalResources;
  48. /**
  49. * @Flow\Inject
  50. * @var ConfigurationService
  51. */
  52. protected $configurationService;
  53. /**
  54. * @Flow\Inject
  55. * @var PrototypeRepository
  56. */
  57. protected $prototypeRepository;
  58. /**
  59. * @Flow\Inject
  60. * @var PrototypeDetailsFactory
  61. */
  62. protected $prototypeDetailsFactory;
  63. /**
  64. * Get all configurations for this site package
  65. *
  66. * @param string $sitePackageKey
  67. */
  68. public function configurationAction($sitePackageKey = null)
  69. {
  70. $sitePackageKey = $sitePackageKey ?: $this->getDefaultSitePackageKey();
  71. $value = [];
  72. $value['sitePackage'] = $sitePackageKey;
  73. $value['ui'] = [
  74. 'sitePackages' => $this->getSitePackages(),
  75. 'viewportPresets' => $this->configurationService->getSiteConfiguration($sitePackageKey, 'ui.viewportPresets'),
  76. 'localePresets' => $this->configurationService->getSiteConfiguration($sitePackageKey, 'ui.localePresets'),
  77. 'hotkeys' => $this->configurationService->getSiteConfiguration($sitePackageKey, 'ui.hotkeys'),
  78. 'preview' => $this->configurationService->getSiteConfiguration($sitePackageKey, 'preview')
  79. ];
  80. $value['styleguideObjects'] = $this->getStyleguideObjects($sitePackageKey);
  81. $this->view->assign('value', $value);
  82. }
  83. /**
  84. * Render informations about the given prototype
  85. *
  86. * @Flow\SkipCsrfProtection
  87. * @param string $sitePackageKey
  88. * @param string $prototypeName
  89. * @return void
  90. */
  91. public function prototypeDetailsAction($sitePackageKey, $prototypeName)
  92. {
  93. $this->response->setContentType('application/json');
  94. $prototype = $this->prototypeRepository
  95. ->findOneByPrototypeNameInSitePackage(
  96. $prototypeName,
  97. $sitePackageKey
  98. );
  99. $prototypeDetails = $this->prototypeDetailsFactory
  100. ->forPrototype($prototype);
  101. return json_encode($prototypeDetails);
  102. }
  103. /**
  104. * Render the given prototype
  105. *
  106. * @Flow\SkipCsrfProtection
  107. * @param string $prototypeName
  108. * @param string $sitePackageKey
  109. * @return void
  110. * @deprecated
  111. */
  112. public function renderPrototypeAction($prototypeName, $sitePackageKey = null)
  113. {
  114. return $this->prototypeDetailsAction($sitePackageKey, $prototypeName);
  115. }
  116. /**
  117. * @return array
  118. */
  119. protected function getSitePackages(): array
  120. {
  121. $sitePackageKeys = $this->getActiveSitePackageKeys();
  122. $result = [];
  123. foreach ($sitePackageKeys as $sitePackageKey) {
  124. $result[$sitePackageKey] = $sitePackageKey;
  125. }
  126. return $result;
  127. }
  128. /**
  129. * @param $sitePackageKey
  130. * @param $styleguideObject
  131. * @return array
  132. * @throws \Neos\Neos\Domain\Exception
  133. */
  134. protected function getStyleguideObjects($sitePackageKey): array
  135. {
  136. $fusionAst = $this->fusionService->getMergedFusionObjectTreeForSitePackage($sitePackageKey);
  137. $styleguideObjects = $this->fusionService->getStyleguideObjectsFromFusionAst($fusionAst);
  138. $prototypeStructures = $this->configurationService->getSiteConfiguration($sitePackageKey, 'ui.structure');
  139. foreach ($styleguideObjects as $prototypeName => &$styleguideObject) {
  140. $styleguideObject['structure'] = $this->getStructureForPrototypeName($prototypeStructures, $prototypeName);
  141. }
  142. $hiddenPrototypeNamePatterns = $this->configurationService->getSiteConfiguration($sitePackageKey, 'hiddenPrototypeNamePatterns');
  143. if (is_array($hiddenPrototypeNamePatterns)) {
  144. $alwaysShowPrototypes = $this->configurationService->getSiteConfiguration($sitePackageKey, 'alwaysShowPrototypes');
  145. foreach ($hiddenPrototypeNamePatterns as $pattern) {
  146. $styleguideObjects = array_filter(
  147. $styleguideObjects,
  148. function ($prototypeName) use ($pattern, $alwaysShowPrototypes) {
  149. if (in_array($prototypeName, $alwaysShowPrototypes, true)) {
  150. return true;
  151. }
  152. return fnmatch($pattern, $prototypeName) === false;
  153. },
  154. ARRAY_FILTER_USE_KEY
  155. );
  156. }
  157. }
  158. return $styleguideObjects;
  159. }
  160. /**
  161. * Find the matching structure for a prototype
  162. *
  163. * @param $prototypeStructures
  164. * @param $prototypeName
  165. * @return array
  166. */
  167. protected function getStructureForPrototypeName($prototypeStructures, $prototypeName)
  168. {
  169. foreach ($prototypeStructures as $structure) {
  170. if (preg_match(sprintf('!%s!', $structure['match']), $prototypeName)) {
  171. return $structure;
  172. }
  173. }
  174. return [
  175. 'label' => 'Other',
  176. 'icon' => 'icon-question',
  177. 'color' => 'white'
  178. ];
  179. }
  180. }