├── .gitignore ├── README ├── lib ├── dd_core_IClassLoader.php ├── dd_core_IResourceLocator.php ├── dd_core_ClassLoaderUtil.php ├── dd_core_ClasspathResourceLocator.php ├── dd_core_ResourceLocatorClassLoader.php ├── dd_core_AbstractClassLoader.php └── dd_core_PathResourceLocator.php ├── tests ├── README └── DdCoreBasicTest.php └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .buildpath 2 | .project 3 | .settings 4 | ._* 5 | build 6 | dist 7 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Dragonfly Development PHP Core Library 2 | http://redmine.dflydev.com/projects/dd-core 3 | 4 | 5 | dd-core provides some basic bootstrapping functionality such as resource 6 | locating (finding files on disk) and class loading. 7 | 8 | -------------------------------------------------------------------------------- /lib/dd_core_IClassLoader.php: -------------------------------------------------------------------------------- 1 | 19 | -------------------------------------------------------------------------------- /lib/dd_core_ClassLoaderUtil.php: -------------------------------------------------------------------------------- 1 | resourceLocator = $resourceLocator; 15 | } 16 | 17 | /** 18 | * (non-PHPdoc) 19 | * @see dd_core_AbstractClassLoader::find() 20 | */ 21 | public function find($classFileName) { 22 | return $this->resourceLocator->find($classFileName, true); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010, Dragonfly Development Inc 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of Dragonfly Development Inc nor the names of its 13 | contributors may be used to endorse or promote products derived from 14 | this software without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 | POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /lib/dd_core_AbstractClassLoader.php: -------------------------------------------------------------------------------- 1 | templateCallbacks = $templateCallbacks; 29 | } else { 30 | $this->templateCallbacks = array( 31 | // TODO Figure out a better way to handle this 32 | array('dd_core_AbstractClassLoader', 'SIMPLE_CLASS_TEMPLATE'), 33 | ); 34 | } 35 | } 36 | 37 | /** 38 | * Load a class 39 | * @param $className 40 | * @param $includeFilename 41 | */ 42 | public function load($className, $includeFilename = null) { 43 | if ( class_exists($className) ) return true; 44 | if ( isset($this->loadAttemptedLocally[$className]) ) return false; 45 | $this->loadAttemptedLocally[$className] = true; 46 | if ( $includeFilename !== null ) { 47 | // If an include filename was specified, we'll 48 | // just assume that is what we are looking for. 49 | require_once($includeFilename); 50 | return true; 51 | } 52 | foreach ( $this->potentialIncludeFilenames($className) as $includeFilename ) { 53 | if ( $fullIncludePath = $this->find($includeFilename) ) { 54 | require_once($fullIncludePath); 55 | return true; 56 | } 57 | } 58 | return false; 59 | } 60 | 61 | /** 62 | * Determine potential include filenames for the specified class name 63 | * @param $className 64 | */ 65 | public function potentialIncludeFilenames($className) { 66 | $potentialIncludeFilenames = array(); 67 | foreach ( $this->templateCallbacks as $callback ) { 68 | $potentialIncludeFilenames[] = call_user_func($callback, $className); 69 | } 70 | return $potentialIncludeFilenames; 71 | } 72 | 73 | /** 74 | * Leave the finding of the class filename up to subclasses 75 | * @param $classFileName 76 | */ 77 | abstract public function find($classFileName); 78 | 79 | } 80 | -------------------------------------------------------------------------------- /lib/dd_core_PathResourceLocator.php: -------------------------------------------------------------------------------- 1 | callingFile = $callingFile; 42 | } 43 | if ( $paths !== null ) { 44 | if ( ! is_array($paths) ) { 45 | $paths = array($paths); 46 | } 47 | foreach ( $paths as $path ) { 48 | $this->paths[] = $path; 49 | } 50 | } 51 | if ( $prependedPaths !== null ) { 52 | if ( ! is_array($prependedPaths) ) { 53 | $prependedPaths = array($prependedPaths); 54 | } 55 | foreach ( $prependedPaths as $path ) { 56 | $this->prependedPaths[] = $path; 57 | } 58 | } 59 | if ( $appendedPaths !== null ) { 60 | if ( ! is_array($appendedPaths) ) { 61 | $appendedPaths = array($appendedPaths); 62 | } 63 | foreach ( $appendedPaths as $path ) { 64 | $this->appendedPaths[] = $path; 65 | } 66 | } 67 | } 68 | 69 | /** 70 | * Find a target file 71 | * @param string $target Target 72 | * @return string 73 | */ 74 | public function find($target, $realPath = false) { 75 | 76 | if ( strpos($target, '/') === 0 ) { 77 | if ( file_exists($target) ) return $realPath ? realpath($target) : $target; 78 | return null; 79 | } 80 | 81 | foreach ( $this->allPaths() as $path ) { 82 | $testLocation = $path . '/' . $target; 83 | // TODO This could possibly be cached eventually. 84 | if ( file_exists($testLocation) ) return $realPath ? realpath($testLocation) : $testLocation; 85 | } 86 | 87 | // Could not be found. 88 | return null; 89 | 90 | } 91 | 92 | /** 93 | * Paths to search 94 | * @return array 95 | */ 96 | public function allPaths() { 97 | $callingFiles = array(); 98 | if ( $this->callingFile ) $callingFiles[] = dirname($this->callingFile); 99 | return array_merge( 100 | $callingFiles, 101 | $this->prependedPaths(), 102 | $this->paths(), 103 | $this->appendedPaths() 104 | ); 105 | 106 | } 107 | 108 | /** 109 | * Paths 110 | * @return array 111 | */ 112 | public function paths() { 113 | return $this->paths; 114 | } 115 | 116 | /** 117 | * Prepend a path to the classpath 118 | * @param string $path Path 119 | */ 120 | public function prependPath($path) { 121 | array_unshift($this->prependedPaths, $path); 122 | } 123 | 124 | /** 125 | * Append a path to the classpath 126 | * @param string $path Path 127 | */ 128 | public function appendPath($path) { 129 | push($this->appendedPaths, $path); 130 | } 131 | 132 | /** 133 | * Prepended paths 134 | * @return array 135 | */ 136 | public function prependedPaths() { 137 | return $this->prependedPaths; 138 | } 139 | 140 | /** 141 | * Appended paths 142 | * @return array 143 | */ 144 | public function appendedPaths() { 145 | return $this->appendedPaths; 146 | } 147 | 148 | } --------------------------------------------------------------------------------