-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autoload : Allow to use PHPWord without Composer
- Loading branch information
Showing
11 changed files
with
221 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters