Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoload : Allow to use PHPWord without Composer #2722

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ The following is a basic usage example of the PHPWord library.

```php
<?php
require_once 'bootstrap.php';

// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();
Expand Down
28 changes: 0 additions & 28 deletions bootstrap.php

This file was deleted.

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
"php samples/Sample_41_TemplateSetChart.php",
"php samples/Sample_42_TemplateSetCheckbox.php",
"php samples/Sample_43_RTLDefault.php",
"php samples/Sample_44_ExtractVariablesFromReaderWord2007.php"
"php samples/Sample_44_ExtractVariablesFromReaderWord2007.php",
"php samples/Sample_45_Autoloader.php"
]
},
"scripts-descriptions": {
Expand Down
1 change: 1 addition & 0 deletions docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- 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)
- Writer HTML: Support for vAlign in Tables by [@SpraxDev](https://github.com/SpraxDev) in [#2675](https://github.com/PHPOffice/PHPWord/pull/2675)
- Added support for PHP 8.4 by [@Progi1984](https://github.com/Progi1984) in [#2660](https://github.com/PHPOffice/PHPWord/pull/2660)
- 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)

### Bug fixes

Expand Down
6 changes: 2 additions & 4 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ To install via Composer, add the following lines to your `composer.json`:
To install manually:

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


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

require_once 'path/to/PhpOffice/Common/src/Common/Autoloader.php';
\PhpOffice\Common\Autoloader::register();

```

The preferred method is the Composer one.

## Samples

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/`.
1 change: 0 additions & 1 deletion docs/usage/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ are provided in the [samples folder](https://github.com/PHPOffice/PHPWord/tree/m

``` php
<?php
require_once 'bootstrap.php';

// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();
Expand Down
93 changes: 93 additions & 0 deletions samples/Sample_45_Autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

use PhpOffice\PhpWord\Style\Font;

define('USE_AUTOLOADER', true);

include_once 'Sample_Header.php';

// New Word Document
echo date('H:i:s') , ' Create new PhpWord object' , EOL;

$languageEnGb = new PhpOffice\PhpWord\Style\Language(PhpOffice\PhpWord\Style\Language::EN_GB);

$phpWord = new PhpOffice\PhpWord\PhpWord();
$phpWord->getSettings()->setThemeFontLang($languageEnGb);

