Package.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 the full name of the package by prefixing it with bundle as needed
  129. *
  130. * NOTE: When possible, use the Bundle::getFullName method since it can
  131. * better handle cases where a bundle is a profile.
  132. *
  133. * @return string
  134. */
  135. public function getFullName() {
  136. if (empty($this->bundle) || $this->inBundle($this->machineName, $this->bundle)) {
  137. return $this->machineName;
  138. }
  139. else {
  140. return $this->bundle . '_' . $this->machineName;
  141. }
  142. }
  143. /**
  144. * @return string
  145. */
  146. public function getName() {
  147. return $this->name;
  148. }
  149. /**
  150. * @return string
  151. */
  152. public function getDescription() {
  153. return $this->description;
  154. }
  155. /**
  156. * @return string
  157. */
  158. public function getVersion() {
  159. return $this->version;
  160. }
  161. /**
  162. * @return int
  163. */
  164. public function getStatus() {
  165. return $this->status;
  166. }
  167. /**
  168. * @return string[]
  169. */
  170. public function getConfig() {
  171. return $this->config;
  172. }
  173. /**
  174. * Append a new filename.
  175. *
  176. * @param string $config
  177. *
  178. * @return $this
  179. */
  180. public function appendConfig($config) {
  181. $this->config[] = $config;
  182. $this->config = array_unique($this->config);
  183. return $this;
  184. }
  185. public function removeConfig($name) {
  186. $this->config = array_diff($this->config, [$name]);
  187. return $this;
  188. }
  189. /**
  190. * @return string
  191. */
  192. public function getBundle() {
  193. return $this->bundle;
  194. }
  195. /**
  196. * @return string[]
  197. */
  198. public function getExcluded() {
  199. return $this->excluded;
  200. }
  201. /**
  202. * @return string[]|bool
  203. */
  204. public function getRequired() {
  205. return $this->required;
  206. }
  207. /**
  208. * @return bool
  209. */
  210. public function getRequiredAll() {
  211. // Mark all as required if the package is not yet exported.
  212. if ($this->getStatus() === FeaturesManagerInterface::STATUS_NO_EXPORT) {
  213. return TRUE;
  214. }
  215. // Mark all as required if required is TRUE.
  216. if (is_bool($this->required)) {
  217. return $this->required;
  218. }
  219. // Mark all as required if required contains all the exported config.
  220. $config_orig = $this->getConfigOrig();
  221. $diff = array_diff($config_orig, $this->required);
  222. return empty($diff);
  223. }
  224. /**
  225. * @return string[]
  226. */
  227. public function getConfigOrig() {
  228. return $this->configOrig;
  229. }
  230. /**
  231. * @return string
  232. */
  233. public function getCore() {
  234. return $this->core;
  235. }
  236. /**
  237. * @return string
  238. */
  239. public function getType() {
  240. return $this->type;
  241. }
  242. /**
  243. * @return \string[]
  244. */
  245. public function getThemes() {
  246. return $this->themes;
  247. }
  248. /**
  249. * @return array
  250. */
  251. public function getInfo() {
  252. return $this->info;
  253. }
  254. /**
  255. * @return mixed
  256. */
  257. public function getState() {
  258. return $this->state;
  259. }
  260. /**
  261. * @return string
  262. */
  263. public function getDirectory() {
  264. return $this->directory;
  265. }
  266. /**
  267. * @return mixed
  268. */
  269. public function getFiles() {
  270. return $this->files;
  271. }
  272. /**
  273. * @return \Drupal\Core\Extension\Extension
  274. */
  275. public function getExtension() {
  276. return $this->extension;
  277. }
  278. public function getDependencies() {
  279. return $this->dependencies;
  280. }
  281. public function removeDependency($name) {
  282. $this->dependencies = array_diff($this->dependencies, [$name]);
  283. return $this;
  284. }
  285. public function getDependencyInfo() {
  286. return isset($this->info['dependencies']) ? $this->info['dependencies'] : [];
  287. }
  288. /**
  289. * Returns the features info.
  290. *
  291. * @return array
  292. */
  293. public function getFeaturesInfo() {
  294. $info = [];
  295. if (!empty($this->bundle)) {
  296. $info['bundle'] = $this->bundle;
  297. }
  298. if (!empty($this->excluded)) {
  299. $info['excluded'] = $this->excluded;
  300. }
  301. if ($this->required !== FALSE) {
  302. $info['required'] = $this->required;
  303. }
  304. return $info;
  305. }
  306. /**
  307. * Sets a new machine name.
  308. *
  309. * @param string $machine_name
  310. * The machine name
  311. *
  312. * @return $this
  313. */
  314. public function setMachineName($machine_name) {
  315. $this->machineName = $machine_name;
  316. return $this;
  317. }
  318. /**
  319. * @param string $name
  320. *
  321. * @return $this
  322. */
  323. public function setName($name) {
  324. $this->name = $name;
  325. return $this;
  326. }
  327. /**
  328. * @param string $description
  329. *
  330. * @return $this
  331. */
  332. public function setDescription($description) {
  333. $this->description = $description;
  334. return $this;
  335. }
  336. /**
  337. * @param string $version
  338. *
  339. * @return $this
  340. */
  341. public function setVersion($version) {
  342. $this->version = $version;
  343. return $this;
  344. }
  345. /**
  346. * @param string $bundle
  347. *
  348. * @return $this
  349. */
  350. public function setBundle($bundle) {
  351. $this->bundle = $bundle;
  352. return $this;
  353. }
  354. /**
  355. * @param array $info
  356. *
  357. * @return $this
  358. */
  359. public function setInfo($info) {
  360. $this->info = $info;
  361. return $this;
  362. }
  363. /**
  364. * @param \string[] $features_info
  365. *
  366. * @return $this
  367. */
  368. public function setFeaturesInfo($features_info) {
  369. if (isset($features_info['bundle'])) {
  370. $this->setBundle($features_info['bundle']);
  371. }
  372. $this->setRequired(isset($features_info['required']) ? $features_info['required'] : false);
  373. $this->setExcluded(isset($features_info['excluded']) ? $features_info['excluded'] : array());
  374. return $this;
  375. }
  376. /**
  377. * @param \string[] $dependencies
  378. *
  379. * @return $this
  380. */
  381. public function setDependencies($dependencies) {
  382. $this->dependencies = $dependencies;
  383. return $this;
  384. }
  385. /**
  386. * @param string $dependency
  387. *
  388. * return $this
  389. */
  390. public function appendDependency($dependency) {
  391. $this->dependencies[] = $dependency;
  392. return $this;
  393. }
  394. /**
  395. * @param int $status
  396. *
  397. * @return $this
  398. */
  399. public function setStatus($status) {
  400. $this->status = $status;
  401. return $this;
  402. }
  403. /**
  404. * @param \string[] $config
  405. *
  406. * @return $this
  407. */
  408. public function setConfig($config) {
  409. $this->config = $config;
  410. return $this;
  411. }
  412. /**
  413. * @param bool $excluded
  414. */
  415. public function setExcluded($excluded) {
  416. $this->excluded = $excluded;
  417. }
  418. /**
  419. * @param bool $required
  420. */
  421. public function setRequired($required) {
  422. $this->required = $required;
  423. }
  424. /**
  425. * @param string $core
  426. */
  427. public function setCore($core) {
  428. $this->core = $core;
  429. }
  430. /**
  431. * @param string $type
  432. */
  433. public function setType($type) {
  434. $this->type = $type;
  435. }
  436. /**
  437. * @param \string[] $themes
  438. */
  439. public function setThemes($themes) {
  440. $this->themes = $themes;
  441. }
  442. /**
  443. * @param int $state
  444. */
  445. public function setState($state) {
  446. $this->state = $state;
  447. }
  448. /**
  449. * @param string $directory
  450. */
  451. public function setDirectory($directory) {
  452. $this->directory = $directory;
  453. }
  454. /**
  455. * @param \string[] $files
  456. */
  457. public function setFiles($files) {
  458. $this->files = $files;
  459. }
  460. /**
  461. * @param array $file_array
  462. *
  463. * @return $this
  464. */
  465. public function appendFile(array $file_array, $key = NULL) {
  466. if (!isset($key)) {
  467. $this->files[] = $file_array;
  468. }
  469. else {
  470. $this->files[$key] = $file_array;
  471. }
  472. return $this;
  473. }
  474. /**
  475. * @param \Drupal\Core\Extension\Extension $extension
  476. */
  477. public function setExtension($extension) {
  478. $this->extension = $extension;
  479. }
  480. /**
  481. * @param \string[] $configOrig
  482. */
  483. public function setConfigOrig($configOrig) {
  484. $this->configOrig = $configOrig;
  485. }
  486. }