Abstract.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_CodeGenerator
  17. * @subpackage PHP
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_CodeGenerator
  25. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. abstract class Zend_CodeGenerator_Abstract
  29. {
  30. /**
  31. * @var string
  32. */
  33. protected $_sourceContent = null;
  34. /**
  35. * @var bool
  36. */
  37. protected $_isSourceDirty = true;
  38. /**
  39. * __construct()
  40. *
  41. * @param array $options
  42. */
  43. public function __construct($options = array())
  44. {
  45. $this->_init();
  46. if ($options != null) {
  47. // use Zend_Config objects if provided
  48. if ($options instanceof Zend_Config) {
  49. $options = $options->toArray();
  50. }
  51. // pass arrays to setOptions
  52. if (is_array($options)) {
  53. $this->setOptions($options);
  54. }
  55. }
  56. $this->_prepare();
  57. }
  58. /**
  59. * setConfig()
  60. *
  61. * @param Zend_Config $config
  62. * @return Zend_CodeGenerator_Abstract
  63. */
  64. public function setConfig(Zend_Config $config)
  65. {
  66. $this->setOptions($config->toArray());
  67. return $this;
  68. }
  69. /**
  70. * setOptions()
  71. *
  72. * @param array $options
  73. * @return Zend_CodeGenerator_Abstract
  74. */
  75. public function setOptions(Array $options)
  76. {
  77. foreach ($options as $optionName => $optionValue) {
  78. $methodName = 'set' . $optionName;
  79. if (method_exists($this, $methodName)) {
  80. $this->{$methodName}($optionValue);
  81. }
  82. }
  83. return $this;
  84. }
  85. /**
  86. * setSourceContent()
  87. *
  88. * @param string $sourceContent
  89. */
  90. public function setSourceContent($sourceContent)
  91. {
  92. $this->_sourceContent = $sourceContent;
  93. return;
  94. }
  95. /**
  96. * getSourceContent()
  97. *
  98. * @return string
  99. */
  100. public function getSourceContent()
  101. {
  102. return $this->_sourceContent;
  103. }
  104. /**
  105. * _init() - this is called before the constuctor
  106. *
  107. */
  108. protected function _init()
  109. {
  110. }
  111. /**
  112. * _prepare() - this is called at construction completion
  113. *
  114. */
  115. protected function _prepare()
  116. {
  117. }
  118. /**
  119. * generate() - must be implemented by the child
  120. *
  121. */
  122. abstract public function generate();
  123. /**
  124. * __toString() - casting to a string will in turn call generate()
  125. *
  126. * @return string
  127. */
  128. final public function __toString()
  129. {
  130. return $this->generate();
  131. }
  132. }