Permalink
Browse files

Implemented the autoloader. Closes #6

  • Loading branch information...
1 parent 58ae00a commit 9dee6c8058926b53ce66029407f286f5747d6b02 @ge-org committed Oct 4, 2011
Showing with 53 additions and 5 deletions.
  1. +25 −0 src/Faultier/FileUpload/Autoloader.php
  2. +2 −5 src/autoload.php
  3. +26 −0 tests/AutoloaderTest.php
View
25 src/Faultier/FileUpload/Autoloader.php
@@ -0,0 +1,25 @@
+<?php
+
+ namespace Faultier\FileUpload;
+
+ class Autoloader {
+
+ static public function register($prepend = false) {
+ return spl_autoload_register(array(new self, 'autoload'), true, $prepend);
+ }
+
+ static public function autoload($class) {
+
+ $file = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.str_replace('\\', '/', $class).'.php';
+
+ if (is_file($file)) {
+ require $file;
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ }
+
+?>
View
7 src/autoload.php
@@ -1,9 +1,6 @@
<?php
- require_once __DIR__.'/Faultier/FileUpload/File.php';
- require_once __DIR__.'/Faultier/FileUpload/FileUpload.php';
- require_once __DIR__.'/Faultier/FileUpload/Constraint/ConstraintInterface.php';
- require_once __DIR__.'/Faultier/FileUpload/Constraint/SizeConstraint.php';
- require_once __DIR__.'/Faultier/FileUpload/Constraint/TypeConstraint.php';
+ require_once __DIR__.'/Faultier/FileUpload/Autoloader.php';
+ Faultier\FileUpload\Autoloader::register();
?>
View
26 tests/AutoloaderTest.php
@@ -0,0 +1,26 @@
+<?php
+
+ class AutoloaderTest extends PHPUnit_Framework_TestCase {
+
+ public function setUp() {
+ require_once __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'Faultier'.DIRECTORY_SEPARATOR.'FileUpload'.DIRECTORY_SEPARATOR.'Autoloader.php';
+ }
+
+ /**
+ * @test
+ */
+ public function register() {
+ $this->assertTrue(Faultier\FileUpload\Autoloader::register());
+ }
+
+ /**
+ * @test
+ */
+ public function autoload() {
+ $this->assertTrue(Faultier\FileUpload\Autoloader::autoload('Faultier\FileUpload\FileUpload'));
+ $this->assertFalse(Faultier\FileUpload\Autoloader::autoload(''));
+ }
+
+ }
+
+?>

0 comments on commit 9dee6c8

Please sign in to comment.