PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/src/application/libraries/Zend/Dojo/BuildLayer.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 536 lines | 249 code | 45 blank | 242 comment | 18 complexity | df23d62e9a3b298bee591ccc34ab42a4 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Dojo
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: BuildLayer.php 23953 2011-05-03 05:47:39Z ralph $
  20. */
  21. /**
  22. * Dojo module layer and custom build profile generation support
  23. *
  24. * @package Zend_Dojo
  25. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. class Zend_Dojo_BuildLayer
  29. {
  30. /**
  31. * Flag: whether or not to consume JS aggregated in the dojo() view
  32. * helper when generate the module layer contents
  33. * @var bool
  34. */
  35. protected $_consumeJavascript = false;
  36. /**
  37. * Flag: whether or not to consume dojo.addOnLoad events registered
  38. * with the dojo() view helper when generating the module layer file
  39. * contents
  40. * @var bool
  41. */
  42. protected $_consumeOnLoad = false;
  43. /**
  44. * Dojo view helper reference
  45. * @var Zend_Dojo_View_Helper_Dojo_Container
  46. */
  47. protected $_dojo;
  48. /**
  49. * Name of the custom layer to generate
  50. * @var string
  51. */
  52. protected $_layerName;
  53. /**
  54. * Path to the custom layer script relative to dojo.js (used when
  55. * creating the build profile)
  56. * @var string
  57. */
  58. protected $_layerScriptPath;
  59. /**
  60. * Build profile options
  61. * @var array
  62. */
  63. protected $_profileOptions = array(
  64. 'action' => 'release',
  65. 'optimize' => 'shrinksafe',
  66. 'layerOptimize' => 'shrinksafe',
  67. 'copyTests' => false,
  68. 'loader' => 'default',
  69. 'cssOptimize' => 'comments',
  70. );
  71. /**
  72. * Associative array of module/path pairs for the build profile
  73. * @var array
  74. */
  75. protected $_profilePrefixes = array();
  76. /**
  77. * Zend_View reference
  78. * @var Zend_View_Interface
  79. */
  80. protected $_view;
  81. /**
  82. * Constructor
  83. *
  84. * @param array|Zend_Config $options
  85. * @return void
  86. * @throws Zend_Dojo_Exception for invalid option argument
  87. */
  88. public function __construct($options = null)
  89. {
  90. if (null !== $options) {
  91. if ($options instanceof Zend_Config) {
  92. $options = $options->toArray();
  93. } elseif (!is_array($options)) {
  94. require_once 'Zend/Dojo/Exception.php';
  95. throw new Zend_Dojo_Exception('Invalid options provided to constructor');
  96. }
  97. $this->setOptions($options);
  98. }
  99. }
  100. /**
  101. * Set options
  102. *
  103. * Proxies to any setter that matches an option key.
  104. *
  105. * @param array $options
  106. * @return Zend_Dojo_BuildLayer
  107. */
  108. public function setOptions(array $options)
  109. {
  110. $methods = get_class_methods($this);
  111. foreach ($options as $key => $value) {
  112. $method = 'set' . ucfirst($key);
  113. if (in_array($method, $methods)) {
  114. $this->$method($value);
  115. }
  116. }
  117. return $this;
  118. }
  119. /**
  120. * Set View object
  121. *
  122. * @param Zend_View_Interface $view
  123. * @return Zend_Dojo_BuildLayer
  124. */
  125. public function setView(Zend_View_Interface $view)
  126. {
  127. $this->_view = $view;
  128. return $this;
  129. }
  130. /**
  131. * Retrieve view object
  132. *
  133. * @return Zend_View_Interface|null
  134. */
  135. public function getView()
  136. {
  137. return $this->_view;
  138. }
  139. /**
  140. * Set dojo() view helper instance
  141. *
  142. * @param Zend_Dojo_View_Helper_Dojo_Container $helper
  143. * @return Zend_Dojo_BuildLayer
  144. */
  145. public function setDojoHelper(Zend_Dojo_View_Helper_Dojo_Container $helper)
  146. {
  147. $this->_dojo = $helper;
  148. return $this;
  149. }
  150. /**
  151. * Retrieve dojo() view helper instance
  152. *
  153. * Will retrieve it from the view object if not registered.
  154. *
  155. * @return Zend_Dojo_View_Helper_Dojo_Container
  156. * @throws Zend_Dojo_Exception if not registered and no view object found
  157. */
  158. public function getDojoHelper()
  159. {
  160. if (null === $this->_dojo) {
  161. if (null === ($view = $this->getView())) {
  162. require_once 'Zend/Dojo/Exception.php';
  163. throw new Zend_Dojo_Exception('View object not registered; cannot retrieve dojo helper');
  164. }
  165. $helper = $view->getHelper('dojo');
  166. $this->setDojoHelper($view->dojo());
  167. }
  168. return $this->_dojo;
  169. }
  170. /**
  171. * Set custom layer name; e.g. "custom.main"
  172. *
  173. * @param string $name
  174. * @return Zend_Dojo_BuildLayer
  175. */
  176. public function setLayerName($name)
  177. {
  178. if (!preg_match('/^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$/i', $name)) {
  179. require_once 'Zend/Dojo/Exception.php';
  180. throw new Zend_Dojo_Exception('Invalid layer name provided; must be of form[a-z][a-z0-9_](\.[a-z][a-z0-9_])+');
  181. }
  182. $this->_layerName = $name;
  183. return $this;
  184. }
  185. /**
  186. * Retrieve custom layer name
  187. *
  188. * @return string|null
  189. */
  190. public function getLayerName()
  191. {
  192. return $this->_layerName;
  193. }
  194. /**
  195. * Set the path to the custom layer script
  196. *
  197. * Should be a path relative to dojo.js
  198. *
  199. * @param string $path
  200. * @return Zend_Dojo_BuildLayer
  201. */
  202. public function setLayerScriptPath($path)
  203. {
  204. $this->_layerScriptPath = (string) $path;
  205. return $this;
  206. }
  207. /**
  208. * Get custom layer script path
  209. *
  210. * @return string|null
  211. */
  212. public function getLayerScriptPath()
  213. {
  214. return $this->_layerScriptPath;
  215. }
  216. /**
  217. * Set flag indicating whether or not to consume JS aggregated in dojo()
  218. * view helper
  219. *
  220. * @param bool $flag
  221. * @return Zend_Dojo_BuildLayer
  222. */
  223. public function setConsumeJavascript($flag)
  224. {
  225. $this->_consumeJavascript = (bool) $flag;
  226. return $this;
  227. }
  228. /**
  229. * Get flag indicating whether or not to consume JS aggregated in dojo()
  230. * view helper
  231. *
  232. * @return bool
  233. */
  234. public function consumeJavascript()
  235. {
  236. return $this->_consumeJavascript;
  237. }
  238. /**
  239. * Set flag indicating whether or not to consume dojo.addOnLoad events
  240. * aggregated in dojo() view helper
  241. *
  242. * @param bool $flag
  243. * @return Zend_Dojo_BuildLayer
  244. */
  245. public function setConsumeOnLoad($flag)
  246. {
  247. $this->_consumeOnLoad = (bool) $flag;
  248. return $this;
  249. }
  250. /**
  251. * Get flag indicating whether or not to consume dojo.addOnLoad events aggregated in dojo() view helper
  252. *
  253. * @return bool
  254. */
  255. public function consumeOnLoad()
  256. {
  257. return $this->_consumeOnLoad;
  258. }
  259. /**
  260. * Set many build profile options at once
  261. *
  262. * @param array $options
  263. * @return Zend_Dojo_BuildLayer
  264. */
  265. public function setProfileOptions(array $options)
  266. {
  267. $this->_profileOptions += $options;
  268. return $this;
  269. }
  270. /**
  271. * Add many build profile options at once
  272. *
  273. * @param array $options
  274. * @return Zend_Dojo_BuildLayer
  275. */
  276. public function addProfileOptions(array $options)
  277. {
  278. $this->_profileOptions = $this->_profileOptions + $options;
  279. return $this;
  280. }
  281. /**
  282. * Add a single build profile option
  283. *
  284. * @param string $key
  285. * @param value $value
  286. * @return Zend_Dojo_BuildLayer
  287. */
  288. public function addProfileOption($key, $value)
  289. {
  290. $this->_profileOptions[(string) $key] = $value;
  291. return $this;
  292. }
  293. /**
  294. * Is a given build profile option set?
  295. *
  296. * @param string $key
  297. * @return bool
  298. */
  299. public function hasProfileOption($key)
  300. {
  301. return array_key_exists((string) $key, $this->_profileOptions);
  302. }
  303. /**
  304. * Retrieve a single build profile option
  305. *
  306. * Returns null if profile option does not exist.
  307. *
  308. * @param string $key
  309. * @return mixed
  310. */
  311. public function getProfileOption($key)
  312. {
  313. if ($this->hasProfileOption($key)) {
  314. return $this->_profileOptions[(string) $key];
  315. }
  316. return null;
  317. }
  318. /**
  319. * Get all build profile options
  320. *
  321. * @return array
  322. */
  323. public function getProfileOptions()
  324. {
  325. return $this->_profileOptions;
  326. }
  327. /**
  328. * Remove a build profile option
  329. *
  330. * @param string $name
  331. * @return Zend_Dojo_BuildLayer
  332. */
  333. public function removeProfileOption($name)
  334. {
  335. if ($this->hasProfileOption($name)) {
  336. unset($this->_profileOptions[(string) $name]);
  337. }
  338. return $this;
  339. }
  340. /**
  341. * Remove all build profile options
  342. *
  343. * @return Zend_Dojo_BuildLayer
  344. */
  345. public function clearProfileOptions()
  346. {
  347. $this->_profileOptions = array();
  348. return $this;
  349. }
  350. /**
  351. * Add a build profile dependency prefix
  352. *
  353. * If just the prefix is passed, sets path to "../$prefix".
  354. *
  355. * @param string $prefix
  356. * @param null|string $path
  357. * @return Zend_Dojo_BuildLayer
  358. */
  359. public function addProfilePrefix($prefix, $path = null)
  360. {
  361. if (null === $path) {
  362. $path = '../' . $prefix;
  363. }
  364. $this->_profilePrefixes[$prefix] = array($prefix, $path);
  365. return $this;
  366. }
  367. /**
  368. * Set multiple dependency prefixes for bulid profile
  369. *
  370. * @param array $prefixes
  371. * @return Zend_Dojo_BuildLayer
  372. */
  373. public function setProfilePrefixes(array $prefixes)
  374. {
  375. foreach ($prefixes as $prefix => $path) {
  376. $this->addProfilePrefix($prefix, $path);
  377. }
  378. return $this;
  379. }
  380. /**
  381. * Get build profile dependency prefixes
  382. *
  383. * @return array
  384. */
  385. public function getProfilePrefixes()
  386. {
  387. $layerName = $this->getLayerName();
  388. if (null !== $layerName) {
  389. $prefix = $this->_getPrefix($layerName);
  390. if (!array_key_exists($prefix, $this->_profilePrefixes)) {
  391. $this->addProfilePrefix($prefix);
  392. }
  393. }
  394. $view = $this->getView();
  395. if (!empty($view)) {
  396. $helper = $this->getDojoHelper();
  397. if ($helper) {
  398. $modules = $helper->getModules();
  399. foreach ($modules as $module) {
  400. $prefix = $this->_getPrefix($module);
  401. if (!array_key_exists($prefix, $this->_profilePrefixes)) {
  402. $this->addProfilePrefix($prefix);
  403. }
  404. }
  405. }
  406. }
  407. return $this->_profilePrefixes;
  408. }
  409. /**
  410. * Generate module layer script
  411. *
  412. * @return string
  413. */
  414. public function generateLayerScript()
  415. {
  416. $helper = $this->getDojoHelper();
  417. $layerName = $this->getLayerName();
  418. $modulePaths = $helper->getModulePaths();
  419. $modules = $helper->getModules();
  420. $onLoadActions = $helper->getOnLoadActions();
  421. $javascript = $helper->getJavascript();
  422. $content = 'dojo.provide("' . $layerName . '");' . "\n\n(function(){\n";
  423. foreach ($modulePaths as $module => $path) {
  424. $content .= sprintf("dojo.registerModulePath(\"%s\", \"%s\");\n", $module, $path);
  425. }
  426. foreach ($modules as $module) {
  427. $content .= sprintf("dojo.require(\"%s\");\n", $module);
  428. }
  429. if ($this->consumeOnLoad()) {
  430. foreach ($helper->getOnLoadActions() as $callback) {
  431. $content .= sprintf("dojo.addOnLoad(%s);\n", $callback);
  432. }
  433. }
  434. if ($this->consumeJavascript()) {
  435. $javascript = implode("\n", $helper->getJavascript());
  436. if (!empty($javascript)) {
  437. $content .= "\n" . $javascript . "\n";
  438. }
  439. }
  440. $content .= "})();";
  441. return $content;
  442. }
  443. /**
  444. * Generate build profile
  445. *
  446. * @return string
  447. */
  448. public function generateBuildProfile()
  449. {
  450. $profileOptions = $this->getProfileOptions();
  451. $layerName = $this->getLayerName();
  452. $layerScriptPath = $this->getLayerScriptPath();
  453. $profilePrefixes = $this->getProfilePrefixes();
  454. if (!array_key_exists('releaseName', $profileOptions)) {
  455. $profileOptions['releaseName'] = substr($layerName, 0, strpos($layerName, '.'));
  456. }
  457. $profile = $profileOptions;
  458. $profile['layers'] = array(array(
  459. 'name' => $layerScriptPath,
  460. 'layerDependencies' => array(),
  461. 'dependencies' => array($layerName),
  462. ));
  463. $profile['prefixes'] = array_values($profilePrefixes);
  464. return 'dependencies = ' . $this->_filterJsonProfileToJavascript($profile) . ';';
  465. }
  466. /**
  467. * Retrieve module prefix
  468. *
  469. * @param string $module
  470. * @return void
  471. */
  472. protected function _getPrefix($module)
  473. {
  474. $segments = explode('.', $module, 2);
  475. return $segments[0];
  476. }
  477. /**
  478. * Filter a JSON build profile to JavaScript
  479. *
  480. * @param string $profile
  481. * @return string
  482. */
  483. protected function _filterJsonProfileToJavascript($profile)
  484. {
  485. require_once 'Zend/Json.php';
  486. $profile = Zend_Json::encode($profile);
  487. $profile = trim($profile, '"');
  488. $profile = preg_replace('/' . preg_quote('\\') . '/', '', $profile);
  489. return $profile;
  490. }
  491. }