IOFactory.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * PHPWord
  4. *
  5. * Copyright (c) 2011 PHPWord
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPWord
  22. * @package PHPWord
  23. * @copyright Copyright (c) 010 PHPWord
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version Beta 0.6.3, 08.07.2011
  26. */
  27. /**
  28. * PHPWord_IOFactory
  29. *
  30. * @category PHPWord
  31. * @package PHPWord
  32. * @copyright Copyright (c) 2011 PHPWord
  33. */
  34. class PHPWord_IOFactory {
  35. /**
  36. * Search locations
  37. *
  38. * @var array
  39. */
  40. private static $_searchLocations = array(
  41. array('type' => 'IWriter', 'path' => 'PHPWord/Writer/{0}.php', 'class' => 'PHPWord_Writer_{0}')
  42. );
  43. /**
  44. * Autoresolve classes
  45. *
  46. * @var array
  47. */
  48. private static $_autoResolveClasses = array(
  49. 'Serialized'
  50. );
  51. /**
  52. * Private constructor for PHPWord_IOFactory
  53. */
  54. private function __construct() { }
  55. /**
  56. * Get search locations
  57. *
  58. * @return array
  59. */
  60. public static function getSearchLocations() {
  61. return self::$_searchLocations;
  62. }
  63. /**
  64. * Set search locations
  65. *
  66. * @param array $value
  67. * @throws Exception
  68. */
  69. public static function setSearchLocations($value) {
  70. if (is_array($value)) {
  71. self::$_searchLocations = $value;
  72. } else {
  73. throw new Exception('Invalid parameter passed.');
  74. }
  75. }
  76. /**
  77. * Add search location
  78. *
  79. * @param string $type Example: IWriter
  80. * @param string $location Example: PHPWord/Writer/{0}.php
  81. * @param string $classname Example: PHPWord_Writer_{0}
  82. */
  83. public static function addSearchLocation($type = '', $location = '', $classname = '') {
  84. self::$_searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname );
  85. }
  86. /**
  87. * Create PHPWord_Writer_IWriter
  88. *
  89. * @param PHPWord $PHPWord
  90. * @param string $writerType Example: Word2007
  91. * @return PHPWord_Writer_IWriter
  92. */
  93. public static function createWriter(PHPWord $PHPWord, $writerType = '') {
  94. $searchType = 'IWriter';
  95. foreach (self::$_searchLocations as $searchLocation) {
  96. if ($searchLocation['type'] == $searchType) {
  97. $className = str_replace('{0}', $writerType, $searchLocation['class']);
  98. $classFile = str_replace('{0}', $writerType, $searchLocation['path']);
  99. $instance = new $className($PHPWord);
  100. if(!is_null($instance)) {
  101. return $instance;
  102. }
  103. }
  104. }
  105. throw new Exception("No $searchType found for type $writerType");
  106. }
  107. }
  108. ?>