/Users/mgirouard/Sites/panda-php/trunk/Panda/Loader/Abstract.php

Go to the documentation of this file.
00001 <?php
00002 
00003 require_once 'Panda/Loader/Interface.php';
00004 require_once 'Panda/Configurable/Interface.php';
00005 
00013 class Panda_Loader_Abstract
00014 implements Panda_Loader_Interface,
00015            Panda_Configurable_Interface
00016 {
00022     protected $namespace;
00023 
00029     protected $baseDir;
00030 
00041     public function configure(array $configuration = array())
00042     {
00043         if (array_key_exists('namespace', $configuration)) {
00044             $this->setNamespace($configuration['namespace']);
00045         }
00046 
00047         if (array_key_exists('baseDir', $configuration)) {
00048             $this->setBaseDir($configuration['baseDir']);
00049         }
00050 
00051         if (array_key_exists('load', $configuration)) {
00052             if (is_string($configuration['load'])) {
00053                 $this->load($configuration['load']);
00054             }
00055             elseif (is_array($configuration['load'])) {
00056                 foreach ($configuration['load'] as $className) {
00057                     $this->load($className);
00058                 }
00059             }
00060         }
00061     }
00062 
00068     public function getNamespace()
00069     {
00070         return $this->namespace;
00071     }
00072 
00078     public function setNamespace($namespace)
00079     {
00080         $this->namespace = $namespace;
00081     }
00082 
00088     public function getBaseDir()
00089     {
00090         return $this->baseDir;
00091     }
00092 
00098     public function setBaseDir($baseDir)
00099     {
00100         $this->baseDir = $baseDir;
00101     }
00102 
00109     public function getClassName($className)
00110     {
00111         // If it's already namespaced, just return it back out
00112         if (preg_match("/^(Panda|$this->namespace)/i", $className)) {
00113             return $className;
00114         }
00115 
00116         return sprintf('%s_%s', $this->namespace, $className);
00117     }
00118 
00125     public function getFileName($className)
00126     {
00127         $className = $this->getClassName($className);
00128 
00129         return sprintf('%s%s%s.php',
00130             $this->baseDir,
00131             DIRECTORY_SEPARATOR,
00132             str_replace('_', DIRECTORY_SEPARATOR, $className)
00133         );
00134     }
00135 
00142     public function load($className)
00143     {
00144         $fileName = $this->getFileName($className);
00145 
00146         if (is_file($fileName)) {
00147             require_once $fileName;
00148             return true;
00149         }
00150         else {
00151             return false;
00152         }
00153     }
00154 
00162     public function loadInstance($className, array $parameters = array())
00163     {
00164         if ($this->load($className)) {
00165             $Class = new ReflectionClass( $this->getClassName($className) );
00166 
00167             if ($Class->hasMethod('__construct')) {
00168                 return $Class->newInstanceArgs($parameters);
00169             }
00170             else {
00171                 return $Class->newInstance();
00172             }
00173         }
00174     }
00175 }

Generated on Thu Sep 4 01:56:50 2008 for Panda PHP Components by  doxygen 1.5.4