Skip to content
This repository
Browse code

Implemented the autoloader. Closes #6

  • Loading branch information...
commit 9dee6c8058926b53ce66029407f286f5747d6b02 1 parent 58ae00a
Georg Dresler authored
25 src/Faultier/FileUpload/Autoloader.php
... ... @@ -0,0 +1,25 @@
  1 +<?php
  2 +
  3 + namespace Faultier\FileUpload;
  4 +
  5 + class Autoloader {
  6 +
  7 + static public function register($prepend = false) {
  8 + return spl_autoload_register(array(new self, 'autoload'), true, $prepend);
  9 + }
  10 +
  11 + static public function autoload($class) {
  12 +
  13 + $file = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.str_replace('\\', '/', $class).'.php';
  14 +
  15 + if (is_file($file)) {
  16 + require $file;
  17 + return true;
  18 + } else {
  19 + return false;
  20 + }
  21 + }
  22 +
  23 + }
  24 +
  25 +?>
7 src/autoload.php
... ... @@ -1,9 +1,6 @@
1 1 <?php
2 2
3   - require_once __DIR__.'/Faultier/FileUpload/File.php';
4   - require_once __DIR__.'/Faultier/FileUpload/FileUpload.php';
5   - require_once __DIR__.'/Faultier/FileUpload/Constraint/ConstraintInterface.php';
6   - require_once __DIR__.'/Faultier/FileUpload/Constraint/SizeConstraint.php';
7   - require_once __DIR__.'/Faultier/FileUpload/Constraint/TypeConstraint.php';
  3 + require_once __DIR__.'/Faultier/FileUpload/Autoloader.php';
  4 + Faultier\FileUpload\Autoloader::register();
8 5
9 6 ?>
26 tests/AutoloaderTest.php
... ... @@ -0,0 +1,26 @@
  1 +<?php
  2 +
  3 + class AutoloaderTest extends PHPUnit_Framework_TestCase {
  4 +
  5 + public function setUp() {
  6 + require_once __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'Faultier'.DIRECTORY_SEPARATOR.'FileUpload'.DIRECTORY_SEPARATOR.'Autoloader.php';
  7 + }
  8 +
  9 + /**
  10 + * @test
  11 + */
  12 + public function register() {
  13 + $this->assertTrue(Faultier\FileUpload\Autoloader::register());
  14 + }
  15 +
  16 + /**
  17 + * @test
  18 + */
  19 + public function autoload() {
  20 + $this->assertTrue(Faultier\FileUpload\Autoloader::autoload('Faultier\FileUpload\FileUpload'));
  21 + $this->assertFalse(Faultier\FileUpload\Autoloader::autoload(''));
  22 + }
  23 +
  24 + }
  25 +
  26 +?>

0 comments on commit 9dee6c8

Please sign in to comment.
Something went wrong with that request. Please try again.