Commits

ad...@admin-PC  committed 2fd23da

Introduce ZendY_Registry_Toolkit

  • Participants

Comments (0)

Files changed (13)

+\.orig$
+\.orig\..*$
+\.chg\..*$
+\.rej$
+\.conflict\~$
+<?php
+if (!defined('PATH_SEPARATOR')) {
+    define('PATH_SEPARATOR', getenv('COMSPEC') ? ';' : ':');
+}
+ini_set(
+    'include_path',
+    ini_get('include_path')
+        . PATH_SEPARATOR . dirname(__FILE__) . '/library'
+);
+
+function __autoload($class)
+{
+    require str_replace('_', '/', $class) . '.php';
+}

File library/AnotherTool.php

+<?php
+class AnotherTool extends ZendY_Registry_Tool
+{
+    public function anotherTool()
+    {
+        return __CLASS__;
+    }
+}

File library/TestTool.php

+<?php
+class TestTool extends ZendY_Registry_Tool
+{
+    public function testTool()
+    {
+        return __CLASS__;
+    }
+}

File library/Zend/Debug.php

+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Debug
+ * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+/**
+ * Concrete class for generating debug dumps related to the output source.
+ *
+ * @category   Zend
+ * @package    Zend_Debug
+ * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+class Zend_Debug
+{
+
+    /**
+     * @var string
+     */
+    protected static $_sapi = null;
+
+    /**
+     * Get the current value of the debug output environment.
+     * This defaults to the value of PHP_SAPI.
+     *
+     * @return string;
+     */
+    public static function getSapi()
+    {
+        if (self::$_sapi === null) {
+            self::$_sapi = PHP_SAPI;
+        }
+        return self::$_sapi;
+    }
+
+    /**
+     * Set the debug ouput environment.
+     * Setting a value of null causes Zend_Debug to use PHP_SAPI.
+     *
+     * @param string $sapi
+     * @return void;
+     */
+    public static function setSapi($sapi)
+    {
+        self::$_sapi = $sapi;
+    }
+
+    /**
+     * Debug helper function.  This is a wrapper for var_dump() that adds
+     * the <pre /> tags, cleans up newlines and indents, and runs
+     * htmlentities() before output.
+     *
+     * @param  mixed  $var   The variable to dump.
+     * @param  string $label OPTIONAL Label to prepend to output.
+     * @param  bool   $echo  OPTIONAL Echo output if true.
+     * @return string
+     */
+    public static function dump($var, $label=null, $echo=true)
+    {
+        // format the label
+        $label = ($label===null) ? '' : rtrim($label) . ' ';
+
+        // var_dump the variable into a buffer and keep the output
+        ob_start();
+        var_dump($var);
+        $output = ob_get_clean();
+
+        // neaten the newlines and indents
+        $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
+        if (self::getSapi() == 'cli') {
+            $output = PHP_EOL . $label
+                    . PHP_EOL . $output
+                    . PHP_EOL;
+        } else {
+            if(!extension_loaded('xdebug')) {
+                $output = htmlspecialchars($output, ENT_QUOTES);
+            }
+
+            $output = '<pre>'
+                    . $label
+                    . $output
+                    . '</pre>';
+        }
+
+        if ($echo) {
+            echo($output);
+        }
+        return $output;
+    }
+
+}

File library/Zend/Registry.php

