Model.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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$
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Tool
  25. * @copyright Copyright (c) 2005-2009 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_Model extends Zend_Tool_Project_Provider_Abstract
  29. {
  30. public static function createResource(Zend_Tool_Project_Profile $profile, $modelName, $moduleName = null)
  31. {
  32. if (!is_string($modelName)) {
  33. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Model::createResource() expects \"modelName\" is the name of a model resource to create.');
  34. }
  35. if (!($modelsDirectory = self::_getModelsDirectoryResource($profile, $moduleName))) {
  36. if ($moduleName) {
  37. $exceptionMessage = 'A model directory for module "' . $moduleName . '" was not found.';
  38. } else {
  39. $exceptionMessage = 'A model directory was not found.';
  40. }
  41. throw new Zend_Tool_Project_Provider_Exception($exceptionMessage);
  42. }
  43. $newModel = $modelsDirectory->createResource(
  44. 'modelFile',
  45. array('modelName' => $modelName, 'moduleName' => $moduleName)
  46. );
  47. return $newModel;
  48. }
  49. /**
  50. * hasResource()
  51. *
  52. * @param Zend_Tool_Project_Profile $profile
  53. * @param string $modelName
  54. * @param string $moduleName
  55. * @return Zend_Tool_Project_Profile_Resource
  56. */
  57. public static function hasResource(Zend_Tool_Project_Profile $profile, $modelName, $moduleName = null)
  58. {
  59. if (!is_string($modelName)) {
  60. throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Model::createResource() expects \"modelName\" is the name of a model resource to check for existence.');
  61. }
  62. $modelsDirectory = self::_getModelsDirectoryResource($profile, $moduleName);
  63. return (($modelsDirectory->search(array('modelFile' => array('modelName' => $modelName)))) instanceof Zend_Tool_Project_Profile_Resource);
  64. }
  65. /**
  66. * _getModelsDirectoryResource()
  67. *
  68. * @param Zend_Tool_Project_Profile $profile
  69. * @param string $moduleName
  70. * @return Zend_Tool_Project_Profile_Resource
  71. */
  72. protected static function _getModelsDirectoryResource(Zend_Tool_Project_Profile $profile, $moduleName = null)
  73. {
  74. $profileSearchParams = array();
  75. if ($moduleName != null && is_string($moduleName)) {
  76. $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName));
  77. }
  78. $profileSearchParams[] = 'modelsDirectory';
  79. return $profile->search($profileSearchParams);
  80. }
  81. /**
  82. * Create a new model
  83. *
  84. * @param string $name
  85. * @param string $module
  86. */
  87. public function create($name, $module = null)
  88. {
  89. $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION);
  90. // determine if testing is enabled in the project
  91. $testingEnabled = false; //Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile);
  92. if (self::hasResource($this->_loadedProfile, $name, $module)) {
  93. throw new Zend_Tool_Project_Provider_Exception('This project already has a model named ' . $name);
  94. }
  95. // Check that there is not a dash or underscore, return if doesnt match regex
  96. if (preg_match('#[_-]#', $name)) {
  97. throw new Zend_Tool_Project_Provider_Exception('Model names should be camel cased.');
  98. }
  99. $name = ucwords($name);
  100. try {
  101. $modelResource = self::createResource($this->_loadedProfile, $name, $module);
  102. if ($testingEnabled) {
  103. $testModelResource = null;
  104. // $testModelResource = Zend_Tool_Project_Provider_Test::createApplicationResource($this->_loadedProfile, $name, 'index', $module);
  105. }
  106. } catch (Exception $e) {
  107. $response = $this->_registry->getResponse();
  108. $response->setException($e);
  109. return;
  110. }
  111. // do the creation
  112. if ($this->_registry->getRequest()->isPretend()) {
  113. $this->_registry->getResponse()->appendContent('Would create a model at ' . $modelResource->getContext()->getPath());
  114. if ($testModelResource) {
  115. $this->_registry->getResponse()->appendContent('Would create a model test file at ' . $testModelResource->getContext()->getPath());
  116. }
  117. } else {
  118. $this->_registry->getResponse()->appendContent('Creating a model at ' . $modelResource->getContext()->getPath());
  119. $modelResource->create();
  120. if ($testModelResource) {
  121. $this->_registry->getResponse()->appendContent('Creating a model test file at ' . $testModelResource->getContext()->getPath());
  122. $testModelResource->create();
  123. }
  124. $this->_storeProfile();
  125. }
  126. }
  127. }