$fontStyleName = 'rStyle';
$phpWord->addFontStyle($fontStyleName, ['bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true]);

$paragraphStyleName = 'pStyle';
$phpWord->addParagraphStyle($paragraphStyleName, ['alignment' => PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'spaceAfter' => 100]);

$phpWord->addTitleStyle(1, ['bold' => true], ['spaceAfter' => 240]);

// New portrait section
$section = $phpWord->addSection();

// Simple text
$section->addTitle('Welcome to PhpWord', 1);
$section->addText('Hello World!');

// $pStyle = new Font();
// $pStyle->setLang()
$section->addText('Ce texte-ci est en français.', ['lang' => PhpOffice\PhpWord\Style\Language::FR_BE]);

// Two text break
$section->addTextBreak(2);

// Define styles
$section->addText('I am styled by a font style definition.', $fontStyleName);
$section->addText('I am styled by a paragraph style definition.', null, $paragraphStyleName);
$section->addText('I am styled by both font and paragraph style.', $fontStyleName, $paragraphStyleName);

$section->addTextBreak();

// Inline font style
$fontStyle['name'] = 'Times New Roman';
$fontStyle['size'] = 20;

$textrun = $section->addTextRun();
$textrun->addText('I am inline styled ', $fontStyle);
$textrun->addText('with ');
$textrun->addText('color', ['color' => '996699']);
$textrun->addText(', ');
$textrun->addText('bold', ['bold' => true]);
$textrun->addText(', ');
$textrun->addText('italic', ['italic' => true]);
$textrun->addText(', ');
$textrun->addText('underline', ['underline' => 'dash']);
$textrun->addText(', ');
$textrun->addText('strikethrough', ['strikethrough' => true]);
$textrun->addText(', ');
$textrun->addText('doubleStrikethrough', ['doubleStrikethrough' => true]);
$textrun->addText(', ');
$textrun->addText('superScript', ['superScript' => true]);
$textrun->addText(', ');
$textrun->addText('subScript', ['subScript' => true]);
$textrun->addText(', ');
$textrun->addText('smallCaps', ['smallCaps' => true]);
$textrun->addText(', ');
$textrun->addText('allCaps', ['allCaps' => true]);
$textrun->addText(', ');
$textrun->addText('fgColor', ['fgColor' => 'yellow']);
$textrun->addText(', ');
$textrun->addText('scale', ['scale' => 200]);
$textrun->addText(', ');
$textrun->addText('spacing', ['spacing' => 120]);
$textrun->addText(', ');
$textrun->addText('kerning', ['kerning' => 10]);
$textrun->addText('. ');

// Link
$section->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub');
$section->addTextBreak();

// Image
$section->addImage(__DIR__ . '/resources/_earth.jpg', ['width' => 18, 'height' => 18]);

// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}
35 changes: 18 additions & 17 deletions samples/Sample_Header.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
<?php
require_once __DIR__ . '/../bootstrap.php';

$vendorDirPath = realpath(__DIR__ . '/../vendor');

if ((defined('USE_AUTOLOADER') && USE_AUTOLOADER == true)
|| !file_exists($vendorDirPath . '/autoload.php')) {
// PhpWord
require_once __DIR__ . '/../src/PhpWord/Autoloader.php';
PhpOffice\PhpWord\Autoloader::register();
} else {
require $vendorDirPath . '/autoload.php';
$dompdfPath = $vendorDirPath . '/dompdf/dompdf';
if (file_exists($dompdfPath)) {
define('DOMPDF_ENABLE_AUTOLOAD', false);
}
}

use PhpOffice\PhpWord\Settings;

Expand All @@ -12,9 +26,7 @@

Settings::loadConfig();

$dompdfPath = $vendorDirPath . '/dompdf/dompdf';
if (file_exists($dompdfPath)) {
define('DOMPDF_ENABLE_AUTOLOAD', false);
if (defined('DOMPDF_ENABLE_AUTOLOAD')) {
Settings::setPdfRenderer(Settings::PDF_RENDERER_DOMPDF, $vendorDirPath . '/dompdf/dompdf');
}

Expand Down Expand Up @@ -60,14 +72,8 @@

/**
* Write documents.
*
* @param PhpOffice\PhpWord\PhpWord $phpWord
* @param string $filename
* @param array $writers
*
* @return string
*/
function write($phpWord, $filename, $writers)
function write(PhpOffice\PhpWord\PhpWord $phpWord, string $filename, array $writers): string
{
$result = '';

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

/**
* Get ending notes.
*
* @param array $writers
* @param mixed $filename
*
* @return string
*/
function getEndingNotes($writers, $filename)
function getEndingNotes(array $writers, string $filename): string
{
$result = '';

Expand Down
47 changes: 47 additions & 0 deletions src/PhpWord/Autoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
declare(strict_types=1);

namespace PhpOffice\PhpWord;

class Autoloader
{
/** @const string */
const NAMESPACE_PREFIX = 'PhpOffice\\PhpWord\\';

public static function register(): void
{
spl_autoload_register([new self(), 'autoload']);
}

public static function autoload(string $class): void
{
$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) {
return;
}
if (file_exists($file)) {
/** @noinspection PhpIncludeInspection Dynamic includes */
require_once $file;
}
}
}
}
56 changes: 56 additions & 0 deletions tests/PhpWordTests/AutoloaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWordTests;

use PhpOffice\PhpWord\Autoloader;
use PHPUnit\Framework\TestCase;

/**
* Test class for PhpOffice\PhpWord\Autoloader.
*/
class AutoloaderTest extends TestCase
{
public function testRegister(): void
{
Autoloader::register();
$splFunctions = spl_autoload_functions();
// @phpstan-ignore-next-line spl_autoload_functions return false < PHP 8.0
if ($splFunctions === false) {
$splFunctions = [];
}

self::assertContains(
['PhpOffice\\PhpWord\\Autoloader', 'autoload'],
$splFunctions
);
}

public function testAutoload(): void
{
$declared = get_declared_classes();
$declaredCount = count($declared);
Autoloader::autoload('Foo');
self::assertCount(
$declaredCount,
get_declared_classes(),
'PhpOffice\\PhpWord\\Autoloader::autoload() is trying to load ' .
'classes outside of the PhpOffice\\PhpWord namespace'
);
}
}
3 changes: 2 additions & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
require_once __DIR__ . '/../bootstrap.php';

require dirname(__DIR__) . '/vendor/autoload.php';

date_default_timezone_set('UTC');

Expand Down
Loading