Made autoloader PSR-4 compliant and removed PHPWORD_BASE_DIR global constant

This commit is contained in:
Gabriel Bull 2014-03-23 12:07:29 -04:00
parent a7444cb482
commit 421b6e6f9d
10 changed files with 42 additions and 52 deletions

View File

@ -10,7 +10,7 @@
syntaxCheck="false"> syntaxCheck="false">
<testsuites> <testsuites>
<testsuite name="PhpWord Test Suite"> <testsuite name="PhpWord Test Suite">
<directory>./tests/PhpWord/</directory> <directory>./test/PhpWord/</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
<filter> <filter>

View File

@ -25,10 +25,6 @@
namespace PhpOffice\PhpWord; namespace PhpOffice\PhpWord;
if (!\defined('PHPWORD_BASE_DIR')) {
\define('PHPWORD_BASE_DIR', \realpath(__DIR__) . \DIRECTORY_SEPARATOR);
}
class Autoloader class Autoloader
{ {
const NAMESPACE_PREFIX = 'PhpOffice\\PhpWord\\'; const NAMESPACE_PREFIX = 'PhpOffice\\PhpWord\\';
@ -38,27 +34,21 @@ class Autoloader
*/ */
public static function register() public static function register()
{ {
\spl_autoload_register(array(new self, 'autoload')); spl_autoload_register(array(new self, 'autoload'));
} }
/** /**
* @param string $fqClassName * @param string $class
*/ */
public static function autoload($fqClassName) public static function autoload($class)
{ {
$namespacePrefixLength = \strlen(self::NAMESPACE_PREFIX); $prefixLength = strlen(self::NAMESPACE_PREFIX);
$className = \substr($fqClassName, $namespacePrefixLength); if (0 === strncmp(self::NAMESPACE_PREFIX, $class, $prefixLength)) {
$file = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, $prefixLength));
if (0 === \strncmp(self::NAMESPACE_PREFIX, $fqClassName, $namespacePrefixLength)) { $file = realpath(__DIR__ . (empty($file) ? '' : DIRECTORY_SEPARATOR) . $file . '.php');
$fqFilename = \PHPWORD_BASE_DIR if (file_exists($file)) {
. \str_replace('\\', \DIRECTORY_SEPARATOR, $className) require_once $file;
. '.php';
if (\file_exists($fqFilename)) {
require_once $fqFilename;
} else {
throw new \Exception("Could not instantiate class.");
} }
} }
} }
} }

View File

