annotate vendor/wikimedia/composer-merge-plugin/src/Merge/PluginState.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@0 3 * This file is part of the Composer Merge plugin.
Chris@0 4 *
Chris@0 5 * Copyright (C) 2015 Bryan Davis, Wikimedia Foundation, and contributors
Chris@0 6 *
Chris@0 7 * This software may be modified and distributed under the terms of the MIT
Chris@0 8 * license. See the LICENSE file for details.
Chris@0 9 */
Chris@0 10
Chris@0 11 namespace Wikimedia\Composer\Merge;
Chris@0 12
Chris@0 13 use Composer\Composer;
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Mutable plugin state
Chris@0 17 *
Chris@0 18 * @author Bryan Davis <bd808@bd808.com>
Chris@0 19 */
Chris@0 20 class PluginState
Chris@0 21 {
Chris@0 22 /**
Chris@0 23 * @var Composer $composer
Chris@0 24 */
Chris@0 25 protected $composer;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * @var array $includes
Chris@0 29 */
Chris@0 30 protected $includes = array();
Chris@0 31
Chris@0 32 /**
Chris@0 33 * @var array $requires
Chris@0 34 */
Chris@0 35 protected $requires = array();
Chris@0 36
Chris@0 37 /**
Chris@0 38 * @var array $duplicateLinks
Chris@0 39 */
Chris@0 40 protected $duplicateLinks = array();
Chris@0 41
Chris@0 42 /**
Chris@0 43 * @var bool $devMode
Chris@0 44 */
Chris@0 45 protected $devMode = false;
Chris@0 46
Chris@0 47 /**
Chris@0 48 * @var bool $recurse
Chris@0 49 */
Chris@0 50 protected $recurse = true;
Chris@0 51
Chris@0 52 /**
Chris@0 53 * @var bool $replace
Chris@0 54 */
Chris@0 55 protected $replace = false;
Chris@0 56
Chris@0 57 /**
Chris@0 58 * @var bool $ignore
Chris@0 59 */
Chris@0 60 protected $ignore = false;
Chris@0 61
Chris@0 62 /**
Chris@0 63 * Whether to merge the -dev sections.
Chris@0 64 * @var bool $mergeDev
Chris@0 65 */
Chris@0 66 protected $mergeDev = true;
Chris@0 67
Chris@0 68 /**
Chris@0 69 * Whether to merge the extra section.
Chris@0 70 *
Chris@0 71 * By default, the extra section is not merged and there will be many
Chris@0 72 * cases where the merge of the extra section is performed too late
Chris@0 73 * to be of use to other plugins. When enabled, merging uses one of
Chris@0 74 * two strategies - either 'first wins' or 'last wins'. When enabled,
Chris@0 75 * 'first wins' is the default behaviour. If Replace mode is activated
Chris@0 76 * then 'last wins' is used.
Chris@0 77 *
Chris@0 78 * @var bool $mergeExtra
Chris@0 79 */
Chris@0 80 protected $mergeExtra = false;
Chris@0 81
Chris@0 82 /**
Chris@0 83 * Whether to merge the extra section in a deep / recursive way.
Chris@0 84 *
Chris@0 85 * By default the extra section is merged with array_merge() and duplicate
Chris@0 86 * keys are ignored. When enabled this allows to merge the arrays recursively
Chris@0 87 * using the following rule: Integer keys are merged, while array values are
Chris@0 88 * replaced where the later values overwrite the former.
Chris@0 89 *
Chris@0 90 * This is useful especially for the extra section when plugins use larger
Chris@0 91 * structures like a 'patches' key with the packages as sub-keys and the
Chris@0 92 * patches as values.
Chris@0 93 *
Chris@0 94 * When 'replace' mode is activated the order of array merges is exchanged.
Chris@0 95 *
Chris@0 96 * @var bool $mergeExtraDeep
Chris@0 97 */
Chris@0 98 protected $mergeExtraDeep = false;
Chris@0 99
Chris@0 100 /**
Chris@0 101 * Whether to merge the scripts section.
Chris@0 102 *
Chris@0 103 * @var bool $mergeScripts
Chris@0 104 */
Chris@0 105 protected $mergeScripts = false;
Chris@0 106
Chris@0 107 /**
Chris@0 108 * @var bool $firstInstall
Chris@0 109 */
Chris@0 110 protected $firstInstall = false;
Chris@0 111
Chris@0 112 /**
Chris@0 113 * @var bool $locked
Chris@0 114 */
Chris@0 115 protected $locked = false;
Chris@0 116
Chris@0 117 /**
Chris@0 118 * @var bool $dumpAutoloader
Chris@0 119 */
Chris@0 120 protected $dumpAutoloader = false;
Chris@0 121
Chris@0 122 /**
Chris@0 123 * @var bool $optimizeAutoloader
Chris@0 124 */
Chris@0 125 protected $optimizeAutoloader = false;
Chris@0 126
Chris@0 127 /**
Chris@0 128 * @param Composer $composer
Chris@0 129 */
Chris@0 130 public function __construct(Composer $composer)
Chris@0 131 {
Chris@0 132 $this->composer = $composer;
Chris@0 133 }
Chris@0 134
Chris@0 135 /**
Chris@0 136 * Load plugin settings
Chris@0 137 */
Chris@0 138 public function loadSettings()
Chris@0 139 {
Chris@0 140 $extra = $this->composer->getPackage()->getExtra();
Chris@0 141 $config = array_merge(
Chris@0 142 array(
Chris@0 143 'include' => array(),
Chris@0 144 'require' => array(),
Chris@0 145 'recurse' => true,
Chris@0 146 'replace' => false,
Chris@0 147 'ignore-duplicates' => false,
Chris@0 148 'merge-dev' => true,
Chris@0 149 'merge-extra' => false,
Chris@0 150 'merge-extra-deep' => false,
Chris@0 151 'merge-scripts' => false,
Chris@0 152 ),
Chris@0 153 isset($extra['merge-plugin']) ? $extra['merge-plugin'] : array()
Chris@0 154 );
Chris@0 155
Chris@0 156 $this->includes = (is_array($config['include'])) ?
Chris@0 157 $config['include'] : array($config['include']);
Chris@0 158 $this->requires = (is_array($config['require'])) ?
Chris@0 159 $config['require'] : array($config['require']);
Chris@0 160 $this->recurse = (bool)$config['recurse'];
Chris@0 161 $this->replace = (bool)$config['replace'];
Chris@0 162 $this->ignore = (bool)$config['ignore-duplicates'];
Chris@0 163 $this->mergeDev = (bool)$config['merge-dev'];
Chris@0 164 $this->mergeExtra = (bool)$config['merge-extra'];
Chris@0 165 $this->mergeExtraDeep = (bool)$config['merge-extra-deep'];
Chris@0 166 $this->mergeScripts = (bool)$config['merge-scripts'];
Chris@0 167 }
Chris@0 168
Chris@0 169 /**
Chris@0 170 * Get list of filenames and/or glob patterns to include
Chris@0 171 *
Chris@0 172 * @return array
Chris@0 173 */
Chris@0 174 public function getIncludes()
Chris@0 175 {
Chris@0 176 return $this->includes;
Chris@0 177 }
Chris@0 178
Chris@0 179 /**
Chris@0 180 * Get list of filenames and/or glob patterns to require
Chris@0 181 *
Chris@0 182 * @return array
Chris@0 183 */
Chris@0 184 public function getRequires()
Chris@0 185 {
Chris@0 186 return $this->requires;
Chris@0 187 }
Chris@0 188
Chris@0 189 /**
Chris@0 190 * Set the first install flag
Chris@0 191 *
Chris@0 192 * @param bool $flag
Chris@0 193 */
Chris@0 194 public function setFirstInstall($flag)
Chris@0 195 {
Chris@0 196 $this->firstInstall = (bool)$flag;
Chris@0 197 }
Chris@0 198
Chris@0 199 /**
Chris@0 200 * Is this the first time that the plugin has been installed?
Chris@0 201 *
Chris@0 202 * @return bool
Chris@0 203 */
Chris@0 204 public function isFirstInstall()
Chris@0 205 {
Chris@0 206 return $this->firstInstall;
Chris@0 207 }
Chris@0 208
Chris@0 209 /**
Chris@0 210 * Set the locked flag
Chris@0 211 *
Chris@0 212 * @param bool $flag
Chris@0 213 */
Chris@0 214 public function setLocked($flag)
Chris@0 215 {
Chris@0 216 $this->locked = (bool)$flag;
Chris@0 217 }
Chris@0 218
Chris@0 219 /**
Chris@0 220 * Was a lockfile present when the plugin was installed?
Chris@0 221 *
Chris@0 222 * @return bool
Chris@0 223 */
Chris@0 224 public function isLocked()
Chris@0 225 {
Chris@0 226 return $this->locked;
Chris@0 227 }
Chris@0 228
Chris@0 229 /**
Chris@0 230 * Should an update be forced?
Chris@0 231 *
Chris@0 232 * @return true If packages are not locked
Chris@0 233 */
Chris@0 234 public function forceUpdate()
Chris@0 235 {
Chris@0 236 return !$this->locked;
Chris@0 237 }
Chris@0 238
Chris@0 239 /**
Chris@0 240 * Set the devMode flag
Chris@0 241 *
Chris@0 242 * @param bool $flag
Chris@0 243 */
Chris@0 244 public function setDevMode($flag)
Chris@0 245 {
Chris@0 246 $this->devMode = (bool)$flag;
Chris@0 247 }
Chris@0 248
Chris@0 249 /**
Chris@0 250 * Should devMode settings be processed?
Chris@0 251 *
Chris@0 252 * @return bool
Chris@0 253 */
Chris@0 254 public function isDevMode()
Chris@0 255 {
Chris@0 256 return $this->shouldMergeDev() && $this->devMode;
Chris@0 257 }
Chris@0 258
Chris@0 259 /**
Chris@0 260 * Should devMode settings be merged?
Chris@0 261 *
Chris@0 262 * @return bool
Chris@0 263 */
Chris@0 264 public function shouldMergeDev()
Chris@0 265 {
Chris@0 266 return $this->mergeDev;
Chris@0 267 }
Chris@0 268
Chris@0 269 /**
Chris@0 270 * Set the dumpAutoloader flag
Chris@0 271 *
Chris@0 272 * @param bool $flag
Chris@0 273 */
Chris@0 274 public function setDumpAutoloader($flag)
Chris@0 275 {
Chris@0 276 $this->dumpAutoloader = (bool)$flag;
Chris@0 277 }
Chris@0 278
Chris@0 279 /**
Chris@0 280 * Is the autoloader file supposed to be written out?
Chris@0 281 *
Chris@0 282 * @return bool
Chris@0 283 */
Chris@0 284 public function shouldDumpAutoloader()
Chris@0 285 {
Chris@0 286 return $this->dumpAutoloader;
Chris@0 287 }
Chris@0 288
Chris@0 289 /**
Chris@0 290 * Set the optimizeAutoloader flag
Chris@0 291 *
Chris@0 292 * @param bool $flag
Chris@0 293 */
Chris@0 294 public function setOptimizeAutoloader($flag)
Chris@0 295 {
Chris@0 296 $this->optimizeAutoloader = (bool)$flag;
Chris@0 297 }
Chris@0 298
Chris@0 299 /**
Chris@0 300 * Should the autoloader be optimized?
Chris@0 301 *
Chris@0 302 * @return bool
Chris@0 303 */
Chris@0 304 public function shouldOptimizeAutoloader()
Chris@0 305 {
Chris@0 306 return $this->optimizeAutoloader;
Chris@0 307 }
Chris@0 308
Chris@0 309 /**
Chris@0 310 * Add duplicate packages
Chris@0 311 *
Chris@0 312 * @param string $type Package type
Chris@0 313 * @param array $packages
Chris@0 314 */
Chris@0 315 public function addDuplicateLinks($type, array $packages)
Chris@0 316 {
Chris@0 317 if (!isset($this->duplicateLinks[$type])) {
Chris@0 318 $this->duplicateLinks[$type] = array();
Chris@0 319 }
Chris@0 320 $this->duplicateLinks[$type] =
Chris@0 321 array_merge($this->duplicateLinks[$type], $packages);
Chris@0 322 }
Chris@0 323
Chris@0 324 /**
Chris@0 325 * Get duplicate packages
Chris@0 326 *
Chris@0 327 * @param string $type Package type
Chris@0 328 * @return array
Chris@0 329 */
Chris@0 330 public function getDuplicateLinks($type)
Chris@0 331 {
Chris@0 332 return isset($this->duplicateLinks[$type]) ?
Chris@0 333 $this->duplicateLinks[$type] : array();
Chris@0 334 }
Chris@0 335
Chris@0 336 /**
Chris@0 337 * Should includes be recursively processed?
Chris@0 338 *
Chris@0 339 * @return bool
Chris@0 340 */
Chris@0 341 public function recurseIncludes()
Chris@0 342 {
Chris@0 343 return $this->recurse;
Chris@0 344 }
Chris@0 345
Chris@0 346 /**
Chris@0 347 * Should duplicate links be replaced in a 'last definition wins' order?
Chris@0 348 *
Chris@0 349 * @return bool
Chris@0 350 */
Chris@0 351 public function replaceDuplicateLinks()
Chris@0 352 {
Chris@0 353 return $this->replace;
Chris@0 354 }
Chris@0 355
Chris@0 356 /**
Chris@0 357 * Should duplicate links be ignored?
Chris@0 358 *
Chris@0 359 * @return bool
Chris@0 360 */
Chris@0 361 public function ignoreDuplicateLinks()
Chris@0 362 {
Chris@0 363 return $this->ignore;
Chris@0 364 }
Chris@0 365
Chris@0 366 /**
Chris@0 367 * Should the extra section be merged?
Chris@0 368 *
Chris@0 369 * By default, the extra section is not merged and there will be many
Chris@0 370 * cases where the merge of the extra section is performed too late
Chris@0 371 * to be of use to other plugins. When enabled, merging uses one of
Chris@0 372 * two strategies - either 'first wins' or 'last wins'. When enabled,
Chris@0 373 * 'first wins' is the default behaviour. If Replace mode is activated
Chris@0 374 * then 'last wins' is used.
Chris@0 375 *
Chris@0 376 * @return bool
Chris@0 377 */
Chris@0 378 public function shouldMergeExtra()
Chris@0 379 {
Chris@0 380 return $this->mergeExtra;
Chris@0 381 }
Chris@0 382
Chris@0 383 /**
Chris@0 384 * Should the extra section be merged deep / recursively?
Chris@0 385 *
Chris@0 386 * By default the extra section is merged with array_merge() and duplicate
Chris@0 387 * keys are ignored. When enabled this allows to merge the arrays recursively
Chris@0 388 * using the following rule: Integer keys are merged, while array values are
Chris@0 389 * replaced where the later values overwrite the former.
Chris@0 390 *
Chris@0 391 * This is useful especially for the extra section when plugins use larger
Chris@0 392 * structures like a 'patches' key with the packages as sub-keys and the
Chris@0 393 * patches as values.
Chris@0 394 *
Chris@0 395 * When 'replace' mode is activated the order of array merges is exchanged.
Chris@0 396 *
Chris@0 397 * @return bool
Chris@0 398 */
Chris@0 399 public function shouldMergeExtraDeep()
Chris@0 400 {
Chris@0 401 return $this->mergeExtraDeep;
Chris@0 402 }
Chris@0 403
Chris@0 404
Chris@0 405 /**
Chris@0 406 * Should the scripts section be merged?
Chris@0 407 *
Chris@0 408 * By default, the scripts section is not merged.
Chris@0 409 *
Chris@0 410 * @return bool
Chris@0 411 */
Chris@0 412 public function shouldMergeScripts()
Chris@0 413 {
Chris@0 414 return $this->mergeScripts;
Chris@0 415 }
Chris@0 416 }
Chris@0 417 // vim:sw=4:ts=4:sts=4:et: