Package.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?php
  2. namespace Drupal\features;
  3. /**
  4. * Defines a value object for storing package related data.
  5. *
  6. * A package contains of a name, version number, containing config etc.
  7. */
  8. class Package {
  9. /**
  10. * @var string
  11. */
  12. protected $machineName = '';
  13. /**
  14. * @var string
  15. */
  16. protected $name = '';
  17. /**
  18. * @var string
  19. */
  20. protected $description = '';
  21. /**
  22. * @todo This could be fetched from the extension object.
  23. *
  24. * @var string
  25. */
  26. protected $version = '';
  27. /**
  28. * @var string
  29. */
  30. protected $core = '8.x';
  31. /**
  32. * @todo This could be fetched from the extension object.
  33. *
  34. * @var string
  35. */
  36. protected $type = 'module';
  37. /**
  38. * @var string[]
  39. */
  40. protected $themes = [];
  41. /**
  42. * @var string
  43. */
  44. protected $bundle;
  45. /**
  46. * @var string[]
  47. */
  48. protected $excluded = [];
  49. /**
  50. * @var string[]|bool
  51. */
  52. protected $required = false;
  53. /**
  54. * @var array
  55. */
  56. protected $info = [];
  57. /**
  58. * @var string[]
  59. */
  60. protected $dependencies = [];
  61. /**
  62. * @todo This could be fetched from the extension object.
  63. *
  64. * @var int
  65. */
  66. protected $status;
  67. /**
  68. * @var int
  69. */
  70. protected $state;
  71. /**
  72. * @todo This could be fetched from the extension object.
  73. *
  74. * @var string
  75. */
  76. protected $directory;
  77. /**
  78. * @var string[]
  79. */
  80. protected $files;
  81. /**
  82. * @var \Drupal\Core\Extension\Extension
  83. */
  84. protected $extension;
  85. /**
  86. * @var string[]
  87. */
  88. protected $config = [];
  89. /**
  90. * @var string[]
  91. */
  92. protected $configOrig = [];
  93. /**
  94. * Creates a new Package instance.
  95. *
  96. * @param string $machine_name
  97. * The machine name.
  98. * @param array $additional_properties
  99. * (optional) Additional properties of the object.
  100. */
  101. public function __construct($machine_name, array $additional_properties = []) {
  102. $this->machineName = $machine_name;
  103. $properties = get_object_vars($this);
  104. foreach ($additional_properties as $property => $value) {
  105. if (!array_key_exists($property, $properties)) {
  106. throw new \InvalidArgumentException('Invalid property: ' . $property);
  107. }
  108. $this->{$property} = $value;
  109. }
  110. }
  111. /**
  112. * @return mixed
  113. */
  114. public function getMachineName() {
  115. return $this->machineName;
  116. }
  117. /**
  118. * Return TRUE if the machine_name already has the bundle prefix.
  119. *
  120. * @param string $machine_name
  121. * @param string $bundle_name
  122. * @return bool
  123. */
  124. protected function inBundle($machine_name, $bundle_name) {
  125. return strpos($machine_name, $bundle_name . '_') === 0;
  126. }
  127. /**
  128. * @return string
  129. */
  130. public function getFullName() {
  131. if (empty($this->bundle) || $this->inBundle($this->machineName, $this->bundle)) {
  132. return $this->machineName;
  133. }
  134. else {
  135. return $this->bundle . '_' . $this->machineName;
  136. }
  137. }
  138. /**
  139. * @return string
  140. */
  141. public function getName() {
  142. return $this->name;
  143. }
  144. /**
  145. * @return string
  146. */
  147. public function getDescription() {
  148. return $this->description;
  149. }
  150. /**
  151. * @return string
  152. */
  153. public function getVersion() {
  154. return $this->version;
  155. }
  156. /**
  157. * @return int
  158. */
  159. public function getStatus() {
  160. return $this->status;
  161. }
  162. /**
  163. * @return string[]
  164. */
  165. public function getConfig() {
  166. return $this->config;
  167. }
  168. /**
  169. * Append a new filename.
  170. *
  171. * @param string $config
  172. *
  173. * @return $this
  174. */
  175. public function appendConfig($config) {
  176. $this->config[] = $config;
  177. $this->config = array_unique($this->config);
  178. return $this;
  179. }
  180. public function removeConfig($name) {
  181. $this->config = array_diff($this->config, [$name]);
  182. return $this;
  183. }
  184. /**
  185. * @return string
  186. */
  187. public function getBundle() {
  188. return $this->bundle;
  189. }
  190. /**
  191. * @return string[]
  192. */
  193. public function getExcluded() {
  194. return $this->excluded;
  195. }
  196. /**
  197. * @return string[]
  198. */
  199. public function getRequired() {
  200. return $this->required;
  201. }
  202. /**
  203. * @return bool
  204. */
  205. public function getRequiredAll() {
  206. $config_orig = $this->getConfigOrig();
  207. $info = is_array($this->required) ? $this->required : array();
  208. $diff = array_diff($config_orig, $info);
  209. // Mark all as required if required:true, or required is empty, or
  210. // if required contains all the exported config
  211. return empty($diff) || empty($info);
  212. }
  213. /**
  214. * @return string[]
  215. */
  216. public function getConfigOrig() {
  217. return $this->configOrig;
  218. }
  219. /**
  220. * @return string
  221. */
  222. public function getCore() {
  223. return $this->core;
  224. }
  225. /**
  226. * @return string
  227. */
  228. public function getType() {
  229. return $this->type;
  230. }
  231. /**
  232. * @return \string[]
  233. */
  234. public function getThemes() {
  235. return $this->themes;
  236. }
  237. /**
  238. * @return array
  239. */
  240. public function getInfo() {
  241. return $this->info;
  242. }
  243. /**
  244. * @return mixed
  245. */
  246. public function getState() {
  247. return $this->state;
  248. }
  249. /**
  250. * @return string
  251. */
  252. public function getDirectory() {
  253. return $this->directory;
  254. }
  255. /**
  256. * @return mixed
  257. */
  258. public function getFiles() {
  259. return $this->files;
  260. }
  261. /**
  262. * @return \Drupal\Core\Extension\Extension
  263. */
  264. public function getExtension() {
  265. return $this->extension;
  266. }
  267. public function getDependencies() {
  268. return $this->dependencies;
  269. }
  270. public function removeDependency($name) {
  271. $this->dependencies = array_diff($this->dependencies, [$name]);
  272. return $this;
  273. }
  274. public function getDependencyInfo() {
  275. return isset($this->info['dependencies']) ? $this->info['dependencies'] : [];
  276. }
  277. /**
  278. * Returns the features info.
  279. *
  280. * @return array
  281. */
  282. public function getFeaturesInfo() {
  283. $info = [];
  284. if (!empty($this->bundle)) {
  285. $info['bundle'] = $this->bundle;
  286. }
  287. if (!empty($this->excluded)) {
  288. $info['excluded'] = $this->excluded;
  289. }
  290. if ($this->required !== FALSE) {
  291. $info['required'] = $this->required;
  292. }
  293. return $info;
  294. }
  295. /**
  296. * Sets a new machine name.
  297. *
  298. * @param string $machine_name
  299. * The machine name
  300. *
  301. * @return $this
  302. */
  303. public function setMachineName($machine_name) {
  304. $this->machineName = $machine_name;
  305. return $this;
  306. }
  307. /**
  308. * @param string $name
  309. *
  310. * @return $this
  311. */
  312. public function setName($name) {
  313. $this->name = $name;
  314. return $this;
  315. }
  316. /**
  317. * @param string $description
  318. *
  319. * @return $this
  320. */
  321. public function setDescription($description) {
  322. $this->description = $description;
  323. return $this;
  324. }
  325. /**
  326. * @param string $version
  327. *
  328. * @return $this
  329. */
  330. public function setVersion($version) {
  331. $this->version = $version;
  332. return $this;
  333. }
  334. /**
  335. * @param string $bundle
  336. *
  337. * @return $this
  338. */
  339. public function setBundle($bundle) {
  340. $this->bundle = $bundle;
  341. return $this;
  342. }
  343. /**
  344. * @param array $info
  345. *
  346. * @return $this
  347. */
  348. public function setInfo($info) {
  349. $this->info = $info;
  350. return $this;
  351. }
  352. /**
  353. * @param \string[] $features_info
  354. *
  355. * @return $this
  356. */
  357. public function setFeaturesInfo($features_info) {
  358. if (isset($features_info['bundle'])) {
  359. $this->setBundle($features_info['bundle']);
  360. }
  361. $this->setRequired(isset($features_info['required']) ? $features_info['required'] : false);
  362. $this->setExcluded(isset($features_info['excluded']) ? $features_info['excluded'] : array());
  363. return $this;
  364. }
  365. /**
  366. * @param \string[] $dependencies
  367. *
  368. * @return $this
  369. */
  370. public function setDependencies($dependencies) {
  371. $this->dependencies = $dependencies;
  372. return $this;
  373. }
  374. /**
  375. * @param string $dependency
  376. *
  377. * return $this
  378. */
  379. public function appendDependency($dependency) {
  380. $this->dependencies[] = $dependency;
  381. return $this;
  382. }
  383. /**
  384. * @param int $status
  385. *
  386. * @return $this
  387. */
  388. public function setStatus($status) {
  389. $this->status = $status;
  390. return $this;
  391. }
  392. /**
  393. * @param \string[] $config
  394. *
  395. * @return $this
  396. */
  397. public function setConfig($config) {
  398. $this->config = $config;
  399. return $this;
  400. }
  401. /**
  402. * @param bool $excluded
  403. */
  404. public function setExcluded($excluded) {
  405. $this->excluded = $excluded;
  406. }
  407. /**
  408. * @param bool $required
  409. */
  410. public function setRequired($required) {
  411. $this->required = $required;
  412. }
  413. /**
  414. * @param string $core
  415. */
  416. public function setCore($core) {
  417. $this->core = $core;
  418. }
  419. /**
  420. * @param string $type
  421. */
  422. public function setType($type) {
  423. $this->type = $type;
  424. }
  425. /**
  426. * @param \string[] $themes
  427. */
  428. public function setThemes($themes) {
  429. $this->themes = $themes;
  430. }
  431. /**
  432. * @param int $state
  433. */
  434. public function setState($state) {
  435. $this->state = $state;
  436. }
  437. /**
  438. * @param string $directory
  439. */
  440. public function setDirectory($directory) {
  441. $this->directory = $directory;
  442. }
  443. /**
  444. * @param \string[] $files
  445. */
  446. public function setFiles($files) {
  447. $this->files = $files;
  448. }
  449. /**
  450. * @param array $file_array
  451. *
  452. * @return $this
  453. */
  454. public function appendFile(array $file_array, $key = NULL) {
  455. if (!isset($key)) {
  456. $this->files[] = $file_array;
  457. }
  458. else {
  459. $this->files[$key] = $file_array;
  460. }
  461. return $this;
  462. }
  463. /**
  464. * @param \Drupal\Core\Extension\Extension $extension
  465. */
  466. public function setExtension($extension) {
  467. $this->extension = $extension;
  468. }
  469. /**
  470. * @param \string[] $configOrig
  471. */
  472. public function setConfigOrig($configOrig) {
  473. $this->configOrig = $configOrig;
  474. }
  475. }