+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Registry
+ * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: Registry.php 15576 2009-05-14 12:41:46Z matthew $
+ */
+
+/**
+ * Generic storage class helps to manage global data.
+ *
+ * @category   Zend
+ * @package    Zend_Registry
+ * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Registry extends ArrayObject
+{
+    /**
+     * Class name of the singleton registry object.
+     * @var string
+     */
+    private static $_registryClassName = 'Zend_Registry';
+
+    /**
+     * Registry object provides storage for shared objects.
+     * @var Zend_Registry
+     */
+    private static $_registry = null;
+
+    /**
+     * Retrieves the default registry instance.
+     *
+     * @return Zend_Registry
+     */
+    public static function getInstance()
+    {
+        if (self::$_registry === null) {
+            self::init();
+        }
+
+        return self::$_registry;
+    }
+
+    /**
+     * Set the default registry instance to a specified instance.
+     *
+     * @param Zend_Registry $registry An object instance of type Zend_Registry,
+     *   or a subclass.
+     * @return void
+     * @throws Zend_Exception if registry is already initialized.
+     */
+    public static function setInstance(Zend_Registry $registry)
+    {
+        if (self::$_registry !== null) {
+            require_once 'Zend/Exception.php';
+            throw new Zend_Exception('Registry is already initialized');
+        }
+
+        self::setClassName(get_class($registry));
+        self::$_registry = $registry;
+    }
+
+    /**
+     * Initialize the default registry instance.
+     *
+     * @return void
+     */
+    protected static function init()
+    {
+        self::setInstance(new self::$_registryClassName());
+    }
+
+    /**
+     * Set the class name to use for the default registry instance.
+     * Does not affect the currently initialized instance, it only applies
+     * for the next time you instantiate.
+     *
+     * @param string $registryClassName
+     * @return void
+     * @throws Zend_Exception if the registry is initialized or if the
+     *   class name is not valid.
+     */
+    public static function setClassName($registryClassName = 'Zend_Registry')
+    {
+        if (self::$_registry !== null) {
+            require_once 'Zend/Exception.php';
+            throw new Zend_Exception('Registry is already initialized');
+        }
+
+        if (!is_string($registryClassName)) {
+            require_once 'Zend/Exception.php';
+            throw new Zend_Exception("Argument is not a class name");
+        }
+
+        /**
+         * @see Zend_Loader
+         */
+        if (!class_exists($registryClassName)) {
+            require_once 'Zend/Loader.php';
+            Zend_Loader::loadClass($registryClassName);
+        }
+
+        self::$_registryClassName = $registryClassName;
+    }
+
+    /**
+     * Unset the default registry instance.
+     * Primarily used in tearDown() in unit tests.
+     * @returns void
+     */
+    public static function _unsetInstance()
+    {
+        self::$_registry = null;
+    }
+
+    /**
+     * getter method, basically same as offsetGet().
+     *
+     * This method can be called from an object of type Zend_Registry, or it
+     * can be called statically.  In the latter case, it uses the default
+     * static instance stored in the class.
+     *
+     * @param string $index - get the value associated with $index
+     * @return mixed
+     * @throws Zend_Exception if no entry is registerd for $index.
+     */
+    public static function get($index)
+    {
+        $instance = self::getInstance();
+
+        if (!$instance->offsetExists($index)) {
+            require_once 'Zend/Exception.php';
+            throw new Zend_Exception("No entry is registered for key '$index'");
+        }
+
+        return $instance->offsetGet($index);
+    }
+
+    /**
+     * setter method, basically same as offsetSet().
+     *
+     * This method can be called from an object of type Zend_Registry, or it
+     * can be called statically.  In the latter case, it uses the default
+     * static instance stored in the class.
+     *
+     * @param string $index The location in the ArrayObject in which to store
+     *   the value.
+     * @param mixed $value The object to store in the ArrayObject.
+     * @return void
+     */
+    public static function set($index, $value)
+    {
+        $instance = self::getInstance();
+        $instance->offsetSet($index, $value);
+    }
+
+    /**
+     * Returns TRUE if the $index is a named value in the registry,
+     * or FALSE if $index was not found in the registry.
+     *
+     * @param  string $index
+     * @return boolean
+     */
+    public static function isRegistered($index)
+    {
+        if (self::$_registry === null) {
+            return false;
+        }
+        return self::$_registry->offsetExists($index);
+    }
+
+    /**
+     * Constructs a parent ArrayObject with default
+     * ARRAY_AS_PROPS to allow acces as an object
+     *
+     * @param array $array data array
+     * @param integer $flags ArrayObject flags
+     */
+    public function __construct($array = array(), $flags = parent::ARRAY_AS_PROPS)
+    {
+        parent::__construct($array, $flags);
+    }
+
+    /**
+     * @param string $index
+     * @returns mixed
+     *
+     * Workaround for http://bugs.php.net/bug.php?id=40442 (ZF-960).
+     */
+    public function offsetExists($index)
+    {
+        return array_key_exists($index, $this);
+    }
+
+}

File library/ZendY/Registry/Tool.php

+<?php
+abstract class ZendY_Registry_Tool
+{
+    protected $_previous = null;
+
+    public function setPrevious(ZendY_Registry_Tool $previous)
+    {
+        if ($this->_previous) {
+            $this->_previous->setPrevious($previous);
+        } else {
+            $this->_previous = $previous;
+        }
+        return $this;
+    }
+
+    public function getPrevious()
+    {
+        return $this->_previous;
+    }
+
+    public function __construct(ZendY_Registry_Tool $previous = null)
+    {
+        if (null !== $previous) {
+            $this->setPrevious($previous);
+        }
+    }
+
+    public function __call($method, $args)
+    {
+        if (null !== $this->_previous) {
+            return $this->_previous->$method();
+        } else {
+            throw new ZendY_Registry_Tool_Exception('Undefined tool "' . $method . '"');
+        }
+    }
+
+    public function get($index)
+    {
+        $method = $index . 'Tool';
+        return $this->$method();
+    }
+}

File library/ZendY/Registry/Tool/Exception.php

+<?php
+
+class ZendY_Registry_Tool_Exception extends Exception
+{}

File library/ZendY/Registry/Toolkit.php

