DbTable.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_Tool
  17. * @subpackage Framework
  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_Tool
  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. class Zend_Tool_Project_Provider_DbTable
  29. extends Zend_Tool_Project_Provider_Abstract
  30. implements Zend_Tool_Framework_Provider_Pretendable
  31. {
  32. protected $_specialties = array('FromDatabase');
  33. /**
  34. * @var Zend_Filter
  35. */
  36. protected $_nameFilter = null;
  37. public static function createResource(Zend_Tool_Project_Profile $profile, $dbTableName, $actualTableName, $moduleName = null)
  38. {
  39. $profileSearchParams = array();
  40. if ($moduleName != null && is_string($moduleName)) {
  41. $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
  42. }
  43. $profileSearchParams[] = 'modelsDirectory';
  44. $modelsDirectory = $profile->search($profileSearchParams);
  45. if (!($modelsDirectory instanceof Zend_Tool_Project_Profile_Resource)) {
  46. throw new Zend_Tool_Project_Provider_Exception(
  47. 'A models directory was not found' .
  48. (($moduleName) ? ' for module ' . $moduleName . '.' : '.')
  49. );
  50. }
  51. if (!($dbTableDirectory = $modelsDirectory->search('DbTableDirectory'))) {
  52. $dbTableDirectory = $modelsDirectory->createResource('DbTableDirectory');
  53. }
  54. $dbTableFile = $dbTableDirectory->createResource('DbTableFile', array('dbTableName' => $dbTableName, 'actualTableName' => $actualTableName));
  55. return $dbTableFile;
  56. }
  57. public static function hasResource(Zend_Tool_Project_Profile $profile, $dbTableName, $moduleName = null)
  58. {
  59. $profileSearchParams = array();
  60. if ($moduleName != null && is_string($moduleName)) {
  61. $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
  62. }
  63. $profileSearchParams[] = 'modelsDirectory';
  64. $modelsDirectory = $profile->search($profileSearchParams);
  65. if (!($modelsDirectory instanceof Zend_Tool_Project_Profile_Resource)
  66. || !($dbTableDirectory = $modelsDirectory->search('DbTableDirectory'))) {
  67. return false;
  68. }
  69. $dbTableFile = $dbTableDirectory->search(array('DbTableFile' => array('dbTableName' => $dbTableName)));
  70. return ($dbTableFile instanceof Zend_Tool_Project_Profile_Resource) ? true : false;
  71. }
  72. public function create($name, $actualTableName, $module = null, $forceOverwrite = false)
  73. {
  74. $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  75. // Check that there is not a dash or underscore, return if doesnt match regex
  76. if (preg_match('#[_-]#', $name)) {
  77. throw new Zend_Tool_Project_Provider_Exception('DbTable names should be camel cased.');
  78. }
  79. $originalName = $name;
  80. $name = ucfirst($name);
  81. if ($actualTableName == '') {
  82. throw new Zend_Tool_Project_Provider_Exception('You must provide both the DbTable name as well as the actual db table\'s name.');
  83. }
  84. if (self::hasResource($this->_loadedProfile, $name, $module)) {
  85. throw new Zend_Tool_Project_Provider_Exception('This project already has a DbTable named ' . $name);
  86. }
  87. // get request/response object
  88. $request = $this->_registry->getRequest();
  89. $response = $this->_registry->getResponse();
  90. // alert the user about inline converted names
  91. $tense = (($request->isPretend()) ? 'would be' : 'is');
  92. if ($name !== $originalName) {
  93. $response->appendContent(
  94. 'Note: The canonical model name that ' . $tense
  95. . ' used with other providers is "' . $name . '";'
  96. . ' not "' . $originalName . '" as supplied',
  97. array('color' => array('yellow'))
  98. );
  99. }
  100. try {
  101. $tableResource = self::createResource($this->_loadedProfile, $name, $actualTableName, $module);
  102. } catch (Exception $e) {
  103. $response = $this->_registry->getResponse();
  104. $response->setException($e);
  105. return;
  106. }
  107. // do the creation
  108. if ($request->isPretend()) {
  109. $response->appendContent('Would create a DbTable at ' . $tableResource->getContext()->getPath());
  110. } else {
  111. $response->appendContent('Creating a DbTable at ' . $tableResource->getContext()->getPath());
  112. $tableResource->create();
  113. $this->_storeProfile();
  114. }
  115. }
  116. /**
  117. * @param string $module Module name action should be applied to.
  118. * @param bool $forceOverwrite Whether should force overwriting previous classes generated
  119. * @return void
  120. */
  121. public function createFromDatabase($module = null, $forceOverwrite = false)
  122. {
  123. $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  124. $bootstrapResource = $this->_loadedProfile->search('BootstrapFile');
  125. /* @var $zendApp Zend_Application */
  126. $zendApp = $bootstrapResource->getApplicationInstance();
  127. try {
  128. $zendApp->bootstrap('db');
  129. } catch (Zend_Application_Exception $e) {
  130. throw new Zend_Tool_Project_Provider_Exception('Db resource not available, you might need to configure a DbAdapter.');
  131. return;
  132. }
  133. /* @var $db Zend_Db_Adapter_Abstract */
  134. $db = $zendApp->getBootstrap()->getResource('db');
  135. $tableResources = array();
  136. foreach ($db->listTables() as $actualTableName) {
  137. $dbTableName = $this->_convertTableNameToClassName($actualTableName);
  138. if (!$forceOverwrite && self::hasResource($this->_loadedProfile, $dbTableName, $module)) {
  139. throw new Zend_Tool_Project_Provider_Exception(
  140. 'This DbTable resource already exists, if you wish to overwrite it, '
  141. . 'pass the "forceOverwrite" flag to this provider.'
  142. );
  143. }
  144. $tableResources[] = self::createResource(
  145. $this->_loadedProfile,
  146. $dbTableName,
  147. $actualTableName,
  148. $module
  149. );
  150. }
  151. if (count($tableResources) == 0) {
  152. $this->_registry->getResponse()->appendContent('There are no tables in the selected database to write.');
  153. }
  154. // do the creation
  155. if ($this->_registry->getRequest()->isPretend()) {
  156. foreach ($tableResources as $tableResource) {
  157. $this->_registry->getResponse()->appendContent('Would create a DbTable at ' . $tableResource->getContext()->getPath());
  158. }
  159. } else {
  160. foreach ($tableResources as $tableResource) {
  161. $this->_registry->getResponse()->appendContent('Creating a DbTable at ' . $tableResource->getContext()->getPath());
  162. $tableResource->create();
  163. }
  164. $this->_storeProfile();
  165. }
  166. }
  167. protected function _convertTableNameToClassName($tableName)
  168. {
  169. if ($this->_nameFilter == null) {
  170. $this->_nameFilter = new Zend_Filter();
  171. $this->_nameFilter
  172. ->addFilter(new Zend_Filter_Word_UnderscoreToCamelCase());
  173. }
  174. return $this->_nameFilter->filter($tableName);
  175. }
  176. }