Commit | Line | Data |
---|---|---|
e39816b5 ARW |
1 | <?php |
2 | ||
b5c2afd0 EM |
3 | /** |
4 | * Class CRM_Core_CodeGen_Main | |
5 | */ | |
e39816b5 ARW |
6 | class CRM_Core_CodeGen_Main { |
7 | var $buildVersion; | |
8 | var $db_version; | |
e39816b5 ARW |
9 | var $cms; // drupal, joomla, wordpress |
10 | ||
11 | var $CoreDAOCodePath; | |
12 | var $sqlCodePath; | |
13 | var $phpCodePath; | |
14 | var $tplCodePath; | |
15 | var $schemaPath; // ex: schema/Schema.xml | |
16 | ||
3530751a TO |
17 | /** |
18 | * @var string|NULL path in which to store a marker that indicates the last execution of | |
19 | * GenCode. If a matching marker already exists, GenCode doesn't run. | |
20 | */ | |
21 | var $digestPath; | |
22 | ||
23 | /** | |
24 | * @var string|NULL a digest of the inputs to the code-generator (eg the properties and source files) | |
25 | */ | |
26 | var $digest; | |
27 | ||
2558c2b0 EM |
28 | /** |
29 | * @param $CoreDAOCodePath | |
30 | * @param $sqlCodePath | |
31 | * @param $phpCodePath | |
32 | * @param $tplCodePath | |
33 | * @param $smartyPluginDirs | |
34 | * @param $argCms | |
35 | * @param $argVersion | |
36 | * @param $schemaPath | |
37 | * @param $digestPath | |
38 | */ | |
00be9182 | 39 | public function __construct($CoreDAOCodePath, $sqlCodePath, $phpCodePath, $tplCodePath, $smartyPluginDirs, $argCms, $argVersion, $schemaPath, $digestPath) { |
e39816b5 ARW |
40 | $this->CoreDAOCodePath = $CoreDAOCodePath; |
41 | $this->sqlCodePath = $sqlCodePath; | |
42 | $this->phpCodePath = $phpCodePath; | |
43 | $this->tplCodePath = $tplCodePath; | |
3530751a TO |
44 | $this->digestPath = $digestPath; |
45 | $this->digest = NULL; | |
e39816b5 | 46 | |
5e434adf ARW |
47 | // default cms is 'drupal', if not specified |
48 | $this->cms = isset($argCms) ? strtolower($argCms) : 'drupal'; | |
e39816b5 | 49 | |
3deba037 | 50 | CRM_Core_CodeGen_Util_Smarty::singleton()->setPluginDirs($smartyPluginDirs); |
e39816b5 | 51 | |
353ffa53 TO |
52 | $versionFile = "version.xml"; |
53 | $versionXML = CRM_Core_CodeGen_Util_Xml::parse($versionFile); | |
54 | $this->db_version = $versionXML->version_no; | |
e39816b5 ARW |
55 | $this->buildVersion = preg_replace('/^(\d{1,2}\.\d{1,2})\.(\d{1,2}|\w{4,7})$/i', '$1', $this->db_version); |
56 | if (isset($argVersion)) { | |
57 | // change the version to that explicitly passed, if any | |
58 | $this->db_version = $argVersion; | |
59 | } | |
60 | ||
61 | $this->schemaPath = $schemaPath; | |
62 | } | |
63 | ||
e39816b5 | 64 | /** |
fe482240 | 65 | * Automatically generate a variety of files. |
e39816b5 | 66 | */ |
00be9182 | 67 | public function main() { |
693f0bff | 68 | if (!empty($this->digestPath) && file_exists($this->digestPath) && $this->hasExpectedFiles()) { |
3530751a TO |
69 | if ($this->getDigest() === file_get_contents($this->digestPath)) { |
70 | echo "GenCode has previously executed. To force execution, please (a) omit CIVICRM_GENCODE_DIGEST\n"; | |
71 | echo "or (b) remove {$this->digestPath} or (c) call GenCode with new parameters.\n"; | |
72 | exit(); | |
73 | } | |
74 | // Once we start GenCode, the old build is invalid | |
75 | unlink($this->digestPath); | |
76 | } | |
77 | ||
86bfa4f6 | 78 | echo "\ncivicrm_domain.version := " . $this->db_version . "\n\n"; |
e39816b5 ARW |
79 | if ($this->buildVersion < 1.1) { |
80 | echo "The Database is not compatible for this version"; | |
81 | exit(); | |
82 | } | |
83 | ||
84 | if (substr(phpversion(), 0, 1) != 5) { | |
85 | echo phpversion() . ', ' . substr(phpversion(), 0, 1) . "\n"; | |
86 | echo " | |
87 | CiviCRM requires a PHP Version >= 5 | |
88 | Please upgrade your php / webserver configuration | |
89 | Alternatively you can get a version of CiviCRM that matches your PHP version | |
90 | "; | |
91 | exit(); | |
92 | } | |
93 | ||
b972ac60 | 94 | $specification = new CRM_Core_CodeGen_Specification(); |
5e434adf ARW |
95 | $specification->parse($this->schemaPath, $this->buildVersion); |
96 | # cheese: | |
97 | $this->database = $specification->database; | |
98 | $this->tables = $specification->tables; | |
e39816b5 | 99 | |
5e434adf | 100 | $this->runAllTasks(); |
3530751a TO |
101 | |
102 | if (!empty($this->digestPath)) { | |
103 | file_put_contents($this->digestPath, $this->getDigest()); | |
104 | } | |
e39816b5 ARW |
105 | } |
106 | ||
00be9182 | 107 | public function runAllTasks() { |
5e434adf | 108 | // TODO: This configuration can be manipulated dynamically. |
3530751a | 109 | $components = $this->getTasks(); |
5e434adf ARW |
110 | foreach ($components as $component) { |
111 | $task = new $component($this); | |
e39816b5 | 112 | |
5e434adf ARW |
113 | if (is_a($task, 'CRM_Core_CodeGen_ITask')) { |
114 | $task->setConfig($this); | |
115 | $task->run(); | |
0db6c3e1 TO |
116 | } |
117 | else { | |
5e434adf | 118 | echo "Bad news: we tried to run a codegen task of an unrecognized type: {$component}\n"; |
e39816b5 ARW |
119 | exit(); |
120 | } | |
121 | } | |
e39816b5 | 122 | } |
3530751a TO |
123 | |
124 | /** | |
a6c01b45 | 125 | * @return array |
16b10e64 | 126 | * Array of class names; each class implements CRM_Core_CodeGen_ITask |
3530751a TO |
127 | */ |
128 | public function getTasks() { | |
129 | $components = array( | |
130 | 'CRM_Core_CodeGen_Config', | |
131 | 'CRM_Core_CodeGen_Reflection', | |
132 | 'CRM_Core_CodeGen_Schema', | |
133 | 'CRM_Core_CodeGen_DAO', | |
32f1f0fe | 134 | //'CRM_Core_CodeGen_Test', |
3530751a TO |
135 | 'CRM_Core_CodeGen_I18n', |
136 | ); | |
137 | return $components; | |
138 | } | |
139 | ||
140 | /** | |
141 | * Compute a digest based on the inputs to the code-generator (ie the properties | |
142 | * of the codegen and the source files loaded by the codegen). | |
143 | * | |
144 | * @return string | |
145 | */ | |
00be9182 | 146 | public function getDigest() { |
3530751a TO |
147 | if ($this->digest === NULL) { |
148 | $srcDir = CRM_Core_CodeGen_Util_File::findCoreSourceDir(); | |
149 | $files = CRM_Core_CodeGen_Util_File::findManyFiles(array( | |
150 | array("$srcDir/CRM/Core/CodeGen", '*.php'), | |
151 | array("$srcDir/xml", "*.php"), | |
152 | array("$srcDir/xml", "*.tpl"), | |
153 | array("$srcDir/xml", "*.xml"), | |
154 | )); | |
155 | ||
156 | $properties = var_export(array( | |
157 | CRM_Core_CodeGen_Util_File::digestAll($files), | |
158 | $this->buildVersion, | |
159 | $this->db_version, | |
160 | $this->cms, | |
161 | $this->CoreDAOCodePath, | |
162 | $this->sqlCodePath, | |
163 | $this->phpCodePath, | |
164 | $this->tplCodePath, | |
165 | $this->schemaPath, | |
166 | $this->getTasks(), | |
167 | ), TRUE); | |
168 | ||
169 | $this->digest = md5($properties); | |
170 | } | |
171 | return $this->digest; | |
172 | } | |
693f0bff | 173 | |
2558c2b0 EM |
174 | /** |
175 | * @return array | |
176 | */ | |
00be9182 | 177 | public function getExpectedFiles() { |
693f0bff TO |
178 | return array( |
179 | $this->sqlCodePath . '/civicrm.mysql', | |
180 | $this->phpCodePath . '/CRM/Contact/DAO/Contact.php', | |
181 | ); | |
182 | } | |
183 | ||
2558c2b0 EM |
184 | /** |
185 | * @return bool | |
186 | */ | |
00be9182 | 187 | public function hasExpectedFiles() { |
693f0bff TO |
188 | foreach ($this->getExpectedFiles() as $file) { |
189 | if (!file_exists($file)) { | |
190 | return FALSE; | |
191 | } | |
192 | } | |
193 | return TRUE; | |
194 | } | |
96025800 | 195 | |
e39816b5 | 196 | } |