Merge pull request #14014 from MegaphoneJon/reporting-14
[civicrm-core.git] / CRM / Core / CodeGen / Main.php
1 <?php
2
3 /**
4 * Class CRM_Core_CodeGen_Main
5 */
6 class CRM_Core_CodeGen_Main {
7 public $buildVersion;
8 public $db_version;
9 /**
10 * drupal, joomla, wordpress
11 * @var string
12 */
13 public $cms;
14
15 public $CoreDAOCodePath;
16 public $sqlCodePath;
17 public $phpCodePath;
18 public $tplCodePath;
19 /**
20 * ex: schema/Schema.xml
21 * @var string
22 */
23 public $schemaPath;
24
25 /**
26 * Definitions of all tables.
27 *
28 * @var array
29 * Ex: $tables['civicrm_address_format']['className'] = 'CRM_Core_DAO_AddressFormat';
30 */
31 public $tables;
32
33 /**
34 * @var array
35 * Ex: $database['tableAttributes_modern'] = "ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci";
36 */
37 public $database;
38
39 /**
40 * @var string|NULL path in which to store a marker that indicates the last execution of
41 * GenCode. If a matching marker already exists, GenCode doesn't run.
42 */
43 public $digestPath;
44
45 /**
46 * @var string|NULL a digest of the inputs to the code-generator (eg the properties and source files)
47 */
48 public $sourceDigest;
49
50 /**
51 * @param $CoreDAOCodePath
52 * @param $sqlCodePath
53 * @param $phpCodePath
54 * @param $tplCodePath
55 * @param $IGNORE
56 * @param $argCms
57 * @param $argVersion
58 * @param $schemaPath
59 * @param $digestPath
60 */
61 public function __construct($CoreDAOCodePath, $sqlCodePath, $phpCodePath, $tplCodePath, $IGNORE, $argCms, $argVersion, $schemaPath, $digestPath) {
62 $this->CoreDAOCodePath = $CoreDAOCodePath;
63 $this->sqlCodePath = $sqlCodePath;
64 $this->phpCodePath = $phpCodePath;
65 $this->tplCodePath = $tplCodePath;
66 $this->digestPath = $digestPath;
67 $this->sourceDigest = NULL;
68
69 // default cms is 'drupal', if not specified
70 $this->cms = isset($argCms) ? strtolower($argCms) : 'drupal';
71
72 $versionFile = $this->phpCodePath . "/xml/version.xml";
73 $versionXML = CRM_Core_CodeGen_Util_Xml::parse($versionFile);
74 $this->db_version = $versionXML->version_no;
75 $this->buildVersion = preg_replace('/^(\d{1,2}\.\d{1,2})\.(\d{1,2}|\w{4,7})$/i', '$1', $this->db_version);
76 if (isset($argVersion)) {
77 // change the version to that explicitly passed, if any
78 $this->db_version = $argVersion;
79 }
80
81 $this->schemaPath = $schemaPath;
82 }
83
84 /**
85 * Automatically generate a variety of files.
86 */
87 public function main() {
88 echo "\ncivicrm_domain.version := " . $this->db_version . "\n\n";
89 if ($this->buildVersion < 1.1) {
90 echo "The Database is not compatible for this version";
91 exit();
92 }
93
94 if (substr(phpversion(), 0, 1) < 5) {
95 echo phpversion() . ', ' . substr(phpversion(), 0, 1) . "\n";
96 echo "
97 CiviCRM requires a PHP Version >= 5
98 Please upgrade your php / webserver configuration
99 Alternatively you can get a version of CiviCRM that matches your PHP version
100 ";
101 exit();
102 }
103
104 foreach ($this->getTasks() as $task) {
105 if (getenv('GENCODE_FORCE') || $task->needsUpdate()) {
106 $task->run();
107 }
108 }
109 }
110
111 /**
112 * @return array
113 * Array<CRM_Core_CodeGen_ITask>.
114 * @throws \Exception
115 */
116 public function getTasks() {
117 $this->init();
118
119 $tasks = [];
120 $tasks[] = new CRM_Core_CodeGen_Config($this);
121 $tasks[] = new CRM_Core_CodeGen_Reflection($this);
122 $tasks[] = new CRM_Core_CodeGen_Schema($this);
123 foreach (array_keys($this->tables) as $name) {
124 $tasks[] = new CRM_Core_CodeGen_DAO($this, $name);
125 }
126 $tasks[] = new CRM_Core_CodeGen_I18n($this);
127 return $tasks;
128 }
129
130 /**
131 * Compute a digest based on the GenCode logic (PHP/tpl).
132 *
133 * @return string
134 */
135 public function getSourceDigest() {
136 if ($this->sourceDigest === NULL) {
137 $srcDir = CRM_Core_CodeGen_Util_File::findCoreSourceDir();
138 $files = CRM_Core_CodeGen_Util_File::findManyFiles([
139 ["$srcDir/CRM/Core/CodeGen", '*.php'],
140 ["$srcDir/xml", "*.php"],
141 ["$srcDir/xml", "*.tpl"],
142 ]);
143
144 $this->sourceDigest = CRM_Core_CodeGen_Util_File::digestAll($files);
145 }
146 return $this->sourceDigest;
147 }
148
149 protected function init() {
150 if (!$this->database || !$this->tables) {
151 $specification = new CRM_Core_CodeGen_Specification();
152 $specification->parse($this->schemaPath, $this->buildVersion);
153 # cheese:
154 $this->database = $specification->database;
155 $this->tables = $specification->tables;
156 }
157 }
158
159 }