4 * Class CRM_Core_CodeGen_Main
6 class CRM_Core_CodeGen_Main
{
9 var $cms; // drupal, joomla, wordpress
15 var $schemaPath; // ex: schema/Schema.xml
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.
24 * @var string|NULL a digest of the inputs to the code-generator (eg the properties and source files)
29 * @param $CoreDAOCodePath
33 * @param $smartyPluginDirs
39 public function __construct($CoreDAOCodePath, $sqlCodePath, $phpCodePath, $tplCodePath, $smartyPluginDirs, $argCms, $argVersion, $schemaPath, $digestPath) {
40 $this->CoreDAOCodePath
= $CoreDAOCodePath;
41 $this->sqlCodePath
= $sqlCodePath;
42 $this->phpCodePath
= $phpCodePath;
43 $this->tplCodePath
= $tplCodePath;
44 $this->digestPath
= $digestPath;
47 // default cms is 'drupal', if not specified
48 $this->cms
= isset($argCms) ?
strtolower($argCms) : 'drupal';
50 CRM_Core_CodeGen_Util_Smarty
::singleton()->setPluginDirs($smartyPluginDirs);
52 $versionFile = "version.xml";
53 $versionXML = CRM_Core_CodeGen_Util_Xml
::parse($versionFile);
54 $this->db_version
= $versionXML->version_no
;
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;
61 $this->schemaPath
= $schemaPath;
65 * Automatically generate a variety of files.
67 public function main() {
68 if (!empty($this->digestPath
) && file_exists($this->digestPath
) && $this->hasExpectedFiles()) {
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";
74 // Once we start GenCode, the old build is invalid
75 unlink($this->digestPath
);
78 echo "\ncivicrm_domain.version := " . $this->db_version
. "\n\n";
79 if ($this->buildVersion
< 1.1) {
80 echo "The Database is not compatible for this version";
84 if (substr(phpversion(), 0, 1) != 5) {
85 echo phpversion() . ', ' . substr(phpversion(), 0, 1) . "\n";
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
94 $specification = new CRM_Core_CodeGen_Specification();
95 $specification->parse($this->schemaPath
, $this->buildVersion
);
97 $this->database
= $specification->database
;
98 $this->tables
= $specification->tables
;
100 $this->runAllTasks();
102 if (!empty($this->digestPath
)) {
103 file_put_contents($this->digestPath
, $this->getDigest());
107 public function runAllTasks() {
108 // TODO: This configuration can be manipulated dynamically.
109 $components = $this->getTasks();
110 foreach ($components as $component) {
111 $task = new $component($this);
113 if (is_a($task, 'CRM_Core_CodeGen_ITask')) {
114 $task->setConfig($this);
118 echo "Bad news: we tried to run a codegen task of an unrecognized type: {$component}\n";
126 * Array of class names; each class implements CRM_Core_CodeGen_ITask
128 public function getTasks() {
130 'CRM_Core_CodeGen_Config',
131 'CRM_Core_CodeGen_Reflection',
132 'CRM_Core_CodeGen_Schema',
133 'CRM_Core_CodeGen_DAO',
134 //'CRM_Core_CodeGen_Test',
135 'CRM_Core_CodeGen_I18n',
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).
146 public function getDigest() {
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"),
156 $properties = var_export(array(
157 CRM_Core_CodeGen_Util_File
::digestAll($files),
161 $this->CoreDAOCodePath
,
169 $this->digest
= md5($properties);
171 return $this->digest
;
177 public function getExpectedFiles() {
179 $this->sqlCodePath
. '/civicrm.mysql',
180 $this->phpCodePath
. '/CRM/Contact/DAO/Contact.php',
187 public function hasExpectedFiles() {
188 foreach ($this->getExpectedFiles() as $file) {
189 if (!file_exists($file)) {