Package.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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[]
  203. */
  204. public function getRequired() {
  205. return $this->required;
  206. }
  207. /**
  208. * @return bool
  209. */
  210. public function getRequiredAll() {
  211. $config_orig = $this->getConfigOrig();
  212. $info = is_array($this->required) ? $this->required : array();
  213. $diff = array_diff($config_orig, $info);
  214. // Mark all as required if required:true, or required is empty, or
  215. // if required contains all the exported config
  216. return empty($diff) || empty($info);
  217. }
  218. /**
  219. * @return string[]
  220. */
  221. public function getConfigOrig() {
  222. return $this->configOrig;
  223. }
  224. /**
  225. * @return string
  226. */
  227. public function getCore() {
  228. return $this->core;
  229. }
  230. /**
  231. * @return string
  232. */
  233. public function getType() {
  234. return $this->type;
  235. }
  236. /**
  237. * @return \string[]
  238. */
  239. public function getThemes() {
  240. return $this->themes;
  241. }
  242. /**
  243. * @return array
  244. */
  245. public function getInfo() {
  246. return $this->info;
  247. }
  248. /**
  249. * @return mixed
  250. */
  251. public function getState() {
  252. return $this->state;
  253. }
  254. /**
  255. * @return string
  256. */
  257. public function getDirectory() {
  258. return $this->directory;
  259. }
  260. /**
  261. * @return mixed
  262. */
  263. public function getFiles() {
  264. return $this->files;
  265. }
  266. /**
  267. * @return \Drupal\Core\Extension\Extension
  268. */
  269. public function getExtension() {
  270. return $this->extension;
  271. }
  272. public function getDependencies() {
  273. return $this->dependencies;
  274. }
  275. public function removeDependency($name) {
  276. $this->dependencies = array_diff($this->dependencies, [$name]);
  277. return $this;
  278. }
  279. public function getDependencyInfo() {
  280. return isset($this->info['dependencies']) ? $this->info['dependencies'] : [];
  281. }
  282. /**
  283. * Returns the features info.
  284. *
  285. * @return array
  286. */
  287. public function getFeaturesInfo() {
  288. $info = [];
  289. if (!empty($this->bundle)) {
  290. $info['bundle'] = $this->bundle;
  291. }
  292. if (!empty($this->excluded)) {
  293. $info['excluded'] = $this->excluded;
  294. }
  295. if ($this->required !== FALSE) {
  296. $info['required'] = $this->required;
  297. }
  298. return $info;
  299. }
  300. /**
  301. * Sets a new machine name.
  302. *
  303. * @param string $machine_name
  304. * The machine name
  305. *
  306. * @return $this
  307. */
  308. public function setMachineName($machine_name) {
  309. $this->machineName = $machine_name;
  310. return $this;
  311. }
  312. /**
  313. * @param string $name
  314. *
  315. * @return $this
  316. */
  317. public function setName($name) {
  318. $this->name = $name;
  319. return $this;
  320. }
  321. /**
  322. * @param string $description
  323. *
  324. * @return $this
  325. */
  326. public function setDescription($description) {
  327. $this->description = $description;
  328. return $this;
  329. }
  330. /**
  331. * @param string $version
  332. *
  333. * @return $this
  334. */
  335. public function setVersion($version) {
  336. $this->version = $version;
  337. return $this;
  338. }
  339. /**
  340. * @param string $bundle
  341. *
  342. * @return $this
  343. */
  344. public function setBundle($bundle) {
  345. $this->bundle = $bundle;
  346. return $this;
  347. }
  348. /**
  349. * @param array $info
  350. *
  351. * @return $this
  352. */
  353. public function setInfo($info) {
  354. $this->info = $info;
  355. return $this;
  356. }
  357. /**
  358. * @param \string[] $features_info
  359. *
  360. * @return $this
  361. */
  362. public function setFeaturesInfo($features_info) {
  363. if (isset($features_info['bundle'])) {
  364. $this->setBundle($features_info['bundle']);
  365. }
  366. $this->setRequired(isset($features_info['required']) ? $features_info['required'] : false);
  367. $this->setExcluded(isset($features_info['excluded']) ? $features_info['excluded'] : array());
  368. return $this;
  369. }
  370. /**
  371. * @param \string[] $dependencies
  372. *
  373. * @return $this
  374. */
  375. public function setDependencies($dependencies) {
  376. $this->dependencies = $dependencies;
  377. return $this;
  378. }
  379. /**
  380. * @param string $dependency
  381. *
  382. * return $this
  383. */
  384. public function appendDependency($dependency) {
  385. $this->dependencies[] = $dependency;
  386. return $this;
  387. }
  388. /**
  389. * @param int $status
  390. *
  391. * @return $this
  392. */
  393. public function setStatus($status) {
  394. $this->status = $status;
  395. return $this;
  396. }
  397. /**
  398. * @param \string[] $config
  399. *
  400. * @return $this
  401. */
  402. public function setConfig($config) {
  403. $this->config = $config;
  404. return $this;
  405. }
  406. /**
  407. * @param bool $excluded
  408. */
  409. public function setExcluded($excluded) {
  410. $this->excluded = $excluded;
  411. }
  412. /**
  413. * @param bool $required
  414. */
  415. public function setRequired($required) {
  416. $this->required = $required;
  417. }
  418. /**
  419. * @param string $core
  420. */
  421. public function setCore($core) {
  422. $this->core = $core;
  423. }
  424. /**
  425. * @param string $type
  426. */
  427. public function setType($type) {
  428. $this->type = $type;
  429. }
  430. /**
  431. * @param \string[] $themes
  432. */
  433. public function setThemes($themes) {
  434. $this->themes = $themes;
  435. }
  436. /**
  437. * @param int $state
  438. */
  439. public function setState($state) {
  440. $this->state = $state;
  441. }
  442. /**
  443. * @param string $directory
  444. */
  445. public function setDirectory($directory) {
  446. $this->directory = $directory;
  447. }
  448. /**
  449. * @param \string[] $files
  450. */
  451. public function setFiles($files) {
  452. $this->files = $files;
  453. }
  454. /**
  455. * @param array $file_array
  456. *
  457. * @return $this
  458. */
  459. public function appendFile(array $file_array, $key = NULL) {
  460. if (!isset($key)) {
  461. $this->files[] = $file_array;
  462. }
  463. else {
  464. $this->files[$key] = $file_array;
  465. }
  466. return $this;
  467. }
  468. /**
  469. * @param \Drupal\Core\Extension\Extension $extension
  470. */
  471. public function setExtension($extension) {
  472. $this->extension = $extension;
  473. }
  474. /**
  475. * @param \string[] $configOrig
  476. */
  477. public function setConfigOrig($configOrig) {
  478. $this->configOrig = $configOrig;
  479. }
  480. }