BuildLayer.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. class Zend_Dojo_BuildLayer
  3. {
  4. protected $_consumeJavascript = false;
  5. protected $_consumeOnLoad = false;
  6. protected $_dojo;
  7. protected $_layerName;
  8. protected $_layerScriptPath;
  9. protected $_profileOptions = array(
  10. 'action' => 'release',
  11. 'optimize' => 'shrinksafe',
  12. 'layerOptimize' => 'shrinksafe',
  13. 'copyTests' => false,
  14. 'loader' => 'default',
  15. 'cssOptimize' => 'comments',
  16. );
  17. protected $_profilePath;
  18. protected $_profilePrefixes = array();
  19. protected $_view;
  20. public function __construct($options = null)
  21. {
  22. if (null !== $options) {
  23. if ($options instanceof Zend_Config) {
  24. $options = $options->toArray();
  25. } elseif (!is_array($options)) {
  26. require_once 'Zend/Dojo/Exception.php';
  27. throw new Zend_Dojo_Exception('Invalid options provided to constructor');
  28. }
  29. $this->setOptions($options);
  30. }
  31. }
  32. public function setOptions(array $options)
  33. {
  34. $methods = get_class_methods($this);
  35. foreach ($options as $key => $value) {
  36. $method = 'set' . ucfirst($key);
  37. if (in_array($method, $methods)) {
  38. $this->$method($value);
  39. }
  40. }
  41. return $this;
  42. }
  43. public function setView(Zend_View_Interface $view)
  44. {
  45. $this->_view = $view;
  46. return $this;
  47. }
  48. public function getView()
  49. {
  50. return $this->_view;
  51. }
  52. public function setDojoHelper(Zend_Dojo_View_Helper_Dojo_Container $helper)
  53. {
  54. $this->_dojo = $helper;
  55. return $this;
  56. }
  57. public function getDojoHelper()
  58. {
  59. if (null === $this->_dojo) {
  60. if (null === ($view = $this->getView())) {
  61. require_once 'Zend/Dojo/Exception.php';
  62. throw new Zend_Dojo_Exception('View object not registered; cannot retrieve dojo helper');
  63. }
  64. $helper = $view->getHelper('dojo');
  65. $this->setDojoHelper($view->dojo());
  66. }
  67. return $this->_dojo;
  68. }
  69. public function setLayerName($name)
  70. {
  71. if (!preg_match('/^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$/i', $name)) {
  72. require_once 'Zend/Dojo/Exception.php';
  73. throw new Zend_Dojo_Exception('Invalid layer name provided; must be of form[a-z][a-z0-9_](\.[a-z][a-z0-9_])+');
  74. }
  75. $this->_layerName = $name;
  76. return $this;
  77. }
  78. public function getLayerName()
  79. {
  80. return $this->_layerName;
  81. }
  82. public function setLayerScriptPath($path)
  83. {
  84. $this->_layerScriptPath = (string) $path;
  85. return $this;
  86. }
  87. public function getLayerScriptPath()
  88. {
  89. return $this->_layerScriptPath;
  90. }
  91. public function setProfilePath($path)
  92. {
  93. $this->_profilePath = (string) $path;
  94. return $this;
  95. }
  96. public function getProfilePath()
  97. {
  98. return $this->_profilePath;
  99. }
  100. public function setConsumeJavascript($flag)
  101. {
  102. $this->_consumeJavascript = (bool) $flag;
  103. return $this;
  104. }
  105. public function consumeJavascript()
  106. {
  107. return $this->_consumeJavascript;
  108. }
  109. public function setConsumeOnLoad($flag)
  110. {
  111. $this->_consumeOnLoad = (bool) $flag;
  112. return $this;
  113. }
  114. public function consumeOnLoad()
  115. {
  116. return $this->_consumeOnLoad;
  117. }
  118. public function setProfileOptions(array $options)
  119. {
  120. $this->_profileOptions += $options;
  121. return $this;
  122. }
  123. public function addProfileOptions(array $options)
  124. {
  125. $this->_profileOptions = $this->_profileOptions + $options;
  126. return $this;
  127. }
  128. public function addProfileOption($key, $value)
  129. {
  130. $this->_profileOptions[(string) $key] = $value;
  131. return $this;
  132. }
  133. public function hasProfileOption($key)
  134. {
  135. return array_key_exists((string) $key, $this->_profileOptions);
  136. }
  137. public function getProfileOption($key)
  138. {
  139. if ($this->hasProfileOption($key)) {
  140. return $this->_profileOptions[(string) $key];
  141. }
  142. return null;
  143. }
  144. public function getProfileOptions()
  145. {
  146. return $this->_profileOptions;
  147. }
  148. public function removeProfileOption($name)
  149. {
  150. if ($this->hasProfileOption($name)) {
  151. unset($this->_profileOptions[(string) $name]);
  152. }
  153. return $this;
  154. }
  155. public function clearProfileOptions()
  156. {
  157. $this->_profileOptions = array();
  158. return $this;
  159. }
  160. public function addProfilePrefix($prefix, $path = null)
  161. {
  162. if (null === $path) {
  163. $path = '../' . $prefix;
  164. }
  165. $this->_profilePrefixes[$prefix] = array($prefix, $path);
  166. return $this;
  167. }
  168. public function setProfilePrefixes(array $prefixes)
  169. {
  170. foreach ($prefixes as $prefix => $path) {
  171. $this->addProfilePrefix($prefix, $path);
  172. }
  173. return $this;
  174. }
  175. public function getProfilePrefixes()
  176. {
  177. $layerName = $this->getLayerName();
  178. if (null !== $layerName) {
  179. $prefix = $this->_getPrefix($layerName);
  180. if (!array_key_exists($prefix, $this->_profilePrefixes)) {
  181. $this->addProfilePrefix($prefix);
  182. }
  183. }
  184. $view = $this->getView();
  185. if (!empty($view)) {
  186. $helper = $this->getDojoHelper();
  187. if ($helper) {
  188. $modules = $helper->getModules();
  189. foreach ($modules as $module) {
  190. $prefix = $this->_getPrefix($module);
  191. if (!array_key_exists($prefix, $this->_profilePrefixes)) {
  192. $this->addProfilePrefix($prefix);
  193. }
  194. }
  195. }
  196. }
  197. return $this->_profilePrefixes;
  198. }
  199. public function generateLayerScript()
  200. {
  201. $helper = $this->getDojoHelper();
  202. $layerName = $this->getLayerName();
  203. $modulePaths = $helper->getModulePaths();
  204. $modules = $helper->getModules();
  205. $onLoadActions = $helper->getOnLoadActions();
  206. $javascript = $helper->getJavascript();
  207. $content = 'dojo.provide("' . $layerName . '");' . "\n\n(function(){\n";
  208. foreach ($modulePaths as $module => $path) {
  209. $content .= sprintf("dojo.registerModulePath(\"%s\", \"%s\");\n", $module, $path);
  210. }
  211. foreach ($modules as $module) {
  212. $content .= sprintf("dojo.require(\"%s\");\n", $module);
  213. }
  214. if ($this->consumeOnLoad()) {
  215. foreach ($helper->getOnLoadActions() as $callback) {
  216. $content .= sprintf("dojo.addOnLoad(%s);\n", $callback);
  217. }
  218. }
  219. if ($this->consumeJavascript()) {
  220. $javascript = implode("\n", $helper->getJavascript());
  221. if (!empty($javascript)) {
  222. $content .= "\n" . $javascript . "\n";
  223. }
  224. }
  225. $content .= "})();";
  226. return $content;
  227. }
  228. public function generateBuildProfile()
  229. {
  230. $profileOptions = $this->getProfileOptions();
  231. $layerName = $this->getLayerName();
  232. $layerScriptPath = $this->getLayerScriptPath();
  233. $profilePrefixes = $this->getProfilePrefixes();
  234. if (!array_key_exists('releaseName', $profileOptions)) {
  235. $profileOptions['releaseName'] = substr($layerName, 0, strpos($layerName, '.'));
  236. }
  237. $profile = $profileOptions;
  238. $profile['layers'] = array(array(
  239. 'name' => $layerScriptPath,
  240. 'layerDependencies' => array(),
  241. 'dependencies' => array($layerName),
  242. ));
  243. $profile['prefixes'] = array_values($profilePrefixes);
  244. return 'dependencies = ' . $this->_filterJsonProfileToJavascript($profile) . ';';
  245. }
  246. protected function _getPrefix($module)
  247. {
  248. $segments = explode('.', $module, 2);
  249. return $segments[0];
  250. }
  251. protected function _filterJsonProfileToJavascript($profile)
  252. {
  253. require_once 'Zend/Json.php';
  254. $profile = Zend_Json::encode($profile);
  255. $profile = preg_replace('/"([^"]*)":/', '$1:', $profile);
  256. $profile = preg_replace('/' . preg_quote('\\') . '/', '', $profile);
  257. return $profile;
  258. }
  259. }