Merge pull request #14326 from civicrm/5.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 * Path in which to store a marker that indicates the last execution of GenCode.
41 *
42 * If a matching marker already exists, GenCode doesn't run.
43 *
44 * @var string|null
45 */
46 public $digestPath;
47
48 /**
49 * Digest of the inputs to the code-generator (eg the properties and source files).
50 *
51 * @var string|null
52 */
53 public $sourceDigest;
54
55 /**
56 * @param $CoreDAOCodePath
57 * @param $sqlCodePath
58 * @param $phpCodePath
59 * @param $tplCodePath
60 * @param $IGNORE
61 * @param $argCms
62 * @param $argVersion
63 * @param $schemaPath
64 * @param $digestPath
65 */
66 public function __construct($CoreDAOCodePath, $sqlCodePath, $phpCodePath, $tplCodePath, $IGNORE, $argCms, $argVersion, $schemaPath, $digestPath) {
67 $this->CoreDAOCodePath = $CoreDAOCodePath;
68 $this->sqlCodePath = $sqlCodePath;
69 $this->phpCodePath = $phpCodePath;
70 $this->tplCodePath = $tplCodePath;
71 $this->digestPath = $digestPath;
72 $this->sourceDigest = NULL;
73
74 // default cms is 'drupal', if not specified
75 $this->cms = isset($argCms) ? strtolower($argCms) : 'drupal';
76
77 $versionFile = $this->phpCodePath . "/xml/version.xml";
78 $versionXML = CRM_Core_CodeGen_Util_Xml::parse($versionFile);
79 $this->db_version = $versionXML->version_no;
80 $this->buildVersion = preg_replace('/^(\d{1,2}\.\d{1,2})\.(\d{1,2}|\w{4,7})$/i', '$1', $this->db_version);
81 if (isset($argVersion)) {
82 // change the version to that explicitly passed, if any
83 $this->db_version = $argVersion;
84 }
85
86 $this->schemaPath = $schemaPath;
87 }
88
89 /**
90 * Automatically generate a variety of files.
91 */
92 public function main() {
93 echo "\ncivicrm_domain.version := " . $this->db_version . "\n\n";
94 if ($this->buildVersion < 1.1) {
95 echo "The Database is not compatible for this version";
96 exit();
97 }
98
99 if (substr(phpversion(), 0, 1) < 5) {
100 echo phpversion() . ', ' . substr(phpversion(), 0, 1) . "\n";
101 echo "
102 CiviCRM requires a PHP Version >= 5
103 Please upgrade your php / webserver configuration
104 Alternatively you can get a version of CiviCRM that matches your PHP version
105 ";
106 exit();
107 }
108
109 foreach ($this->getTasks() as $task) {
110 if (getenv('GENCODE_FORCE') || $task->needsUpdate()) {
111 $task->run();
112 }
113 }
114 }
115
116 /**
117 * @return array
118 * Array<CRM_Core_CodeGen_ITask>.
119 * @throws \Exception
120 */
121 public function getTasks() {
122 $this->init();
123
124 $tasks = [];
125 $tasks[] = new CRM_Core_CodeGen_Config($this);
126 $tasks[] = new CRM_Core_CodeGen_Reflection($this);
127 $tasks[] = new CRM_Core_CodeGen_Schema($this);
128 foreach (array_keys($this->tables) as $name) {
129 $tasks[] = new CRM_Core_CodeGen_DAO($this, $name);
130 }
131 $tasks[] = new CRM_Core_CodeGen_I18n($this);
132 return $tasks;
133 }
134
135 /**
136 * Compute a digest based on the GenCode logic (PHP/tpl).
137 *
138 * @return string
139 */
140 public function getSourceDigest() {
141 if ($this->sourceDigest === NULL) {
142 $srcDir = CRM_Core_CodeGen_Util_File::findCoreSourceDir();
143 $files = CRM_Core_CodeGen_Util_File::findManyFiles([
144 ["$srcDir/CRM/Core/CodeGen", '*.php'],
145 ["$srcDir/xml", "*.php"],
146 ["$srcDir/xml", "*.tpl"],
147 ]);
148
149 $this->sourceDigest = CRM_Core_CodeGen_Util_File::digestAll($files);
150 }
151 return $this->sourceDigest;
152 }
153
154 protected function init() {
155 if (!$this->database || !$this->tables) {
156 $specification = new CRM_Core_CodeGen_Specification();
157 $specification->parse($this->schemaPath, $this->buildVersion);
158 # cheese:
159 $this->database = $specification->database;
160 $this->tables = $specification->tables;
161 }
162 }
163
164 }