Koowa_Service
[ class tree: Koowa_Service ] [ index: Koowa_Service ] [ all elements ]

Source for file identifier.php

Documentation is available at identifier.php

  1. <?php
  2. /**
  3.  * @version     $Id: identifier.php 4628 2012-05-06 19:56:43Z johanjanssens $
  4.  * @package        Koowa_Service
  5.  * @subpackage  Identifier
  6.  * @copyright    Copyright (C) 2007 - 2012 Johan Janssens. All rights reserved.
  7.  * @license        GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  8.  */
  9.  
  10. /**
  11.  * Service Identifier
  12.  *
  13.  * Wraps identifiers of the form [application::]type.package.[.path].name
  14.  * in an object, providing public accessors and methods for derived formats.
  15.  *
  16.  * @author      Johan Janssens <johan@nooku.org>
  17.  * @package     Koowa_Service
  18.  * @subpackage  Identifier
  19.  */
  20. class KServiceIdentifier implements KServiceIdentifierInterface
  21. {
  22.     /**
  23.      * An associative array of application paths
  24.      *
  25.      * @var array 
  26.      */
  27.     protected static $_applications array();
  28.  
  29.     /**
  30.      * Associative array of identifier adapters
  31.      *
  32.      * @var array 
  33.      */
  34.     protected static $_locators array();
  35.  
  36.     /**
  37.      * The identifier
  38.      *
  39.      * @var string 
  40.      */
  41.     protected $_identifier = '';
  42.  
  43.     /**
  44.      * The application name
  45.      *
  46.      * @var string 
  47.      */
  48.     protected $_application = '';
  49.  
  50.     /**
  51.      * The identifier type [com|plg|mod]
  52.      *
  53.      * @var string 
  54.      */
  55.     protected $_type = '';
  56.  
  57.     /**
  58.      * The identifier package
  59.      *
  60.      * @var string 
  61.      */
  62.     protected $_package = '';
  63.  
  64.     /**
  65.      * The identifier path
  66.      *
  67.      * @var array 
  68.      */
  69.     protected $_path = array();
  70.  
  71.     /**
  72.      * The identifier object name
  73.      *
  74.      * @var string 
  75.      */
  76.     protected $_name = '';
  77.  
  78.     /**
  79.      * The file path
  80.      *
  81.      * @var string 
  82.      */
  83.     protected $_filepath = '';
  84.  
  85.      /**
  86.      * The classname
  87.      *
  88.      * @var string 
  89.      */
  90.     protected $_classname = '';
  91.  
  92.     /**
  93.      * The base path
  94.      *
  95.      * @var string 
  96.      */
  97.     protected $_basepath = '';
  98.  
  99.     /**
  100.      * Constructor
  101.      *
  102.      * @param   string   Identifier string or object in [application::]type.package.[.path].name format
  103.      * @throws  KServiceIdentifierException if the identfier is not valid
  104.      */
  105.     public function __construct($identifier)
  106.     {
  107.         //Check if the identifier is valid
  108.         if(strpos($identifier':'=== FALSE{
  109.             throw new KServiceIdentifierException('Malformed identifier : '.$identifier);
  110.         }
  111.  
  112.         //Get the parts
  113.         if(false === $parts parse_url($identifier)) {
  114.             throw new KServiceIdentifierException('Malformed identifier : '.$identifier);
  115.         }
  116.  
  117.         // Set the type
  118.         $this->type $parts['scheme'];
  119.  
  120.         //Set the application
  121.         if(isset($parts['host'])) {
  122.             $this->application $parts['host'];
  123.         }
  124.  
  125.         // Set the path
  126.         $this->_path = trim($parts['path']'/');
  127.         $this->_path = explode('.'$this->_path);
  128.  
  129.         // Set the extension (first part)
  130.         $this->_package = array_shift($this->_path);
  131.  
  132.         // Set the name (last part)
  133.         if(count($this->_path)) {
  134.             $this->_name = array_pop($this->_path);
  135.         }
  136.  
  137.         //Cache the identifier to increase performance
  138.         $this->_identifier = $identifier;
  139.     }
  140.  
  141.     /**
  142.      * Serialize the identifier
  143.      *
  144.      * @return string     The serialised identifier
  145.      */
  146.     public function serialize()
  147.     {
  148.         $data array(
  149.             'application' => $this->_application,
  150.             'type'          => $this->_type,
  151.             'package'      => $this->_package,
  152.             'path'          => $this->_path,
  153.             'name'          => $this->_name,
  154.             'identifier'  => $this->_identifier,
  155.             'basepath'    => $this->_basepath,
  156.             'filepath'      => $this->filepath,
  157.             'classname'   => $this->classname,
  158.         );
  159.  
  160.         return serialize($data);
  161.     }
  162.  
  163.     /**
  164.      * Unserialize the identifier
  165.      *
  166.      * @return string     The serialised identifier
  167.      */
  168.     public function unserialize($data)
  169.     {
  170.         $data unserialize($data);
  171.  
  172.         foreach($data as $property => $value{
  173.             $this->{'_'.$property$value;
  174.         }
  175.     }
  176.  
  177.     /**
  178.      * Set an application path
  179.      *
  180.      * @param string    The name of the application
  181.      * @param string    The path of the application
  182.      * @return void 
  183.      */
  184.     public static function setApplication($application$path)
  185.     {
  186.         self::$_applications[$application$path;
  187.     }
  188.  
  189.     /**
  190.      * Get an application path
  191.      *
  192.      * @param string    The name of the application
  193.      * @return string    The path of the application
  194.      */
  195.     public static function getApplication($application)
  196.     {
  197.         return isset(self::$_applications[$application]self::$_applications[$applicationnull;
  198.     }
  199.  
  200.     /**
  201.      * Get a list of applications
  202.      *
  203.      * @return array 
  204.      */
  205.     public static function getApplications()
  206.     {
  207.         return self::$_applications;
  208.     }
  209.  
  210.     /**
  211.      * Add a identifier adapter
  212.      *
  213.      * @param object    KServiceLocator
  214.      * @return void 
  215.      */
  216.     public static function addLocator(KServiceLocatorInterface $locator)
  217.     {
  218.         self::$_locators[$locator->getType()$locator;
  219.     }
  220.  
  221.     /**
  222.      * Get the registered adapters
  223.      *
  224.      * @return array 
  225.      */
  226.     public static function getLocators()
  227.     {
  228.         return self::$_locators;
  229.     }
  230.  
  231.     /**
  232.      * Implements the virtual class properties
  233.      *
  234.      * This functions creates a string representation of the identifier.
  235.      *
  236.      * @param   string  The virtual property to set.
  237.      * @param   string  Set the virtual property to this value.
  238.      */
  239.     public function __set($property$value)
  240.     {
  241.         if(isset($this->{'_'.$property}))
  242.         {
  243.             //Force the path to an array
  244.             if($property == 'path')
  245.             {
  246.                 if(is_scalar($value)) {
  247.                      $value = (array) $value;
  248.                 }
  249.             }
  250.  
  251.             //Set the basepath
  252.             if($property == 'application')
  253.             {
  254.                if(!isset(self::$_applications[$value])) {
  255.                     throw new KServiceIdentifierException('Unknow application : '.$value);
  256.                }
  257.  
  258.                $this->_basepath self::$_applications[$value];
  259.             }
  260.  
  261.             //Set the type
  262.             if($property == 'type')
  263.             {
  264.                 //Check the type
  265.                 if(!isset(self::$_locators[$value]))  {
  266.                     throw new KServiceIdentifierException('Unknow type : '.$value);
  267.                 }
  268.             }
  269.  
  270.             //Set the properties
  271.             $this->{'_'.$property$value;
  272.  
  273.             //Unset the properties
  274.             $this->_identifier '';
  275.             $this->_classname  '';
  276.             $this->_filepath   '';
  277.         }
  278.     }
  279.  
  280.     /**
  281.      * Implements access to virtual properties by reference so that it appears to be
  282.      * a public property.
  283.      *
  284.      * @param   string  The virtual property to return.
  285.      * @return  array   The value of the virtual property.
  286.      */
  287.     public function &__get($property)
  288.     {
  289.         if(isset($this->{'_'.$property}))
  290.         {
  291.             if($property == 'filepath' && empty($this->_filepath)) {
  292.                 $this->_filepath self::$_locators[$this->_type]->findPath($this);
  293.             }
  294.  
  295.             if($property == 'classname' && empty($this->_classname)) {
  296.                 $this->_classname self::$_locators[$this->_type]->findClass($this);
  297.             }
  298.  
  299.             return $this->{'_'.$property};
  300.         }
  301.     }
  302.  
  303.     /**
  304.      * This function checks if a virtual property is set.
  305.      *
  306.      * @param   string  The virtual property to return.
  307.      * @return  boolean True if it exists otherwise false.
  308.      */
  309.     public function __isset($property)
  310.     {
  311.         return isset($this->{'_'.$property});
  312.     }
  313.  
  314.     /**
  315.      * Formats the indentifier as a [application::]type.package.[.path].name string
  316.      *
  317.      * @return string 
  318.      */
  319.     public function __toString()
  320.     {
  321.         if($this->_identifier == '')
  322.         {
  323.             if(!empty($this->_type)) {
  324.                 $this->_identifier .= $this->_type;
  325.             }
  326.  
  327.             if(!empty($this->_application)) {
  328.                 $this->_identifier .= '://'.$this->_application.'/';
  329.             else {
  330.                 $this->_identifier .= ':';
  331.             }
  332.  
  333.             if(!empty($this->_package)) {
  334.                 $this->_identifier .= $this->_package;
  335.             }
  336.  
  337.             if(count($this->_path)) {
  338.                 $this->_identifier .= '.'.implode('.',$this->_path);
  339.             }
  340.  
  341.             if(!empty($this->_name)) {
  342.                 $this->_identifier .= '.'.$this->_name;
  343.             }
  344.         }
  345.  
  346.         return $this->_identifier;
  347.     }
  348. }

Documentation generated on Sun, 19 May 2013 03:03:45 +0200 by phpDocumentor 1.4.3