PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/legacy/Zikula/View/Plugin.php

https://github.com/antoniom/core
PHP | 204 lines | 106 code | 28 blank | 70 comment | 24 complexity | 450cfb1dba03b9306ab04fdb1a71c6f3 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MIT
  1. <?php
  2. /**
  3. * Copyright Zikula Foundation 2009 - Zikula Application Framework
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPLv3 (or at your option, any later version).
  9. * @package Zikula_View
  10. *
  11. * Please see the NOTICE file distributed with this source code for further
  12. * information regarding copyright and licensing.
  13. */
  14. use \Symfony\Component\DependencyInjection\ContainerBuilder;
  15. /**
  16. * Zikula_View_Plugin for plugin system.
  17. */
  18. class Zikula_View_Plugin extends Zikula_View
  19. {
  20. /**
  21. * The plugin name.
  22. *
  23. * @var string
  24. */
  25. protected $pluginName;
  26. /**
  27. * Constructor.
  28. *
  29. * @param ServiceManager $container ServiceManager.
  30. * @param string $module Module name ("zikula" for system plugins).
  31. * @param string $pluginName Plugin name.
  32. * @param integer|null $caching Whether or not to cache (Zikula_View::CACHE_*) or use config variable (null).
  33. */
  34. public function __construct(ServiceManager $container, $module = 'zikula', $pluginName, $caching = null)
  35. {
  36. parent::__construct($container, $module, $caching);
  37. $this->pluginName = $pluginName;
  38. if ($this->modinfo['type'] == ModUtil::TYPE_CORE) {
  39. $path = "plugins/{$pluginName}/templates/plugins";
  40. } else {
  41. $base = ModUtil::getBaseDir($this->modinfo['name']);
  42. $path = "$base/{$this->modinfo['directory']}/plugins/{$pluginName}/templates/plugins";
  43. }
  44. $this->addPluginDir($path);
  45. }
  46. /**
  47. * Plugin name getter.
  48. *
  49. * @return string The plugin name.
  50. */
  51. public function getPluginName()
  52. {
  53. return $this->pluginName;
  54. }
  55. /**
  56. * Setup the current instance of the Zikula_View class and return it back to the module.
  57. *
  58. * @param string $moduleName Module name.
  59. * @param string $pluginName Plugin name.
  60. * @param integer|null $caching Whether or not to cache (Zikula_View::CACHE_*) or use config variable (null).
  61. * @param string $cache_id Cache Id.
  62. *
  63. * @return Zikula_View_Plugin instance.
  64. */
  65. public static function getPluginInstance($moduleName, $pluginName, $caching = null, $cache_id = null)
  66. {
  67. $container = ServiceUtil::getManager();
  68. $serviceId = strtolower(sprintf('zikula.renderplugin.%s.%s', $moduleName, $pluginName));
  69. if (!$container->has($serviceId)) {
  70. $view = new self($container, $moduleName, $pluginName, $caching);
  71. $container->set($serviceId, $view);
  72. } else {
  73. return $container->get($serviceId);
  74. }
  75. if (!is_null($caching)) {
  76. $view->caching = $caching;
  77. }
  78. if (!is_null($cache_id)) {
  79. $view->cache_id = $cache_id;
  80. }
  81. if ($moduleName === null) {
  82. $moduleName = $view->toplevelmodule;
  83. }
  84. if (!array_key_exists($moduleName, $view->module)) {
  85. $view->module[$moduleName] = ModUtil::getInfoFromName($moduleName);
  86. //$instance->modinfo = ModUtil::getInfoFromName($module);
  87. $view->_addPluginsDir($moduleName);
  88. }
  89. // for {gt} template plugin to detect gettext domain
  90. if ($view->module[$moduleName]['type'] == ModUtil::TYPE_MODULE || $view->module[$moduleName]['type'] == ModUtil::TYPE_SYSTEM) {
  91. $view->domain = ZLanguage::getModulePluginDomain($view->module[$moduleName]['name'], $view->getPluginName());
  92. } elseif ($view->module[$moduleName]['type'] == ModUtil::TYPE_CORE) {
  93. $view->domain = ZLanguage::getSystemPluginDomain($view->getPluginName());
  94. }
  95. return $view;
  96. }
  97. /**
  98. * Add a plugins dir to _plugin_dir property array.
  99. *
  100. * @param string $module Module name.
  101. * @param string $plugin Plugin name.
  102. *
  103. * @return void
  104. */
  105. private function _addPluginsDir($module, $plugin)
  106. {
  107. if (empty($module)) {
  108. return;
  109. }
  110. $modinfo = ModUtil::getInfoFromName($module);
  111. if (!$modinfo) {
  112. return;
  113. }
  114. switch ($modinfo['type']) {
  115. case ModUtil::TYPE_MODULE :
  116. $mpluginPath = "modules/" . $modinfo['directory'] . "/plugins/$plugin/Resources/views/plugins";
  117. if (!is_dir($mpluginPath)) {
  118. $mpluginPath = "modules/" . $modinfo['directory'] . "/plugins/$plugin/templates/plugins";
  119. }
  120. break;
  121. case ModUtil::TYPE_SYSTEM :
  122. $mpluginPath = "system/" . $modinfo['directory'] . "/plugins/$plugin/Rsources/view/plugins";
  123. if (!is_dir($mpluginPath)) {
  124. $mpluginPath = "system/" . $modinfo['directory'] . "/plugins/$plugin/templates/plugins";
  125. }
  126. break;
  127. case ModUtil::TYPE_CORE:
  128. $mpluginPath = "plugins/$plugin/Resources/views/plugins";
  129. if (!is_dir($mpluginPath)) {
  130. $mpluginPath = "plugins/$plugin/templates/plugins";
  131. }
  132. }
  133. $this->addPluginDir($mpluginsDir);
  134. }
  135. /**
  136. * Checks which path to use for required template.
  137. *
  138. * @param string $template Template name.
  139. *
  140. * @return string Template path.
  141. */
  142. public function get_template_path($template)
  143. {
  144. static $cache = array();
  145. if (isset($cache[$template])) {
  146. return $cache[$template];
  147. }
  148. // the current module
  149. //$modname = ModUtil::getName();
  150. foreach ($this->module as $module => $modinfo) {
  151. // prepare the values for OS
  152. $module = $modinfo['name'];
  153. $os_module = DataUtil::formatForOS($module);
  154. //$os_theme = DataUtil::formatForOS($this->theme);
  155. $os_dir = ($modinfo['type'] == ModUtil::TYPE_MODULE) ? 'modules' : 'system';
  156. $ostemplate = DataUtil::formatForOS($template);
  157. // check the module for which we're looking for a template is the
  158. // same as the top level mods. This limits the places to look for
  159. // templates.
  160. $base = ($modinfo['type'] == ModUtil::TYPE_CORE) ? '' : "$os_dir/$os_module/";
  161. //$configPath = ($modinfo['type'] == ModUtil::TYPE_CORE) ? 'zikula/' : "$os_module/";
  162. $search_path = array(
  163. //"config/plugins/$configPath/{$this->pluginName}/templates", //global path
  164. "{$base}plugins/{$this->pluginName}/templates"
  165. );
  166. foreach ($search_path as $path) {
  167. if (is_readable("$path/$ostemplate")) {
  168. $cache[$template] = $path;
  169. return $path;
  170. }
  171. }
  172. }
  173. // when we arrive here, no path was found
  174. return false;
  175. }
  176. }