[Solar-svn] Revision 2882

pmjones at solarphp.com pmjones at solarphp.com
Wed Oct 17 08:32:00 CDT 2007


actually add the registry, dir, and file classes



Added: trunk/Solar/Dir.php
===================================================================
--- trunk/Solar/Dir.php	                        (rev 0)
+++ trunk/Solar/Dir.php	2007-10-17 13:32:00 UTC (rev 2882)
@@ -0,0 +1,181 @@
+<?php
+class Solar_Dir {
+    
+    /**
+     * 
+     * The OS-specific temporary directory location.
+     * 
+     * @var string
+     * 
+     */
+    protected static $_tmp;
+    
+    /**
+     * 
+     * Hack for [[php::is_dir() | ]] that checks the include_path.
+     * 
+     * Use this to see if a directory exists anywhere in the include_path.
+     * 
+     * {{code: php
+     *     $dir = Solar_Dir::exists('path/to/dir')
+     *     if ($dir) {
+     *         $files = scandir($dir);
+     *     } else {
+     *         echo "Not found in the include-path.";
+     *     }
+     * }}
+     * 
+     * @param string $dir Check for this directory in the include_path.
+     * 
+     * @return mixed If the directory exists in the include_path, returns the
+     * absolute path; if not, returns boolean false.
+     * 
+     */
+    public static function exists($dir)
+    {
+        // no file requested?
+        $dir = trim($dir);
+        if (! $dir) {
+            return false;
+        }
+        
+        // using an absolute path for the file?
+        // dual check for Unix '/' and Windows '\',
+        // or Windows drive letter and a ':'.
+        $abs = ($dir[0] == '/' || $dir[0] == '\\' || $dir[1] == ':');
+        if ($abs && is_dir($dir)) {
+            return $dir;
+        }
+        
+        // using a relative path on the file
+        $path = explode(PATH_SEPARATOR, ini_get('include_path'));
+        foreach ($path as $base) {
+            // strip Unix '/' and Windows '\'
+            $target = rtrim($base, '\\/') . DIRECTORY_SEPARATOR . $dir;
+            if (is_dir($target)) {
+                return $target;
+            }
+        }
+        
+        // never found it
+        return false;
+    }
+    
+    /**
+     * 
+     * "Fixes" a directory string for the operating system.
+     * 
+     * Use slashes anywhere you need a directory separator. Then run the
+     * string through fixdir() and the slashes will be converted to the
+     * proper separator (for example '\' on Windows).
+     * 
+     * Always adds a final trailing separator.
+     * 
+     * @param string $dir The directory string to 'fix'.
+     * 
+     * @return string The "fixed" directory string.
+     * 
+     */
+    public static function fix($dir)
+    {
+        $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir);
+        return rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
+    }
+    
+    /**
+     * 
+     * Convenience method for dirname() and higher-level directories.
+     * 
+     * @param string $file Get the dirname() of this file.
+     * 
+     * @param int $up Move up in the directory structure this many 
+     * times, default 0.
+     * 
+     * @return string The dirname() of the file.
+     * 
+     */
+    public static function name($file, $up = 0)
+    {
+        $dir = dirname($file);
+        while ($up --) {
+            $dir = dirname($dir);
+        }
+        return $dir;
+    }
+    
+    /**
+     * 
+     * Returns the OS-specific directory for temporary files.
+     * 
+     * @return string The temporary directory path.
+     * 
+     */
+    public static function tmp($sub = '')
+    {
+        // find the tmp dir if needed
+        if (! Solar_Dir::$_tmp) {
+            
+            // use the system if we can
+            if (function_exists('sys_get_temp_dir')) {
+                $tmp = sys_get_temp_dir();
+            } else {
+                $tmp = Solar_Dir::_tmp();
+            }
+            
+            // remove trailing separator and save
+            Solar_Dir::$_tmp = rtrim($tmp, DIRECTORY_SEPARATOR);
+        }
+        
+        // do we have a subdirectory request?
+        $sub = trim($sub);
+        if ($sub) {
+            // remove leading and trailing separators, and force exactly
+            // one trailing separator
+            $sub = trim($sub, DIRECTORY_SEPARATOR)
+                 . DIRECTORY_SEPARATOR;
+        }
+        
+        return Solar_Dir::$_tmp . DIRECTORY_SEPARATOR . $sub;
+    }
+    
+    /**
+     * 
+     * Returns the OS-specific temporary directory location.
+     * 
+     * @return string The temp directory path.
+     * 
+     */
+    protected static function _tmp()
+    {
+        // non-Windows system?
+        if (strtolower(substr(PHP_OS, 0, 3)) != 'win') {
+            $tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR'];
+            if ($tmp) {
+                return $tmp;
+            } else {
+                return '/tmp';
+            }
+        }
+        
+        // Windows 'TEMP'
+        $tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP'];
+        if ($tmp) {
+            return $tmp;
+        }
+    
+        // Windows 'TMP'
+        $tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP'];
+        if ($tmp) {
+            return $tmp;
+        }
+    
+        // Windows 'windir'
+        $tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir'];
+        if ($tmp) {
+            return $tmp;
+        }
+    
+        // final fallback for Windows
+        return getenv('SystemRoot') . '\\temp';
+    }
+}
\ No newline at end of file

