Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 *  $Id: PropelOMTask.php 1262 2009-10-26 20:54:39Z francois $
5
 *
6
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17
 *
18
 * This software consists of voluntary contributions made by many individuals
19
 * and is licensed under the LGPL. For more information please see
20
 * <http://propel.phpdb.org>.
21
 */
22
 
23
require_once 'propel/phing/AbstractPropelDataModelTask.php';
24
include_once 'propel/engine/builder/om/ClassTools.php';
25
require_once 'propel/engine/builder/om/OMBuilder.php';
26
 
27
/**
28
 * This Task creates the OM classes based on the XML schema file.
29
 *
30
 * @author     Hans Lellelid <hans@xmpl.org>
31
 * @package    propel.phing
32
 */
33
class PropelOMTask extends AbstractPropelDataModelTask {
34
 
35
	/**
36
	 * The platform (php4, php5, etc.) for which the om is being built.
37
	 * @var        string
38
	 */
39
	private $targetPlatform;
40
 
41
	/**
42
	 * Sets the platform (php4, php5, etc.) for which the om is being built.
43
	 * @param      string $v
44
	 */
45
	public function setTargetPlatform($v) {
46
		$this->targetPlatform = $v;
47
	}
48
 
49
	/**
50
	 * Gets the platform (php4, php5, etc.) for which the om is being built.
51
	 * @return     string
52
	 */
53
	public function getTargetPlatform() {
54
		return $this->targetPlatform;
55
	}
56
 
57
	/**
58
	 * Utility method to create directory for package if it doesn't already exist.
59
	 * @param      string $path The [relative] package path.
60
	 * @throws     BuildException - if there is an error creating directories
61
	 */
62
	protected function ensureDirExists($path)
63
	{
64
		$f = new PhingFile($this->getOutputDirectory(), $path);
65
		if (!$f->exists()) {
66
			if (!$f->mkdirs()) {
67
				throw new BuildException("Error creating directories: ". $f->getPath());
68
			}
69
		}
70
	}
71
 
72
	/**
73
	 * Uses a builder class to create the output class.
74
	 * This method assumes that the DataModelBuilder class has been initialized with the build properties.
75
	 * @param      OMBuilder $builder
76
	 * @param      boolean $overwrite Whether to overwrite existing files with te new ones (default is YES).
77
	 * @todo       -cPropelOMTask Consider refactoring build() method into AbstractPropelDataModelTask (would need to be more generic).
78
	 */
79
	protected function build(OMBuilder $builder, $overwrite = true)
80
	{
81
 
82
		$path = $builder->getClassFilePath();
83
		$this->ensureDirExists(dirname($path));
84
 
85
		$_f = new PhingFile($this->getOutputDirectory(), $path);
86
		if ($overwrite || !$_f->exists()) {
87
			$this->log("\t\t-> " . $builder->getClassname() . " [builder: " . get_class($builder) . "]");
88
			$script = $builder->build();
89
			file_put_contents($_f->getAbsolutePath(), $script);
90
			foreach ($builder->getWarnings() as $warning) {
91
				$this->log($warning, Project::MSG_WARN);
92
			}
93
		} else {
94
			$this->log("\t\t-> (exists) " . $builder->getClassname());
95
		}
96
 
97
	}
98
 
99
	/**
100
	 * Main method builds all the targets for a typical propel project.
101
	 */
102
	public function main()
103
	{
104
		// check to make sure task received all correct params
105
		$this->validate();
106
 
107
		$generatorConfig = $this->getGeneratorConfig();
108
 
109
		foreach ($this->getDataModels() as $dataModel) {
110
			$this->log("Processing Datamodel : " . $dataModel->getName());
111
 
112
			foreach ($dataModel->getDatabases() as $database) {
113
 
114
				$this->log("  - processing database : " . $database->getName());
115
 
116
				foreach ($database->getTables() as $table) {
117
 
118
					if (!$table->isForReferenceOnly()) {
119
 
120
						$this->log("\t+ " . $table->getName());
121
 
122
						// -----------------------------------------------------------------------------------------
123
						// Create Peer, Object, and TableMap classes
124
						// -----------------------------------------------------------------------------------------
125
 
126
						// these files are always created / overwrite any existing files
127
						foreach (array('peer', 'object', 'tablemap') as $target) {
128
							$builder = $generatorConfig->getConfiguredBuilder($table, $target);
129
							$this->build($builder);
130
						}
131
 
132
						// -----------------------------------------------------------------------------------------
133
						// Create [empty] stub Peer and Object classes if they don't exist
134
						// -----------------------------------------------------------------------------------------
135
 
136
						// these classes are only generated if they don't already exist
137
						foreach (array('peerstub', 'objectstub') as $target) {
138
							$builder = $generatorConfig->getConfiguredBuilder($table, $target);
139
							$this->build($builder, $overwrite=false);
140
						}
141
 
142
						// -----------------------------------------------------------------------------------------
143
						// Create [empty] stub child Object classes if they don't exist
144
						// -----------------------------------------------------------------------------------------
145
 
146
						// If table has enumerated children (uses inheritance) then create the empty child stub classes if they don't already exist.
147
						if ($table->getChildrenColumn()) {
148
							$col = $table->getChildrenColumn();
149
							if ($col->isEnumeratedClasses()) {
150
								foreach ($col->getChildren() as $child) {
151
									$builder = $generatorConfig->getConfiguredBuilder($table, 'objectmultiextend');
152
									$builder->setChild($child);
153
									$this->build($builder, $overwrite=false);
154
								} // foreach
155
							} // if col->is enumerated
156
						} // if tbl->getChildrenCol
157
 
158
 
159
						// -----------------------------------------------------------------------------------------
160
						// Create [empty] Interface if it doesn't exist
161
						// -----------------------------------------------------------------------------------------
162
 
163
						// Create [empty] interface if it does not already exist
164
						if ($table->getInterface()) {
165
							$builder = $generatorConfig->getConfiguredBuilder($table, 'interface');
166
							$this->build($builder, $overwrite=false);
167
						}
168
 
169
						// -----------------------------------------------------------------------------------------
170
						// Create tree Node classes
171
						// -----------------------------------------------------------------------------------------
172
 
173
						if ($table->treeMode()) {
174
							switch($table->treeMode()) {
175
								case 'NestedSet':
176
									foreach (array('nestedsetpeer', 'nestedset') as $target) {
177
										$builder = $generatorConfig->getConfiguredBuilder($table, $target);
178
										$this->build($builder);
179
									}
180
								break;
181
 
182
								case 'MaterializedPath':
183
									foreach (array('nodepeer', 'node') as $target) {
184
										$builder = $generatorConfig->getConfiguredBuilder($table, $target);
185
										$this->build($builder);
186
									}
187
 
188
									foreach (array('nodepeerstub', 'nodestub') as $target) {
189
										$builder = $generatorConfig->getConfiguredBuilder($table, $target);
190
										$this->build($builder, $overwrite=false);
191
									}
192
								break;
193
 
194
								case 'AdjacencyList':
195
									// No implementation for this yet.
196
								default:
197
								break;
198
							}
199
 
200
						} // if Table->treeMode()
201
 
202
 
203
					} // if !$table->isForReferenceOnly()
204
 
205
				} // foreach table
206
 
207
			} // foreach database
208
 
209
		} // foreach dataModel
210
 
211
	} // main()
212
}