DbTable.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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-2009 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. * @see Zend_Tool_Project_Provider_Abstract
  24. */
  25. require_once 'Zend/Tool/Project/Provider/Abstract.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Tool
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Tool_Project_Provider_DbTable extends Zend_Tool_Project_Provider_Abstract
  33. {
  34. protected $_specialties = array('FromDatabase');
  35. /**
  36. * @var Zend_Filter
  37. */
  38. protected $_nameFilter = null;
  39. public static function createResource(Zend_Tool_Project_Profile $profile, $dbTableName, $actualTableName, $moduleName = null)
  40. {
  41. $profileSearchParams = array();
  42. if ($moduleName != null && is_string($moduleName)) {
  43. $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
  44. }
  45. $profileSearchParams[] = 'modelsDirectory';
  46. $modelsDirectory = $profile->search($profileSearchParams);
  47. if (!($dbTableDirectory = $modelsDirectory->search('DbTableDirectory'))) {
  48. $dbTableDirectory = $modelsDirectory->createResource('DbTableDirectory');
  49. }
  50. $dbTableFile = $dbTableDirectory->createResource('DbTableFile', array('dbTableName' => $dbTableName, 'actualTableName' => $actualTableName));
  51. return $dbTableFile;
  52. }
  53. public static function hasResource(Zend_Tool_Project_Profile $profile, $dbTableName, $moduleName = null)
  54. {
  55. $profileSearchParams = array();
  56. if ($moduleName != null && is_string($moduleName)) {
  57. $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
  58. }
  59. $profileSearchParams[] = 'modelsDirectory';
  60. $modelsDirectory = $profile->search($profileSearchParams);
  61. if (!($dbTableDirectory = $modelsDirectory->search('DbTableDirectory'))) {
  62. return false;
  63. }
  64. $dbTableFile = $dbTableDirectory->search(array('DbTableFile' => array('dbTableName' => $dbTableName)));
  65. return ($dbTableFile instanceof Zend_Tool_Project_Profile_Resource) ? true : false;
  66. }
  67. public function create($name, $actualTableName, $module = null, $forceOverwrite = true)
  68. {
  69. $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  70. if ($actualTableName == '') {
  71. throw new Zend_Tool_Project_Provider_Exception('You must provide both the DbTable name as well as the actual db table\'s name.');
  72. }
  73. if (self::hasResource($this->_loadedProfile, $name, $module)) {
  74. throw new Zend_Tool_Project_Provider_Exception('This project already has a DbTable named ' . $name);
  75. }
  76. // Check that there is not a dash or underscore, return if doesnt match regex
  77. if (preg_match('#[_-]#', $name)) {
  78. throw new Zend_Tool_Project_Provider_Exception('DbTable names should be camel cased.');
  79. }
  80. $name = ucwords($name);
  81. try {
  82. $modelResource = self::createResource($this->_loadedProfile, $name, $actualTableName, $module);
  83. } catch (Exception $e) {
  84. $response = $this->_registry->getResponse();
  85. $response->setException($e);
  86. return;
  87. }
  88. // do the creation
  89. if ($this->_registry->getRequest()->isPretend()) {
  90. $this->_registry->getResponse()->appendContent('Would create a DbTable at ' . $modelResource->getContext()->getPath());
  91. } else {
  92. $this->_registry->getResponse()->appendContent('Creating a model at ' . $modelResource->getContext()->getPath());
  93. $modelResource->create();
  94. $this->_storeProfile();
  95. }
  96. }
  97. public function createFromDatabase($module = null, $forceOverwrite = true)
  98. {
  99. $bootstrapResource = $profile->search('Bootstrap');
  100. $bi = $bootstrapResource->getApplicationInstance();
  101. var_dump($bi);
  102. }
  103. protected function _convertTableNameToClassName($tableName)
  104. {
  105. if ($this->_nameFilter == null) {
  106. $this->_nameFilter = new Zend_Filter();
  107. $this->_nameFilter
  108. ->addFilter(new Zend_Filter_Word_UnderscoreToCamelCase());
  109. }
  110. return $this->_nameFilter->filter($tableName);
  111. }
  112. }