PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/app/lib/core/Zend/Tool/Project/Provider/DbTable.php

https://bitbucket.org/Sinfin/pawtucket
PHP | 220 lines | 135 code | 49 blank | 36 comment | 25 complexity | a9431eed14ab36721590e1c1bf14636b MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0
  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. // 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. public function createFromDatabase($module = null, $forceOverwrite = false)
  117. {
  118. $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  119. $bootstrapResource = $this->_loadedProfile->search('BootstrapFile');
  120. /* @var $zendApp Zend_Application */
  121. $zendApp = $bootstrapResource->getApplicationInstance();
  122. try {
  123. $zendApp->bootstrap('db');
  124. } catch (Zend_Application_Exception $e) {
  125. throw new Zend_Tool_Project_Provider_Exception('Db resource not available, you might need to configure a DbAdapter.');
  126. return;
  127. }
  128. /* @var $db Zend_Db_Adapter_Abstract */
  129. $db = $zendApp->getBootstrap()->getResource('db');
  130. $tableResources = array();
  131. foreach ($db->listTables() as $actualTableName) {
  132. $dbTableName = $this->_convertTableNameToClassName($actualTableName);
  133. if (!$forceOverwrite && self::hasResource($this->_loadedProfile, $dbTableName, $module)) {
  134. throw new Zend_Tool_Project_Provider_Exception(
  135. 'This DbTable resource already exists, if you wish to overwrite it, '
  136. . 'pass the "forceOverwrite" flag to this provider.'
  137. );
  138. }
  139. $tableResources[] = self::createResource(
  140. $this->_loadedProfile,
  141. $dbTableName,
  142. $actualTableName,
  143. $module
  144. );
  145. }
  146. if (count($tableResources) == 0) {
  147. $this->_registry->getResponse()->appendContent('There are no tables in the selected database to write.');
  148. }
  149. // do the creation
  150. if ($this->_registry->getRequest()->isPretend()) {
  151. foreach ($tableResources as $tableResource) {
  152. $this->_registry->getResponse()->appendContent('Would create a DbTable at ' . $tableResource->getContext()->getPath());
  153. }
  154. } else {
  155. foreach ($tableResources as $tableResource) {
  156. $this->_registry->getResponse()->appendContent('Creating a DbTable at ' . $tableResource->getContext()->getPath());
  157. $tableResource->create();
  158. }
  159. $this->_storeProfile();
  160. }
  161. }
  162. protected function _convertTableNameToClassName($tableName)
  163. {
  164. if ($this->_nameFilter == null) {
  165. $this->_nameFilter = new Zend_Filter();
  166. $this->_nameFilter
  167. ->addFilter(new Zend_Filter_Word_UnderscoreToCamelCase());
  168. }
  169. return $this->_nameFilter->filter($tableName);
  170. }
  171. }