Skip to content

Commit 639f396

Browse files
authored
Autoload : Allow to use PHPWord without Composer (PHPOffice#2722)
1 parent b285578 commit 639f396

File tree

11 files changed

+221
-53
lines changed

11 files changed

+221
-53
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ The following is a basic usage example of the PHPWord library.
8181

8282
```php
8383
<?php
84-
require_once 'bootstrap.php';
8584

8685
// Creating the new document...
8786
$phpWord = new \PhpOffice\PhpWord\PhpWord();

bootstrap.php

-28
This file was deleted.

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@
9595
"php samples/Sample_41_TemplateSetChart.php",
9696
"php samples/Sample_42_TemplateSetCheckbox.php",
9797
"php samples/Sample_43_RTLDefault.php",
98-
"php samples/Sample_44_ExtractVariablesFromReaderWord2007.php"
98+
"php samples/Sample_44_ExtractVariablesFromReaderWord2007.php",
99+
"php samples/Sample_45_Autoloader.php"
99100
]
100101
},
101102
"scripts-descriptions": {

docs/changes/1.x/1.4.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Writer ODText: Support for ListItemRun by [@Progi1984](https://github.com/Progi1984) fixing [#2159](https://github.com/PHPOffice/PHPWord/issues/2159), [#2620](https://github.com/PHPOffice/PHPWord/issues/2620) in [#2669](https://github.com/PHPOffice/PHPWord/pull/2669)
88
- Writer HTML: Support for vAlign in Tables by [@SpraxDev](https://github.com/SpraxDev) in [#2675](https://github.com/PHPOffice/PHPWord/pull/2675)
99
- Added support for PHP 8.4 by [@Progi1984](https://github.com/Progi1984) in [#2660](https://github.com/PHPOffice/PHPWord/pull/2660)
10+
- Autoload : Allow to use PHPWord without Composer fixing [#2543](https://github.com/PHPOffice/PHPWord/issues/2543), [#2552](https://github.com/PHPOffice/PHPWord/issues/2552), [#2716](https://github.com/PHPOffice/PHPWord/issues/2716), [#2717](https://github.com/PHPOffice/PHPWord/issues/2717) in [#2722](https://github.com/PHPOffice/PHPWord/pull/2722)
1011

1112
### Bug fixes
1213

docs/install.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ To install via Composer, add the following lines to your `composer.json`:
3232
To install manually:
3333

3434
* [download PHPOffice\PHPWord package from GitHub](https://github.com/PHPOffice/PHPWord/archive/master.zip)
35-
* [download PHPOffice\Common package from GitHub](https://github.com/PHPOffice/Common/archive/master.zip)
3635
* extract the package and put the contents to your machine.
3736

3837

@@ -42,11 +41,10 @@ To install manually:
4241
require_once 'path/to/PHPWord/src/PhpWord/Autoloader.php';
4342
\PhpOffice\PhpWord\Autoloader::register();
4443

45-
require_once 'path/to/PhpOffice/Common/src/Common/Autoloader.php';
46-
\PhpOffice\Common\Autoloader::register();
47-
4844
```
4945

46+
The preferred method is the Composer one.
47+
5048
## Samples
5149

5250
After installation, you can browse and use the samples that we've provided, either by command line or using browser. If you can access your PhpWord library folder using browser, point your browser to the `samples` folder, e.g. `http://localhost/PhpWord/samples/`.

docs/usage/introduction.md

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ are provided in the [samples folder](https://github.com/PHPOffice/PHPWord/tree/m
77

88
``` php
99
<?php
10-
require_once 'bootstrap.php';
1110

1211
// Creating the new document...
1312
$phpWord = new \PhpOffice\PhpWord\PhpWord();

samples/Sample_45_Autoloader.php

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
use PhpOffice\PhpWord\Style\Font;
4+
5+
define('USE_AUTOLOADER', true);
6+
7+
include_once 'Sample_Header.php';
8+
9+
// New Word Document
10+
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
11+
12+
$languageEnGb = new PhpOffice\PhpWord\Style\Language(PhpOffice\PhpWord\Style\Language::EN_GB);
13+
14+
$phpWord = new PhpOffice\PhpWord\PhpWord();
15+
$phpWord->getSettings()->setThemeFontLang($languageEnGb);
16+
17+
$fontStyleName = 'rStyle';
18+
$phpWord->addFontStyle($fontStyleName, ['bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true]);
19+
20+
$paragraphStyleName = 'pStyle';
21+
$phpWord->addParagraphStyle($paragraphStyleName, ['alignment' => PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'spaceAfter' => 100]);
22+
23+
$phpWord->addTitleStyle(1, ['bold' => true], ['spaceAfter' => 240]);
24+
25+
// New portrait section
26+
$section = $phpWord->addSection();
27+
28+
// Simple text
29+
$section->addTitle('Welcome to PhpWord', 1);
30+
$section->addText('Hello World!');
31+
32+
// $pStyle = new Font();
33+
// $pStyle->setLang()
34+
$section->addText('Ce texte-ci est en français.', ['lang' => PhpOffice\PhpWord\Style\Language::FR_BE]);
35+
36+
// Two text break
37+
$section->addTextBreak(2);
38+
39+
// Define styles
40+
$section->addText('I am styled by a font style definition.', $fontStyleName);
41+
$section->addText('I am styled by a paragraph style definition.', null, $paragraphStyleName);
42+
$section->addText('I am styled by both font and paragraph style.', $fontStyleName, $paragraphStyleName);
43+
44+
$section->addTextBreak();
45+
46+
// Inline font style
47+
$fontStyle['name'] = 'Times New Roman';
48+
$fontStyle['size'] = 20;
49+
50+
$textrun = $section->addTextRun();
51+
$textrun->addText('I am inline styled ', $fontStyle);
52+
$textrun->addText('with ');
53+
$textrun->addText('color', ['color' => '996699']);
54+
$textrun->addText(', ');
55+
$textrun->addText('bold', ['bold' => true]);
56+
$textrun->addText(', ');
57+
$textrun->addText('italic', ['italic' => true]);
58+
$textrun->addText(', ');
59+
$textrun->addText('underline', ['underline' => 'dash']);
60+
$textrun->addText(', ');
61+
$textrun->addText('strikethrough', ['strikethrough' => true]);
62+
$textrun->addText(', ');
63+
$textrun->addText('doubleStrikethrough', ['doubleStrikethrough' => true]);
64+
$textrun->addText(', ');
65+
$textrun->addText('superScript', ['superScript' => true]);
66+
$textrun->addText(', ');
67+
$textrun->addText('subScript', ['subScript' => true]);
68+
$textrun->addText(', ');
69+
$textrun->addText('smallCaps', ['smallCaps' => true]);
70+
$textrun->addText(', ');
71+
$textrun->addText('allCaps', ['allCaps' => true]);
72+
$textrun->addText(', ');
73+
$textrun->addText('fgColor', ['fgColor' => 'yellow']);
74+
$textrun->addText(', ');
75+
$textrun->addText('scale', ['scale' => 200]);
76+
$textrun->addText(', ');
77+
$textrun->addText('spacing', ['spacing' => 120]);
78+
$textrun->addText(', ');
79+
$textrun->addText('kerning', ['kerning' => 10]);
80+
$textrun->addText('. ');
81+
82+
// Link
83+
$section->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub');
84+
$section->addTextBreak();
85+
86+
// Image
87+
$section->addImage(__DIR__ . '/resources/_earth.jpg', ['width' => 18, 'height' => 18]);
88+
89+
// Save file
90+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
91+
if (!CLI) {
92+
include_once 'Sample_Footer.php';
93+
}

samples/Sample_Header.php

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
<?php
2-
require_once __DIR__ . '/../bootstrap.php';
2+
3+
$vendorDirPath = realpath(__DIR__ . '/../vendor');
4+
5+
if ((defined('USE_AUTOLOADER') && USE_AUTOLOADER == true)
6+
|| !file_exists($vendorDirPath . '/autoload.php')) {
7+
// PhpWord
8+
require_once __DIR__ . '/../src/PhpWord/Autoloader.php';
9+
PhpOffice\PhpWord\Autoloader::register();
10+
} else {
11+
require $vendorDirPath . '/autoload.php';
12+
$dompdfPath = $vendorDirPath . '/dompdf/dompdf';
13+
if (file_exists($dompdfPath)) {
14+
define('DOMPDF_ENABLE_AUTOLOAD', false);
15+
}
16+
}
317

418
use PhpOffice\PhpWord\Settings;
519

@@ -12,9 +26,7 @@
1226

1327
Settings::loadConfig();
1428

15-
$dompdfPath = $vendorDirPath . '/dompdf/dompdf';
16-
if (file_exists($dompdfPath)) {
17-
define('DOMPDF_ENABLE_AUTOLOAD', false);
29+
if (defined('DOMPDF_ENABLE_AUTOLOAD')) {
1830
Settings::setPdfRenderer(Settings::PDF_RENDERER_DOMPDF, $vendorDirPath . '/dompdf/dompdf');
1931
}
2032

@@ -60,14 +72,8 @@
6072

6173
/**
6274
* Write documents.
63-
*
64-
* @param PhpOffice\PhpWord\PhpWord $phpWord
65-
* @param string $filename
66-
* @param array $writers
67-
*
68-
* @return string
6975
*/
70-
function write($phpWord, $filename, $writers)
76+
function write(PhpOffice\PhpWord\PhpWord $phpWord, string $filename, array $writers): string
7177
{
7278
$result = '';
7379

@@ -90,13 +96,8 @@ function write($phpWord, $filename, $writers)
9096

9197
/**
9298
* Get ending notes.
93-
*
94-
* @param array $writers
95-
* @param mixed $filename
96-
*
97-
* @return string
9899
*/
99-
function getEndingNotes($writers, $filename)
100+
function getEndingNotes(array $writers, string $filename): string
100101
{
101102
$result = '';
102103

src/PhpWord/Autoloader.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
declare(strict_types=1);
19+
20+
namespace PhpOffice\PhpWord;
21+
22+
class Autoloader
23+
{
24+
/** @const string */
25+
const NAMESPACE_PREFIX = 'PhpOffice\\PhpWord\\';
26+
27+
public static function register(): void
28+
{
29+
spl_autoload_register([new self(), 'autoload']);
30+
}
31+
32+
public static function autoload(string $class): void
33+
{
34+
$prefixLength = strlen(self::NAMESPACE_PREFIX);
35+
if (0 === strncmp(self::NAMESPACE_PREFIX, $class, $prefixLength)) {
36+
$file = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, $prefixLength));
37+
$file = realpath(__DIR__ . (empty($file) ? '' : DIRECTORY_SEPARATOR) . $file . '.php');
38+
if (!$file) {
39+
return;
40+
}
41+
if (file_exists($file)) {
42+
/** @noinspection PhpIncludeInspection Dynamic includes */
43+
require_once $file;
44+
}
45+
}
46+
}
47+
}

tests/PhpWordTests/AutoloaderTest.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpWordTests;
20+
21+
use PhpOffice\PhpWord\Autoloader;
22+
use PHPUnit\Framework\TestCase;
23+
24+
/**
25+
* Test class for PhpOffice\PhpWord\Autoloader.
26+
*/
27+
class AutoloaderTest extends TestCase
28+
{
29+
public function testRegister(): void
30+
{
31+
Autoloader::register();
32+
$splFunctions = spl_autoload_functions();
33+
// @phpstan-ignore-next-line spl_autoload_functions return false < PHP 8.0
34+
if ($splFunctions === false) {
35+
$splFunctions = [];
36+
}
37+
38+
self::assertContains(
39+
['PhpOffice\\PhpWord\\Autoloader', 'autoload'],
40+
$splFunctions
41+
);
42+
}
43+
44+
public function testAutoload(): void
45+
{
46+
$declared = get_declared_classes();
47+
$declaredCount = count($declared);
48+
Autoloader::autoload('Foo');
49+
self::assertCount(
50+
$declaredCount,
51+
get_declared_classes(),
52+
'PhpOffice\\PhpWord\\Autoloader::autoload() is trying to load ' .
53+
'classes outside of the PhpOffice\\PhpWord namespace'
54+
);
55+
}
56+
}

tests/bootstrap.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*
1616
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
1717
*/
18-
require_once __DIR__ . '/../bootstrap.php';
18+
19+
require dirname(__DIR__) . '/vendor/autoload.php';
1920

2021
date_default_timezone_set('UTC');
2122

0 commit comments

Comments
 (0)