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

/library/Zend/Tool/Project/Provider/DbTable.php

https://github.com/Exercise/zf2
PHP | 231 lines | 137 code | 50 blank | 44 comment | 25 complexity | 12ad8f7098156f73c648b74e6b83515b MD5 | raw file
  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$
  21. */
  22. /**
  23. * @namespace
  24. */
  25. namespace Zend\Tool\Project\Provider;
  26. use Zend\Tool\Project\Profile as ProjectProfile;
  27. /**
  28. * @uses \Zend\Filter\FilterChain
  29. * @uses \Zend\Filter\Word\UnderscoreToCamelCase
  30. * @uses \Zend\Tool\Framework\Provider\Pretendable
  31. * @uses \Zend\Tool\Project\Provider\AbstractProvider
  32. * @uses \Zend\Tool\Project\Provider\Exception
  33. * @category Zend
  34. * @package Zend_Tool
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class DbTable
  39. extends AbstractProvider
  40. implements \Zend\Tool\Framework\Provider\Pretendable
  41. {
  42. protected $_specialties = array('FromDatabase');
  43. /**
  44. * @var \Zend\Filter\FilterChain
  45. */
  46. protected $_nameFilter = null;
  47. public static function createResource(ProjectProfile $profile, $dbTableName, $actualTableName, $moduleName = null)
  48. {
  49. $profileSearchParams = array();
  50. if ($moduleName != null && is_string($moduleName)) {
  51. $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
  52. }
  53. $profileSearchParams[] = 'modelsDirectory';
  54. $modelsDirectory = $profile->search($profileSearchParams);
  55. if (!($modelsDirectory instanceof ProjectProfile\Resource)) {
  56. throw new Exception(
  57. 'A models directory was not found' .
  58. (($moduleName) ? ' for module ' . $moduleName . '.' : '.')
  59. );
  60. }
  61. if (!($dbTableDirectory = $modelsDirectory->search('DbTableDirectory'))) {
  62. $dbTableDirectory = $modelsDirectory->createResource('DbTableDirectory');
  63. }
  64. $dbTableFile = $dbTableDirectory->createResource('DbTableFile', array('dbTableName' => $dbTableName, 'actualTableName' => $actualTableName));
  65. return $dbTableFile;
  66. }
  67. public static function hasResource(ProjectProfile $profile, $dbTableName, $moduleName = null)
  68. {
  69. $profileSearchParams = array();
  70. if ($moduleName != null && is_string($moduleName)) {
  71. $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
  72. }
  73. $profileSearchParams[] = 'modelsDirectory';
  74. $modelsDirectory = $profile->search($profileSearchParams);
  75. if (!($modelsDirectory instanceof ProjectProfile\Resource)
  76. || !($dbTableDirectory = $modelsDirectory->search('DbTableDirectory'))) {
  77. return false;
  78. }
  79. $dbTableFile = $dbTableDirectory->search(array('DbTableFile' => array('dbTableName' => $dbTableName)));
  80. return ($dbTableFile instanceof ProjectProfile\Resource) ? true : false;
  81. }
  82. public function create($name, $actualTableName, $module = null, $forceOverwrite = false)
  83. {
  84. $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  85. // Check that there is not a dash or underscore, return if doesnt match regex
  86. if (preg_match('#[_-]#', $name)) {
  87. throw new Exception('DbTable names should be camel cased.');
  88. }
  89. $originalName = $name;
  90. $name = ucfirst($name);
  91. if ($actualTableName == '') {
  92. throw new Exception('You must provide both the DbTable name as well as the actual db table\'s name.');
  93. }
  94. if (self::hasResource($this->_loadedProfile, $name, $module)) {
  95. throw new Exception('This project already has a DbTable named ' . $name);
  96. }
  97. // get request/response object
  98. $request = $this->_registry->getRequest();
  99. $response = $this->_registry->getResponse();
  100. // alert the user about inline converted names
  101. $tense = (($request->isPretend()) ? 'would be' : 'is');
  102. if ($name !== $originalName) {
  103. $response->appendContent(
  104. 'Note: The canonical model name that ' . $tense
  105. . ' used with other providers is "' . $name . '";'
  106. . ' not "' . $originalName . '" as supplied',
  107. array('color' => array('yellow'))
  108. );
  109. }
  110. try {
  111. $tableResource = self::createResource($this->_loadedProfile, $name, $actualTableName, $module);
  112. } catch (\Exception $e) {
  113. $response = $this->_registry->getResponse();
  114. $response->setException($e);
  115. return;
  116. }
  117. // do the creation
  118. if ($request->isPretend()) {
  119. $response->appendContent('Would create a DbTable at ' . $tableResource->getContext()->getPath());
  120. } else {
  121. $response->appendContent('Creating a DbTable at ' . $tableResource->getContext()->getPath());
  122. $tableResource->create();
  123. $this->_storeProfile();
  124. }
  125. }
  126. public function createFromDatabase($module = null, $forceOverwrite = false)
  127. {
  128. $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  129. $bootstrapResource = $this->_loadedProfile->search('BootstrapFile');
  130. /* @var $zendApp Zend_Application */
  131. $zendApp = $bootstrapResource->getApplicationInstance();
  132. try {
  133. $zendApp->bootstrap('db');
  134. } catch (\Zend\Application\Exception $e) {
  135. throw new Exception('Db resource not available, you might need to configure a DbAdapter.');
  136. return;
  137. }
  138. /* @var $db Zend_Db_Adapter_Abstract */
  139. $db = $zendApp->getBootstrap()->getResource('db');
  140. $tableResources = array();
  141. foreach ($db->listTables() as $actualTableName) {
  142. $dbTableName = $this->_convertTableNameToClassName($actualTableName);
  143. if (!$forceOverwrite && self::hasResource($this->_loadedProfile, $dbTableName, $module)) {
  144. throw new Exception(
  145. 'This DbTable resource already exists, if you wish to overwrite it, '
  146. . 'pass the "forceOverwrite" flag to this provider.'
  147. );
  148. }
  149. $tableResources[] = self::createResource(
  150. $this->_loadedProfile,
  151. $dbTableName,
  152. $actualTableName,
  153. $module
  154. );
  155. }
  156. if (count($tableResources) == 0) {
  157. $this->_registry->getResponse()->appendContent('There are no tables in the selected database to write.');
  158. }
  159. // do the creation
  160. if ($this->_registry->getRequest()->isPretend()) {
  161. foreach ($tableResources as $tableResource) {
  162. $this->_registry->getResponse()->appendContent('Would create a DbTable at ' . $tableResource->getContext()->getPath());
  163. }
  164. } else {
  165. foreach ($tableResources as $tableResource) {
  166. $this->_registry->getResponse()->appendContent('Creating a DbTable at ' . $tableResource->getContext()->getPath());
  167. $tableResource->create();
  168. }
  169. $this->_storeProfile();
  170. }
  171. }
  172. protected function _convertTableNameToClassName($tableName)
  173. {
  174. if ($this->_nameFilter == null) {
  175. $this->_nameFilter = new \Zend\Filter\FilterChain();
  176. $this->_nameFilter
  177. ->addFilter(new \Zend\Filter\Word\UnderscoreToCamelCase());
  178. }
  179. return $this->_nameFilter->filter($tableName);
  180. }
  181. }