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