From: <gem...@li...> - 2012-06-15 10:30:31
|
Revision: 767 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=767&view=rev Author: mennodekker Date: 2012-06-15 10:30:24 +0000 (Fri, 15 Jun 2012) Log Message: ----------- Load snippets via Gems_Loader, allowing prefixed snippets. Added some backward compatibility to loader: no prefix with multiple dirs is handled fine now. The backup to defer to the Mutil_Snippets_SnippetLoader will throw an error but that should not happen. Modified Paths: -------------- trunk/library/classes/Gems/Controller/Action.php trunk/library/classes/Gems/Controller/ModelActionAbstract.php trunk/library/classes/Gems/Controller/ModelSnippetActionAbstract.php trunk/library/classes/Gems/Loader/LoaderAbstract.php trunk/library/classes/Gems/Loader.php Added Paths: ----------- trunk/library/classes/Gems/Snippets/SnippetLoader.php Modified: trunk/library/classes/Gems/Controller/Action.php =================================================================== --- trunk/library/classes/Gems/Controller/Action.php 2012-06-15 07:47:40 UTC (rev 766) +++ trunk/library/classes/Gems/Controller/Action.php 2012-06-15 10:30:24 UTC (rev 767) @@ -1,34 +1,34 @@ <?php - -/** - * Copyright (c) 2011, Erasmus MC - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of Erasmus MC nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - + /** + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** * File descr * * @author Matijs de Jong <mj...@ma...> @@ -79,4 +79,13 @@ parent::initHtml(); } + + /** + * Stub for overruling default snippet loader initiation. + */ + protected function loadSnippetLoader() + { + // Create the snippet with this controller as the parameter source + $this->snippetLoader = $this->loader->getSnippetLoader($this); + } } \ No newline at end of file Modified: trunk/library/classes/Gems/Controller/ModelActionAbstract.php =================================================================== --- trunk/library/classes/Gems/Controller/ModelActionAbstract.php 2012-06-15 07:47:40 UTC (rev 766) +++ trunk/library/classes/Gems/Controller/ModelActionAbstract.php 2012-06-15 10:30:24 UTC (rev 767) @@ -218,4 +218,13 @@ parent::initHtml($reset); } + + /** + * Stub for overruling default snippet loader initiation. + */ + protected function loadSnippetLoader() + { + // Create the snippet with this controller as the parameter source + $this->snippetLoader = $this->loader->getSnippetLoader($this); + } } Modified: trunk/library/classes/Gems/Controller/ModelSnippetActionAbstract.php =================================================================== --- trunk/library/classes/Gems/Controller/ModelSnippetActionAbstract.php 2012-06-15 07:47:40 UTC (rev 766) +++ trunk/library/classes/Gems/Controller/ModelSnippetActionAbstract.php 2012-06-15 10:30:24 UTC (rev 767) @@ -406,6 +406,15 @@ } /** + * Stub for overruling default snippet loader initiation. + */ + protected function loadSnippetLoader() + { + // Create the snippet with this controller as the parameter source + $this->snippetLoader = $this->loader->getSnippetLoader($this); + } + + /** * Action for showing an item page with title */ public function showAction() Modified: trunk/library/classes/Gems/Loader/LoaderAbstract.php =================================================================== --- trunk/library/classes/Gems/Loader/LoaderAbstract.php 2012-06-15 07:47:40 UTC (rev 766) +++ trunk/library/classes/Gems/Loader/LoaderAbstract.php 2012-06-15 10:30:24 UTC (rev 767) @@ -73,7 +73,7 @@ * * @var array Of prefix => path strings for class lookup */ - private $_dirs; + protected $_dirs; /** * This array holds a cache of requested class -> resulting classname pairs so we don't have @@ -159,7 +159,7 @@ { // echo $name . ($create ? ' create' : ' not created') . "<br/>\n"; - $cname = '_' . trim(str_replace('/', '_', ucfirst($name)), '_'); + $cname = trim(str_replace('/', '_', ucfirst($name)), '_'); $cfile = str_replace('_', '/', $cname) . '.php'; $found = false; @@ -173,13 +173,24 @@ } if (!$found) { - foreach ($this->_dirs as $prefix => $path) { - $fprefix = str_replace('_', '/', $prefix); - if ($obj = $this->_loadClassPath($path . '/' . $fprefix . $cfile, $prefix . $cname, $create, $arguments)) { - $found = true; - $this->_loaded[$cname] = get_class($obj); - break; + foreach ($this->_dirs as $prefix => $paths) { + if (!empty($prefix)) { + $fprefix = '/' . str_replace('_', '/', $prefix); + $prefix .= '_'; + } else { + $fprefix = ''; } + + if (!is_array($paths)) { + $paths = array($paths); + } + foreach ($paths as $path) { + if ($obj = $this->_loadClassPath($path . $fprefix . '/' . $cfile, $prefix . $cname, $create, $arguments)) { + $found = true; + $this->_loaded[$cname] = get_class($obj); + break 2; + } + } } } @@ -192,7 +203,9 @@ return $obj; } - //print_r($this->_dirs); + + // Throw exception when not found + throw new Gems_Exception_Coding(__CLASS__ . '->' . __FUNCTION__ . ' cannot find class with name ' .$name . ' in ' . print_r($this->_dirs, true)); } /** Modified: trunk/library/classes/Gems/Loader.php =================================================================== --- trunk/library/classes/Gems/Loader.php 2012-06-15 07:47:40 UTC (rev 766) +++ trunk/library/classes/Gems/Loader.php 2012-06-15 10:30:24 UTC (rev 767) @@ -83,6 +83,12 @@ /** * + * @var Gems_Snippets_SnippetLoader + */ + protected $snippetLoader; + + /** + * * @var Gems_Tracker */ protected $tracker; @@ -202,6 +208,19 @@ } /** + * + * @return Gems_Snippets_SnippetLoader + */ + public function getSnippetLoader($container) + { + $class = $this->_getClass('snippetLoader', 'Snippets_SnippetLoader'); + + //now add the calling class as a container + $class->addRegistryContainer($container); + return $class; + } + + /** * * @return Gems_Task_TaskAbstract */ Added: trunk/library/classes/Gems/Snippets/SnippetLoader.php =================================================================== --- trunk/library/classes/Gems/Snippets/SnippetLoader.php (rev 0) +++ trunk/library/classes/Gems/Snippets/SnippetLoader.php 2012-06-15 10:30:24 UTC (rev 767) @@ -0,0 +1,117 @@ +<?php + +/** + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Short description of file + * + * @package Gems + * @subpackage + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id: Sample.php 215 2011-07-12 08:52:54Z michiel $ + */ + +/** + * Short description for SnippetLoader + * + * Long description for class SnippetLoader (if any)... + * + * @package Gems + * @subpackage Sample + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.0 + * @deprecated Class deprecated since version 2.0 + */ +class Gems_Snippets_SnippetLoader extends Gems_Loader_TargetLoaderAbstract implements MUtil_Snippets_SnippetLoaderInterface +{ + protected $cascade = 'Snippets'; + + protected $loader; + + /** + * @var MUtil_Snippets_SnippetLoader + */ + protected $backup; + + public function __construct($container = null, $dirs = array()) { + parent::__construct($container, $dirs); + $this->backup = new MUtil_Snippets_SnippetLoader($this); + } + + + public function addDirectory($dir) + { + if (!array_key_exists('', $this->_dirs)) { + $this->_dirs[''] = array(); + } + array_unshift($this->_dirs[''], $dir); + + $this->backup->addDirectory($dir); + } + + public function addSource($container_or_pairs) + { + $this->backup->addSource($container_or_pairs); + } + + public function getDirectories() + { + $this->backup->getDirectories(); + } + + public function getSnippet($filename, array $extraSourceParameters = null) + { + try { + $this->addRegistryContainer($extraSourceParameters, 'tmpContainer'); + $snippet = $this->_loadClass($filename, true); + $this->removeRegistryContainer('tmpContainer'); + } catch (Exception $exc) { + MUtil_Echo::track($exc->getMessage()); + throwException($exc); + //Class loading failed, now defer + //$snippet = $this->backup->getSnippet($filename, $extraSourceParameters); + } + + return $snippet; + } + + public function getSource() + { + $this->backup->getSource(); + } + + public function setDirectories(array $dirs) + { + $this->backup->setDirectories($dirs); + } + + public function setSource(MUtil_Registry_SourceInterface $source) + { + $this->backup->setSource($source); + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |