Skip to content

Commit 421b6e6

Browse files
committed
Made autoloader PSR-4 compliant and removed PHPWORD_BASE_DIR global constant
1 parent a7444cb commit 421b6e6

File tree

10 files changed

+42
-52
lines changed

10 files changed

+42
-52
lines changed

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 Test Suite">
13-
<directory>./tests/PhpWord/</directory>
13+
<directory>./test/PhpWord/</directory>
1414
</testsuite>
1515
</testsuites>
1616
<filter>

src/PhpWord/Autoloader.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525

2626
namespace PhpOffice\PhpWord;
2727

28-
if (!\defined('PHPWORD_BASE_DIR')) {
29-
\define('PHPWORD_BASE_DIR', \realpath(__DIR__) . \DIRECTORY_SEPARATOR);
30-
}
31-
3228
class Autoloader
3329
{
3430
const NAMESPACE_PREFIX = 'PhpOffice\\PhpWord\\';
@@ -38,27 +34,21 @@ class Autoloader
3834
*/
3935
public static function register()
4036
{
41-
\spl_autoload_register(array(new self, 'autoload'));
37+
spl_autoload_register(array(new self, 'autoload'));
4238
}
4339

4440
/**
45-
* @param string $fqClassName
41+
* @param string $class
4642
*/
47-
public static function autoload($fqClassName)
43+
public static function autoload($class)
4844
{
49-
$namespacePrefixLength = \strlen(self::NAMESPACE_PREFIX);
50-
$className = \substr($fqClassName, $namespacePrefixLength);
51-
52-
if (0 === \strncmp(self::NAMESPACE_PREFIX, $fqClassName, $namespacePrefixLength)) {
53-
$fqFilename = \PHPWORD_BASE_DIR
54-
. \str_replace('\\', \DIRECTORY_SEPARATOR, $className)
55-
. '.php';
56-
57-
if (\file_exists($fqFilename)) {
58-
require_once $fqFilename;
59-
} else {
60-
throw new \Exception("Could not instantiate class.");
45+
$prefixLength = strlen(self::NAMESPACE_PREFIX);
46+
if (0 === strncmp(self::NAMESPACE_PREFIX, $class, $prefixLength)) {
47+
$file = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, $prefixLength));
48+
$file = realpath(__DIR__ . (empty($file) ? '' : DIRECTORY_SEPARATOR) . $file . '.php');
49+
if (file_exists($file)) {
50+
require_once $file;
6151
}
6252
}
6353
}
64-
}
54+
}

src/PhpWord/PhpWord.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@
3131
use PhpOffice\PhpWord\Style;
3232
use PhpOffice\PhpWord\Template;
3333

34-
// @codeCoverageIgnoreStart
35-
if (!defined('PHPWORD_BASE_DIR')) {
36-
define('PHPWORD_BASE_DIR', \realpath(__DIR__) . \DIRECTORY_SEPARATOR);
37-
require \PHPWORD_BASE_DIR . 'Autoloader.php';
38-
\PhpOffice\PhpWord\Autoloader::register();
39-
}
40-
// @codeCoverageIgnoreEnd
41-
4234
class PhpWord
4335
{
4436
const DEFAULT_FONT_COLOR = '000000'; // HEX

src/PhpWord/Reader/Word2007.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@
3030
use PhpOffice\PhpWord\Exceptions\Exception;
3131
use PhpOffice\PhpWord\Shared\File;
3232

33-
if (!defined('PHPWORD_BASE_DIR')) {
34-
define('PHPWORD_BASE_DIR', \dirname(__FILE__) . '/../../');
35-
require(PHPWORD_BASE_DIR . 'Autoloader.php');
36-
}
37-
3833
class Word2007 extends AbstractReader implements IReader
3934
{
4035
/**

src/PhpWord/Section.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public function addObject($src, $style = null)
240240
$ext = substr($ext, 0, -1);
241241
}
242242

243-
$iconSrc = \PHPWORD_BASE_DIR . '_staticDocParts/';
243+
$iconSrc = __DIR__ . '/_staticDocParts/';
244244
if (!file_exists($iconSrc . '_' . $ext . '.png')) {
245245
$iconSrc = $iconSrc . '_default.png';
246246
} else {

src/PhpWord/Section/Table/Cell.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public function addObject($src, $style = null)
260260
$ext = substr($ext, 0, -1);
261261
}
262262

263-
$iconSrc = \PHPWORD_BASE_DIR . '_staticDocParts/';
263+
$iconSrc = __DIR__ . '/../../_staticDocParts/';
264264
if (!file_exists($iconSrc . '_' . $ext . '.png')) {
265265
$iconSrc = $iconSrc . '_default.png';
266266
} else {

src/PhpWord/Writer/Word2007.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ public function save($pFilename = null)
187187
$objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->_document));
188188

189189
// Write static files
190-
$objZip->addFile(\PHPWORD_BASE_DIR . '_staticDocParts/numbering.xml', 'word/numbering.xml');
191-
$objZip->addFile(\PHPWORD_BASE_DIR . '_staticDocParts/settings.xml', 'word/settings.xml');
192-
$objZip->addFile(\PHPWORD_BASE_DIR . '_staticDocParts/theme1.xml', 'word/theme/theme1.xml');
193-
$objZip->addFile(\PHPWORD_BASE_DIR . '_staticDocParts/webSettings.xml', 'word/webSettings.xml');
194-
$objZip->addFile(\PHPWORD_BASE_DIR . '_staticDocParts/fontTable.xml', 'word/fontTable.xml');
190+
$objZip->addFile(__DIR__ . '/../_staticDocParts/numbering.xml', 'word/numbering.xml');
191+
$objZip->addFile(__DIR__ . '/../_staticDocParts/settings.xml', 'word/settings.xml');
192+
$objZip->addFile(__DIR__ . '/../_staticDocParts/theme1.xml', 'word/theme/theme1.xml');
193+
$objZip->addFile(__DIR__ . '/../_staticDocParts/webSettings.xml', 'word/webSettings.xml');
194+
$objZip->addFile(__DIR__ . '/../_staticDocParts/fontTable.xml', 'word/fontTable.xml');
195195

196196

197197
// Close file

test/PhpWord/Tests/_includes/TestHelperDOCX.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ class TestHelperDOCX
1010
static protected $file;
1111

1212
/**
13-
* @param \PhpOffice\PhpWord\PhpWord $phpWord
14-
* @return \PhpWord\Tests\XmlDocument
13+
* @param \PhpOffice\PhpWord\PhpWord $phpWord
14+
* @param string $writerName
15+
* @return \PhpOffice\PhpWord\Tests\XmlDocument
1516
*/
1617
public static function getDocument(PhpWord $phpWord, $writerName = 'Word2007')
1718
{
@@ -68,4 +69,4 @@ public static function getFile()
6869
{
6970
return self::$file;
7071
}
71-
}
72+
}

test/PhpWord/Tests/_includes/XmlDocument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,4 @@ public function elementExists($path, $file = 'word/document.xml')
110110
$nodeList = $this->getNodeList($path, $file);
111111
return !($nodeList->length == 0);
112112
}
113-
}
113+
}

test/bootstrap.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
<?php
2-
\date_default_timezone_set('UTC');
2+
date_default_timezone_set('UTC');
33

44
// defining base dir for tests
5-
if (!\defined('PHPWORD_TESTS_BASE_DIR')) {
6-
\define('PHPWORD_TESTS_BASE_DIR', \realpath(__DIR__ . '/..'));
5+
if (!defined('PHPWORD_TESTS_BASE_DIR')) {
6+
define('PHPWORD_TESTS_BASE_DIR', realpath(__DIR__ . '/..'));
77
}
88

9-
// loading classes with PSR-4 autoloader
10-
require_once __DIR__ . '/../../src/PhpWord/Autoloader.php';
11-
\PhpOffice\PhpWord\Autoloader::register();
9+
$vendor = realpath(__DIR__ . '/../vendor');
10+
11+
if (file_exists($vendor . "/autoload.php")) {
12+
require $vendor . "/autoload.php";
13+
} else {
14+
$vendor = realpath(__DIR__ . '/../../../');
15+
if (file_exists($vendor . "/autoload.php")) {
16+
require $vendor . "/autoload.php";
17+
} else {
18+
throw new Exception("Unable to load dependencies");
19+
}
20+
}
1221

1322
spl_autoload_register(function ($class) {
1423
$class = ltrim($class, '\\');
@@ -21,4 +30,7 @@
2130
require_once $file;
2231
}
2332
}
24-
});
33+
});
34+
35+
require_once __DIR__ . "/../src/PhpWord/Autoloader.php";
36+
PhpOffice\PhpWord\Autoloader::register();

0 commit comments

Comments
 (0)