Skip to content

Commit 6f2297e

Browse files
committed
Added PSR-4 Autoloader
1 parent d5e55e7 commit 6f2297e

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

Classes/PHPWord/Autoloader.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,22 @@
3131

3232
/**
3333
* Class PHPWord_Autoloader
34+
*
35+
* TODO: remove legacy autoloader once everything is moved to namespaces
3436
*/
3537
class PHPWord_Autoloader
3638
{
39+
const PREFIX = 'PHPWord';
40+
3741
/**
3842
* Register the autoloader
3943
*
4044
* @return void
4145
*/
4246
public static function register()
4347
{
44-
spl_autoload_register(array('PHPWord_Autoloader', 'load'));
48+
spl_autoload_register(array('PHPWord_Autoloader', 'load')); // Legacy
49+
spl_autoload_register(array(new self, 'autoload')); // PSR-4
4550
}
4651

4752
/**
@@ -60,4 +65,21 @@ public static function load($strObjectName)
6065

6166
return null;
6267
}
68+
69+
/**
70+
* Autoloader
71+
*
72+
* @param string
73+
*/
74+
public static function autoload($class)
75+
{
76+
$prefixLength = strlen(self::PREFIX);
77+
if (0 === strncmp(self::PREFIX, $class, $prefixLength)) {
78+
$file = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, $prefixLength));
79+
$file = realpath(__DIR__ . (empty($file) ? '' : DIRECTORY_SEPARATOR) . $file . '.php');
80+
if (file_exists($file)) {
81+
require_once $file;
82+
}
83+
}
84+
}
6385
}

Tests/PHPWord/AutoloaderTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,30 @@
33

44
use PHPUnit_Framework_TestCase;
55
use PHPWord_Autoloader;
6+
use PHPWord_Autoloader as Autoloader;
67

78
class AutoloaderTest extends PHPUnit_Framework_TestCase
89
{
910
public function testRegister()
1011
{
1112
PHPWord_Autoloader::register();
1213
$this->assertContains(array('PHPWord_Autoloader', 'load'), spl_autoload_functions());
14+
$this->assertContains(array('PHPWord_Autoloader', 'autoload'), spl_autoload_functions());
1315
}
1416

15-
public function testAutoload()
17+
public function testAutoloadLegacy()
1618
{
1719
$this->assertNull(PHPWord_Autoloader::load('Foo'), 'PHPWord_Autoloader::load() is trying to load classes outside of the PHPWord namespace');
1820
$this->assertTrue(PHPWord_Autoloader::load('PHPWord'), 'PHPWord_Autoloader::load() failed to autoload the PHPWord class');
1921
}
22+
23+
public function testAutoload()
24+
{
25+
$declared = get_declared_classes();
26+
$declaredCount = count($declared);
27+
Autoloader::autoload('Foo');
28+
$this->assertEquals($declaredCount, count(get_declared_classes()), 'PHPWord\\Autoloader::autoload() is trying to load classes outside of the PHPWord namespace');
29+
Autoloader::autoload('PHPWord\\Exceptions\\InvalidStyleException'); // TODO change this class to the main PHPWord class when it is namespaced
30+
$this->assertTrue(in_array('PHPWord\\Exceptions\\InvalidStyleException', get_declared_classes()), 'PHPWord\\Autoloader::autoload() failed to autoload the PHPWord\\Exceptions\\InvalidStyleException class');
31+
}
2032
}

0 commit comments

Comments
 (0)