Author: uncleringo
Date: 2010-01-21 13:41:11 +0100 (Thu, 21 Jan 2010)
New Revision: 26986
Added:
plugins/sfJSLibManagerPlugin/tags/sfJSLibManagerPlugin-1.0.3/
plugins/sfJSLibManagerPlugin/tags/sfJSLibManagerPlugin-1.0.3/LICENSE
plugins/sfJSLibManagerPlugin/tags/sfJSLibManagerPlugin-1.0.3/README
plugins/sfJSLibManagerPlugin/tags/sfJSLibManagerPlugin-1.0.3/lib/
plugins/sfJSLibManagerPlugin/tags/sfJSLibManagerPlugin-1.0.3/lib/sfJSLibManager.class.php
Log:
Tagging the 1.0.3 release
Copied: plugins/sfJSLibManagerPlugin/tags/sfJSLibManagerPlugin-1.0.3/LICENSE
(from rev 26984, plugins/sfJSLibManagerPlugin/trunk/LICENSE)
===================================================================
--- plugins/sfJSLibManagerPlugin/tags/sfJSLibManagerPlugin-1.0.3/LICENSE
(rev 0)
+++ plugins/sfJSLibManagerPlugin/tags/sfJSLibManagerPlugin-1.0.3/LICENSE
2010-01-21 12:41:11 UTC (rev 26986)
@@ -0,0 +1,19 @@
+Copyright (c) 2008 Rich Birch
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
\ No newline at end of file
Copied: plugins/sfJSLibManagerPlugin/tags/sfJSLibManagerPlugin-1.0.3/README
(from rev 26984, plugins/sfJSLibManagerPlugin/trunk/README)
===================================================================
--- plugins/sfJSLibManagerPlugin/tags/sfJSLibManagerPlugin-1.0.3/README
(rev 0)
+++ plugins/sfJSLibManagerPlugin/tags/sfJSLibManagerPlugin-1.0.3/README
2010-01-21 12:41:11 UTC (rev 26986)
@@ -0,0 +1,70 @@
+sfJSLibManagerPlugin
+==========================
+
+A simple plugin to manage the inclusion of a javascript library's relevant js &
+css files via a single call to a static method
+
+ sfJSLibManager::addLib('my_js_lib');
+
+
+Installation
+------------
+
+To install the plugin for a symfony project, the usual process is to use the
+symfony command line:
+
+ php symfony plugin:install sfJSLibManagerPlugin
+
+If the installation of either package fails then you can manually download the
+tgz file from
+
+
http://plugins.symfony-project.org/get/sfJSLibManagerPlugin/sfJSLibManagerPlugin-1.0.0.tgz
+
+and then ask symfony to install the downloaded file
+
+ php symfony plugin:install sfJSLibManagerPlugin-1.0.0.tgz
+
+Enable the plugin if necessary by editing
config/ProjectConfiguration.class.php:
+
+ $this->enablePlugins('sfJSLibManagerPlugin');
+
+
+Usage
+-----
+
+The plugin looks for a library's config which should be defined in
settings.yml.
+This specifies the location of the js & css files for a particular javascript
+library and should look something like the following:
+
+all:
+ .settings:
+ js_lib_jquery_ui:
+ dependencies: jquery # optional list of dependencies
+ web_dir: / # where to find the js & css directories
+ js_files: jquery-ui-1.7.2.custom.min.js # single file or array of
files
+ css_files: jquery-ui-1.7.2.custom.css # single file or array of
files
+
+The addLib() method will return true on success and false on failure (because
no
+settings were foudn for the requested library for example)
+
+Settings
+--------
+
+The settings should appear under a section labelled as the name of the library
+prefixed by js_lib_, and then name of the library is all that get's passed to
+the addLib() method. Eg:
+
+ sfJSLibManager::addLib('jquery_ui');
+
+dependencies - if dependencies are listed here then the plugin will attempt to
+load them before it loads the requested library. Failure to find config for a
+dependency will result in the library not being loaded, and the addLib() method
+will return false
+
+web_dir - tells the manager where to find the asset dirs (js & css) and it's
+relative to /web
+
+js_files - optional setting which lists one or more javascript files to include
+
+css_files - optional setting which lists one or more css files to include
+
Copied:
plugins/sfJSLibManagerPlugin/tags/sfJSLibManagerPlugin-1.0.3/lib/sfJSLibManager.class.php
(from rev 26984,
plugins/sfJSLibManagerPlugin/trunk/lib/sfJSLibManager.class.php)
===================================================================
---
plugins/sfJSLibManagerPlugin/tags/sfJSLibManagerPlugin-1.0.3/lib/sfJSLibManager.class.php
(rev 0)
+++
plugins/sfJSLibManagerPlugin/tags/sfJSLibManagerPlugin-1.0.3/lib/sfJSLibManager.class.php
2010-01-21 12:41:11 UTC (rev 26986)
@@ -0,0 +1,232 @@
+<?php
+
+class sfJSLibManager {
+
+ private static $libs_loaded = array();
+
+ public static function getStylesheets($lib_name, array $lib_config = null)
+ {
+ return sfJSLibManager::getLibAssets($lib_name, $lib_config, 'css');
+ }
+
+ public static function getJavascripts($lib_name, array $lib_config = null)
+ {
+ return sfJSLibManager::getLibAssets($lib_name, $lib_config, 'js');
+ }
+
+ private static function getLibAssets($lib_name, array $lib_config = null,
$asset_type)
+ {
+ $assets = array();
+
+ if (is_null($lib_config))
+ {
+ $lib_config = sfConfig::get('sf_js_lib_' . $lib_name, false);
+ }
+
+ if ($lib_config)
+ {
+ if (!empty($lib_config['dependencies']))
+ {
+ $assets = sfJSLibManager::getDependencies($lib_config['dependencies'],
$asset_type);
+ }
+
+ $assets = array_merge($assets, sfJSLibManager::getAssets($lib_config,
$asset_type));
+ }
+
+ return $assets;
+ }
+
+ /**
+ *
+ * @param string $lib_name
+ * @param array $lib_config
+ * @return boolean success or failure
+ *
+ * First tries to load an dependency lib's asset files before trying to load
+ * any javascript & css asset files as specified in the library's settings
+ *
+ */
+ public static function addLib($lib_name, array $lib_config = null)
+ {
+ if (is_null($lib_config))
+ {
+ $lib_config = sfConfig::get('sf_js_lib_' . $lib_name, false);
+ }
+
+ /**
+ * Return if we don't find any settings for the library
+ */
+
+ if (!$lib_config)
+ {
+ return false;
+ }
+
+ if (sfJSLibManager::isLoaded($lib_name, $lib_config))
+ {
+ return true;
+ }
+
+ if (!empty($lib_config['dependencies']))
+ {
+ if (!sfJSLibManager::addDependencies($lib_config['dependencies']))
+ {
+ return false;
+ }
+ }
+
+ if (!sfJSLibManager::addAssets($lib_config))
+ {
+ return false;
+ }
+
+ sfJSLibManager::addLoaded($lib_name, $lib_config);
+
+ return true;
+ }
+
+ private static function libToString($lib_name, $lib_config)
+ {
+ return $lib_name . '_' . serialize($lib_config);
+ }
+
+ /**
+ *
+ * @param string $loaded_name
+ * @return boolean true if the
+ */
+ public static function isLoaded($lib_name, $lib_config)
+ {
+ $lib_string = sfJSLibManager::libToString($lib_name, $lib_config);
+
+ return in_array($lib_string, self::$libs_loaded);
+ }
+
+ private static function addLoaded($lib_name, $lib_config)
+ {
+ $lib_string = sfJSLibManager::libToString($lib_name, $lib_config);
+
+ return self::$libs_loaded[] = $lib_string;
+ }
+
+ /**
+ *
+ * @param array $lib_config
+ *
+ * Load any assets specified in the assoc $lib_config array
+ * (type => loadMethod)
+ *
+ */
+ private static function getAssets($lib_config, $asset_type)
+ {
+ $assets = array();
+
+ $setting = $asset_type . '_files';
+
+ if (!empty($lib_config[$setting]))
+ {
+ if (!is_array($lib_config[$setting]))
+ {
+ $lib_config[$setting] = array($lib_config[$setting]);
+ }
+
+ foreach ($lib_config[$setting] as $asset_file)
+ {
+ $assets[] = $lib_config['web_dir'] . '/' . $asset_type . '/' .
$asset_file;
+ }
+ }
+
+ return $assets;
+ }
+
+ /**
+ *
+ * @param array $lib_config
+ *
+ * Load any assets specified in the assoc $lib_config array
+ * (type => loadMethod)
+ *
+ */
+ private static function addAssets($lib_config)
+ {
+ $response = sfContext::getInstance()->getResponse();
+
+ /**
+ * Loop through the specified javascript & css files and add them to the
+ * response
+ */
+
+ $asset_types = array(
+ 'js' => 'addJavascript',
+ 'css' => 'addStyleSheet',
+ );
+
+ foreach ($asset_types as $asset_type => $method)
+ {
+ $setting = $asset_type . '_files';
+
+ if (empty($lib_config[$setting]))
+ {
+ continue;
+ }
+
+ if (!is_array($lib_config[$setting]))
+ {
+ $lib_config[$setting] = array($lib_config[$setting]);
+ }
+
+ foreach ($lib_config[$setting] as $asset_file)
+ {
+ $response->$method($lib_config['web_dir'] . '/' . $asset_type . '/' .
$asset_file);
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ *
+ * @param mixed $dependencies
+ * @return array
+ */
+ private static function getDependencies($dependencies, $asset_type)
+ {
+ $assets = array();
+
+ if (!is_array($dependencies))
+ {
+ $dependencies = array($dependencies);
+ }
+
+ foreach ($dependencies as $dependency)
+ {
+ $assets = array_merge($assets,
sfJSLibManager::getStylesheets($dependency, 'css'));
+ }
+
+ return $assets;
+ }
+
+ /**
+ *
+ * @param mixed $dependencies
+ * @return boolean success or failure
+ */
+ private static function addDependencies($dependencies)
+ {
+
+ if (!is_array($dependencies))
+ {
+ $dependencies = array($dependencies);
+ }
+
+ foreach ($dependencies as $dependency)
+ {
+ if (!sfJSLibManager::addLib($dependency))
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}
\ No newline at end of file
--
You received this message because you are subscribed to the Google Groups
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/symfony-svn?hl=en.