Skip to content

Commit 7080ebe

Browse files
committed
Merge pull request #75 from gabrielbull/master
Added PSR-4 Autoloader and move phpunit.xml.dist back to root of project
2 parents c5acf36 + 6f2297e commit 7080ebe

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
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
}

changelog.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ Changes in branch for release 0.7.1 :
2626
- Bugfix: (gabrielbull) - Fixed bug with cell styling
2727
- Bugfix: (gabrielbull) - Fixed bug list items inside of cells
2828
- Feature: (gabrielbull) - Word2007 : Support sections page numbering
29+
- Feature: (gabrielbull) - Word2007 : Added support for line height
2930
- QA: (Progi1984) - UnitTests
3031

31-
Fixed in branch for release 0.7.0 :
32+
Changes in branch for release 0.7.0 :
3233
- Bugfix: (RomanSyroeshko) GH-32 - "Warning: Invalid error type specified in ...\PHPWord.php on line 226" is thrown when the specified template file is not found
3334
- Bugfix: (RomanSyroeshko) GH-34 - PHPWord_Shared_String.IsUTF8 returns FALSE for Cyrillic UTF-8 input
3435
- Bugfix: (RomanSyroeshko) GH-38 - Temporary files naming logic in PHPWord_Template can lead to a collision
@@ -37,12 +38,12 @@ Fixed in branch for release 0.7.0 :
3738
- Feature: (kaystrobach) - Word2007 : Add rowspan and colspan to cells
3839
- Feature: (RLovelett) - Word2007 : Support for tab stops
3940
- Feature: (RLovelett) - Word2007 : Support Multiple headers
40-
- Feature: (gavroche) - Word2007 : Wrapping Styles to Images
41+
- Feature: (gabrielbull) - Word2007 : Wrapping Styles to Images
4142
- General: (MarkBaker) - Add superscript/subscript styling in Excel2007 Writer
4243
- General: (deds) - add indentation support to paragraphs
4344
- General: (Progi1984) GH-27 - Support for Composer
4445
- General: (Progi1984) - Basic CI with Travis
4546
- General: (Progi1984) - Added PHPWord_Exception and exception when could not copy the template
4647
- General: (Progi1984) - IMPROVED : Moved examples out of Classes directory
4748
- General: (Esmeraldo) CP-49 - IMPROVED : Advanced string replace in setValue for Template
48-
- Feature: (gavroche) - Added support for image wrapping style
49+
- Feature: (gabrielbull) - Added support for image wrapping style

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
},
1212
{
1313
"name": "Gabriel Bull",
14-
"email": "[email protected]"
14+
"email": "[email protected]",
15+
"homepage": "http://gabrielbull.com/"
1516
},
1617
{
1718
"name": "Franck Lefevre",

Tests/phpunit.xml.dist renamed to phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
syntaxCheck="false">
1111
<testsuites>
1212
<testsuite name="PHPWord Unit Test Suite">
13-
<directory>./PHPWord/</directory>
13+
<directory>./Tests/PHPWord/</directory>
1414
</testsuite>
1515
</testsuites>
1616
<filter>

0 commit comments

Comments
 (0)