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

Source for file Loader.php

Documentation is available at Loader.php

  1. <?php
  2. /**
  3.  * Console Library
  4.  *
  5.  * LICENSE
  6.  *
  7.  * "THE BEER-WARE LICENSE" (Revision 42):
  8.  * "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
  9.  * As long as you retain this notice you can do whatever you want with
  10.  * this stuff. If we meet some day, and you think this stuff is worth it,
  11.  * you can buy me a beer in return.
  12.  *
  13.  * $Author: sxs $
  14.  * $Revision: 161 $
  15.  *
  16.  * @category    Console
  17.  * @package     Console_Application
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. /**
  22.  * Console_Exception.
  23.  *
  24.  * @see Console_Exception
  25.  */
  26. require_once('Console/Exception.php');
  27.  
  28. /**
  29.  * Class for running a Console_Application_Abstract-Script.
  30.  *
  31.  * @final
  32.  * @category   Console
  33.  * @package    Console_Application
  34.  * @uses       Console_Exception
  35.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  36.  */
  37. final class Console_Loader {
  38.     /**
  39.      * Loads a class depending on the class name.
  40.      * All _ in the classname are replaced with DIRECTORY_SEPARATOR.
  41.      * 
  42.      * @static
  43.      * @access public
  44.      * @param string $name 
  45.      * @throws Console_Exception If the generated filename is not valid
  46.      * @throws Console_Exception If the file is not readable
  47.      * @throws Console_Exception If the file doesnt contain the class/interface
  48.      */
  49.     public static function loadClass($name{
  50.         if (class_exists($namefalse|| interface_exists($namefalse)) {
  51.             return;
  52.         }
  53.  
  54.         $file str_replace('_'DIRECTORY_SEPARATOR$name'.php';
  55.  
  56.         if (!self::isFileNameValid($file)) {
  57.             $message 'Illegal character in filename: ' $file;
  58.             throw new Console_Exception($message);
  59.         }
  60.  
  61.         if (!self::isReadable($file)) {
  62.             $message "File: $file is not readable!";
  63.             throw new Console_Exception($message);
  64.         }
  65.  
  66.         require_once($file);
  67.  
  68.         if (!class_exists($namefalse&& !interface_exists($namefalse)) {
  69.             $message  "Class or interface '$name";
  70.             $message .= "was not found in the file '$file'";
  71.             throw new Console_Exception($message);
  72.         }
  73.     }
  74.  
  75.     /**
  76.      * Checks if a file is readable.
  77.      *
  78.      * @static
  79.      * @access public
  80.      * @param  string $filename 
  81.      * @return bool 
  82.      */
  83.     public static function isReadable($filename{
  84.         if (!$fh @fopen($filename'r'true)) {
  85.             return false;
  86.         }
  87.  
  88.         @fclose($fh);
  89.  
  90.         return true;
  91.     }
  92.  
  93.     /**
  94.      * Checks if a filename is valid.
  95.      *
  96.      * @static
  97.      * @access public
  98.      * @param  string $filename 
  99.      * @return bool 
  100.      */
  101.     public static function isFileNameValid($filename{
  102.         if (=== preg_match('/[^a-z0-9\\/\\\\_.:-]/i'$filename)) {
  103.             return true;
  104.         else {
  105.             return false;
  106.         }
  107.     }
  108. }

Documentation generated on Mon, 17 Aug 2009 15:52:43 +0200 by phpDocumentor 1.4.2