PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Core/CodeGenerator/Abstract.php

https://bitbucket.org/Yuri_m/clean-zfext-by-yuri-git
PHP | 158 lines | 87 code | 17 blank | 54 comment | 6 complexity | a3b9f1ab188526ea93369119ca63f57b MD5 | raw file
  1. <?php
  2. /**
  3. * Abstract code genaretor
  4. *
  5. * @category Core
  6. * @package Core_CodeGenerator
  7. * @author V.Leontiev
  8. *
  9. * @version $Id$
  10. */
  11. abstract class Core_CodeGenerator_Abstract
  12. {
  13. protected $_stringSeparator = "\n";
  14. /**
  15. * @var Zend_Config
  16. */
  17. protected $_config;
  18. /**
  19. * @var Zend_Tool_Project_Profile
  20. */
  21. protected $_profile;
  22. /**
  23. * Initialize start variable
  24. *
  25. * @param Zend_Config $config
  26. * @param Zend_Tool_Project_Profile $profile
  27. * @return void
  28. */
  29. public function init(Zend_Config $config, Zend_Tool_Project_Profile $profile)
  30. {
  31. $this->_config = $config;
  32. $this->_profile = $profile;
  33. }
  34. /**
  35. * Create method use Zend_CodeGenerator_Php_Method
  36. *
  37. * @param string $methodName
  38. * @param array $params
  39. * @param string $body
  40. * @param string $visibility
  41. * @return Zend_CodeGenerator_Php_Method
  42. */
  43. protected function _createMethod($methodName, $params = array(), $body = null, $visibility = 'public')
  44. {
  45. $thisMethodName = '_getBodyFor' . ucfirst($methodName);
  46. if (method_exists($this, $thisMethodName)) {
  47. $body = $this->$thisMethodName();
  48. }
  49. $method = new Zend_CodeGenerator_Php_Method();
  50. $method->setName($methodName);
  51. $method->setVisibility($visibility);
  52. $method->setBody($body);
  53. if (is_array($params) && !empty($params)) {
  54. $method->setParameters($params);
  55. }
  56. return $method;
  57. }
  58. /**
  59. * Generate standart docblock
  60. *
  61. * @return Zend_CodeGenerator_Php_Docblock
  62. */
  63. protected function _generateDocBlock()
  64. {
  65. return new Zend_CodeGenerator_Php_Docblock(array(
  66. 'longDescription' => 'This is a class generated with Zend_CodeGenerator.',
  67. 'tags' => array(
  68. array(
  69. 'name' => 'category',
  70. 'description' => '###CATEGORY###'
  71. ),
  72. array(
  73. 'name' => 'package',
  74. 'description' => '###PACKAGE###'
  75. ),
  76. array(
  77. 'name' => 'subpackage',
  78. 'description' => '###SUBPACKAGE###'
  79. ),
  80. array(
  81. 'name' => 'version',
  82. 'description' => '$Id$',
  83. ),
  84. array(
  85. 'name' => 'license',
  86. 'description' => 'New BSD',
  87. ),
  88. )
  89. ));
  90. }
  91. /**
  92. * Get path to resource
  93. *
  94. * @param string $resourceType
  95. * @return string
  96. */
  97. protected function _getModulePath()
  98. {
  99. $matchSearchConstraints = array(
  100. 'modulesDirectory',
  101. 'moduleDirectory' => array(
  102. 'moduleName' => $this->_config->moduleName
  103. )
  104. );
  105. $result = $this->_profile->search($matchSearchConstraints);
  106. return $result->getPath();
  107. }
  108. /**
  109. * Write body to file
  110. *
  111. * @param string $body
  112. * @param string $filePath
  113. */
  114. protected function _generateFile($body, $filePath)
  115. {
  116. $this->_createDir(dirname($filePath));
  117. if (!file_exists($filePath) || $this->_config->rewrite) {
  118. $fileModel = new Zend_CodeGenerator_Php_File();
  119. $fileModel->setBody($body);
  120. $fileModel->setFilename($filePath);
  121. $fileModel->write();
  122. }
  123. }
  124. /**
  125. * Create not exists directories
  126. *
  127. * @param string $directory directroy path
  128. * @return void
  129. */
  130. protected function _createDir($directory)
  131. {
  132. if (!is_dir($directory)) {
  133. $previewDir = substr($directory, 0, -strlen(strrchr($directory, DIRECTORY_SEPARATOR)));
  134. $this->_createDir($previewDir);
  135. $newDir = substr($directory, -strlen(strrchr($directory, DIRECTORY_SEPARATOR))+1);
  136. chdir($previewDir);
  137. mkdir($newDir);
  138. }
  139. return;
  140. }
  141. }