@ -31,14 +31,6 @@ use PhpOffice\PhpWord\Section;
use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Template; use PhpOffice\PhpWord\Template;
// @codeCoverageIgnoreStart
if (!defined('PHPWORD_BASE_DIR')) {
define('PHPWORD_BASE_DIR', \realpath(__DIR__) . \DIRECTORY_SEPARATOR);
require \PHPWORD_BASE_DIR . 'Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register();
}
// @codeCoverageIgnoreEnd
class PhpWord class PhpWord
{ {
const DEFAULT_FONT_COLOR = '000000'; // HEX const DEFAULT_FONT_COLOR = '000000'; // HEX

View File

@ -30,11 +30,6 @@ use PhpOffice\PhpWord\DocumentProperties;
use PhpOffice\PhpWord\Exceptions\Exception; use PhpOffice\PhpWord\Exceptions\Exception;
use PhpOffice\PhpWord\Shared\File; use PhpOffice\PhpWord\Shared\File;
if (!defined('PHPWORD_BASE_DIR')) {
define('PHPWORD_BASE_DIR', \dirname(__FILE__) . '/../../');
require(PHPWORD_BASE_DIR . 'Autoloader.php');
}
class Word2007 extends AbstractReader implements IReader class Word2007 extends AbstractReader implements IReader
{ {
/** /**

View File

@ -240,7 +240,7 @@ class Section
$ext = substr($ext, 0, -1); $ext = substr($ext, 0, -1);
} }
$iconSrc = \PHPWORD_BASE_DIR . '_staticDocParts/'; $iconSrc = __DIR__ . '/_staticDocParts/';
if (!file_exists($iconSrc . '_' . $ext . '.png')) { if (!file_exists($iconSrc . '_' . $ext . '.png')) {
$iconSrc = $iconSrc . '_default.png'; $iconSrc = $iconSrc . '_default.png';
} else { } else {

View File

@ -260,7 +260,7 @@ class Cell
$ext = substr($ext, 0, -1); $ext = substr($ext, 0, -1);
} }
$iconSrc = \PHPWORD_BASE_DIR . '_staticDocParts/'; $iconSrc = __DIR__ . '/../../_staticDocParts/';
if (!file_exists($iconSrc . '_' . $ext . '.png')) { if (!file_exists($iconSrc . '_' . $ext . '.png')) {
$iconSrc = $iconSrc . '_default.png'; $iconSrc = $iconSrc . '_default.png';
} else { } else {

View File

@ -187,11 +187,11 @@ class Word2007 implements IWriter
$objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->_document)); $objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->_document));
// Write static files // Write static files
$objZip->addFile(\PHPWORD_BASE_DIR . '_staticDocParts/numbering.xml', 'word/numbering.xml'); $objZip->addFile(__DIR__ . '/../_staticDocParts/numbering.xml', 'word/numbering.xml');
$objZip->addFile(\PHPWORD_BASE_DIR . '_staticDocParts/settings.xml', 'word/settings.xml'); $objZip->addFile(__DIR__ . '/../_staticDocParts/settings.xml', 'word/settings.xml');
$objZip->addFile(\PHPWORD_BASE_DIR . '_staticDocParts/theme1.xml', 'word/theme/theme1.xml'); $objZip->addFile(__DIR__ . '/../_staticDocParts/theme1.xml', 'word/theme/theme1.xml');
$objZip->addFile(\PHPWORD_BASE_DIR . '_staticDocParts/webSettings.xml', 'word/webSettings.xml'); $objZip->addFile(__DIR__ . '/../_staticDocParts/webSettings.xml', 'word/webSettings.xml');
$objZip->addFile(\PHPWORD_BASE_DIR . '_staticDocParts/fontTable.xml', 'word/fontTable.xml'); $objZip->addFile(__DIR__ . '/../_staticDocParts/fontTable.xml', 'word/fontTable.xml');
// Close file // Close file

View File

@ -10,8 +10,9 @@ class TestHelperDOCX
static protected $file; static protected $file;
/** /**
* @param \PhpOffice\PhpWord\PhpWord $phpWord * @param \PhpOffice\PhpWord\PhpWord $phpWord
* @return \PhpWord\Tests\XmlDocument * @param string $writerName
* @return \PhpOffice\PhpWord\Tests\XmlDocument
*/ */
public static function getDocument(PhpWord $phpWord, $writerName = 'Word2007') public static function getDocument(PhpWord $phpWord, $writerName = 'Word2007')
{ {
@ -68,4 +69,4 @@ class TestHelperDOCX
{ {
return self::$file; return self::$file;
} }
} }

View File

@ -110,4 +110,4 @@ class XmlDocument
$nodeList = $this->getNodeList($path, $file); $nodeList = $this->getNodeList($path, $file);
return !($nodeList->length == 0); return !($nodeList->length == 0);
} }
} }

View File

@ -1,14 +1,23 @@
<?php <?php
\date_default_timezone_set('UTC'); date_default_timezone_set('UTC');
// defining base dir for tests // defining base dir for tests
if (!\defined('PHPWORD_TESTS_BASE_DIR')) { if (!defined('PHPWORD_TESTS_BASE_DIR')) {
\define('PHPWORD_TESTS_BASE_DIR', \realpath(__DIR__ . '/..')); define('PHPWORD_TESTS_BASE_DIR', realpath(__DIR__ . '/..'));
} }
// loading classes with PSR-4 autoloader $vendor = realpath(__DIR__ . '/../vendor');
require_once __DIR__ . '/../../src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register(); if (file_exists($vendor . "/autoload.php")) {
require $vendor . "/autoload.php";
} else {
$vendor = realpath(__DIR__ . '/../../../');
if (file_exists($vendor . "/autoload.php")) {
require $vendor . "/autoload.php";
} else {
throw new Exception("Unable to load dependencies");
}
}
spl_autoload_register(function ($class) { spl_autoload_register(function ($class) {
$class = ltrim($class, '\\'); $class = ltrim($class, '\\');
@ -21,4 +30,7 @@ spl_autoload_register(function ($class) {
require_once $file; require_once $file;
} }
} }
}); });
require_once __DIR__ . "/../src/PhpWord/Autoloader.php";
PhpOffice\PhpWord\Autoloader::register();