星期日, 9月 12, 2010

Zend - Infrastructure

  • Zend_Config
    • Use/Purpose

      designed to simplify access to, and use of , configuration data within applications.
    • Multiple Environs

      • PHP Array
        <?php

        //Given an array of configuration data
        $configArray = array(
        'webhost' => 'ww.example.com',
        'database' => array(...
        )
        );

        //Create the object-oriendted wrapper upon the configuration dataconsumption
        require_once 'Zend/Config.php';
        $config = new Zend_Config(configArray );
        //Print a configuration datum (results in 'ww.example.com')

        echo $config->webhost;
      • PHP Configuration File
        <?php
        //config.php
        return array(
        'webhost' => 'ww.example.com',
        'database' => array(...


        <?php
        //Configuration consumption
        require_once 'Zend/Config.php';
        $config = new Zend_Config(require 'config.php');

        //Print a configuration datum (results in 'ww.example.com')
        echo $config->webhost;

        Zend_Config實作了Countable及Iterator interfaces,因此可用count()及foreach
        Two Zend_Config objects can be merged into a single object using the merge() function
    • .ini Files

      • .ini可宣告[production],[testing],[development]等,配合環境變數讀取不同的config

        $config = new Zend_Config_Ini('/path/to/config.ini'),'staging');
        echo $config->database->params->hosts;
        [staging:production] //staging繼承production的變數
        不要把變數內容值用單引號包起來,會把單引號當成值
      • Zend_Config_Xml

        $config = new Zend_Config_Xml('/path/to/config.xml'),'staging');
        echo $config->database->params->hosts;

    • Bootstrap File
    • Config Objects
  • Zend_Exception
    Purpose: recover from the failure
    <?php
    try{
    Zend_Loader::loadClass('nonexistantclass');
    }catch (Zend_Exception $e){
    echo "Caught exception: " . get_class($e) . "\n";
    echo "Message: " . $e->getMessage() . "\n";
    //other code to recover from the failure;
    }

  • Zend_Registry
    Purpose: a container for storing objects and values in the application space.
    Constructing a Registry
    <?php
    $registry = new Zend_Registry(array('index' => $value));


    Initializing the Static Registry
    <?php
    Zend_Registry::setInstance(array('index' => $value));


    The setInstance() method throws Zend_Exception if the static registry has already been initialized
  • Zend_Version
    <?php
    //return -1(older), 0(the same) or 1(newer)
    $cmp = Zend_Version::compareVBersion('1.0.0');
  • Zend_Loader
    Purpose: includes methods to help you load files dynamically
    Zend_Loader::loadFile($filename,$path,$once) is a wrapper for the PHP function include() and throws Zend_Exception on failure.
    limit: $filename argument can only contain alphanumeric characters, hyphens("-"), or periods("."), and must not contain any path information.
    $once is boolean, if TRUE, equals include_once(), otherwise include() is used.

    Loading Classes
    limit:同loadFile,多了underscores("_")
    Zend_Loader::loadClass($class, $dirs)
    $class = "Container_Tree"; //equals Container/Tree.php
    <?php
    Zend_Loader::loadClass('Container_Tree',
     array(
      '/home/productioin/mylib',
      '/home/productioin/myapp'
     )
    );

    會在給予的路徑參數中找claess,如果找不到就丟了Zend_Exception
    Plugin Loader
    <?php
    $loader = new Zend_Loader_PluginLoader();
    $loader->addPrefixPath('Zend_View_Helper', 'Zend/View/elper/')
        ->addPrefixPath('Foo_View_Helper','application/modules/foo/views/helpers');
  • Zend_Session
    Purpose: helps manage and preserve session data, a logical complement of cookie data, across multiple page requests by the same client.

    Zend_Session_Namespace

    就是PHP中的$_SESSION
    $sess = new Zend_Session_Namespace();
    $sees->tree = $tree;

    Operation

    當第一個session被requested時,Zend_Session就自動啟動PHP session,不必等到Zend_Session::start().
    The PHP session will use defaults from Zend_Session, unless modified by Zend_Session::setOptions().

沒有留言: