|
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: Model.php 20851 2010-02-02 21:45:51Z ralph $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @category Zend |
|
25 * @package Zend_Tool |
|
26 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
27 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
28 */ |
|
29 class Zend_Tool_Project_Provider_Model extends Zend_Tool_Project_Provider_Abstract |
|
30 { |
|
31 |
|
32 public static function createResource(Zend_Tool_Project_Profile $profile, $modelName, $moduleName = null) |
|
33 { |
|
34 if (!is_string($modelName)) { |
|
35 throw new Zend_Tool_Project_Provider_Exception('Zend_Tool_Project_Provider_Model::createResource() expects \"modelName\" is the name of a model resource to create.'); |
|
36 } |
|
37 |
|
38 if (!($modelsDirectory = self::_getModelsDirectoryResource($profile, $moduleName))) { |
|
39 if ($moduleName) { |
|
40 $exceptionMessage = 'A model directory for module "' . $moduleName . '" was not found.'; |
|
41 } else { |
|
42 $exceptionMessage = 'A model directory was not found.'; |
|
43 } |
|
44 throw new Zend_Tool_Project_Provider_Exception($exceptionMessage); |
|
45 } |
|
46 |
|
47 $newModel = $modelsDirectory->createResource( |
|
48 'modelFile', |
|
49 array('modelName' => $modelName, 'moduleName' => $moduleName) |
|
50 ); |
|
51 |
|
52 return $newModel; |
|
53 } |
|
54 |
|
55 /** |
|
56 * hasResource() |
|
57 * |
|
58 * @param Zend_Tool_Project_Profile $profile |
|
59 * @param string $modelName |
|
60 * @param string $moduleName |
|
61 * @return Zend_Tool_Project_Profile_Resource |
|
62 */ |
|
63 public static function hasResource(Zend_Tool_Project_Profile $profile, $modelName, $moduleName = null) |
|
64 { |
|
65 if (!is_string($modelName)) { |
|
66 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.'); |
|
67 } |
|
68 |
|
69 $modelsDirectory = self::_getModelsDirectoryResource($profile, $moduleName); |
|
70 return (($modelsDirectory->search(array('modelFile' => array('modelName' => $modelName)))) instanceof Zend_Tool_Project_Profile_Resource); |
|
71 } |
|
72 |
|
73 /** |
|
74 * _getModelsDirectoryResource() |
|
75 * |
|
76 * @param Zend_Tool_Project_Profile $profile |
|
77 * @param string $moduleName |
|
78 * @return Zend_Tool_Project_Profile_Resource |
|
79 */ |
|
80 protected static function _getModelsDirectoryResource(Zend_Tool_Project_Profile $profile, $moduleName = null) |
|
81 { |
|
82 $profileSearchParams = array(); |
|
83 |
|
84 if ($moduleName != null && is_string($moduleName)) { |
|
85 $profileSearchParams = array('modulesDirectory', 'moduleDirectory' => array('moduleName' => $moduleName)); |
|
86 } |
|
87 |
|
88 $profileSearchParams[] = 'modelsDirectory'; |
|
89 |
|
90 return $profile->search($profileSearchParams); |
|
91 } |
|
92 |
|
93 /** |
|
94 * Create a new model |
|
95 * |
|
96 * @param string $name |
|
97 * @param string $module |
|
98 */ |
|
99 public function create($name, $module = null) |
|
100 { |
|
101 $this->_loadProfile(self::NO_PROFILE_THROW_EXCEPTION); |
|
102 |
|
103 $originalName = $name; |
|
104 |
|
105 $name = ucwords($name); |
|
106 |
|
107 // determine if testing is enabled in the project |
|
108 $testingEnabled = false; //Zend_Tool_Project_Provider_Test::isTestingEnabled($this->_loadedProfile); |
|
109 $testModelResource = null; |
|
110 |
|
111 // Check that there is not a dash or underscore, return if doesnt match regex |
|
112 if (preg_match('#[_-]#', $name)) { |
|
113 throw new Zend_Tool_Project_Provider_Exception('Model names should be camel cased.'); |
|
114 } |
|
115 |
|
116 if (self::hasResource($this->_loadedProfile, $name, $module)) { |
|
117 throw new Zend_Tool_Project_Provider_Exception('This project already has a model named ' . $name); |
|
118 } |
|
119 |
|
120 // get request/response object |
|
121 $request = $this->_registry->getRequest(); |
|
122 $response = $this->_registry->getResponse(); |
|
123 |
|
124 // alert the user about inline converted names |
|
125 $tense = (($request->isPretend()) ? 'would be' : 'is'); |
|
126 |
|
127 if ($name !== $originalName) { |
|
128 $response->appendContent( |
|
129 'Note: The canonical model name that ' . $tense |
|
130 . ' used with other providers is "' . $name . '";' |
|
131 . ' not "' . $originalName . '" as supplied', |
|
132 array('color' => array('yellow')) |
|
133 ); |
|
134 } |
|
135 |
|
136 try { |
|
137 $modelResource = self::createResource($this->_loadedProfile, $name, $module); |
|
138 |
|
139 if ($testingEnabled) { |
|
140 // $testModelResource = Zend_Tool_Project_Provider_Test::createApplicationResource($this->_loadedProfile, $name, 'index', $module); |
|
141 } |
|
142 |
|
143 } catch (Exception $e) { |
|
144 $response->setException($e); |
|
145 return; |
|
146 } |
|
147 |
|
148 // do the creation |
|
149 if ($request->isPretend()) { |
|
150 |
|
151 $response->appendContent('Would create a model at ' . $modelResource->getContext()->getPath()); |
|
152 |
|
153 if ($testModelResource) { |
|
154 $response->appendContent('Would create a model test file at ' . $testModelResource->getContext()->getPath()); |
|
155 } |
|
156 |
|
157 } else { |
|
158 |
|
159 $response->appendContent('Creating a model at ' . $modelResource->getContext()->getPath()); |
|
160 $modelResource->create(); |
|
161 |
|
162 if ($testModelResource) { |
|
163 $response->appendContent('Creating a model test file at ' . $testModelResource->getContext()->getPath()); |
|
164 $testModelResource->create(); |
|
165 } |
|
166 |
|
167 $this->_storeProfile(); |
|
168 } |
|
169 |
|
170 } |
|
171 |
|
172 |
|
173 } |