Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| IOFactory | |
100.00% |
16 / 16 |
|
100.00% |
5 / 5 |
11 | |
100.00% |
1 / 1 |
| createWriter | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| createReader | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| load | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| loadClass | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
| isConcreteClass | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of PHPProject - A pure PHP library for reading and writing |
| 5 | * presentations documents. |
| 6 | * |
| 7 | * PHPProject 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 | * @link https://github.com/PHPOffice/PHPProject |
| 15 | * @copyright 2009-2014 PHPProject contributors |
| 16 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 17 | */ |
| 18 | |
| 19 | declare(strict_types=1); |
| 20 | |
| 21 | namespace PhpOffice\PhpProject; |
| 22 | |
| 23 | /** |
| 24 | * IOFactory |
| 25 | */ |
| 26 | class IOFactory |
| 27 | { |
| 28 | /** |
| 29 | * Autoresolve classes |
| 30 | * |
| 31 | * @var array |
| 32 | */ |
| 33 | private static $autoResolveClasses = array('GanttProject'); |
| 34 | |
| 35 | /** |
| 36 | * Create writer |
| 37 | * |
| 38 | * @param PhpProject $phpProject |
| 39 | * @param string $name |
| 40 | * @return \PhpOffice\PhpProject\Writer\WriterInterface |
| 41 | */ |
| 42 | public static function createWriter(PhpProject $phpProject, string $name = 'GanttProject'): Writer\WriterInterface |
| 43 | { |
| 44 | $class = 'PhpOffice\\PhpProject\\Writer\\' . $name; |
| 45 | return self::loadClass($class, $name, 'writer', $phpProject); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Create reader |
| 50 | * |
| 51 | * @param string $name |
| 52 | * @return \PhpOffice\PhpProject\Reader\ReaderInterface |
| 53 | */ |
| 54 | public static function createReader(string $name = 'GanttProject'): Reader\ReaderInterface |
| 55 | { |
| 56 | $class = 'PhpOffice\\PhpProject\\Reader\\' . $name; |
| 57 | return self::loadClass($class, $name, 'reader'); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Loads PHPProject from file using automatic \PhpOffice\PhpProject\Reader\ReaderInterface resolution |
| 62 | * |
| 63 | * @param string $pFilename |
| 64 | * @return PhpProject |
| 65 | * @throws \Exception |
| 66 | */ |
| 67 | public static function load(string $pFilename): PhpProject |
| 68 | { |
| 69 | // Try loading using self::$autoResolveClasses |
| 70 | foreach (self::$autoResolveClasses as $autoResolveClass) { |
| 71 | $reader = self::createReader($autoResolveClass); |
| 72 | if ($reader->canRead($pFilename)) { |
| 73 | return $reader->load($pFilename); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | throw new \Exception("Could not automatically determine \PhpOffice\PhpProject\Reader\ReaderInterface for file."); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Load class |
| 82 | * |
| 83 | * @param string $class |
| 84 | * @param string $name |
| 85 | * @param string $type |
| 86 | * @param \PhpOffice\PhpProject\PhpProject $phpProject |
| 87 | * @throws \Exception |
| 88 | * @return \PhpOffice\PhpProject\Reader\ReaderInterface|\PhpOffice\PhpProject\Writer\WriterInterface |
| 89 | */ |
| 90 | private static function loadClass(string $class, string $name, string $type, ?PhpProject $phpProject = null) |
| 91 | { |
| 92 | if (class_exists($class) && self::isConcreteClass($class)) { |
| 93 | if (is_null($phpProject)) { |
| 94 | return new $class(); |
| 95 | } else { |
| 96 | return new $class($phpProject); |
| 97 | } |
| 98 | } else { |
| 99 | throw new \Exception('"'.$name.'" is not a valid '.$type.'.'); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Is it a concrete class? |
| 105 | * |
| 106 | * @param string $class |
| 107 | * @return bool |
| 108 | */ |
| 109 | private static function isConcreteClass(string $class): bool |
| 110 | { |
| 111 | $reflection = new \ReflectionClass($class); |
| 112 | |
| 113 | return !$reflection->isAbstract() && !$reflection->isInterface(); |
| 114 | } |
| 115 | } |