[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Dojo / BuildLayer.php
blobf85d805bbc90943ee3274033c09fcf7cec6aeed0
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Dojo
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 * @version $Id$
22 /**
23 * Dojo module layer and custom build profile generation support
25 * @package Zend_Dojo
26 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
27 * @license http://framework.zend.com/license/new-bsd New BSD License
29 class Zend_Dojo_BuildLayer
31 /**
32 * Flag: whether or not to consume JS aggregated in the dojo() view
33 * helper when generate the module layer contents
34 * @var bool
36 protected $_consumeJavascript = false;
38 /**
39 * Flag: whether or not to consume dojo.addOnLoad events registered
40 * with the dojo() view helper when generating the module layer file
41 * contents
42 * @var bool
44 protected $_consumeOnLoad = false;
46 /**
47 * Dojo view helper reference
48 * @var Zend_Dojo_View_Helper_Dojo_Container
50 protected $_dojo;
52 /**
53 * Name of the custom layer to generate
54 * @var string
56 protected $_layerName;
58 /**
59 * Path to the custom layer script relative to dojo.js (used when
60 * creating the build profile)
61 * @var string
63 protected $_layerScriptPath;
65 /**
66 * Build profile options
67 * @var array
69 protected $_profileOptions = array(
70 'action' => 'release',
71 'optimize' => 'shrinksafe',
72 'layerOptimize' => 'shrinksafe',
73 'copyTests' => false,
74 'loader' => 'default',
75 'cssOptimize' => 'comments',
78 /**
79 * Associative array of module/path pairs for the build profile
80 * @var array
82 protected $_profilePrefixes = array();
84 /**
85 * Zend_View reference
86 * @var Zend_View_Interface
88 protected $_view;
90 /**
91 * Constructor
93 * @param array|Zend_Config $options
94 * @return void
95 * @throws Zend_Dojo_Exception for invalid option argument
97 public function __construct($options = null)
99 if (null !== $options) {
100 if ($options instanceof Zend_Config) {
101 $options = $options->toArray();
102 } elseif (!is_array($options)) {
103 require_once 'Zend/Dojo/Exception.php';
104 throw new Zend_Dojo_Exception('Invalid options provided to constructor');
106 $this->setOptions($options);
111 * Set options
113 * Proxies to any setter that matches an option key.
115 * @param array $options
116 * @return Zend_Dojo_BuildLayer
118 public function setOptions(array $options)
120 $methods = get_class_methods($this);
121 foreach ($options as $key => $value) {
122 $method = 'set' . ucfirst($key);
123 if (in_array($method, $methods)) {
124 $this->$method($value);
127 return $this;
131 * Set View object
133 * @param Zend_View_Interface $view
134 * @return Zend_Dojo_BuildLayer
136 public function setView(Zend_View_Interface $view)
138 $this->_view = $view;
139 return $this;
143 * Retrieve view object
145 * @return Zend_View_Interface|null
147 public function getView()
149 return $this->_view;
153 * Set dojo() view helper instance
155 * @param Zend_Dojo_View_Helper_Dojo_Container $helper
156 * @return Zend_Dojo_BuildLayer
158 public function setDojoHelper(Zend_Dojo_View_Helper_Dojo_Container $helper)
160 $this->_dojo = $helper;
161 return $this;
165 * Retrieve dojo() view helper instance
167 * Will retrieve it from the view object if not registered.
169 * @return Zend_Dojo_View_Helper_Dojo_Container
170 * @throws Zend_Dojo_Exception if not registered and no view object found
172 public function getDojoHelper()
174 if (null === $this->_dojo) {
175 if (null === ($view = $this->getView())) {
176 require_once 'Zend/Dojo/Exception.php';
177 throw new Zend_Dojo_Exception('View object not registered; cannot retrieve dojo helper');
179 $helper = $view->getHelper('dojo');
180 $this->setDojoHelper($view->dojo());
182 return $this->_dojo;
186 * Set custom layer name; e.g. "custom.main"
188 * @param string $name
189 * @return Zend_Dojo_BuildLayer
191 public function setLayerName($name)
193 if (!preg_match('/^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$/i', $name)) {
194 require_once 'Zend/Dojo/Exception.php';
195 throw new Zend_Dojo_Exception('Invalid layer name provided; must be of form[a-z][a-z0-9_](\.[a-z][a-z0-9_])+');
197 $this->_layerName = $name;
198 return $this;
202 * Retrieve custom layer name
204 * @return string|null
206 public function getLayerName()
208 return $this->_layerName;
212 * Set the path to the custom layer script
214 * Should be a path relative to dojo.js
216 * @param string $path
217 * @return Zend_Dojo_BuildLayer
219 public function setLayerScriptPath($path)
221 $this->_layerScriptPath = (string) $path;
222 return $this;
226 * Get custom layer script path
228 * @return string|null
230 public function getLayerScriptPath()
232 return $this->_layerScriptPath;
236 * Set flag indicating whether or not to consume JS aggregated in dojo()
237 * view helper
239 * @param bool $flag
240 * @return Zend_Dojo_BuildLayer
242 public function setConsumeJavascript($flag)
244 $this->_consumeJavascript = (bool) $flag;
245 return $this;
249 * Get flag indicating whether or not to consume JS aggregated in dojo()
250 * view helper
252 * @return bool
254 public function consumeJavascript()
256 return $this->_consumeJavascript;
260 * Set flag indicating whether or not to consume dojo.addOnLoad events
261 * aggregated in dojo() view helper
263 * @param bool $flag
264 * @return Zend_Dojo_BuildLayer
266 public function setConsumeOnLoad($flag)
268 $this->_consumeOnLoad = (bool) $flag;
269 return $this;
273 * Get flag indicating whether or not to consume dojo.addOnLoad events aggregated in dojo() view helper
275 * @return bool
277 public function consumeOnLoad()
279 return $this->_consumeOnLoad;
283 * Set many build profile options at once
285 * @param array $options
286 * @return Zend_Dojo_BuildLayer
288 public function setProfileOptions(array $options)
290 $this->_profileOptions += $options;
291 return $this;
295 * Add many build profile options at once
297 * @param array $options
298 * @return Zend_Dojo_BuildLayer
300 public function addProfileOptions(array $options)
302 $this->_profileOptions = $this->_profileOptions + $options;
303 return $this;
307 * Add a single build profile option
309 * @param string $key
310 * @param value $value
311 * @return Zend_Dojo_BuildLayer
313 public function addProfileOption($key, $value)
315 $this->_profileOptions[(string) $key] = $value;
316 return $this;
320 * Is a given build profile option set?
322 * @param string $key
323 * @return bool
325 public function hasProfileOption($key)
327 return array_key_exists((string) $key, $this->_profileOptions);
331 * Retrieve a single build profile option
333 * Returns null if profile option does not exist.
335 * @param string $key
336 * @return mixed
338 public function getProfileOption($key)
340 if ($this->hasProfileOption($key)) {
341 return $this->_profileOptions[(string) $key];
343 return null;
347 * Get all build profile options
349 * @return array
351 public function getProfileOptions()
353 return $this->_profileOptions;
357 * Remove a build profile option
359 * @param string $name
360 * @return Zend_Dojo_BuildLayer
362 public function removeProfileOption($name)
364 if ($this->hasProfileOption($name)) {
365 unset($this->_profileOptions[(string) $name]);
367 return $this;
371 * Remove all build profile options
373 * @return Zend_Dojo_BuildLayer
375 public function clearProfileOptions()
377 $this->_profileOptions = array();
378 return $this;
382 * Add a build profile dependency prefix
384 * If just the prefix is passed, sets path to "../$prefix".
386 * @param string $prefix
387 * @param null|string $path
388 * @return Zend_Dojo_BuildLayer
390 public function addProfilePrefix($prefix, $path = null)
392 if (null === $path) {
393 $path = '../' . $prefix;
395 $this->_profilePrefixes[$prefix] = array($prefix, $path);
396 return $this;
400 * Set multiple dependency prefixes for bulid profile
402 * @param array $prefixes
403 * @return Zend_Dojo_BuildLayer
405 public function setProfilePrefixes(array $prefixes)
407 foreach ($prefixes as $prefix => $path) {
408 $this->addProfilePrefix($prefix, $path);
410 return $this;
414 * Get build profile dependency prefixes
416 * @return array
418 public function getProfilePrefixes()
420 $layerName = $this->getLayerName();
421 if (null !== $layerName) {
422 $prefix = $this->_getPrefix($layerName);
423 if (!array_key_exists($prefix, $this->_profilePrefixes)) {
424 $this->addProfilePrefix($prefix);
427 $view = $this->getView();
428 if (!empty($view)) {
429 $helper = $this->getDojoHelper();
430 if ($helper) {
431 $modules = $helper->getModules();
432 foreach ($modules as $module) {
433 $prefix = $this->_getPrefix($module);
434 if (!array_key_exists($prefix, $this->_profilePrefixes)) {
435 $this->addProfilePrefix($prefix);
440 return $this->_profilePrefixes;
444 * Generate module layer script
446 * @return string
448 public function generateLayerScript()
450 $helper = $this->getDojoHelper();
451 $layerName = $this->getLayerName();
452 $modulePaths = $helper->getModulePaths();
453 $modules = $helper->getModules();
454 $onLoadActions = $helper->getOnLoadActions();
455 $javascript = $helper->getJavascript();
457 $content = 'dojo.provide("' . $layerName . '");' . "\n\n(function(){\n";
459 foreach ($modulePaths as $module => $path) {
460 $content .= sprintf("dojo.registerModulePath(\"%s\", \"%s\");\n", $module, $path);
462 foreach ($modules as $module) {
463 $content .= sprintf("dojo.require(\"%s\");\n", $module);
466 if ($this->consumeOnLoad()) {
467 foreach ($helper->getOnLoadActions() as $callback) {
468 $content .= sprintf("dojo.addOnLoad(%s);\n", $callback);
471 if ($this->consumeJavascript()) {
472 $javascript = implode("\n", $helper->getJavascript());
473 if (!empty($javascript)) {
474 $content .= "\n" . $javascript . "\n";
478 $content .= "})();";
480 return $content;
484 * Generate build profile
486 * @return string
488 public function generateBuildProfile()
490 $profileOptions = $this->getProfileOptions();
491 $layerName = $this->getLayerName();
492 $layerScriptPath = $this->getLayerScriptPath();
493 $profilePrefixes = $this->getProfilePrefixes();
495 if (!array_key_exists('releaseName', $profileOptions)) {
496 $profileOptions['releaseName'] = substr($layerName, 0, strpos($layerName, '.'));
499 $profile = $profileOptions;
500 $profile['layers'] = array(array(
501 'name' => $layerScriptPath,
502 'layerDependencies' => array(),
503 'dependencies' => array($layerName),
505 $profile['prefixes'] = array_values($profilePrefixes);
507 return 'dependencies = ' . $this->_filterJsonProfileToJavascript($profile) . ';';
511 * Retrieve module prefix
513 * @param string $module
514 * @return void
516 protected function _getPrefix($module)
518 $segments = explode('.', $module, 2);
519 return $segments[0];
523 * Filter a JSON build profile to JavaScript
525 * @param string $profile
526 * @return string
528 protected function _filterJsonProfileToJavascript($profile)
530 require_once 'Zend/Json.php';
531 $profile = Zend_Json::encode($profile);
532 $profile = trim($profile, '"');
533 $profile = preg_replace('/' . preg_quote('\\') . '/', '', $profile);
534 return $profile;