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