1 |
<?php
|
2 |
/*
|
3 |
* $Id$
|
4 |
*
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
16 |
*
|
17 |
* This software consists of voluntary contributions made by many individuals
|
18 |
* and is licensed under the LGPL. For more information, see
|
19 |
* <http://www.phpdoctrine.org>.
|
20 |
*/
|
21 |
|
22 |
/**
|
23 |
* Doctrine_Plugin
|
24 |
*
|
25 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
26 |
* @package Doctrine
|
27 |
* @subpackage Plugin
|
28 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
29 |
* @version $Revision$
|
30 |
* @link www.phpdoctrine.org
|
31 |
* @since 1.0
|
32 |
*/
|
33 |
abstract class Doctrine_Plugin
|
34 |
{
|
35 |
/**
|
36 |
* @var array $_options an array of plugin specific options
|
37 |
*/
|
38 |
protected $_options = array('generateFiles' => false,
|
39 |
'identifier' => false,
|
40 |
'generateFiles' => false,
|
41 |
'table' => false,
|
42 |
'pluginTable' => false,
|
43 |
'children' => array(),);
|
44 |
|
45 |
/**
|
46 |
* __get
|
47 |
* an alias for getOption
|
48 |
*
|
49 |
* @param string $option
|
50 |
*/
|
51 |
public function __get($option)
|
52 |
{
|
53 |
if (isset($this->_options[$option])) {
|
54 |
return $this->_options[$option];
|
55 |
}
|
56 |
return null;
|
57 |
}
|
58 |
|
59 |
/**
|
60 |
* __isset
|
61 |
*
|
62 |
* @param string $option
|
63 |
*/
|
64 |
public function __isset($option)
|
65 |
{
|
66 |
return isset($this->_options[$option]);
|
67 |
}
|
68 |
|
69 |
/**
|
70 |
* returns the value of an option
|
71 |
*
|
72 |
* @param $option the name of the option to retrieve
|
73 |
* @return mixed the value of the option
|
74 |
*/
|
75 |
public function getOption($name)
|
76 |
{
|
77 |
if ( ! isset($this->_options[$name])) {
|
78 |
throw new Doctrine_Plugin_Exception('Unknown option ' . $name);
|
79 |
}
|
80 |
|
81 |
return $this->_options[$name];
|
82 |
}
|
83 |
|
84 |
/**
|
85 |
* sets given value to an option
|
86 |
*
|
87 |
* @param $option the name of the option to be changed
|
88 |
* @param $value the value of the option
|
89 |
* @return Doctrine_Plugin this object
|
90 |
*/
|
91 |
public function setOption($name, $value)
|
92 |
{
|
93 |
$this->_options[$name] = $value;
|
94 |
|
95 |
return $this;
|
96 |
}
|
97 |
|
98 |
public function addChild(Doctrine_Template $template)
|
99 |
{
|
100 |
$this->_options['children'][] = $template;
|
101 |
}
|
102 |
|
103 |
/**
|
104 |
* returns all options and their associated values
|
105 |
*
|
106 |
* @return array all options as an associative array
|
107 |
*/
|
108 |
public function getOptions()
|
109 |
{
|
110 |
return $this->_options;
|
111 |
}
|
112 |
|
113 |
public function buildPluginDefinition(Doctrine_Table $table)
|
114 |
{
|
115 |
$this->_options['table'] = $table;
|
116 |
|
117 |
$this->_options['className'] = str_replace('%CLASS%',
|
118 |
$this->_options['table']->getComponentName(),
|
119 |
$this->_options['className']);
|
120 |
|
121 |
// check that class doesn't exist (otherwise we cannot create it)
|
122 |
if (class_exists($this->_options['className'])) {
|
123 |
return false;
|
124 |
}
|
125 |
|
126 |
$this->buildDefinition();
|
127 |
}
|
128 |
|
129 |
abstract public function buildDefinition();
|
130 |
|
131 |
public function buildForeignKeys(Doctrine_Table $table)
|
132 |
{
|
133 |
$id = $table->getIdentifier();
|
134 |
|
135 |
$fk = array();
|
136 |
foreach ((array) $id as $column) {
|
137 |
$def = $table->getDefinitionOf($column);
|
138 |
|
139 |
unset($def['autoincrement']);
|
140 |
unset($def['sequence']);
|
141 |
unset($def['unique']);
|
142 |
|
143 |
$fk[$column] = $def;
|
144 |
}
|
145 |
|
146 |
return $fk;
|
147 |
}
|
148 |
|
149 |
public function generateChildDefinitions()
|
150 |
{
|
151 |
foreach ($this->_options['children'] as $child) {
|
152 |
$this->_options['pluginTable']->addTemplate(get_class($child), $child);
|
153 |
|
154 |
$child->setTable($this->_options['pluginTable']);
|
155 |
|
156 |
$child->setUp();
|
157 |
}
|
158 |
}
|
159 |
|
160 |
/**
|
161 |
* generates foreign keys for the plugin table based on the owner table
|
162 |
*
|
163 |
* the foreign keys generated by this method can be used for
|
164 |
* setting the relations between the owner and the plugin classes
|
165 |
*
|
166 |
* @param Doctrine_Table $table the table object that owns the plugin
|
167 |
* @return array an array of foreign key definitions
|
168 |
*/
|
169 |
public function generateForeignKeys(Doctrine_Table $table)
|
170 |
{
|
171 |
$fk = array();
|
172 |
|
173 |
foreach ((array) $table->getIdentifier() as $column) {
|
174 |
$def = $table->getDefinitionOf($column);
|
175 |
|
176 |
unset($def['autoincrement']);
|
177 |
unset($def['sequence']);
|
178 |
unset($def['primary']);
|
179 |
|
180 |
$col = $column;
|
181 |
|
182 |
$def['primary'] = true;
|
183 |
$fk[$col] = $def;
|
184 |
}
|
185 |
return $fk;
|
186 |
}
|
187 |
|
188 |
/**
|
189 |
* generates a relation array to given table
|
190 |
*
|
191 |
* this method can be used for generating the relation from the plugin
|
192 |
* table to the owner table
|
193 |
*
|
194 |
* @param Doctrine_Table $table the table object to construct the relation to
|
195 |
* @param array $foreignKeys an array of foreign keys
|
196 |
* @return array the generated relation array
|
197 |
*/
|
198 |
public function generateRelation(Doctrine_Table $table, array $foreignKeys)
|
199 |
{
|
200 |
$local = (count($foreignKeys) > 1) ? array_keys($foreignKeys) : key($foreignKeys);
|
201 |
|
202 |
$relation = array($table->getComponentName() =>
|
203 |
array('local' => $local,
|
204 |
'foreign' => $table->getIdentifier(),
|
205 |
'onDelete' => 'CASCADE',
|
206 |
'onUpdate' => 'CASCADE'));
|
207 |
|
208 |
return $relation;
|
209 |
}
|
210 |
|
211 |
/**
|
212 |
* generates the class definition for plugin class
|
213 |
*
|
214 |
* @param array $options plugin class options, keys representing the option names
|
215 |
* and values as option values
|
216 |
* @param array $columns the plugin class columns, keys representing the column names
|
217 |
* and values as column definitions
|
218 |
* @param array $relations the bound relations of the plugin class
|
219 |
* @return void
|
220 |
*/
|
221 |
public function generateClass($options, $columns, $relations)
|
222 |
{
|
223 |
$builder = new Doctrine_Import_Builder();
|
224 |
|
225 |
if ($this->_options['generateFiles']) {
|
226 |
if (isset($this->_options['generatePath']) && $this->_options['generatePath']) {
|
227 |
$builder->setTargetPath($this->_options['generatePath']);
|
228 |
|
229 |
$builder->buildRecord($options, $columns, $relations);
|
230 |
} else {
|
231 |
throw new Doctrine_Plugin_Exception('If you wish to generate files then you must specify the path to generate the files in.');
|
232 |
}
|
233 |
} else {
|
234 |
$def = $builder->buildDefinition($options, $columns, $relations);
|
235 |
|
236 |
eval($def);
|
237 |
}
|
238 |
}
|
239 |
}
|