Added: trunk/Solar/File.php
===================================================================
--- trunk/Solar/File.php	                        (rev 0)
+++ trunk/Solar/File.php	2007-10-17 13:32:00 UTC (rev 2882)
@@ -0,0 +1,105 @@
+<?php
+class Solar_File {
+    
+    protected static $_file;
+    
+    /**
+     * 
+     * Hack for [[php::file_exists() | ]] that checks the include_path.
+     * 
+     * Use this to see if a file exists anywhere in the include_path.
+     * 
+     * {{code: php
+     *     $file = 'path/to/file.php';
+     *     if (Solar_File::exists('path/to/file.php')) {
+     *         include $file;
+     *     }
+     * }}
+     * 
+     * @param string $file Check for this file in the include_path.
+     * 
+     * @return mixed If the file exists and is readble in the include_path,
+     * returns the path and filename; if not, returns boolean false.
+     * 
+     */
+    public static function exists($file)
+    {
+        // no file requested?
+        $file = trim($file);
+        if (! $file) {
+            return false;
+        }
+        
+        // using an absolute path for the file?
+        // dual check for Unix '/' and Windows '\',
+        // or Windows drive letter and a ':'.
+        $abs = ($file[0] == '/' || $file[0] == '\\' || $file[1] == ':');
+        if ($abs && file_exists($file)) {
+            return $file;
+        }
+        
+        // using a relative path on the file
+        $path = explode(PATH_SEPARATOR, ini_get('include_path'));
+        foreach ($path as $base) {
+            // strip Unix '/' and Windows '\'
+            $target = rtrim($base, '\\/') . DIRECTORY_SEPARATOR . $file;
+            if (file_exists($target)) {
+                return $target;
+            }
+        }
+        
+        // never found it
+        return false;
+    }
+    
+    /**
+     * 
+     * Returns the OS-specific directory for temporary files, optionally with
+     * a path added to it.
+     * 
+     * @param string $add Add this to the end of the temporary directory
+     * path.
+     * 
+     * @return string The temp directory path, with optional suffix added.
+     * 
+     */
+    public static function tmp($file)
+    {
+        // convert slashes to OS-specific separators,
+        // then remove leading and trailing separators
+        $file = str_replace('/', DIRECTORY_SEPARATOR, $file);
+        $file = trim($file, DIRECTORY_SEPARATOR);
+        
+        // return the tmp dir plus file name
+        return Solar_Dir::tmp() . $file;
+    }
+    
+    /**
+     * 
+     * Uses [[php::include() | ]] to run a script in a limited scope.
+     * 
+     * @param string $file The file to include.
+     * 
+     * @return mixed The return value of the included file.
+     * 
+     */
+    public static function load($file)
+    {
+        Solar_File::$_file = Solar_File::exists($file);
+        if (! Solar_File::$_file) {
+            // could not open the file for reading
+            throw Solar::exception(
+                'Solar_File',
+                'ERR_FILE_NOT_READABLE',
+                'File does not exist or is not readable',
+                array('file' => $file)
+            );
+        }
+        
+        // clean up the local scope, then include the file and
+        // return its results.
+        unset($file);
+        return include Solar_File::$_file;
+    }
+    
+}
\ No newline at end of file

