source: trunk/generator/classes/propel/engine/builder/om/OMBuilder.php @ 186

Revision 186, 11.0 KB checked in by hans, 5 years ago (diff)

issue #115. Fixing carriage returns.

  • Property svn:keywords set to Id Rev Date
Line 
1<?php
2
3/*
4 *  $Id$
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
23require_once 'propel/engine/builder/DataModelBuilder.php';
24
25/**
26 * Baseclass for OM-building classes.
27 *
28 * OM-building classes are those that build a PHP (or other) class to service
29 * a single table.  This includes Peer classes, Entity classes, Map classes,
30 * Node classes, etc.
31 *
32 * @author Hans Lellelid <hans@xmpl.org>
33 * @package propel.engine.builder.om
34 */
35abstract class OMBuilder extends DataModelBuilder {     
36       
37        /**
38         * Peer builder class for current table.
39         * @var DataModelBuilder
40         */
41        private $peerBuilder;
42       
43        /**
44         * Stub Peer builder class for current table.
45         * @var DataModelBuilder
46         */
47        private $stubPeerBuilder;
48       
49        /**
50         * Object builder class for current table.
51         * @var DataModelBuilder
52         */
53        private $objectBuilder;
54
55        /**
56         * Stub Object builder class for current table.
57         * @var DataModelBuilder
58         */
59        private $stubObjectBuilder;
60       
61        /**
62         * MapBuilder builder class for current table.
63         * @var DataModelBuilder
64         */
65        private $mapBuilderBuilder;
66       
67        /**
68         * Stub Interface builder class for current table.
69         * @var DataModelBuilder
70         */
71        private $interfaceBuilder;
72       
73        /**
74         * Stub child object for current table.
75         * @var DataModelBuilder
76         */
77        private $multiExtendObjectBuilder;
78       
79        /**
80         * Node object builder for current table.
81         * @var DataModelBuilder
82         */
83        private $nodeBuilder;
84       
85        /**
86         * Node peer builder for current table.
87         * @var DataModelBuilder
88         */
89        private $nodePeerBuilder;
90       
91        /**
92         * Stub node object builder for current table.
93         * @var DataModelBuilder
94         */
95        private $stubNodeBuilder;
96
97        /**
98         * Stub node peer builder for current table.
99         * @var DataModelBuilder
100         */
101        private $stubNodePeerBuilder;
102       
103       
104        /**
105         * Returns new or existing Peer builder class for this table.
106         * @return DataModelBuilder
107         */
108        public function getPeerBuilder()
109        {
110                if (!isset($this->peerBuilder)) {
111                        $this->peerBuilder = DataModelBuilder::builderFactory($this->getTable(), 'peer');
112                }
113                return $this->peerBuilder;
114        }
115       
116        /**
117         * Returns new or existing stub Peer builder class for this table.
118         * @return DataModelBuilder
119         */
120        public function getStubPeerBuilder()
121        {
122                if (!isset($this->stubPeerBuilder)) {
123                        $this->stubPeerBuilder = DataModelBuilder::builderFactory($this->getTable(), 'peerstub');
124                }
125                return $this->stubPeerBuilder; 
126        }
127       
128        /**
129         * Returns new or existing Object builder class for this table.
130         * @return DataModelBuilder
131         */
132        public function getObjectBuilder()
133        {
134                if (!isset($this->objectBuilder)) {
135                        $this->objectBuilder = DataModelBuilder::builderFactory($this->getTable(), 'object');
136                }
137                return $this->objectBuilder;
138       
139        }
140       
141        /**
142         * Returns new or existing stub Object builder class for this table.
143         * @return DataModelBuilder
144         */
145        public function getStubObjectBuilder()
146        {
147                if (!isset($this->stubObjectBuilder)) {
148                        $this->stubObjectBuilder = DataModelBuilder::builderFactory($this->getTable(), 'objectstub');
149                }
150                return $this->stubObjectBuilder;       
151        }
152       
153        /**
154         * Returns new or existing MapBuilder builder class for this table.
155         * @return DataModelBuilder
156         */
157        public function getMapBuilderBuilder()
158        {
159                if (!isset($this->mapBuilderBuilder)) {
160                        $this->mapBuilderBuilder = DataModelBuilder::builderFactory($this->getTable(), 'mapbuilder');
161                }
162                return $this->mapBuilderBuilder;       
163        }
164       
165        /**
166         * Returns new or existing stub Interface builder class for this table.
167         * @return DataModelBuilder
168         */
169        public function getInterfaceBuilder()
170        {
171                if (!isset($this->interfaceBuilder)) {
172                        $this->interfaceBuilder = DataModelBuilder::builderFactory($this->getTable(), 'interface');
173                }
174                return $this->interfaceBuilder; 
175        }
176       
177        /**
178         * Returns new or existing stub child object builder class for this table.
179         * @return DataModelBuilder
180         */
181        public function getMultiExtendObjectBuilder()
182        {
183                if (!isset($this->multiExtendObjectBuilder)) {
184                        $this->multiExtendObjectBuilder = DataModelBuilder::builderFactory($this->getTable(), 'objectmultiextend');
185                }
186                return $this->multiExtendObjectBuilder; 
187        }
188       
189        /**
190         * Returns new or existing node Object builder class for this table.
191         * @return DataModelBuilder
192         */
193        public function getNodeBuilder()
194        {
195                if (!isset($this->nodeBuilder)) {
196                        $this->nodeBuilder = DataModelBuilder::builderFactory($this->getTable(), 'node');
197                }
198                return $this->nodeBuilder;     
199        }
200       
201        /**
202         * Returns new or existing node Peer builder class for this table.
203         * @return DataModelBuilder
204         */
205        public function getNodePeerBuilder()
206        {
207                if (!isset($this->nodePeerBuilder)) {
208                        $this->nodePeerBuilder = DataModelBuilder::builderFactory($this->getTable(), 'nodepeer');
209                }
210                return $this->nodePeerBuilder; 
211        }
212       
213        /**
214         * Returns new or existing stub node Object builder class for this table.
215         * @return DataModelBuilder
216         */
217        public function getStubNodeBuilder()
218        {
219                if (!isset($this->stubNodeBuilder)) {
220                        $this->stubNodeBuilder = DataModelBuilder::builderFactory($this->getTable(), 'nodestub');
221                }
222                return $this->stubNodeBuilder; 
223        }
224
225        /**
226         * Returns new or existing stub node Peer builder class for this table.
227         * @return DataModelBuilder
228         */
229        public function getStubNodePeerBuilder()
230        {
231                if (!isset($this->stubNodePeerBuilder)) {
232                        $this->stubNodePeerBuilder = DataModelBuilder::builderFactory($this->getTable(), 'nodepeerstub');
233                }
234                return $this->stubNodePeerBuilder;     
235        }
236       
237        /**
238         * Convenience method to return a NEW Peer class builder instance.
239         * This is used very frequently from the peer and object builders to get
240         * a peer builder for a RELATED table.
241         * @param Table $table
242         * @return PeerBuilder
243         */
244        public static function getNewPeerBuilder(Table $table)
245        {
246                return DataModelBuilder::builderFactory($table, 'peer');
247        }
248       
249        /**
250         * Convenience method to return a NEW Object class builder instance.
251         * This is used very frequently from the peer and object builders to get
252         * an object builder for a RELATED table.
253         * @param Table $table
254         * @return ObjectBuilder
255         */
256        public static function getNewObjectBuilder(Table $table)
257        {
258                return DataModelBuilder::builderFactory($table, 'object');
259        }
260       
261        /**
262         * Builds the PHP source for current class and returns it as a string.
263         *
264         * This is the main entry point and defines a basic structure that classes should follow.
265         * In most cases this method will not need to be overridden by subclasses.  This method
266         * does assume that the output language is PHP code, so it will need to be overridden if
267         * this is not the case.
268         *
269         * @return string The resulting PHP sourcecode.
270         */
271        public function build()
272        {
273                $script = "<" . "?php\n"; // intentional concatenation         
274                $this->addIncludes($script);
275                $this->addClassOpen($script);
276                $this->addClassBody($script);   
277                $this->addClassClose($script);
278                return $script;
279        }
280       
281        /**
282         * Returns the name of the current class being built.
283         * @return string
284         */
285        abstract public function getClassname();
286       
287        /**
288         * Gets the dot-path representation of current class being built.
289         * @return string
290         */
291        public function getClasspath()
292        {
293                if ($this->getPackage()) {
294                    $path = $this->getPackage() . '.' . $this->getClassname();
295                } else {
296                        $path = $this->getClassname();
297                }
298                return $path;
299        }
300       
301        /**
302         * Gets the full path to the file for the current class.
303         * @return string
304         */
305        public function getClassFilePath()
306        {
307                return parent::getFilePath($this->getPackage(), $this->getClassname());
308        }
309
310        /**
311         * Gets package name for this table.
312         * This is overridden by child classes that have different packages.
313         * @return string
314         */
315        public function getPackage()
316        {
317                $pkg = ($this->getTable()->getPackage() ? $this->getTable()->getPackage() : $this->getDatabase()->getPackage());
318                if (!$pkg) {
319                    $pkg = $this->getBuildProperty('targetPackage');
320                }
321                return $pkg;
322        }
323       
324        /**
325         * Returns filesystem path for current package.
326         * @return string
327         */
328        public function getPackagePath()
329        {
330                return strtr($this->getPackage(), '.', '/');
331        }
332       
333        /**
334         * Shortcut method to return the [stub] peer classname for current table.
335         * This is the classname that is used whenever object or peer classes want
336         * to invoke methods of the peer classes.
337         * @return string (e.g. 'MyPeer')
338         * @see StubPeerBuilder::getClassname()
339         */
340        public function getPeerClassname() {
341                return $this->getStubPeerBuilder()->getClassname();
342        }
343               
344        /**
345         * Returns the object classname for current table.
346         * This is the classname that is used whenever object or peer classes want
347         * to invoke methods of the object classes.
348         * @return string (e.g. 'My')
349         * @see StubPeerBuilder::getClassname()
350         */
351        public function getObjectClassname() {
352                return $this->getStubObjectBuilder()->getClassname();
353        }
354       
355        /**
356         * Get the column constant name (e.g. PeerName::COLUMN_NAME).
357     *
358     * @param Column $col The column we need a name for.
359     * @param string $phpName The PHP Name of the peer class. The 'Peer' is appended automatically.
360     *
361     * @return string If $phpName is provided, then will return {$phpName}Peer::COLUMN_NAME; if not, then uses current table COLUMN_NAME.
362     */
363    public function getColumnConstant($col, $phpName = null)
364        {       
365                if ($col === null) {
366                    $e = new Exception("No col specified.");
367                        print $e;
368                        throw $e;
369                }
370                $classname = $this->getPeerClassname($phpName);
371               
372        // was it overridden in schema.xml ?
373        if ($col->getPeerName()) {
374            $const = strtoupper($col->getPeerName());
375        } else {
376            $const = strtoupper($col->getName());
377        }
378                return $classname.'::'.$const;
379    }
380       
381        /**
382     * Gets just classname, given a dot-path to class.
383     * @param string $qualifiedName
384     * @return string
385     */
386    public function classname($qualifiedName)
387    {
388        $pos = strrpos($qualifiedName, '.');
389        if ($pos === false) { 
390            return $qualifiedName;  // there is no '.' in the qualifed name
391        } else {
392            return substr($qualifiedName, $pos + 1); // start just after '.'
393        }
394    }
395       
396        /**
397     * Gets the basePeer path if specified for table/db. 
398     * If not, will return 'propel.util.BasePeer'
399     * @return string
400     */
401    public function getBasePeer(Table $table) {
402        $class = $table->getBasePeer();
403        if ($class === null) {
404            $class = "propel.util.BasePeer";
405        }
406        return $class;
407    }   
408       
409}
Note: See TracBrowser for help on using the repository browser.