/src/classes/XLite/Module/CDev/DrupalConnector/Drupal/ADrupal.php

https://github.com/Koc/core · PHP · 254 lines · 74 code · 25 blank · 155 comment · 2 complexity · 21fc5c24dd1bb81d8eccd4402f27a563 MD5 · raw file

  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * PHP version 5.3.0
  17. *
  18. * @category LiteCommerce
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2011 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link http://www.litecommerce.com/
  23. * @see ____file_see____
  24. * @since 1.0.0
  25. */
  26. namespace XLite\Module\CDev\DrupalConnector\Drupal;
  27. /**
  28. * ADrupal
  29. *
  30. * @see ____class_see____
  31. * @since 1.0.0
  32. */
  33. abstract class ADrupal extends \XLite\Base\Singleton
  34. {
  35. /**
  36. * Initialized handler instance
  37. *
  38. * @var \XLite\Module\CDev\DrupalConnector\Handler
  39. * @see ____var_see____
  40. * @since 1.0.0
  41. */
  42. protected $handler;
  43. /**
  44. * Already registered resources
  45. *
  46. * @var array
  47. * @see ____var_see____
  48. * @since 1.0.0
  49. */
  50. protected static $registeredResources = array('js' => array(), 'css' => array());
  51. /**
  52. * Resources weight counter
  53. *
  54. * @var integer
  55. * @see ____var_see____
  56. * @since 1.0.0
  57. */
  58. protected static $resourcesCounter = 0;
  59. // ------------------------------ Application layer -
  60. /**
  61. * Return instance of current CMS connector
  62. *
  63. * @return \XLite\Module\CDev\DrupalConnector\Handler
  64. * @see ____func_see____
  65. * @since 1.0.0
  66. */
  67. protected function getHandler()
  68. {
  69. if (!isset($this->handler)) {
  70. $this->handler = \XLite\Module\CDev\DrupalConnector\Handler::getInstance();
  71. $this->handler->init();
  72. }
  73. return $this->handler;
  74. }
  75. /**
  76. * Execute a controller action
  77. *
  78. * @param string $target Controller target
  79. * @param string $action Action to perform OPTIONAL
  80. * @param array $data Request data OPTIONAL
  81. *
  82. * @return void
  83. * @see ____func_see____
  84. * @since 1.0.0
  85. */
  86. protected function runController($target, $action = null, array $data = array())
  87. {
  88. $data = array('target' => $target, 'action' => $action) + $data;
  89. $this->getHandler()->mapRequest(array(\XLite\Core\CMSConnector::NO_REDIRECT => true) + $data);
  90. $this->getHandler()->runController(md5(serialize($data)));
  91. }
  92. // ------------------------------ Resources (CSS and JS) -
  93. /**
  94. * Get resources (from list) which are not already registered
  95. *
  96. * @param string $type Resource type ("js" or "css")
  97. * @param array $files Resource files
  98. *
  99. * @return array
  100. * @see ____func_see____
  101. * @since 1.0.0
  102. */
  103. protected function getUniqueResources($type, array $files)
  104. {
  105. static::$registeredResources[$type] = array_merge(static::$registeredResources[$type], $files);
  106. return $files;
  107. }
  108. /**
  109. * Get JS scope
  110. *
  111. * @param string $file Resource file path
  112. *
  113. * @return string
  114. * @see ____func_see____
  115. * @since 1.0.0
  116. */
  117. protected function getJSScope($file)
  118. {
  119. return preg_match('/.skins.common.js./Ss', $file) ? 'header' : 'footer';
  120. }
  121. /**
  122. * Get file unique basename
  123. *
  124. * @param string $file Resource file path
  125. *
  126. * @return string
  127. * @see ____func_see____
  128. * @since 1.0.0
  129. */
  130. protected function getResourceBasename($file)
  131. {
  132. return preg_replace('/\.(css|js)$/Ss', '.' . uniqid() . '.$1', basename($file));
  133. }
  134. /**
  135. * Get resource description in Drupal format
  136. *
  137. * @param array $file Resource file info
  138. *
  139. * @return void
  140. * @see ____func_see____
  141. * @since 1.0.0
  142. */
  143. protected function getResourceInfoCommon(array $file)
  144. {
  145. return array(
  146. 'type' => 'file',
  147. 'basename' => $this->getResourceBasename($file['file']),
  148. 'weight' => isset($file['weight']) ? $file['weight'] : static::$resourcesCounter++,
  149. );
  150. }
  151. /**
  152. * Get resource description in Drupal format
  153. *
  154. * @param array $file Resource file info
  155. *
  156. * @return void
  157. * @see ____func_see____
  158. * @since 1.0.0
  159. */
  160. protected function getResourceInfoJS(array $file)
  161. {
  162. $scope = $this->getJSScope($file['file']);
  163. return array(
  164. 'scope' => $scope,
  165. 'defer' => ('footer' == $scope),
  166. );
  167. }
  168. /**
  169. * Get resource description in Drupal format
  170. *
  171. * @param array $file Resource file info
  172. *
  173. * @return void
  174. * @see ____func_see____
  175. * @since 1.0.0
  176. */
  177. protected function getResourceInfoCSS(array $file)
  178. {
  179. return array(
  180. 'group' => CSS_DEFAULT,
  181. 'media' => isset($file['media']) ? $file['media'] : 'all',
  182. );
  183. }
  184. /**
  185. * Get resource description in Drupal format
  186. *
  187. * @param string $type Resource type ("js" or "css")
  188. * @param array $file Resource file info
  189. *
  190. * @return void
  191. * @see ____func_see____
  192. * @since 1.0.0
  193. */
  194. protected function getResourceInfo($type, array $file)
  195. {
  196. return $this->getResourceInfoCommon($file) + $this->{__FUNCTION__ . strtoupper($type)}($file);
  197. }
  198. /**
  199. * Register single resource
  200. *
  201. * @param string $type Resource type ("js" or "css")
  202. * @param array $file Resource file info
  203. *
  204. * @return mixed
  205. * @see ____func_see____
  206. * @since 1.0.0
  207. */
  208. protected function registerResource($type, array $file)
  209. {
  210. return call_user_func_array('drupal_add_' . $type, array($file['file'], $this->getResourceInfo($type, $file)));
  211. }
  212. /**
  213. * Register LC widget resources
  214. *
  215. * @param \XLite\View\AView $widget LC widget to get resources list
  216. *
  217. * @return void
  218. * @see ____func_see____
  219. * @since 1.0.0
  220. */
  221. protected function registerResources(\XLite\View\AView $widget)
  222. {
  223. foreach ($widget->getRegisteredResources() as $type => $files) {
  224. foreach ($this->getUniqueResources($type, $files) as $file) {
  225. $this->registerResource($type, $file);
  226. }
  227. }
  228. }
  229. }