Added: trunk/Solar/Registry.php
===================================================================
--- trunk/Solar/Registry.php	                        (rev 0)
+++ trunk/Solar/Registry.php	2007-10-17 13:32:00 UTC (rev 2882)
@@ -0,0 +1,107 @@
+<?php
+class Solar_Registry {
+    
+    /**
+     * 
+     * Map of registry names to object instances (or their specs for on-demand 
+     * creation).
+     * 
+     * @param array
+     * 
+     */
+    protected static $_obj = array();
+    
+    /**
+     * 
+     * Accesses an object in the registry.
+     * 
+     * @param string $name The registered name.
+     * 
+     * @return object The object registered under $name.
+     * 
+     * @todo Localize these errors.
+     * 
+     */
+    public static function get($name)
+    {
+        // has the shared object already been loaded?
+        if (! Solar_Registry::exists($name)) {
+            throw Solar::exception(
+                'Solar_Registry',
+                'ERR_NOT_IN_REGISTRY',
+                "Object with name '$name' not in registry.",
+                array('name' => $name)
+            );
+        }
+        
+        // was the registration for a lazy-load?
+        if (is_array(Solar_Registry::$_obj[$name])) {
+            $val = Solar_Registry::$_obj[$name];
+            $obj = Solar::factory($val[0], $val[1]);
+            Solar_Registry::$_obj[$name] = $obj;
+        }
+        
+        // done
+        return Solar_Registry::$_obj[$name];
+    }
+    
+    /**
+     * 
+     * Registers an object under a unique name.
+     * 
+     * @param string $name The name under which to register the object.
+     * 
+     * @param object|string $spec The registry specification.
+     * 
+     * @param mixed $config If lazy-loading, use this as the config.
+     * 
+     * @return void
+     * 
+     * @todo Localize these errors.
+     * 
+     */
+    public static function set($name, $spec, $config = null)
+    {
+        if (Solar_Registry::exists($name)) {
+            // name already exists in registry
+            $class = get_class(Solar_Registry::$_obj[$name]);
+            throw Solar::exception(
+                'Solar_Registry',
+                'ERR_REGISTRY_NAME_EXISTS',
+                "Object with '$name' of class '$class' already in registry", 
+                array('name' => $name, 'class' => $class)
+            );
+        }
+        
+        // register as an object, or as a class and config?
+        if (is_object($spec)) {
+            // directly register the object
+            Solar_Registry::$_obj[$name] = $spec;
+        } elseif (is_string($spec)) {
+            // register a class and config for lazy loading
+            Solar_Registry::$_obj[$name] = array($spec, $config);
+        } else {
+            throw Solar::exception(
+                'Solar_Registry',
+                'ERR_REGISTRY_FAILURE',
+                'Please pass an object, or a class name and a config array',
+                array()
+            );
+        }
+    }
+    
+    /**
+     * 
+     * Check to see if an object name already exists in the registry.
+     * 
+     * @param string $name The name to check.
+     * 
+     * @return bool
+     * 
+     */
+    public static function exists($name)
+    {
+        return ! empty(Solar_Registry::$_obj[$name]);
+    }
+    
+}
\ No newline at end of file




More information about the Solar-svn mailing list