+<?php
+class ZendY_Registry_Toolkit extends Zend_Registry
+{
+    protected $_tool = null;
+
+    public function setTool(ZendY_Registry_Tool $tool)
+    {
+        $this->_tool = $tool;
+        return $this;
+    }
+
+    public function getTool()
+    {
+        return $this->_tool;
+    }
+
+    public function addTool(ZendY_Registry_Tool $tool)
+    {
+        if ($this->_tool) {
+            $tool->setPrevious($this->_tool);
+        }
+        $this->setTool($tool);
+        return $this;
+    }
+
+    public function emptyTool()
+    {
+        $this->_tool = null;
+        return $this;
+    }
+
+    public function offsetExists($index)
+    {
+        if (parent::offsetExists($index)) {
+            return true;
+        } else {
+            try {
+                $this->offsetSet($index, $this->_tool->get($index));
+                return true;
+            } catch (ZendY_Registry_Tool_Exception $e) {
+                return false;
+            }        
+        }
+    }
+}

File library/ZendY/View/Helper/Highlight.php

+<?php
+class ZendY_View_Helper_Highlight extends Zend_View_Helper_Abstract
+{
+    protected $_scriptPath = '';
+    protected $_stylePath = '';
+    protected $_tabReplace = '';
+    protected $_languages = array();
+
+    public function setBasePath($path)
+    {
+        $this->_basePath = $path;
+        return $this;
+    }
+
+    public function getBasePath()
+    {
+        return $this->_basePath;
+    }
+
+    public function setScriptPath($path)
+    {
+        $this->_scriptPath = $this->getBasePath() . $path;
+        return $this;
+    }
+
+    public function getScriptPath()
+    {
+        return $this->_scriptPath;
+    }
+
+    public function setStylePath($path)
+    {
+        $this->_stylePath = $this->getBasePath() . $path;
+        return $this;
+    }
+
+    public function getStylePath()
+    {
+        return $this->_stylePath;
+    }
+
+    public function setTabReplace($tabReplace)
+    {
+        $this->_tabReplace = $tabReplace;
+        return $this;
+    }
+
+    public function getTabReplace()
+    {
+        return $this->_tabReplace;
+    }
+
+    public function setLanguages(array $languages)
+    {
+        $this->_languages = $languages;
+        return $this;
+    }
+
+    public function getLanguages()
+    {
+        return $this->_languages;
+    }
+
+    public function setOptions(array $options)
+    {
+        foreach ($options as $key => $value) {
+            $methodName = 'set' . $key;
+            if (method_exists($this, $methodName)) {
+                $this->$methodName($value);
+            } else {
+                throw new Zend_View_Exception('Unknown option "' . $key . '"');
+            }
+        }
+        return $this;
+    }
+
+    public function highlight(array $options = array())
+    {
+        $this->setOptions($options);
+        return $this;
+    }
+
+    protected function _getScript()
+    {
+        $script = '';
+
+        $tabReplace = $this->getTabReplace();
+        if ('' !== $tabReplace) {
+            $tabReplace = str_replace('"', '\"', $tabReplace);
+            $script .= 'hljs.tabReplace = "' . $tabReplace . '";' . PHP_EOL;
+        }
+
+        $languages = $this->getLanguages();
+        if (array() !== $languages) {
+            $languages = '"' . implode('", "', $languages) . '"';
+        } else {
+            $languages = '';
+        }
+        $script .= 'hljs.initHighlightingOnLoad(' . $languages . ');' . PHP_EOL;
+
+        return $script;
+    }
+
+    public function inject()
+    {
+        $localPath = $this->getScriptPath();
+        if ('' !== $localPath) {
+	        $this->view->headScript()
+		        ->appendFile($localPath)
+		        ->appendScript($this->_getScript())
+		    ;
+        } else {
+            throw new Zend_View_Exception('Option "scriptPath" must be set');
+        }
+
+        $stylePath = $this->getStylePath();
+        if ('' !== $stylePath) {
+		    $this->view->headLink()
+		        ->appendStylesheet($stylePath)
+		    ;
+        }
+    }
+
+    public function __toString()
+    {
+        $this->inject();
+        return '';
+    }
+}

File nbproject/project.properties

+include.path=${php.global.include.path}
+source.encoding=UTF-8
+src.dir=.
+tags.asp=false
+tags.short=true
+web.root=.

File nbproject/project.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://www.netbeans.org/ns/project/1">
+    <type>org.netbeans.modules.php.project</type>
+    <configuration>
+        <data xmlns="http://www.netbeans.org/ns/php-project/1">
+            <name>PhpProject1</name>
+        </data>
+    </configuration>
+</project>

File zendRegistry.php

+<?php
+require_once './init.php';
+
+Zend_Registry::setInstance(new ZendY_Registry_Toolkit());
+$toolkit = Zend_Registry::getInstance();
+
+$toolkit->addTool(new TestTool());
+$toolkit->addTool(new AnotherTool());
+
+Zend_Debug::dump(Zend_Registry::get('test'));
+Zend_Debug::dump(Zend_Registry::get('another'));