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