Chris@0: Chris@0: */ Chris@0: class PluginState Chris@0: { Chris@0: /** Chris@0: * @var Composer $composer Chris@0: */ Chris@0: protected $composer; Chris@0: Chris@0: /** Chris@0: * @var array $includes Chris@0: */ Chris@0: protected $includes = array(); Chris@0: Chris@0: /** Chris@0: * @var array $requires Chris@0: */ Chris@0: protected $requires = array(); Chris@0: Chris@0: /** Chris@0: * @var array $duplicateLinks Chris@0: */ Chris@0: protected $duplicateLinks = array(); Chris@0: Chris@0: /** Chris@0: * @var bool $devMode Chris@0: */ Chris@0: protected $devMode = false; Chris@0: Chris@0: /** Chris@0: * @var bool $recurse Chris@0: */ Chris@0: protected $recurse = true; Chris@0: Chris@0: /** Chris@0: * @var bool $replace Chris@0: */ Chris@0: protected $replace = false; Chris@0: Chris@0: /** Chris@0: * @var bool $ignore Chris@0: */ Chris@0: protected $ignore = false; Chris@0: Chris@0: /** Chris@0: * Whether to merge the -dev sections. Chris@0: * @var bool $mergeDev Chris@0: */ Chris@0: protected $mergeDev = true; Chris@0: Chris@0: /** Chris@0: * Whether to merge the extra section. Chris@0: * Chris@0: * By default, the extra section is not merged and there will be many Chris@0: * cases where the merge of the extra section is performed too late Chris@0: * to be of use to other plugins. When enabled, merging uses one of Chris@0: * two strategies - either 'first wins' or 'last wins'. When enabled, Chris@0: * 'first wins' is the default behaviour. If Replace mode is activated Chris@0: * then 'last wins' is used. Chris@0: * Chris@0: * @var bool $mergeExtra Chris@0: */ Chris@0: protected $mergeExtra = false; Chris@0: Chris@0: /** Chris@0: * Whether to merge the extra section in a deep / recursive way. Chris@0: * Chris@0: * By default the extra section is merged with array_merge() and duplicate Chris@0: * keys are ignored. When enabled this allows to merge the arrays recursively Chris@0: * using the following rule: Integer keys are merged, while array values are Chris@0: * replaced where the later values overwrite the former. Chris@0: * Chris@0: * This is useful especially for the extra section when plugins use larger Chris@0: * structures like a 'patches' key with the packages as sub-keys and the Chris@0: * patches as values. Chris@0: * Chris@0: * When 'replace' mode is activated the order of array merges is exchanged. Chris@0: * Chris@0: * @var bool $mergeExtraDeep Chris@0: */ Chris@0: protected $mergeExtraDeep = false; Chris@0: Chris@0: /** Chris@0: * Whether to merge the scripts section. Chris@0: * Chris@0: * @var bool $mergeScripts Chris@0: */ Chris@0: protected $mergeScripts = false; Chris@0: Chris@0: /** Chris@0: * @var bool $firstInstall Chris@0: */ Chris@0: protected $firstInstall = false; Chris@0: Chris@0: /** Chris@0: * @var bool $locked Chris@0: */ Chris@0: protected $locked = false; Chris@0: Chris@0: /** Chris@0: * @var bool $dumpAutoloader Chris@0: */ Chris@0: protected $dumpAutoloader = false; Chris@0: Chris@0: /** Chris@0: * @var bool $optimizeAutoloader Chris@0: */ Chris@0: protected $optimizeAutoloader = false; Chris@0: Chris@0: /** Chris@0: * @param Composer $composer Chris@0: */ Chris@0: public function __construct(Composer $composer) Chris@0: { Chris@0: $this->composer = $composer; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Load plugin settings Chris@0: */ Chris@0: public function loadSettings() Chris@0: { Chris@0: $extra = $this->composer->getPackage()->getExtra(); Chris@0: $config = array_merge( Chris@0: array( Chris@0: 'include' => array(), Chris@0: 'require' => array(), Chris@0: 'recurse' => true, Chris@0: 'replace' => false, Chris@0: 'ignore-duplicates' => false, Chris@0: 'merge-dev' => true, Chris@0: 'merge-extra' => false, Chris@0: 'merge-extra-deep' => false, Chris@0: 'merge-scripts' => false, Chris@0: ), Chris@0: isset($extra['merge-plugin']) ? $extra['merge-plugin'] : array() Chris@0: ); Chris@0: Chris@0: $this->includes = (is_array($config['include'])) ? Chris@0: $config['include'] : array($config['include']); Chris@0: $this->requires = (is_array($config['require'])) ? Chris@0: $config['require'] : array($config['require']); Chris@0: $this->recurse = (bool)$config['recurse']; Chris@0: $this->replace = (bool)$config['replace']; Chris@0: $this->ignore = (bool)$config['ignore-duplicates']; Chris@0: $this->mergeDev = (bool)$config['merge-dev']; Chris@0: $this->mergeExtra = (bool)$config['merge-extra']; Chris@0: $this->mergeExtraDeep = (bool)$config['merge-extra-deep']; Chris@0: $this->mergeScripts = (bool)$config['merge-scripts']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get list of filenames and/or glob patterns to include Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getIncludes() Chris@0: { Chris@0: return $this->includes; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get list of filenames and/or glob patterns to require Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getRequires() Chris@0: { Chris@0: return $this->requires; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the first install flag Chris@0: * Chris@0: * @param bool $flag Chris@0: */ Chris@0: public function setFirstInstall($flag) Chris@0: { Chris@0: $this->firstInstall = (bool)$flag; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Is this the first time that the plugin has been installed? Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isFirstInstall() Chris@0: { Chris@0: return $this->firstInstall; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the locked flag Chris@0: * Chris@0: * @param bool $flag Chris@0: */ Chris@0: public function setLocked($flag) Chris@0: { Chris@0: $this->locked = (bool)$flag; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Was a lockfile present when the plugin was installed? Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isLocked() Chris@0: { Chris@0: return $this->locked; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Should an update be forced? Chris@0: * Chris@0: * @return true If packages are not locked Chris@0: */ Chris@0: public function forceUpdate() Chris@0: { Chris@0: return !$this->locked; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the devMode flag Chris@0: * Chris@0: * @param bool $flag Chris@0: */ Chris@0: public function setDevMode($flag) Chris@0: { Chris@0: $this->devMode = (bool)$flag; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Should devMode settings be processed? Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isDevMode() Chris@0: { Chris@0: return $this->shouldMergeDev() && $this->devMode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Should devMode settings be merged? Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function shouldMergeDev() Chris@0: { Chris@0: return $this->mergeDev; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the dumpAutoloader flag Chris@0: * Chris@0: * @param bool $flag Chris@0: */ Chris@0: public function setDumpAutoloader($flag) Chris@0: { Chris@0: $this->dumpAutoloader = (bool)$flag; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Is the autoloader file supposed to be written out? Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function shouldDumpAutoloader() Chris@0: { Chris@0: return $this->dumpAutoloader; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Set the optimizeAutoloader flag Chris@0: * Chris@0: * @param bool $flag Chris@0: */ Chris@0: public function setOptimizeAutoloader($flag) Chris@0: { Chris@0: $this->optimizeAutoloader = (bool)$flag; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Should the autoloader be optimized? Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function shouldOptimizeAutoloader() Chris@0: { Chris@0: return $this->optimizeAutoloader; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Add duplicate packages Chris@0: * Chris@0: * @param string $type Package type Chris@0: * @param array $packages Chris@0: */ Chris@0: public function addDuplicateLinks($type, array $packages) Chris@0: { Chris@0: if (!isset($this->duplicateLinks[$type])) { Chris@0: $this->duplicateLinks[$type] = array(); Chris@0: } Chris@0: $this->duplicateLinks[$type] = Chris@0: array_merge($this->duplicateLinks[$type], $packages); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get duplicate packages Chris@0: * Chris@0: * @param string $type Package type Chris@0: * @return array Chris@0: */ Chris@0: public function getDuplicateLinks($type) Chris@0: { Chris@0: return isset($this->duplicateLinks[$type]) ? Chris@0: $this->duplicateLinks[$type] : array(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Should includes be recursively processed? Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function recurseIncludes() Chris@0: { Chris@0: return $this->recurse; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Should duplicate links be replaced in a 'last definition wins' order? Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function replaceDuplicateLinks() Chris@0: { Chris@0: return $this->replace; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Should duplicate links be ignored? Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function ignoreDuplicateLinks() Chris@0: { Chris@0: return $this->ignore; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Should the extra section be merged? Chris@0: * Chris@0: * By default, the extra section is not merged and there will be many Chris@0: * cases where the merge of the extra section is performed too late Chris@0: * to be of use to other plugins. When enabled, merging uses one of Chris@0: * two strategies - either 'first wins' or 'last wins'. When enabled, Chris@0: * 'first wins' is the default behaviour. If Replace mode is activated Chris@0: * then 'last wins' is used. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function shouldMergeExtra() Chris@0: { Chris@0: return $this->mergeExtra; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Should the extra section be merged deep / recursively? Chris@0: * Chris@0: * By default the extra section is merged with array_merge() and duplicate Chris@0: * keys are ignored. When enabled this allows to merge the arrays recursively Chris@0: * using the following rule: Integer keys are merged, while array values are Chris@0: * replaced where the later values overwrite the former. Chris@0: * Chris@0: * This is useful especially for the extra section when plugins use larger Chris@0: * structures like a 'patches' key with the packages as sub-keys and the Chris@0: * patches as values. Chris@0: * Chris@0: * When 'replace' mode is activated the order of array merges is exchanged. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function shouldMergeExtraDeep() Chris@0: { Chris@0: return $this->mergeExtraDeep; Chris@0: } Chris@0: Chris@0: Chris@0: /** Chris@0: * Should the scripts section be merged? Chris@0: * Chris@0: * By default, the scripts section is not merged. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function shouldMergeScripts() Chris@0: { Chris@0: return $this->mergeScripts; Chris@0: } Chris@0: } Chris@0: // vim:sw=4:ts=4:sts=4:et: