Merge pull request #75 from gabrielbull/master

Added PSR-4 Autoloader and move phpunit.xml.dist back to root of project
This commit is contained in:
Progi1984 2014-03-02 19:59:32 +01:00
commit 7080ebe1af
5 changed files with 43 additions and 7 deletions

View File

@ -31,9 +31,13 @@ if (!defined('PHPWORD_BASE_PATH')) {
/**
* Class PHPWord_Autoloader
*
* TODO: remove legacy autoloader once everything is moved to namespaces
*/
class PHPWord_Autoloader
{
const PREFIX = 'PHPWord';
/**
* Register the autoloader
*
@ -41,7 +45,8 @@ class PHPWord_Autoloader
*/
public static function register()
{
spl_autoload_register(array('PHPWord_Autoloader', 'load'));
spl_autoload_register(array('PHPWord_Autoloader', 'load')); // Legacy
spl_autoload_register(array(new self, 'autoload')); // PSR-4
}
/**
@ -60,4 +65,21 @@ class PHPWord_Autoloader
return null;
}
/**
* Autoloader
*
* @param string
*/
public static function autoload($class)
{
$prefixLength = strlen(self::PREFIX);
if (0 === strncmp(self::PREFIX, $class, $prefixLength)) {
$file = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, $prefixLength));
$file = realpath(__DIR__ . (empty($file) ? '' : DIRECTORY_SEPARATOR) . $file . '.php');
if (file_exists($file)) {
require_once $file;
}
}
}
}

View File

@ -3,6 +3,7 @@ namespace PHPWord\Tests;
use PHPUnit_Framework_TestCase;
use PHPWord_Autoloader;
use PHPWord_Autoloader as Autoloader;
class AutoloaderTest extends PHPUnit_Framework_TestCase
{
@ -10,11 +11,22 @@ class AutoloaderTest extends PHPUnit_Framework_TestCase
{
PHPWord_Autoloader::register();
$this->assertContains(array('PHPWord_Autoloader', 'load'), spl_autoload_functions());
$this->assertContains(array('PHPWord_Autoloader', 'autoload'), spl_autoload_functions());
}
public function testAutoload()
public function testAutoloadLegacy()
{
$this->assertNull(PHPWord_Autoloader::load('Foo'), 'PHPWord_Autoloader::load() is trying to load classes outside of the PHPWord namespace');
$this->assertTrue(PHPWord_Autoloader::load('PHPWord'), 'PHPWord_Autoloader::load() failed to autoload the PHPWord class');
}
public function testAutoload()
{
$declared = get_declared_classes();
$declaredCount = count($declared);
Autoloader::autoload('Foo');
$this->assertEquals($declaredCount, count(get_declared_classes()), 'PHPWord\\Autoloader::autoload() is trying to load classes outside of the PHPWord namespace');
Autoloader::autoload('PHPWord\\Exceptions\\InvalidStyleException'); // TODO change this class to the main PHPWord class when it is namespaced
$this->assertTrue(in_array('PHPWord\\Exceptions\\InvalidStyleException', get_declared_classes()), 'PHPWord\\Autoloader::autoload() failed to autoload the PHPWord\\Exceptions\\InvalidStyleException class');
}
}

View File

@ -26,9 +26,10 @@ Changes in branch for release 0.7.1 :
- Bugfix: (gabrielbull) - Fixed bug with cell styling
- Bugfix: (gabrielbull) - Fixed bug list items inside of cells
- Feature: (gabrielbull) - Word2007 : Support sections page numbering
- Feature: (gabrielbull) - Word2007 : Added support for line height
- QA: (Progi1984) - UnitTests
Fixed in branch for release 0.7.0 :
Changes in branch for release 0.7.0 :
- 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
- Bugfix: (RomanSyroeshko) GH-34 - PHPWord_Shared_String.IsUTF8 returns FALSE for Cyrillic UTF-8 input
- Bugfix: (RomanSyroeshko) GH-38 - Temporary files naming logic in PHPWord_Template can lead to a collision
@ -37,7 +38,7 @@ Fixed in branch for release 0.7.0 :
- Feature: (kaystrobach) - Word2007 : Add rowspan and colspan to cells
- Feature: (RLovelett) - Word2007 : Support for tab stops
- Feature: (RLovelett) - Word2007 : Support Multiple headers
- Feature: (gavroche) - Word2007 : Wrapping Styles to Images
- Feature: (gabrielbull) - Word2007 : Wrapping Styles to Images
- General: (MarkBaker) - Add superscript/subscript styling in Excel2007 Writer
- General: (deds) - add indentation support to paragraphs
- General: (Progi1984) GH-27 - Support for Composer
@ -45,4 +46,4 @@ Fixed in branch for release 0.7.0 :
- General: (Progi1984) - Added PHPWord_Exception and exception when could not copy the template
- General: (Progi1984) - IMPROVED : Moved examples out of Classes directory
- General: (Esmeraldo) CP-49 - IMPROVED : Advanced string replace in setValue for Template
- Feature: (gavroche) - Added support for image wrapping style
- Feature: (gabrielbull) - Added support for image wrapping style

View File

@ -11,7 +11,8 @@
},
{
"name": "Gabriel Bull",
"email": "gavroche.bull@gmail.com"
"email": "me@gabrielbull.com",
"homepage": "http://gabrielbull.com/"
},
{
"name": "Franck Lefevre",

View File

@ -10,7 +10,7 @@
syntaxCheck="false">
<testsuites>
<testsuite name="PHPWord Unit Test Suite">
<directory>./PHPWord/</directory>
<directory>./Tests/PHPWord/</directory>
</testsuite>
</testsuites>
<filter>