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">
<testsuites>
<testsuite name="PhpWord Test Suite">
<directory>./tests/PhpWord/</directory>
<directory>./test/PhpWord/</directory>
</testsuite>
</testsuites>
<filter>

View File

@ -25,10 +25,6 @@
namespace PhpOffice\PhpWord;
if (!\defined('PHPWORD_BASE_DIR')) {
\define('PHPWORD_BASE_DIR', \realpath(__DIR__) . \DIRECTORY_SEPARATOR);
}
class Autoloader
{
const NAMESPACE_PREFIX = 'PhpOffice\\PhpWord\\';
@ -38,26 +34,20 @@ class Autoloader
*/
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);
$className = \substr($fqClassName, $namespacePrefixLength);
if (0 === \strncmp(self::NAMESPACE_PREFIX, $fqClassName, $namespacePrefixLength)) {
$fqFilename = \PHPWORD_BASE_DIR
. \str_replace('\\', \DIRECTORY_SEPARATOR, $className)
. '.php';
if (\file_exists($fqFilename)) {
require_once $fqFilename;
} else {
throw new \Exception("Could not instantiate class.");
$prefixLength = strlen(self::NAMESPACE_PREFIX);
if (0 === strncmp(self::NAMESPACE_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

@ -31,14 +31,6 @@ use PhpOffice\PhpWord\Section;
use PhpOffice\PhpWord\Style;
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
{
const DEFAULT_FONT_COLOR = '000000'; // HEX

View File

@ -30,11 +30,6 @@ use PhpOffice\PhpWord\DocumentProperties;
use PhpOffice\PhpWord\Exceptions\Exception;
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
{
/**

View File

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

View File

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

View File

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

View File

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

View File

@ -1,14 +1,23 @@
<?php
\date_default_timezone_set('UTC');
date_default_timezone_set('UTC');
// defining base dir for tests
if (!\defined('PHPWORD_TESTS_BASE_DIR')) {
\define('PHPWORD_TESTS_BASE_DIR', \realpath(__DIR__ . '/..'));
if (!defined('PHPWORD_TESTS_BASE_DIR')) {
define('PHPWORD_TESTS_BASE_DIR', realpath(__DIR__ . '/..'));
}
// loading classes with PSR-4 autoloader
require_once __DIR__ . '/../../src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register();
$vendor = realpath(__DIR__ . '/../vendor');
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) {
$class = ltrim($class, '\\');
@ -22,3 +31,6 @@ spl_autoload_register(function ($class) {
}
}
});
require_once __DIR__ . "/../src/PhpWord/Autoloader.php";
PhpOffice\PhpWord\Autoloader::register();