DbTable.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: View.php 18386 2009-09-23 20:44:43Z ralph $
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Tool
  25. * @copyright Copyright (c) 2005-2010 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. if ($actualTableName == '') {
  76. throw new Zend_Tool_Project_Provider_Exception('You must provide both the DbTable name as well as the actual db table\'s name.');
  77. }
  78. if (self::hasResource($this->_loadedProfile, $name, $module)) {
  79. throw new Zend_Tool_Project_Provider_Exception('This project already has a DbTable named ' . $name);
  80. }
  81. // Check that there is not a dash or underscore, return if doesnt match regex
  82. if (preg_match('#[_-]#', $name)) {
  83. throw new Zend_Tool_Project_Provider_Exception('DbTable names should be camel cased.');
  84. }
  85. $name = ucwords($name);
  86. try {
  87. $tableResource = self::createResource($this->_loadedProfile, $name, $actualTableName, $module);
  88. } catch (Exception $e) {
  89. $response = $this->_registry->getResponse();
  90. $response->setException($e);
  91. return;
  92. }
  93. // do the creation
  94. if ($this->_registry->getRequest()->isPretend()) {
  95. $this->_registry->getResponse()->appendContent('Would create a DbTable at ' . $tableResource->getContext()->getPath());
  96. } else {
  97. $this->_registry->getResponse()->appendContent('Creating a DbTable at ' . $tableResource->getContext()->getPath());
  98. $tableResource->create();
  99. $this->_storeProfile();
  100. }
  101. }
  102. public function createFromDatabase($module = null, $forceOverwrite = false)
  103. {
  104. $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  105. $bootstrapResource = $this->_loadedProfile->search('BootstrapFile');
  106. /* @var $zendApp Zend_Application */
  107. $zendApp = $bootstrapResource->getApplicationInstance();
  108. try {
  109. $zendApp->bootstrap('db');
  110. } catch (Zend_Application_Exception $e) {
  111. throw new Zend_Tool_Project_Provider_Exception('Db resource not available, you might need to configure a DbAdapter.');
  112. return;
  113. }
  114. /* @var $db Zend_Db_Adapter_Abstract */
  115. $db = $zendApp->getBootstrap()->getResource('db');
  116. $tableResources = array();
  117. foreach ($db->listTables() as $actualTableName) {
  118. $dbTableName = $this->_convertTableNameToClassName($actualTableName);
  119. if (!$forceOverwrite && self::hasResource($this->_loadedProfile, $dbTableName, $module)) {
  120. throw new Zend_Tool_Project_Provider_Exception(
  121. 'This DbTable resource already exists, if you wish to overwrite it, '
  122. . 'pass the "forceOverwrite" flag to this provider.'
  123. );
  124. }
  125. $tableResources[] = self::createResource(
  126. $this->_loadedProfile,
  127. $dbTableName,
  128. $actualTableName,
  129. $module
  130. );
  131. }
  132. if (count($tableResources) == 0) {
  133. $this->_registry->getResponse()->appendContent('There are no tables in the selected database to write.');
  134. }
  135. // do the creation
  136. if ($this->_registry->getRequest()->isPretend()) {
  137. foreach ($tableResources as $tableResource) {
  138. $this->_registry->getResponse()->appendContent('Would create a DbTable at ' . $tableResource->getContext()->getPath());
  139. }
  140. } else {
  141. foreach ($tableResources as $tableResource) {
  142. $this->_registry->getResponse()->appendContent('Creating a DbTable at ' . $tableResource->getContext()->getPath());
  143. $tableResource->create();
  144. }
  145. $this->_storeProfile();
  146. }
  147. }
  148. protected function _convertTableNameToClassName($tableName)
  149. {
  150. if ($this->_nameFilter == null) {
  151. $this->_nameFilter = new Zend_Filter();
  152. $this->_nameFilter
  153. ->addFilter(new Zend_Filter_Word_UnderscoreToCamelCase());
  154. }
  155. return $this->_nameFilter->filter($tableName);
  156. }
  157. }