Derivative of dojo container helper that provides control over which dojo components are rendered.
More...
List of all members.
Detailed Description
Derivative of dojo container helper that provides control over which dojo components are rendered.
- Copyright:
- 2011-2012 Perforce Software. All rights reserved
- License:
- Please see LICENSE.txt in top-level folder of this distribution.
- Version:
- 2012.2/486814
Member Function Documentation
P4Cms_View_Helper_Dojo_Container::__toString |
( |
| ) |
|
Extend toString to perform auto build if enabled.
P4Cms_View_Helper_Dojo_Container::_buildDojo |
( |
| ) |
[protected] |
Generate a single dojo javascript file containing:
- the dojo base
- module path registration
- all of the the required modules (recursively building dependencies)
Re-builds whenever a top-level module changes. Does not detect changes in indirectly required modules.
{
if (!$this->getAssetHandler()) {
P4Cms_Log::log(
"Failed to build dojo. Asset handler is unset.",
P4Cms_Log::ERR
);
return;
}
if (!$this->getDocumentRoot()) {
P4Cms_Log::log(
"Failed to build dojo. Document root is unset.",
P4Cms_Log::ERR
);
return;
}
$latest = 0;
$files = array();
$ignored = array();
foreach ($this->getModules() as $module) {
$file = $this->_moduleToFilename($module);
if (!file_exists($file)) {
$ignored[] = $module;
continue;
}
$time = filemtime($file);
$files[] = $file;
$latest = $time > $latest ? $time : $latest;
}
if (empty($files)) {
return;
}
$compressed = $this->_canGzipCompress() && $this->_clientAcceptsGzip();
$buildFile = 'dojo-' . md5(implode(',', $files) . $this->getLocale() . $latest)
. ($compressed ? '.jsgz' : '.js');
if (!$this->getAssetHandler()->exists($buildFile)) {
$built = array();
$base = $this->_buildModule("dojo", $built);
foreach ($this->getModulePaths() as $module => $path) {
$base .= 'dojo.registerModulePath("'
. $this->view->escape($module) . '", "'
. $this->view->escape($path) . '");';
}
$modules = "";
foreach ($this->getModules() as $module) {
$modules .= $this->_buildModule($module, $built);
}
$modules = $this->_makeConditional($modules, basename($buildFile));
$build = $base . $modules;
if ($compressed) {
$build = gzencode($build, 9);
}
if (!$this->getAssetHandler()->put($buildFile, $build)) {
return;
}
}
$this->_modules = $ignored;
$request = Zend_Controller_Front::getInstance()->getRequest();
$this->_buildUri = $this->getAssetHandler()->uri($buildFile);
$this->_built = true;
}
P4Cms_View_Helper_Dojo_Container::_buildModule |
( |
$ |
module, |
|
|
array &$ |
built = array() |
|
) |
| |
Recursive function to build a module and all of its dependencies.
Works by expanding dojo.require statements into the contents of the named module.
Note this method is public so that we can call it from an anonymous function. Consider it protected.
- Parameters:
-
string | $module | the name of the module to build. |
array | &$built | optional - by reference - list of modules already built. |
- Returns:
- string the resulting js.
{
$file = $this->_moduleToFilename($module);
if (!is_file($file)) {
return false;
}
if (!array_key_exists($module, $built)) {
$built[$module] = true;
} else {
return;
}
$build = file_get_contents($file);
$self = $this;
$build = preg_replace_callback(
'/d(?:ojo)?.requireLocalization\(([^)]+)\)\s*;?/i',
function($match) use (&$built, $self)
{
$args = str_getcsv($match[1]);
if (!isset($args[0], $args[1])) {
return $match[0];
}
$module = $args[0];
$bundle = $args[1];
$package = $module . '.nls.' . $bundle;
if (!array_key_exists($package, $built)) {
$built[$package] = true;
} else {
return $match[0];
}
$file = $self->getBestLocaleFile($module, $bundle);
if (!$file) {
return $match[0];
}
$locale = $self->getLocale();
$lang = reset(explode('_', $locale));
$data = file_get_contents($file);
$js = '(function(){'
. 'var data = ' . $data . ';'
. 'dojo.getObject("' . $package . '", true);'
. $package . ' = {'
. $locale . ': data,'
. $lang . ': data,'
. 'ROOT: data};'
. 'dojo.provide("' . $package . '");'
. 'dojo.provide("' . $package . '.' . $locale . '");'
. 'dojo.provide("' . $package . '.' . $lang . '");'
. 'dojo.provide("' . $package . '.ROOT");'
. '})();';
return $js . $match[0];
},
$build
);
$build = preg_replace_callback(
'/dojo.i18n._preloadLocalizations\([\'"]?([^\'")]+)[\'"]?[^)]*\)\s*;?/i',
function($match) use (&$built, $self)
{
$package = $match[1];
if (!array_key_exists($package, $built)) {
$built[$package] = true;
} else {
return $match[0];
}
$file = $self->getBestPreloadLocaleFile($package);
if (!$file) {
return $match[0];
}
return file_get_contents($file) . $match[0];
},
$build
);
$build = preg_replace_callback(
'/d(?:ojo)?.require\([\'"]?([^\'")]+)[\'"]?\)\s*;?/i',
function($match) use (&$built, $self)
{
$build = $self->_buildModule($match[1], $built);
if ($build !== false) {
return $build;
}
return $match[0];
},
$build
);
$build = $this->_makeConditional($build, $module);
return $build;
}
P4Cms_View_Helper_Dojo_Container::_canGzipCompress |
( |
| ) |
[protected] |
Check if this PHP can generate gzip compressed data.
- Returns:
- bool true if this PHP has gzip support.
{
return function_exists('gzencode');
}
P4Cms_View_Helper_Dojo_Container::_clientAcceptsGzip |
( |
| ) |
[protected] |
Check if the client can accept gzip encoded content.
- Returns:
- bool true if the client supports gzip; false otherwise.
{
$front = Zend_Controller_Front::getInstance();
$request = $front->getRequest();
$accepts = $request->getHeader('Accept-Encoding');
return strpos($accepts, 'gzip') !== false;
}
P4Cms_View_Helper_Dojo_Container::_makeConditional |
( |
$ |
js, |
|
|
$ |
module |
|
) |
| [protected] |
Wrap the given js in a hasResource check unless it already has one.
- Parameters:
-
string | $js | the js to wrap |
string | $module | the originating module. |
- Returns:
- string the conditional js.
{
if ($module == 'dojo') {
return "\nif (typeof dojo === 'undefined') {" . $js . "\n}";
}
$pattern = '/dojo._hasResource\[[\'"]' . $module . '[\'"]\]/';
if (preg_match($pattern, $js)) {
return $js;
}
return "\nif(!dojo._hasResource['$module']){"
. "dojo._hasResource['$module']=true;"
. $js
. "\n}";
}
P4Cms_View_Helper_Dojo_Container::_moduleToFilename |
( |
$ |
module | ) |
[protected] |
Attempt to determine the local filename for a given dojo module.
- Parameters:
-
string | $module | the name of the dojo module to get the filename for. |
- Returns:
- string the likely filename of the module.
{
$paths = $this->getModulePaths();
if ($module === 'dojo') {
return $this->getDocumentRoot() . $this->getLocalPath();
}
$basePath = dirname(dirname($this->getLocalPath()));
$paths['dojo'] = $basePath . '/dojo';
$paths['dijit'] = $basePath . '/dijit';
$paths['dojox'] = $basePath . '/dojox';
$path = null;
$parts = explode(".", $module);
$extra = array();
while ($parts && !$path) {
$key = implode(".", $parts);
if (isset($paths[$key])) {
$path = $paths[$key];
} else {
array_unshift($extra, array_pop($parts));
}
}
if (!$path) {
return null;
}
$file = $this->getDocumentRoot() . $path
. (count($extra) ? "/" . implode("/", $extra) : "")
. ".js";
return $file;
}
P4Cms_View_Helper_Dojo_Container::_renderDjConfig |
( |
| ) |
[protected] |
Don't render config if disabled and ensure the baseUrl gets set if we have a dojo build (as indicated by buildUri)
- Returns:
- string rendered dojo config.
{
if (!$this->_render['config']) {
return;
}
if ($this->_buildUri) {
$this->setDjConfigOption('baseUrl', dirname($this->getLocalPath()) . '/');
}
return parent::_renderDjConfig();
}
P4Cms_View_Helper_Dojo_Container::_renderDojoScriptTag |
( |
| ) |
[protected] |
Don't render dojo script tag if disabled or if we have a dojo build (as indicated by buildUri)
- Returns:
- string rendered dojo script tag.
P4Cms_View_Helper_Dojo_Container::_renderExtras |
( |
| ) |
[protected] |
Include dojo build in extras.
Don't render extras if disabled.
- Returns:
- string rendered dojo extras.
P4Cms_View_Helper_Dojo_Container::_renderLayers |
( |
| ) |
[protected] |
Don't render layers if disabled.
- Returns:
- string rendered dojo layers.
P4Cms_View_Helper_Dojo_Container::_renderStylesheets |
( |
| ) |
[protected] |
Don't render stylesheets if disabled.
- Returns:
- string rendered dojo stylesheets.
P4Cms_View_Helper_Dojo_Container::clearModulePaths |
( |
| ) |
|
P4Cms_View_Helper_Dojo_Container::clearModules |
( |
| ) |
|
P4Cms_View_Helper_Dojo_Container::clearOnLoad |
( |
| ) |
|
P4Cms_View_Helper_Dojo_Container::clearZendLoad |
( |
| ) |
|
P4Cms_View_Helper_Dojo_Container::getAssetHandler |
( |
| ) |
|
Get the asset handler used to store the aggregated dojo file.
- Returns:
- P4Cms_AssetHandlerInterface|null the handler to use or null
{
return $this->_assetHandler;
}
P4Cms_View_Helper_Dojo_Container::getBestLocaleFile |
( |
$ |
module, |
|
|
$ |
bundle |
|
) |
| |
Finds the closest matching package for the client's locale.
Searches for the exact locale, then language, then 'root'. For example, if the locale is 'en-us', looks for:
<module>/nls/en-us/<bundle>.js <module>/nls/en/<bundle>.js <module>/nls/<bundle>.js
- Parameters:
-
string | $module | the name of the dojo module to get a localization package for (e.g. 'dijit') |
string | $bundle | the name of the locale bundle to get (e.g. 'common') |
- Returns:
- string the filename of the best matching localizaton package
{
$locale = $this->getLocale(true);
$path = substr($this->_moduleToFilename($module), 0, -3) . '/nls/';
$attempts = array($locale . '/', reset(explode('-', $locale)) . '/', '');
foreach ($attempts as $attempt) {
$file = $path . $attempt . $bundle . '.js';
if (is_readable($file)) {
return $file;
}
}
return false;
}
P4Cms_View_Helper_Dojo_Container::getBestPreloadLocaleFile |
( |
$ |
package | ) |
|
Finds the best preloadable locale file for the client's locale.
Searches for the exact locale, then language, then 'root'. For example, if the locale is 'en-us', looks for:
<module>_en-us.js <module>_en.js <module>_ROOT.js
- Parameters:
-
string | $package | the name of the preloadable locale package (e.g. 'dojox.grid.nls.DataGrid') |
- Returns:
- string the filename of the best matching localizaton package
{
$locale = $this->getLocale(true);
$path = substr($this->_moduleToFilename($package), 0, -3);
$attempts = array($locale, reset(explode('-', $locale)), 'ROOT');
foreach ($attempts as $attempt) {
$file = $path . "_" . $attempt . '.js';
if (is_readable($file)) {
return $file;
}
}
return false;
}
P4Cms_View_Helper_Dojo_Container::getDocumentRoot |
( |
| ) |
|
Get the file-system path to the document root.
- Returns:
- string the location of the public folder.
{
return $this->_documentRoot;
}
P4Cms_View_Helper_Dojo_Container::getLocale |
( |
$ |
hyphenate = false | ) |
|
Get the client's (browser) locale - normalized to lower case because that is how dojo likes it.
- Parameters:
-
bool | $hyphenate | optional - use hyphen instead of underscore to separate the language from the territory (defaults to false). |
- Returns:
- string the client's locale string.
{
if (!$this->_locale) {
$this->_locale = strtolower(new Zend_Locale);
}
return $hyphenate
? str_replace('_', '-', $this->_locale)
: $this->_locale;
}
Set the asset handler used to store the aggregated dojo file.
- Parameters:
-
- Returns:
- P4Cms_View_Helper_Dojo_Container provides fluent interface.
{
$this->_assetHandler = $handler;
return $this;
}
P4Cms_View_Helper_Dojo_Container::setAutoBuild |
( |
$ |
build | ) |
|
Enable or disable automatic dojo builds.
- Parameters:
-
bool | $build | set to true to enable, false to disable. |
- Returns:
- P4Cms_View_Helper_Dojo_Container provides fluent interface.
{
$this->_build = (bool) $build;
return $this;
}
P4Cms_View_Helper_Dojo_Container::setDocumentRoot |
( |
$ |
path | ) |
|
Set the file-system path to the document root.
- Parameters:
-
string | $path | the location of the public folder. |
- Returns:
- P4Cms_View_Helper_Dojo_Container provides fluent interface.
{
$this->_documentRoot = rtrim($path, '/\\');
return $this;
}
P4Cms_View_Helper_Dojo_Container::setRender |
( |
$ |
elements | ) |
|
Set which dojo elements you want to be rendered.
- Parameters:
-
array | $elements | list of dojo elements to render (e.g. array('extras', 'layers')). valid elements are:
- config
- scriptTag
- extras
- layers
- stylesheets
|
- Returns:
- P4Cms_View_Helper_Dojo_Container provides fluent interface.
{
foreach ($this->_render as $element => $render) {
if (in_array($element, $elements)) {
$this->_render[$element] = true;
} else {
$this->_render[$element] = false;
}
}
return $this;
}
Member Data Documentation
P4Cms_View_Helper_Dojo_Container::$_assetHandler = null [protected] |
P4Cms_View_Helper_Dojo_Container::$_build = false [protected] |
P4Cms_View_Helper_Dojo_Container::$_buildUri = null [protected] |
P4Cms_View_Helper_Dojo_Container::$_built = false [protected] |
P4Cms_View_Helper_Dojo_Container::$_documentRoot = null [protected] |
P4Cms_View_Helper_Dojo_Container::$_locale = null [protected] |
P4Cms_View_Helper_Dojo_Container::$_render [protected] |
Initial value: array(
'config' => true,
'scriptTag' => true,
'extras' => true,
'layers' => true,
'stylesheets' => true
)
The documentation for this class was generated from the following file: