Created
June 13, 2012 08:28
-
-
Save anthonysterling/2922784 to your computer and use it in GitHub Desktop.
A CodeIgniter library which provides access, and functionality from, Symfony's UniversalClassLoader from within CodeIgniter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
* | |
* A CodeIgniter library which provides access, and functionality from, | |
* Symfony's UniversalClassLoader from within CodeIgniter. | |
* | |
* <code> | |
* $this->load->library('autoloader'); | |
* $this->autoloader->registerNamespaces(array( | |
* 'Symfony\Component' => 'component', | |
* )); | |
* </code> | |
* | |
* @author Anthony Sterling | |
* | |
*/ | |
class Autoloader | |
{ | |
protected | |
$vendorPath = null, | |
$autoloader = null; | |
/** | |
* Constructor. | |
* | |
* @param string $vendorPath Sets the $vendorPath, defaults to CI's APPPATH/vendor/ | |
* | |
* @return void | |
*/ | |
public function __construct($vendorPath = null) | |
{ | |
if(null === $vendorPath) | |
{ | |
$vendorPath = APPPATH . 'vendor/'; | |
} | |
$this->setVendorPath($vendorPath); | |
$this->setIncludePath(); | |
$this->setAutoloader(); | |
} | |
/** | |
* Returns the vendor path. | |
* | |
* @return string | |
*/ | |
public function getVendorPath() | |
{ | |
return $this->vendorPath; | |
} | |
/** | |
* Sets the vendor path to look for autoloaded libraries. | |
* | |
* @param string $vendorPath The path in which to look libraries | |
* @param bool $resolve Indicates whether or not to 'realpath' the $vendorPath | |
* | |
* @return object Autoloader | |
*/ | |
public function setVendorPath($vendorPath, $resolve = true) | |
{ | |
if($resolve) | |
{ | |
$vendorPath = realpath($vendorPath); | |
} | |
if( ! is_dir($vendorPath)) | |
{ | |
$error = sprintf( | |
'Autoloader: The path %s does not exist', | |
$vendorPath | |
); | |
trigger_error($error, E_USER_ERROR); | |
} | |
$this->vendorPath = $vendorPath; | |
return $this; | |
} | |
/** | |
* Adds the defined vendor path to the include path. | |
* | |
* @return object Autoloader | |
*/ | |
protected function setIncludePath() | |
{ | |
$path = get_include_path() . PATH_SEPARATOR . $this->getVendorPath(); | |
if( ! set_include_path($path)) | |
{ | |
$error = sprintf( | |
'Autoloader: Cannot set include path to %s', | |
$path | |
); | |
trigger_error($error, E_USER_ERROR); | |
} | |
return $this; | |
} | |
/** | |
* Creates, and sets, an instance of Symfony's UniversalClassLoader. | |
* | |
* @return object Autoloader | |
*/ | |
protected function setAutoloader() | |
{ | |
require_once 'Symfony/Component/ClassLoader/UniversalClassLoader.php'; | |
$autoloader = new Symfony\Component\ClassLoader\UniversalClassLoader; | |
$autoloader->useIncludePath(true); | |
$this->autoloader = $autoloader; | |
return $this; | |
} | |
/** | |
* Proxies any method calls to this library to Symfony's | |
* UniversalClassLoader instance. | |
* | |
* @return mixed | |
*/ | |
public function __call($method, $args) | |
{ | |
if( ! method_exists($this->autoloader, $method)) | |
{ | |
$error = sprintf( | |
'Autoloader: Cannot call UniversalClassLoader::%s', | |
$method | |
); | |
trigger_error($error, E_USER_ERROR); | |
} | |
return call_user_func_array( | |
array($this->autoloader, $method), | |
$args | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment