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.com>.
|
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.com
|
31 |
* @since 1.0
|
32 |
*/
|
33 |
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 |
|
41 |
/**
|
42 |
* __get
|
43 |
* an alias for getOption
|
44 |
*
|
45 |
* @param string $option
|
46 |
*/
|
47 |
public function __get($option)
|
48 |
{
|
49 |
if (isset($this->_options[$option])) {
|
50 |
return $this->_options[$option];
|
51 |
}
|
52 |
return null;
|
53 |
}
|
54 |
|
55 |
/**
|
56 |
* __isset
|
57 |
*
|
58 |
* @param string $option
|
59 |
*/
|
60 |
public function __isset($option)
|
61 |
{
|
62 |
return isset($this->_options[$option]);
|
63 |
}
|
64 |
|
65 |
/**
|
66 |
* returns the value of an option
|
67 |
*
|
68 |
* @param $option the name of the option to retrieve
|
69 |
* @return mixed the value of the option
|
70 |
*/
|
71 |
public function getOption($name)
|
72 |
{
|
73 |
if ( ! isset($this->_options[$name])) {
|
74 |
throw new Doctrine_Plugin_Exception('Unknown option ' . $name);
|
75 |
}
|
76 |
|
77 |
return $this->_options[$name];
|
78 |
}
|
79 |
|
80 |
/**
|
81 |
* sets given value to an option
|
82 |
*
|
83 |
* @param $option the name of the option to be changed
|
84 |
* @param $value the value of the option
|
85 |
* @return Doctrine_Plugin this object
|
86 |
*/
|
87 |
public function setOption($name, $value)
|
88 |
{
|
89 |
$this->_options[$name] = $value;
|
90 |
|
91 |
return $this;
|
92 |
}
|
93 |
|
94 |
/**
|
95 |
* returns all options and their associated values
|
96 |
*
|
97 |
* @return array all options as an associative array
|
98 |
*/
|
99 |
public function getOptions()
|
100 |
{
|
101 |
return $this->_options;
|
102 |
}
|
103 |
|
104 |
/**
|
105 |
* generates foreign keys for the plugin table based on the owner table
|
106 |
*
|
107 |
* the foreign keys generated by this method can be used for
|
108 |
* setting the relations between the owner and the plugin classes
|
109 |
*
|
110 |
* @param Doctrine_Table $table the table object that owns the plugin
|
111 |
* @return array an array of foreign key definitions
|
112 |
*/
|
113 |
public function generateForeignKeys(Doctrine_Table $table)
|
114 |
{
|
115 |
$fk = array();
|
116 |
|
117 |
foreach ((array) $table->getIdentifier() as $column) {
|
118 |
$def = $table->getDefinitionOf($column);
|
119 |
|
120 |
unset($def['autoincrement']);
|
121 |
unset($def['sequence']);
|
122 |
unset($def['primary']);
|
123 |
|
124 |
$col = $column;
|
125 |
|
126 |
$def['primary'] = true;
|
127 |
$fk[$col] = $def;
|
128 |
}
|
129 |
return $fk;
|
130 |
}
|
131 |
|
132 |
/**
|
133 |
* generates a relation array to given table
|
134 |
*
|
135 |
* this method can be used for generating the relation from the plugin
|
136 |
* table to the owner table
|
137 |
*
|
138 |
* @param Doctrine_Table $table the table object to construct the relation to
|
139 |
* @param array $foreignKeys an array of foreign keys
|
140 |
* @return array the generated relation array
|
141 |
*/
|
142 |
public function generateRelation(Doctrine_Table $table, array $foreignKeys)
|
143 |
{
|
144 |
$local = (count($foreignKeys) > 1) ? array_keys($foreignKeys) : key($foreignKeys);
|
145 |
|
146 |
$relation = array($table->getComponentName() =>
|
147 |
array('local' => $local,
|
148 |
'foreign' => $table->getIdentifier(),
|
149 |
'onDelete' => 'CASCADE',
|
150 |
'onUpdate' => 'CASCADE'));
|
151 |
|
152 |
return $relation;
|
153 |
}
|
154 |
|
155 |
/**
|
156 |
* generates the class definition for plugin class
|
157 |
*
|
158 |
* @param array $options plugin class options, keys representing the option names
|
159 |
* and values as option values
|
160 |
* @param array $columns the plugin class columns, keys representing the column names
|
161 |
* and values as column definitions
|
162 |
* @param array $relations the bound relations of the plugin class
|
163 |
* @return void
|
164 |
*/
|
165 |
public function generateClass($options, $columns, $relations)
|
166 |
{
|
167 |
$builder = new Doctrine_Import_Builder();
|
168 |
|
169 |
if ($this->_options['generateFiles']) {
|
170 |
if (isset($this->_options['generatePath']) && $this->_options['generatePath']) {
|
171 |
$builder->setTargetPath($this->_options['generatePath']);
|
172 |
|
173 |
$builder->buildRecord($options, $columns, $relations);
|
174 |
} else {
|
175 |
throw new Doctrine_Plugin_Exception('If you wish to generate files then you must specify the path to generate the files in.');
|
176 |
}
|
177 |
} else {
|
178 |
$def = $builder->buildDefinition($options, $columns, $relations);
|
179 |
|
180 |
eval($def);
|
181 |
}
|
182 |
}
|
183 |
} |