#!/usr/bin/env php * Jordi Boggiano * * For the full copyright and license information, please view * the license that is located at the bottom of this file. */ // Avoid APC causing random fatal errors per https://github.com/composer/composer/issues/264 if (extension_loaded('apc') && filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN) && filter_var(ini_get('apc.cache_by_default'), FILTER_VALIDATE_BOOLEAN)) { if (version_compare(phpversion('apc'), '3.0.12', '>=')) { ini_set('apc.cache_by_default', 0); } else { fwrite(STDERR, 'Warning: APC <= 3.0.12 may cause fatal errors when running composer commands.'.PHP_EOL); fwrite(STDERR, 'Update APC, or set apc.enable_cli or apc.cache_by_default to 0 in your php.ini.'.PHP_EOL); } } if (!class_exists('Phar')) { echo 'PHP\'s phar extension is missing. Composer requires it to run. Enable the extension or recompile php without --disable-phar then try again.' . PHP_EOL; exit(1); } Phar::mapPhar('composer.phar'); require 'phar://composer.phar/bin/composer'; __HALT_COMPILER(); ?>  composer.phar!src/Composer/Advisory/Auditor.php!c1src/Composer/Advisory/PartialSecurityAdvisory.php !c $}zߤ*src/Composer/Advisory/SecurityAdvisory.php!c!+src/Composer/Autoload/AutoloadGenerator.php'!c'g[+src/Composer/Autoload/ClassMapGenerator.php!c|S(src/Composer/Autoload/PhpFileCleaner.php!ckgsrc/Composer/Cache.php!cլ %src/Composer/Command/AboutCommand.phpW!cW:!'src/Composer/Command/ArchiveCommand.phpY!cY7c%src/Composer/Command/AuditCommand.php2 !c2 hņ$src/Composer/Command/BaseCommand.php&!c& ^/E.src/Composer/Command/BaseDependencyCommand.php,!c,_դ$src/Composer/Command/BumpCommand.phpi!ciB2f1src/Composer/Command/CheckPlatformReqsCommand.phpy!cy*src/Composer/Command/ClearCacheCommand.php !c *(src/Composer/Command/CompletionTrait.phpW!cW@Ί&src/Composer/Command/ConfigCommand.php`!c`al-src/Composer/Command/CreateProjectCommand.phpJ!cJJ$'src/Composer/Command/DependsCommand.phpF!cF{e(src/Composer/Command/DiagnoseCommand.phpN!cN;v,src/Composer/Command/DumpAutoloadCommand.php!c,K$src/Composer/Command/ExecCommand.phpl !cl ; $src/Composer/Command/FundCommand.php!c5`G&src/Composer/Command/GlobalCommand.php!c$src/Composer/Command/HomeCommand.php^!c^"+C$src/Composer/Command/InitCommand.phpB!cBSȤ'src/Composer/Command/InstallCommand.php!c !(src/Composer/Command/LicensesCommand.php!cƃi](src/Composer/Command/OutdatedCommand.php!c.src/Composer/Command/PackageDiscoveryTrait.php4!c4l)src/Composer/Command/ProhibitsCommand.php!c񡁤)src/Composer/Command/ReinstallCommand.phpd!cdf&src/Composer/Command/RemoveCommand.php,!c,U:w'src/Composer/Command/RequireCommand.phpJ!cJVQ)src/Composer/Command/RunScriptCommand.php!c6%+src/Composer/Command/ScriptAliasCommand.php!c,HDͤ&src/Composer/Command/SearchCommand.phpc!cc6Ƥ*src/Composer/Command/SelfUpdateCommand.phpwK!cwKiu$src/Composer/Command/ShowCommand.php!cv*&src/Composer/Command/StatusCommand.phpU!cUީ(src/Composer/Command/SuggestsCommand.php !c fk&src/Composer/Command/UpdateCommand.php2!c2%(src/Composer/Command/ValidateCommand.php !c src/Composer/Composer.php!csrc/Composer/Config.php5!c5r-src/Composer/Config/ConfigSourceInterface.php!cӤ(src/Composer/Config/JsonConfigSource.phpc!cc!T-$src/Composer/Console/Application.phpnK!cnKi*src/Composer/Console/GithubActionError.php!c`,src/Composer/Console/HtmlOutputFormatter.php!c[K*,src/Composer/Console/Input/InputArgument.php!cK*src/Composer/Console/Input/InputOption.phpg!cg7'4-src/Composer/DependencyResolver/Decisions.php!c]1src/Composer/DependencyResolver/DefaultPolicy.phpx!cx/U/src/Composer/DependencyResolver/GenericRule.php!cZ)8src/Composer/DependencyResolver/LocalRepoTransaction.php!cߚH3src/Composer/DependencyResolver/LockTransaction.phpI !cI V萯5src/Composer/DependencyResolver/MultiConflictRule.php+!c+w>src/Composer/DependencyResolver/Operation/InstallOperation.php!c=Isrc/Composer/DependencyResolver/Operation/MarkAliasInstalledOperation.php!c1NJKsrc/Composer/DependencyResolver/Operation/MarkAliasUninstalledOperation.php!ctw @src/Composer/DependencyResolver/Operation/OperationInterface.php!cW=src/Composer/DependencyResolver/Operation/SolverOperation.phpH!cH@src/Composer/DependencyResolver/Operation/UninstallOperation.php!c|R=src/Composer/DependencyResolver/Operation/UpdateOperation.php!c7椤3src/Composer/DependencyResolver/PolicyInterface.php!c(src/Composer/DependencyResolver/Pool.php!cxF!/src/Composer/DependencyResolver/PoolBuilder.phpA!cAGV1src/Composer/DependencyResolver/PoolOptimizer.php)!c)!\+src/Composer/DependencyResolver/Problem.phpL!cLO+src/Composer/DependencyResolver/Request.php!c*֤(src/Composer/DependencyResolver/Rule.php<0!c<01src/Composer/DependencyResolver/Rule2Literals.php!c3v+src/Composer/DependencyResolver/RuleSet.php !c T :4src/Composer/DependencyResolver/RuleSetGenerator.php=!c=k3src/Composer/DependencyResolver/RuleSetIterator.php!c y2src/Composer/DependencyResolver/RuleWatchChain.php{!c{hD2src/Composer/DependencyResolver/RuleWatchGraph.php !c Z{|1src/Composer/DependencyResolver/RuleWatchNode.phpv!cvp*src/Composer/DependencyResolver/Solver.php9!c9v{6src/Composer/DependencyResolver/SolverBugException.php!cz=;src/Composer/DependencyResolver/SolverProblemsException.php!c./src/Composer/DependencyResolver/Transaction.php-!c--src/Composer/Downloader/ArchiveDownloader.php!c>'쐤1src/Composer/Downloader/ChangeReportInterface.php!cB+src/Composer/Downloader/DownloadManager.php!cc/src/Composer/Downloader/DownloaderInterface.php!cGޤ3src/Composer/Downloader/DvcsDownloaderInterface.php!c%'*src/Composer/Downloader/FileDownloader.php/!c/z/src/Composer/Downloader/FilesystemException.php/!c/!&,src/Composer/Downloader/FossilDownloader.phpi !ci Z)src/Composer/Downloader/GitDownloader.phpD!cDJ*src/Composer/Downloader/GzipDownloader.php!cv(src/Composer/Downloader/HgDownloader.php? !c? /8src/Composer/Downloader/MaxFileSizeExceededException.php!cںԤ*src/Composer/Downloader/PathDownloader.php*!c*?a.src/Composer/Downloader/PerforceDownloader.php !c <*src/Composer/Downloader/PharDownloader.php!cX)src/Composer/Downloader/RarDownloader.php!cGU)src/Composer/Downloader/SvnDownloader.phpt!ct/*)src/Composer/Downloader/TarDownloader.php!cr߂.src/Composer/Downloader/TransportException.phps!cs 9src/Composer/Downloader/VcsCapableDownloaderInterface.php!c6)src/Composer/Downloader/VcsDownloader.php2!c2AOI(src/Composer/Downloader/XzDownloader.php!c&r)src/Composer/Downloader/ZipDownloader.phpG !cG [&&src/Composer/EventDispatcher/Event.php!c0src/Composer/EventDispatcher/EventDispatcher.php86!c86/' G9src/Composer/EventDispatcher/EventSubscriberInterface.php!c}=9src/Composer/EventDispatcher/ScriptExecutionException.php!cXvϤ9src/Composer/Exception/IrrecoverableDownloadException.php!c04 )src/Composer/Exception/NoSslException.php!cUҤsrc/Composer/Factory.phpO!cOD2Tsrc/Composer/Filter/PlatformRequirementFilter/IgnoreAllPlatformRequirementFilter.phpS!cS~JUsrc/Composer/Filter/PlatformRequirementFilter/IgnoreListPlatformRequirementFilter.phpY!cYшXsrc/Composer/Filter/PlatformRequirementFilter/IgnoreNothingPlatformRequirementFilter.php!c~&#Rsrc/Composer/Filter/PlatformRequirementFilter/PlatformRequirementFilterFactory.phpw!cwTTsrc/Composer/Filter/PlatformRequirementFilter/PlatformRequirementFilterInterface.php!cnsrc/Composer/IO/BaseIO.php!c4դsrc/Composer/IO/BufferIO.php;!c;Uvsrc/Composer/IO/ConsoleIO.php}!c}MӤsrc/Composer/IO/IOInterface.php!csrc/Composer/IO/NullIO.phpO!cObssrc/Composer/Installer.php!c`*src/Composer/Installer/BinaryInstaller.phpS)!cS)]2src/Composer/Installer/BinaryPresenceInterface.php!c3.src/Composer/Installer/InstallationManager.php3!c3V )src/Composer/Installer/InstallerEvent.php*!c*FZ٤*src/Composer/Installer/InstallerEvents.php!c>Ǥ-src/Composer/Installer/InstallerInterface.phpx!cxب+src/Composer/Installer/LibraryInstaller.php !c ^/src/Composer/Installer/MetapackageInstaller.php!cs(src/Composer/Installer/NoopInstaller.phpa!caxQn'src/Composer/Installer/PackageEvent.php!cb\A(src/Composer/Installer/PackageEvents.php!cK*src/Composer/Installer/PluginInstaller.php !c Po+src/Composer/Installer/ProjectInstaller.php9 !c9 Uц4src/Composer/Installer/SuggestedPackagesReporter.php!cOsrc/Composer/Json/JsonFile.php!c:#src/Composer/Json/JsonFormatter.php!cK Aɤ%src/Composer/Json/JsonManipulator.php)4!c)4#-src/Composer/Json/JsonValidationException.php!c^^2src/Composer/PHPStan/ConfigReturnTypeExtension.php!cݗB:src/Composer/PHPStan/RuleReasonDataReturnTypeExtension.php~!c~H%src/Composer/Package/AliasPackage.phpk!ck7src/Composer/Package/Archiver/ArchivableFilesFilter.php1!c1{7ю7src/Composer/Package/Archiver/ArchivableFilesFinder.phpJ!cJ20src/Composer/Package/Archiver/ArchiveManager.phpi!cig[3src/Composer/Package/Archiver/ArchiverInterface.phpS!cS?3src/Composer/Package/Archiver/BaseExcludeFilter.php!cx6Ȥ7src/Composer/Package/Archiver/ComposerExcludeFilter.php?!c??`^2src/Composer/Package/Archiver/GitExcludeFilter.php$!c$t8N.src/Composer/Package/Archiver/PharArchiver.php!c-٪-src/Composer/Package/Archiver/ZipArchiver.php !c m$src/Composer/Package/BasePackage.php!c̤*src/Composer/Package/Comparer/Comparer.php !c 7F-src/Composer/Package/CompleteAliasPackage.phpq !cq #(src/Composer/Package/CompletePackage.php !c J1src/Composer/Package/CompletePackageInterface.php!c+src/Composer/Package/Dumper/ArrayDumper.phpH !cH (src/Composer/Package/Link.php!c"j+src/Composer/Package/Loader/ArrayLoader.php-!c-zh7src/Composer/Package/Loader/InvalidPackageException.php!cM*src/Composer/Package/Loader/JsonLoader.php!c褢4/src/Composer/Package/Loader/LoaderInterface.php!cFB1src/Composer/Package/Loader/RootPackageLoader.php!ci1`5src/Composer/Package/Loader/ValidatingArrayLoader.php#N!c#N/㸥src/Composer/Package/Locker.phpT%!cT% src/Composer/Package/Package.php4$!c4$!^)src/Composer/Package/PackageInterface.php !c ])src/Composer/Package/RootAliasPackage.php !c A:D$src/Composer/Package/RootPackage.php!c -src/Composer/Package/RootPackageInterface.php9!c90src/Composer/Package/Version/StabilityFilter.php!cr.src/Composer/Package/Version/VersionBumper.php!cdD/src/Composer/Package/Version/VersionGuesser.php'!c'ƤΤ.src/Composer/Package/Version/VersionParser.php!c0src/Composer/Package/Version/VersionSelector.php!c& src/Composer/PartialComposer.php!c &src/Composer/Platform/HhvmDetector.php-!c-#!src/Composer/Platform/Runtime.php!co^1%!src/Composer/Platform/Version.php!cv-src/Composer/Plugin/Capability/Capability.phpp!cp#c;2src/Composer/Plugin/Capability/CommandProvider.php!cF٤src/Composer/Plugin/Capable.php!ch$src/Composer/Plugin/CommandEvent.php?!c?|U`.src/Composer/Plugin/PluginBlockedException.php!cr$src/Composer/Plugin/PluginEvents.php!c\K֤'src/Composer/Plugin/PluginInterface.php!c%src/Composer/Plugin/PluginManager.phpL!cLy>i-src/Composer/Plugin/PostFileDownloadEvent.php!c@>*src/Composer/Plugin/PreCommandRunEvent.php/!c/3 ,src/Composer/Plugin/PreFileDownloadEvent.php!c`[*src/Composer/Plugin/PrePoolCreateEvent.php!cۤ4src/Composer/Question/StrictConfirmationQuestion.php!cxA5src/Composer/Repository/AdvisoryProviderInterface.php!cа+src/Composer/Repository/ArrayRepository.php4!c4դ.src/Composer/Repository/ArtifactRepository.phpB !cB SNj2src/Composer/Repository/CanonicalPackagesTrait.php!c{.src/Composer/Repository/ComposerRepository.php9!c9{CZ/src/Composer/Repository/CompositeRepository.phpy !cy ;src/Composer/Repository/ConfigurableRepositoryInterface.php!cce*20src/Composer/Repository/FilesystemRepository.php"!c"4ਤ,src/Composer/Repository/FilterRepository.php !c +#4src/Composer/Repository/InstalledArrayRepository.phpX!cX,[9src/Composer/Repository/InstalledFilesystemRepository.phpJ!cJ'kC/src/Composer/Repository/InstalledRepository.phpK!cKyᒤ8src/Composer/Repository/InstalledRepositoryInterface.php!cg6src/Composer/Repository/InvalidRepositoryException.php!c W<_/src/Composer/Repository/LockArrayRepository.php!c^-src/Composer/Repository/PackageRepository.php !c \/**src/Composer/Repository/PathRepository.php!c,j *src/Composer/Repository/PearRepository.php!cQuj.src/Composer/Repository/PlatformRepository.phpR!cR}-src/Composer/Repository/RepositoryFactory.phpW!cW(/src/Composer/Repository/RepositoryInterface.php!c, :-src/Composer/Repository/RepositoryManager.php !c ,}~7src/Composer/Repository/RepositorySecurityException.php!cqt)src/Composer/Repository/RepositorySet.php !c .+src/Composer/Repository/RepositoryUtils.php!cIz1src/Composer/Repository/RootPackageRepository.php\!c\`9,src/Composer/Repository/Vcs/FossilDriver.phpB!cB~*2src/Composer/Repository/Vcs/GitBitbucketDriver.phpe&!ce& iä)src/Composer/Repository/Vcs/GitDriver.php[!c[-Ĥ,src/Composer/Repository/Vcs/GitHubDriver.php.8!c.8V+,src/Composer/Repository/Vcs/GitLabDriver.php1!c1dHw(src/Composer/Repository/Vcs/HgDriver.php!c&/N.src/Composer/Repository/Vcs/PerforceDriver.php !c %ڤ)src/Composer/Repository/Vcs/SvnDriver.phpi!ci" ֤)src/Composer/Repository/Vcs/VcsDriver.php !c &:2src/Composer/Repository/Vcs/VcsDriverInterface.php!cT)src/Composer/Repository/VcsRepository.php+4!c+41src/Composer/Repository/VersionCacheInterface.php!ch3src/Composer/Repository/WritableArrayRepository.php!cl&#7src/Composer/Repository/WritableRepositoryInterface.phpw!cw~0src/Composer/Script/Event.php!cy$src/Composer/Script/ScriptEvents.phpI!cID4 src/Composer/SelfUpdate/Keys.php!c s$src/Composer/SelfUpdate/Versions.php^ !c^ o src/Composer/Util/AuthHelper.php"!c"^src/Composer/Util/Bitbucket.php!c(5$src/Composer/Util/ComposerMirror.php!c)Yz3%src/Composer/Util/ConfigValidator.phpe!ce3"src/Composer/Util/ErrorHandler.phpL!cLQ% src/Composer/Util/Filesystem.php=!c=O;Фsrc/Composer/Util/Git.phpB:!cB:src/Composer/Util/GitHub.phpF!cFSsrc/Composer/Util/GitLab.php!csrc/Composer/Util/Hg.phpO!cOЄ>)src/Composer/Util/Http/CurlDownloader.phpJ!cJ'/('src/Composer/Util/Http/CurlResponse.php!c;&src/Composer/Util/Http/ProxyHelper.php !c /\_ 'src/Composer/Util/Http/ProxyManager.phpx !cx 'src/Composer/Util/Http/RequestProxy.phpg!cġ#src/Composer/Util/Http/Response.php!cmR^$src/Composer/Util/HttpDownloader.php'!c'AJsrc/Composer/Util/IniHelper.php!cmBsrc/Composer/Util/Loop.php !c '&src/Composer/Util/MetadataMinifier.phpC!cCd $src/Composer/Util/NoProxyPattern.php(!c(M !src/Composer/Util/PackageInfo.php!c#src/Composer/Util/PackageSorter.php!cXѷsrc/Composer/Util/Perforce.php"4!c"4EQ[src/Composer/Util/Platform.php!c8?%src/Composer/Util/ProcessExecutor.phpv !cv &src/Composer/Util/RemoteFilesystem.php@!c@ RUsrc/Composer/Util/Silencer.php!c*src/Composer/Util/StreamContextFactory.php!ce6src/Composer/Util/Svn.php!cL8 src/Composer/Util/SyncHelper.php_!c_3Psrc/Composer/Util/Tar.php!ct?src/Composer/Util/TlsHelper.phpj !cj ЧQsrc/Composer/Util/Url.phpk !ck 6src/Composer/Util/Zip.php!cDّsrc/bootstrap.php !c -Ȥ%src/Composer/Autoload/ClassLoader.php>!c>5Ky"src/Composer/InstalledVersions.php:!c:畎K#res/composer-repository-schema.json!c|0res/composer-schema.json!cĤvendor/autoload.phps!csפvendor/composer/ClassLoader.php\!c\Zu%vendor/composer/InstalledVersions.phpk!ckj%vendor/composer/autoload_classmap.php(!c(Ai"vendor/composer/autoload_files.php!ch-'vendor/composer/autoload_namespaces.phpZ!cZᖤ!vendor/composer/autoload_psr4.php_!c_Wz!vendor/composer/autoload_real.php!c#vendor/composer/autoload_static.phpy!cyl A!vendor/composer/ca-bundle/LICENSE!cG _(vendor/composer/ca-bundle/res/cacert.pem e!c eB{֤*vendor/composer/ca-bundle/src/CaBundle.php$!c$n+vendor/composer/class-map-generator/LICENSE!c=4vendor/composer/class-map-generator/src/ClassMap.php!cc[=vendor/composer/class-map-generator/src/ClassMapGenerator.php!c4vendor/composer/class-map-generator/src/FileList.php$!c$hSq:vendor/composer/class-map-generator/src/PhpFileCleaner.php!c5]9vendor/composer/class-map-generator/src/PhpFileParser.php !c zvendor/composer/installed.phpp!cp )vendor/composer/metadata-minifier/LICENSE!cǤ:vendor/composer/metadata-minifier/src/MetadataMinifier.php!c0&vendor/composer/pcre/LICENSE!cǤ+vendor/composer/pcre/src/MatchAllResult.php!c8aǔ6vendor/composer/pcre/src/MatchAllWithOffsetsResult.php&!c& |(vendor/composer/pcre/src/MatchResult.php!c3vendor/composer/pcre/src/MatchWithOffsetsResult.php!c<¤*vendor/composer/pcre/src/PcreException.phpI!cI 7!vendor/composer/pcre/src/Preg.php!c%"vendor/composer/pcre/src/Regex.php !c @g*vendor/composer/pcre/src/ReplaceResult.php !c n0vendor/composer/semver/LICENSE!cSRm)vendor/composer/semver/src/Comparator.php!c^_E/vendor/composer/semver/src/CompilingMatcher.phpc!cc/vendor/composer/semver/src/Constraint/Bound.phpw!cwW4]W4vendor/composer/semver/src/Constraint/Constraint.php!c=vendor/composer/semver/src/Constraint/ConstraintInterface.php!c5y<vendor/composer/semver/src/Constraint/MatchAllConstraint.php!cE=vendor/composer/semver/src/Constraint/MatchNoneConstraint.php!c֯خ9vendor/composer/semver/src/Constraint/MultiConstraint.php!cĤ'vendor/composer/semver/src/Interval.php!c=[i(vendor/composer/semver/src/Intervals.php+!c+@1%vendor/composer/semver/src/Semver.php!c-i,vendor/composer/semver/src/VersionParser.php,!c,Sr%vendor/composer/spdx-licenses/LICENSE!cSRm6vendor/composer/spdx-licenses/res/spdx-exceptions.json !c 4DC4vendor/composer/spdx-licenses/res/spdx-licenses.json<!c<=82vendor/composer/spdx-licenses/src/SpdxLicenses.php{!c{cE&vendor/composer/xdebug-handler/LICENSE+!c+@T0vendor/composer/xdebug-handler/src/PhpConfig.php!cgԏv.vendor/composer/xdebug-handler/src/Process.php!co7-vendor/composer/xdebug-handler/src/Status.php !c xf174vendor/composer/xdebug-handler/src/XdebugHandler.phpr)!cr)8c6(vendor/justinrainbow/json-schema/LICENSE"!c" |Nvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/BaseConstraint.php !c wlTvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/CollectionConstraint.php !c =Jvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php !c PFFSvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/ConstraintInterface.php!c QNvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/EnumConstraint.php\!c\_}Gvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Factory.php !c _Pvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/FormatConstraint.phps!csd ޤPvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/NumberConstraint.php !c e Pvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/ObjectConstraint.php!c%lPvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/SchemaConstraint.php& !c& 78Pvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/StringConstraint.phpz!czf~Xvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/TypeCheck/LooseTypeCheck.phpa!ca qäYvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/TypeCheck/StrictTypeCheck.php!c4~\vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/TypeCheck/TypeCheckInterface.php!c+jNvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/TypeConstraint.php*!c*14NSvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php,"!c,"-Fvendor/justinrainbow/json-schema/src/JsonSchema/Entity/JsonPointer.php!c{NPvendor/justinrainbow/json-schema/src/JsonSchema/Exception/ExceptionInterface.phpI!cI%|Vvendor/justinrainbow/json-schema/src/JsonSchema/Exception/InvalidArgumentException.php!cGTvendor/justinrainbow/json-schema/src/JsonSchema/Exception/InvalidConfigException.phpl!clA!LפTvendor/justinrainbow/json-schema/src/JsonSchema/Exception/InvalidSchemaException.phpl!cl2]vendor/justinrainbow/json-schema/src/JsonSchema/Exception/InvalidSchemaMediaTypeException.phpu!cu=hWvendor/justinrainbow/json-schema/src/JsonSchema/Exception/InvalidSourceUriException.phpw!cwN-[Svendor/justinrainbow/json-schema/src/JsonSchema/Exception/JsonDecodingException.php!c\ Wvendor/justinrainbow/json-schema/src/JsonSchema/Exception/ResourceNotFoundException.phpo!copNvendor/justinrainbow/json-schema/src/JsonSchema/Exception/RuntimeException.php!c%^vendor/justinrainbow/json-schema/src/JsonSchema/Exception/UnresolvableJsonPointerException.php!cu-#1Rvendor/justinrainbow/json-schema/src/JsonSchema/Exception/UriResolverException.phpj!cj>Qvendor/justinrainbow/json-schema/src/JsonSchema/Exception/ValidationException.phpf!cfKvendor/justinrainbow/json-schema/src/JsonSchema/Iterator/ObjectIterator.php!cM;vendor/justinrainbow/json-schema/src/JsonSchema/Rfc3339.php!cf4Avendor/justinrainbow/json-schema/src/JsonSchema/SchemaStorage.php !c &z^Jvendor/justinrainbow/json-schema/src/JsonSchema/SchemaStorageInterface.php!co+}Tvendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/AbstractRetriever.php!c[AGvendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/Curl.php!cǒRvendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/FileGetContents.php !c Rvendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/PredefinedArray.php,!c,15Xvendor/justinrainbow/json-schema/src/JsonSchema/Uri/Retrievers/UriRetrieverInterface.php!cCvendor/justinrainbow/json-schema/src/JsonSchema/Uri/UriResolver.php !c PDvendor/justinrainbow/json-schema/src/JsonSchema/Uri/UriRetriever.php@!c@53Hvendor/justinrainbow/json-schema/src/JsonSchema/UriResolverInterface.php!cJIvendor/justinrainbow/json-schema/src/JsonSchema/UriRetrieverInterface.php!ce=vendor/justinrainbow/json-schema/src/JsonSchema/Validator.phps!cs#vendor/psr/container/LICENSE{!c{e8vendor/psr/container/src/ContainerExceptionInterface.phpN!cNL/vendor/psr/container/src/ContainerInterface.php!c7vendor/psr/container/src/NotFoundExceptionInterface.phpq!cqRvendor/psr/log/LICENSE?!c? )vendor/psr/log/Psr/Log/AbstractLogger.php;!c;>3[3vendor/psr/log/Psr/Log/InvalidArgumentException.php`!c` X1#vendor/psr/log/Psr/Log/LogLevel.php!cj8/vendor/psr/log/Psr/Log/LoggerAwareInterface.php|!c|$+vendor/psr/log/Psr/Log/LoggerAwareTrait.php!cTB*vendor/psr/log/Psr/Log/LoggerInterface.php!cx&vendor/psr/log/Psr/Log/LoggerTrait.phpk!ck}%vendor/psr/log/Psr/Log/NullLogger.php!cX)vendor/psr/log/Psr/Log/Test/DummyTest.phpp!cp3vendor/psr/log/Psr/Log/Test/LoggerInterfaceTest.php !c $/Ҥ*vendor/psr/log/Psr/Log/Test/TestLogger.php<!c<(Ivendor/react/promise/LICENSEi!ci3}8vendor/react/promise/src/CancellablePromiseInterface.php!c'.vendor/react/promise/src/CancellationQueue.php9!c9\o|%vendor/react/promise/src/Deferred.php!cE)6vendor/react/promise/src/Exception/LengthException.php^!c^?q5vendor/react/promise/src/ExtendedPromiseInterface.php!c2U-vendor/react/promise/src/FulfilledPromise.php!c2(vendor/react/promise/src/LazyPromise.php(!c(>!k$vendor/react/promise/src/Promise.php!c nc-vendor/react/promise/src/PromiseInterface.php!cA.vendor/react/promise/src/PromisorInterface.phpa!caə,vendor/react/promise/src/RejectedPromise.php!cR8vendor/react/promise/src/UnhandledRejectionException.php!csP&vendor/react/promise/src/functions.php9!c9ϟ.vendor/react/promise/src/functions_include.php]!c]Q<vendor/seld/jsonlint/LICENSE$!c$4:~@vendor/seld/jsonlint/src/Seld/JsonLint/DuplicateKeyException.php}!c}ۅD5vendor/seld/jsonlint/src/Seld/JsonLint/JsonParser.php4!c4񀗾0vendor/seld/jsonlint/src/Seld/JsonLint/Lexer.php!cA`;;vendor/seld/jsonlint/src/Seld/JsonLint/ParsingException.php(!c(>v}4vendor/seld/jsonlint/src/Seld/JsonLint/Undefined.php>!c>qvendor/seld/phar-utils/LICENSE$!c$,M%vendor/seld/phar-utils/src/Linter.phpi!ciޤ)vendor/seld/phar-utils/src/Timestamps.php^ !c^ D"vendor/seld/signal-handler/LICENSE$!c$,M0vendor/seld/signal-handler/src/SignalHandler.php!c &vendor/symfony/console/Application.phpp!cpI_d.vendor/symfony/console/Attribute/AsCommand.php!c32vendor/symfony/console/CI/GithubActionReporter.php!c 5 vendor/symfony/console/Color.phph!chGb痤*vendor/symfony/console/Command/Command.phpF'!cF'2vendor/symfony/console/Command/CompleteCommand.php!c 8vendor/symfony/console/Command/DumpCompletionCommand.php!cXw6.vendor/symfony/console/Command/HelpCommand.php !c }.vendor/symfony/console/Command/LazyCommand.php!c~.vendor/symfony/console/Command/ListCommand.php1 !c1 $D|0vendor/symfony/console/Command/LockableTrait.php!c.V=vendor/symfony/console/Command/SignalableCommandInterface.php!cB`?vendor/symfony/console/CommandLoader/CommandLoaderInterface.phpQ!cQH?vendor/symfony/console/CommandLoader/ContainerCommandLoader.phpU!cU_=vendor/symfony/console/CommandLoader/FactoryCommandLoader.php!cd"z5vendor/symfony/console/Completion/CompletionInput.php!c$k;vendor/symfony/console/Completion/CompletionSuggestions.php!cAAvendor/symfony/console/Completion/Output/BashCompletionOutput.phpg!cgWFvendor/symfony/console/Completion/Output/CompletionOutputInterface.phpF!cF2nM0vendor/symfony/console/Completion/Suggestion.php3!c3̆ (vendor/symfony/console/ConsoleEvents.php!cgw]!vendor/symfony/console/Cursor.php !c ƫDvendor/symfony/console/DependencyInjection/AddConsoleCommandPass.phpf!cf# $<vendor/symfony/console/Descriptor/ApplicationDescription.php !c =ĥ0vendor/symfony/console/Descriptor/Descriptor.php}!c}3^9vendor/symfony/console/Descriptor/DescriptorInterface.php!c@4vendor/symfony/console/Descriptor/JsonDescriptor.php!c1%菤8vendor/symfony/console/Descriptor/MarkdownDescriptor.phpQ!cQtv4vendor/symfony/console/Descriptor/TextDescriptor.php"!c"ȸˤ3vendor/symfony/console/Descriptor/XmlDescriptor.php!c4vendor/symfony/console/Event/ConsoleCommandEvent.php!c O~2vendor/symfony/console/Event/ConsoleErrorEvent.php!c-vendor/symfony/console/Event/ConsoleEvent.php!co٤3vendor/symfony/console/Event/ConsoleSignalEvent.phpG!cG 6vendor/symfony/console/Event/ConsoleTerminateEvent.php~!c~hr֤6vendor/symfony/console/EventListener/ErrorListener.php&!c&X뮤=vendor/symfony/console/Exception/CommandNotFoundException.php!cw7vendor/symfony/console/Exception/ExceptionInterface.phpy!cy9[&=vendor/symfony/console/Exception/InvalidArgumentException.php!c̽Z;vendor/symfony/console/Exception/InvalidOptionException.php!cH3vendor/symfony/console/Exception/LogicException.php!cO\e:vendor/symfony/console/Exception/MissingInputException.php!cS ?vendor/symfony/console/Exception/NamespaceNotFoundException.php!cn5vendor/symfony/console/Exception/RuntimeException.php!c,68vendor/symfony/console/Formatter/NullOutputFormatter.php!c!& u=vendor/symfony/console/Formatter/NullOutputFormatterStyle.php !c %h4vendor/symfony/console/Formatter/OutputFormatter.php!cB=vendor/symfony/console/Formatter/OutputFormatterInterface.php!cY ߤ9vendor/symfony/console/Formatter/OutputFormatterStyle.phpH!cH2bBvendor/symfony/console/Formatter/OutputFormatterStyleInterface.php!cZä>vendor/symfony/console/Formatter/OutputFormatterStyleStack.php!cFxFvendor/symfony/console/Formatter/WrappableOutputFormatterInterface.php!cZ6vendor/symfony/console/Helper/DebugFormatterHelper.php!c62vendor/symfony/console/Helper/DescriptorHelper.php!cḡ(vendor/symfony/console/Helper/Dumper.php!cܽ1vendor/symfony/console/Helper/FormatterHelper.phpi!ciw(vendor/symfony/console/Helper/Helper.php9 !c9 ;S1vendor/symfony/console/Helper/HelperInterface.php!c+vendor/symfony/console/Helper/HelperSet.phpG!cGː2vendor/symfony/console/Helper/InputAwareHelper.phpc!cc/vendor/symfony/console/Helper/ProcessHelper.phpW !cW e -vendor/symfony/console/Helper/ProgressBar.phpg/!cg/w13vendor/symfony/console/Helper/ProgressIndicator.php!c=֤0vendor/symfony/console/Helper/QuestionHelper.php-!c-n7vendor/symfony/console/Helper/SymfonyQuestionHelper.php !c d'vendor/symfony/console/Helper/Table.php7J!c7JLˤ+vendor/symfony/console/Helper/TableCell.phpA!cAӠ0vendor/symfony/console/Helper/TableCellStyle.php!cэ_+vendor/symfony/console/Helper/TableRows.php)!c)W3z0vendor/symfony/console/Helper/TableSeparator.php!c,vendor/symfony/console/Helper/TableStyle.php!c֤*vendor/symfony/console/Input/ArgvInput.php)!c)d+vendor/symfony/console/Input/ArrayInput.php !c $&vendor/symfony/console/Input/Input.php !c 1.vendor/symfony/console/Input/InputArgument.php!c& 4vendor/symfony/console/Input/InputAwareInterface.php!cO0vendor/symfony/console/Input/InputDefinition.php#!c# !3/vendor/symfony/console/Input/InputInterface.php!cG,vendor/symfony/console/Input/InputOption.php !c B@9vendor/symfony/console/Input/StreamableInputInterface.php!cB,vendor/symfony/console/Input/StringInput.php?!c?ˤvendor/symfony/console/LICENSE+!c+̤/vendor/symfony/console/Logger/ConsoleLogger.php !c Q0vendor/symfony/console/Output/BufferedOutput.phpl!cly:/vendor/symfony/console/Output/ConsoleOutput.php !c ¤8vendor/symfony/console/Output/ConsoleOutputInterface.php!c6vendor/symfony/console/Output/ConsoleSectionOutput.php !c O0ɤ,vendor/symfony/console/Output/NullOutput.phpB!cB%#RƤ(vendor/symfony/console/Output/Output.php !c C,1vendor/symfony/console/Output/OutputInterface.php!c.vendor/symfony/console/Output/StreamOutput.php!c1X5vendor/symfony/console/Output/TrimmedBufferOutput.php!cű 2vendor/symfony/console/Question/ChoiceQuestion.php !c c䦤8vendor/symfony/console/Question/ConfirmationQuestion.php!cyń,vendor/symfony/console/Question/Question.php !c T4vendor/symfony/console/Resources/bin/hiddeninput.exe$!c$v0vendor/symfony/console/Resources/completion.bashX !cX bm 8vendor/symfony/console/SignalRegistry/SignalRegistry.php!!c!~.:3vendor/symfony/console/SingleCommandApplication.php5!c5⢤,vendor/symfony/console/Style/OutputStyle.phpt!ctO;Ƥ/vendor/symfony/console/Style/StyleInterface.php !c ݡ-vendor/symfony/console/Style/SymfonyStyle.php-'!c-'#vendor/symfony/console/Terminal.php !c g}ؤ3vendor/symfony/console/Tester/ApplicationTester.php!cI9vendor/symfony/console/Tester/CommandCompletionTester.php!cʛ;/vendor/symfony/console/Tester/CommandTester.php!cD@vendor/symfony/console/Tester/Constraint/CommandIsSuccessful.php!cƃ-vendor/symfony/console/Tester/TesterTrait.php !c 2'L,vendor/symfony/deprecation-contracts/LICENSE+!c+v1vendor/symfony/deprecation-contracts/function.php=!c= :vendor/symfony/filesystem/Exception/ExceptionInterface.php|!c|D=vendor/symfony/filesystem/Exception/FileNotFoundException.php!c&3vendor/symfony/filesystem/Exception/IOException.php!c\Τ<vendor/symfony/filesystem/Exception/IOExceptionInterface.php!cjwM@vendor/symfony/filesystem/Exception/InvalidArgumentException.php!c!Ǥ8vendor/symfony/filesystem/Exception/RuntimeException.php!cUUH(vendor/symfony/filesystem/Filesystem.php@!c@)6b!vendor/symfony/filesystem/LICENSE+!c+̤"vendor/symfony/filesystem/Path.php='!c='4L/vendor/symfony/finder/Comparator/Comparator.php3!c3h3vendor/symfony/finder/Comparator/DateComparator.php!c%e5vendor/symfony/finder/Comparator/NumberComparator.phpz!cz+h9vendor/symfony/finder/Exception/AccessDeniedException.php!cs>vendor/symfony/finder/Exception/DirectoryNotFoundException.php!ca) vendor/symfony/finder/Finder.php'!c'?#vendor/symfony/finder/Gitignore.php!cs 4vendor/symfony/finder/Glob.php!cDb7vendor/symfony/finder/Iterator/CustomFilterIterator.phpc!cc7:vendor/symfony/finder/Iterator/DateRangeFilterIterator.php!cPs;vendor/symfony/finder/Iterator/DepthRangeFilterIterator.php!cAvendor/symfony/finder/Iterator/ExcludeDirectoryFilterIterator.php/!c/n<9vendor/symfony/finder/Iterator/FileTypeFilterIterator.php!c^"<vendor/symfony/finder/Iterator/FilecontentFilterIterator.phpW!cW٤9vendor/symfony/finder/Iterator/FilenameFilterIterator.php!c=/vendor/symfony/finder/Iterator/LazyIterator.phpQ!cQn]=vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php!c5vendor/symfony/finder/Iterator/PathFilterIterator.php!cg"_=vendor/symfony/finder/Iterator/RecursiveDirectoryIterator.php !c v2:vendor/symfony/finder/Iterator/SizeRangeFilterIterator.php|!c|3ٟ3vendor/symfony/finder/Iterator/SortableIterator.php( !c( ;vendor/symfony/finder/Iterator/VcsIgnoredFilterIterator.phpw !cw ŵ3vendor/symfony/finder/LICENSE+!c+̤%vendor/symfony/finder/SplFileInfo.php!cy'vendor/symfony/polyfill-ctype/Ctype.php !c ~d%vendor/symfony/polyfill-ctype/LICENSE+!c+E:+vendor/symfony/polyfill-ctype/bootstrap.php2!c2a8-vendor/symfony/polyfill-ctype/bootstrap80.phph!chKy2vendor/symfony/polyfill-intl-grapheme/Grapheme.php!c̻9-vendor/symfony/polyfill-intl-grapheme/LICENSE+!c+{3vendor/symfony/polyfill-intl-grapheme/bootstrap.php!c9|5vendor/symfony/polyfill-intl-grapheme/bootstrap80.phpY !cY !/vendor/symfony/polyfill-intl-normalizer/LICENSE+!c+{6vendor/symfony/polyfill-intl-normalizer/Normalizer.php!cb"qFvendor/symfony/polyfill-intl-normalizer/Resources/stubs/Normalizer.php.!c.Qs$Rvendor/symfony/polyfill-intl-normalizer/Resources/unidata/canonicalComposition.php=!c=*o?Tvendor/symfony/polyfill-intl-normalizer/Resources/unidata/canonicalDecomposition.phpa!caR}Lvendor/symfony/polyfill-intl-normalizer/Resources/unidata/combiningClass.phpt.!ct. qܤXvendor/symfony/polyfill-intl-normalizer/Resources/unidata/compatibilityDecomposition.php!coe)5vendor/symfony/polyfill-intl-normalizer/bootstrap.php!cPD7vendor/symfony/polyfill-intl-normalizer/bootstrap80.php!c=r(vendor/symfony/polyfill-mbstring/LICENSE+!c+{-vendor/symfony/polyfill-mbstring/Mbstring.phptJ!ctJڟV@vendor/symfony/polyfill-mbstring/Resources/unidata/lowerCase.phpT!cT+Fvendor/symfony/polyfill-mbstring/Resources/unidata/titleCaseRegexp.php!cy_@vendor/symfony/polyfill-mbstring/Resources/unidata/upperCase.php8[!c8[+R*.vendor/symfony/polyfill-mbstring/bootstrap.php!cNZ^0vendor/symfony/polyfill-mbstring/bootstrap80.php !c D פ%vendor/symfony/polyfill-php73/LICENSE+!c+E:'vendor/symfony/polyfill-php73/Php73.phpn!cnCsl?vendor/symfony/polyfill-php73/Resources/stubs/JsonException.php[!c[Mܤ+vendor/symfony/polyfill-php73/bootstrap.php!cAY8Ƥ%vendor/symfony/polyfill-php80/LICENSE&!c&z)'vendor/symfony/polyfill-php80/Php80.php !c [k *vendor/symfony/polyfill-php80/PhpToken.php!cMI;vendor/symfony/polyfill-php80/Resources/stubs/Attribute.php!cФ:vendor/symfony/polyfill-php80/Resources/stubs/PhpToken.php!c}GG<vendor/symfony/polyfill-php80/Resources/stubs/Stringable.phpb!cbћ<Evendor/symfony/polyfill-php80/Resources/stubs/UnhandledMatchError.phpT!cTO<vendor/symfony/polyfill-php80/Resources/stubs/ValueError.phpK!cK+vendor/symfony/polyfill-php80/bootstrap.php!c7vendor/symfony/process/Exception/ExceptionInterface.phpy!cyqVXJ=vendor/symfony/process/Exception/InvalidArgumentException.php!c+_3vendor/symfony/process/Exception/LogicException.php!c ;vendor/symfony/process/Exception/ProcessFailedException.phpx!cxzy=vendor/symfony/process/Exception/ProcessSignaledException.php!cYש=vendor/symfony/process/Exception/ProcessTimedOutException.php1!c1'Z5vendor/symfony/process/Exception/RuntimeException.php!c:+vendor/symfony/process/ExecutableFinder.php!c=&vendor/symfony/process/InputStream.php/!c/evendor/symfony/process/LICENSE+!c+̤.vendor/symfony/process/PhpExecutableFinder.php!c9e%vendor/symfony/process/PhpProcess.php!cs.vendor/symfony/process/Pipes/AbstractPipes.php !c kSQ/vendor/symfony/process/Pipes/PipesInterface.php!cfQ *vendor/symfony/process/Pipes/UnixPipes.php!cVv-vendor/symfony/process/Pipes/WindowsPipes.php, !c, xQjä"vendor/symfony/process/Process.phpe!ce )'vendor/symfony/process/ProcessUtils.php!cX7vendor/symfony/service-contracts/Attribute/Required.php!cxj네@vendor/symfony/service-contracts/Attribute/SubscribedService.php !c 1IT(vendor/symfony/service-contracts/LICENSE+!c+JE3vendor/symfony/service-contracts/ResetInterface.phpy!cyj8vendor/symfony/service-contracts/ServiceLocatorTrait.php !c "5=vendor/symfony/service-contracts/ServiceProviderInterface.php!cRk?vendor/symfony/service-contracts/ServiceSubscriberInterface.php!cd ;vendor/symfony/service-contracts/ServiceSubscriberTrait.phpS !cS 4<vendor/symfony/service-contracts/Test/ServiceLocatorTest.php!cW7|G(vendor/symfony/string/AbstractString.php3!c3֤/vendor/symfony/string/AbstractUnicodeString.phpR!cREOuA$vendor/symfony/string/ByteString.php+!c+KĤ)vendor/symfony/string/CodePointString.php!c;3ˤ6vendor/symfony/string/Exception/ExceptionInterface.phps!csqHV<vendor/symfony/string/Exception/InvalidArgumentException.php!cA4vendor/symfony/string/Exception/RuntimeException.php!cug4vendor/symfony/string/Inflector/EnglishInflector.php!c3d3vendor/symfony/string/Inflector/FrenchInflector.php !c H6vendor/symfony/string/Inflector/InflectorInterface.php!chDvendor/symfony/string/LICENSE+!c+$vendor/symfony/string/LazyString.php !c (L<vendor/symfony/string/Resources/data/wcswidth_table_wide.php^!c^%o)<vendor/symfony/string/Resources/data/wcswidth_table_zero.php?!c?&^}-vendor/symfony/string/Resources/functions.php!ccڪ".vendor/symfony/string/Slugger/AsciiSlugger.php!c32vendor/symfony/string/Slugger/SluggerInterface.php!cQ'vendor/symfony/string/UnicodeString.php%!c% פ bin/composerd !cd q٤LICENSE.!c. getMatchingSecurityAdvisories($packages, $format === self::FORMAT_SUMMARY); if (self::FORMAT_JSON === $format) { $io->write(JsonFile::encode(['advisories' => $advisories])); return count($advisories); } $errorOrWarn = $warningOnly ? 'warning' : 'error'; if (count($advisories) > 0) { [$affectedPackages, $totalAdvisories] = $this->countAdvisories($advisories); $plurality = $totalAdvisories === 1 ? 'y' : 'ies'; $pkgPlurality = $affectedPackages === 1 ? '' : 's'; $punctuation = $format === 'summary' ? '.' : ':'; $io->writeError("<$errorOrWarn>Found $totalAdvisories security vulnerability advisor{$plurality} affecting $affectedPackages package{$pkgPlurality}{$punctuation}"); $this->outputAdvisories($io, $advisories, $format); return $affectedPackages; } $io->writeError('No security vulnerability advisories found'); return 0; } private function countAdvisories(array $advisories): array { $count = 0; foreach ($advisories as $packageAdvisories) { $count += count($packageAdvisories); } return [count($advisories), $count]; } private function outputAdvisories(IOInterface $io, array $advisories, string $format): void { switch ($format) { case self::FORMAT_TABLE: if (!($io instanceof ConsoleIO)) { throw new InvalidArgumentException('Cannot use table format with ' . get_class($io)); } $this->outputAvisoriesTable($io, $advisories); return; case self::FORMAT_PLAIN: $this->outputAdvisoriesPlain($io, $advisories); return; case self::FORMAT_SUMMARY: $io->writeError('Run composer audit for a full list of advisories.'); return; default: throw new InvalidArgumentException('Invalid format "'.$format.'".'); } } private function outputAvisoriesTable(ConsoleIO $io, array $advisories): void { foreach ($advisories as $packageAdvisories) { foreach ($packageAdvisories as $advisory) { $io->getTable() ->setHorizontal() ->setHeaders([ 'Package', 'CVE', 'Title', 'URL', 'Affected versions', 'Reported at', ]) ->addRow([ $advisory->packageName, $this->getCVE($advisory), $advisory->title, $this->getURL($advisory), $advisory->affectedVersions->getPrettyString(), $advisory->reportedAt->format(DATE_ATOM), ]) ->setColumnWidth(1, 80) ->setColumnMaxWidth(1, 80) ->render(); } } } private function outputAdvisoriesPlain(IOInterface $io, array $advisories): void { $error = []; $firstAdvisory = true; foreach ($advisories as $packageAdvisories) { foreach ($packageAdvisories as $advisory) { if (!$firstAdvisory) { $error[] = '--------'; } $error[] = "Package: ".$advisory->packageName; $error[] = "CVE: ".$this->getCVE($advisory); $error[] = "Title: ".OutputFormatter::escape($advisory->title); $error[] = "URL: ".$this->getURL($advisory); $error[] = "Affected versions: ".OutputFormatter::escape($advisory->affectedVersions->getPrettyString()); $error[] = "Reported at: ".$advisory->reportedAt->format(DATE_ATOM); $firstAdvisory = false; } } $io->writeError($error); } private function getCVE(SecurityAdvisory $advisory): string { if ($advisory->cve === null) { return 'NO CVE'; } return ''.$advisory->cve.''; } private function getURL(SecurityAdvisory $advisory): string { if ($advisory->link === null) { return ''; } return 'link).'>'.OutputFormatter::escape($advisory->link).''; } } parseConstraints($data['affectedVersions']); if (isset($data['title'], $data['sources'], $data['reportedAt'])) { return new SecurityAdvisory($packageName, $data['advisoryId'], $constraint, $data['title'], $data['sources'], new \DateTimeImmutable($data['reportedAt'], new \DateTimeZone('UTC')), $data['cve'] ?? null, $data['link'] ?? null); } return new self($packageName, $data['advisoryId'], $constraint); } public function __construct(string $packageName, string $advisoryId, ConstraintInterface $affectedVersions) { $this->advisoryId = $advisoryId; $this->packageName = $packageName; $this->affectedVersions = $affectedVersions; } } title = $title; $this->sources = $sources; $this->reportedAt = $reportedAt; $this->cve = $cve; $this->link = $link; } } eventDispatcher = $eventDispatcher; $this->io = $io ?? new NullIO(); $this->platformRequirementFilter = PlatformRequirementFilterFactory::ignoreNothing(); } public function setDevMode(bool $devMode = true) { $this->devMode = $devMode; } public function setClassMapAuthoritative(bool $classMapAuthoritative) { $this->classMapAuthoritative = $classMapAuthoritative; } public function setApcu(bool $apcu, ?string $apcuPrefix = null) { $this->apcu = $apcu; $this->apcuPrefix = $apcuPrefix !== null ? $apcuPrefix : $apcuPrefix; } public function setRunScripts(bool $runScripts = true) { $this->runScripts = $runScripts; } public function setIgnorePlatformRequirements($ignorePlatformReqs) { trigger_error('AutoloadGenerator::setIgnorePlatformRequirements is deprecated since Composer 2.2, use setPlatformRequirementFilter instead.', E_USER_DEPRECATED); $this->setPlatformRequirementFilter(PlatformRequirementFilterFactory::fromBoolOrList($ignorePlatformReqs)); } public function setPlatformRequirementFilter(PlatformRequirementFilterInterface $platformRequirementFilter) { $this->platformRequirementFilter = $platformRequirementFilter; } public function dump(Config $config, InstalledRepositoryInterface $localRepo, RootPackageInterface $rootPackage, InstallationManager $installationManager, string $targetDir, bool $scanPsrPackages = false, ?string $suffix = null) { if ($this->classMapAuthoritative) { $scanPsrPackages = true; } if (null === $this->devMode) { $this->devMode = false; $installedJson = new JsonFile($config->get('vendor-dir').'/composer/installed.json'); if ($installedJson->exists()) { $installedJson = $installedJson->read(); if (isset($installedJson['dev'])) { $this->devMode = $installedJson['dev']; } } } if ($this->runScripts) { if (!isset($_SERVER['COMPOSER_DEV_MODE'])) { Platform::putEnv('COMPOSER_DEV_MODE', $this->devMode ? '1' : '0'); } $this->eventDispatcher->dispatchScript(ScriptEvents::PRE_AUTOLOAD_DUMP, $this->devMode, [], [ 'optimize' => $scanPsrPackages, ]); } $classMapGenerator = new ClassMapGenerator(['php', 'inc', 'hh']); $classMapGenerator->avoidDuplicateScans(); $filesystem = new Filesystem(); $filesystem->ensureDirectoryExists($config->get('vendor-dir')); $basePath = $filesystem->normalizePath(realpath(realpath(Platform::getCwd()))); $vendorPath = $filesystem->normalizePath(realpath(realpath($config->get('vendor-dir')))); $useGlobalIncludePath = $config->get('use-include-path'); $prependAutoloader = $config->get('prepend-autoloader') === false ? 'false' : 'true'; $targetDir = $vendorPath.'/'.$targetDir; $filesystem->ensureDirectoryExists($targetDir); $vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true); $vendorPathToTargetDirCode = $filesystem->findShortestPathCode($vendorPath, realpath($targetDir), true); $appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, $basePath, true); $appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode); $namespacesFile = <<getDevPackageNames(); $packageMap = $this->buildPackageMap($installationManager, $rootPackage, $localRepo->getCanonicalPackages()); if ($this->devMode) { $filteredDevPackages = false; } else { $filteredDevPackages = $devPackageNames ?: true; } $autoloads = $this->parseAutoloads($packageMap, $rootPackage, $filteredDevPackages); foreach ($autoloads['psr-0'] as $namespace => $paths) { $exportedPaths = []; foreach ($paths as $path) { $exportedPaths[] = $this->getPathCode($filesystem, $basePath, $vendorPath, $path); } $exportedPrefix = var_export($namespace, true); $namespacesFile .= " $exportedPrefix => "; $namespacesFile .= "array(".implode(', ', $exportedPaths)."),\n"; } $namespacesFile .= ");\n"; foreach ($autoloads['psr-4'] as $namespace => $paths) { $exportedPaths = []; foreach ($paths as $path) { $exportedPaths[] = $this->getPathCode($filesystem, $basePath, $vendorPath, $path); } $exportedPrefix = var_export($namespace, true); $psr4File .= " $exportedPrefix => "; $psr4File .= "array(".implode(', ', $exportedPaths)."),\n"; } $psr4File .= ");\n"; $targetDirLoader = null; $mainAutoload = $rootPackage->getAutoload(); if ($rootPackage->getTargetDir() && !empty($mainAutoload['psr-0'])) { $levels = substr_count($filesystem->normalizePath($rootPackage->getTargetDir()), '/') + 1; $prefixes = implode(', ', array_map(static function ($prefix): string { return var_export($prefix, true); }, array_keys($mainAutoload['psr-0']))); $baseDirFromTargetDirCode = $filesystem->findShortestPathCode($targetDir, $basePath, true); $targetDirLoader = <<scanPaths($dir, $this->buildExclusionRegex($dir, $excluded)); } if ($scanPsrPackages) { $namespacesToScan = []; foreach (['psr-4', 'psr-0'] as $psrType) { foreach ($autoloads[$psrType] as $namespace => $paths) { $namespacesToScan[$namespace][] = ['paths' => $paths, 'type' => $psrType]; } } krsort($namespacesToScan); foreach ($namespacesToScan as $namespace => $groups) { foreach ($groups as $group) { foreach ($group['paths'] as $dir) { $dir = $filesystem->normalizePath($filesystem->isAbsolutePath($dir) ? $dir : $basePath.'/'.$dir); if (!is_dir($dir)) { continue; } $classMapGenerator->scanPaths($dir, $this->buildExclusionRegex($dir, $excluded), $group['type'], $namespace); } } } } $classMap = $classMapGenerator->getClassMap(); foreach ($classMap->getAmbiguousClasses() as $className => $ambiguousPaths) { if (count($ambiguousPaths) > 1) { $this->io->writeError( 'Warning: Ambiguous class resolution, "'.$className.'"'. ' was found '. (count($ambiguousPaths) + 1) .'x: in "'.$classMap->getClassPath($className).'" and "'. implode('", "', $ambiguousPaths) .'", the first will be used.' ); } else { $this->io->writeError( 'Warning: Ambiguous class resolution, "'.$className.'"'. ' was found in both "'.$classMap->getClassPath($className).'" and "'. implode('", "', $ambiguousPaths) .'", the first will be used.' ); } } foreach ($classMap->getPsrViolations() as $msg) { $this->io->writeError("$msg"); } $classMap->addClass('Composer\InstalledVersions', $vendorPath . '/composer/InstalledVersions.php'); $classMap->sort(); $classmapFile = <<getMap() as $className => $path) { $pathCode = $this->getPathCode($filesystem, $basePath, $vendorPath, $path).",\n"; $classmapFile .= ' '.var_export($className, true).' => '.$pathCode; } $classmapFile .= ");\n"; if ('' === $suffix) { $suffix = null; } if (null === $suffix) { $suffix = $config->get('autoloader-suffix'); if (null === $suffix && Filesystem::isReadable($vendorPath.'/autoload.php')) { $content = file_get_contents($vendorPath.'/autoload.php'); if (Preg::isMatch('{ComposerAutoloaderInit([^:\s]+)::}', $content, $match)) { $suffix = $match[1]; } } if (null === $suffix) { $suffix = md5(uniqid('', true)); } } $filesystem->filePutContentsIfModified($targetDir.'/autoload_namespaces.php', $namespacesFile); $filesystem->filePutContentsIfModified($targetDir.'/autoload_psr4.php', $psr4File); $filesystem->filePutContentsIfModified($targetDir.'/autoload_classmap.php', $classmapFile); $includePathFilePath = $targetDir.'/include_paths.php'; if ($includePathFileContents = $this->getIncludePathsFile($packageMap, $filesystem, $basePath, $vendorPath, $vendorPathCode, $appBaseDirCode)) { $filesystem->filePutContentsIfModified($includePathFilePath, $includePathFileContents); } elseif (file_exists($includePathFilePath)) { unlink($includePathFilePath); } $includeFilesFilePath = $targetDir.'/autoload_files.php'; if ($includeFilesFileContents = $this->getIncludeFilesFile($autoloads['files'], $filesystem, $basePath, $vendorPath, $vendorPathCode, $appBaseDirCode)) { $filesystem->filePutContentsIfModified($includeFilesFilePath, $includeFilesFileContents); } elseif (file_exists($includeFilesFilePath)) { unlink($includeFilesFilePath); } $filesystem->filePutContentsIfModified($targetDir.'/autoload_static.php', $this->getStaticFile($suffix, $targetDir, $vendorPath, $basePath)); $checkPlatform = $config->get('platform-check') !== false && !($this->platformRequirementFilter instanceof IgnoreAllPlatformRequirementFilter); $platformCheckContent = null; if ($checkPlatform) { $platformCheckContent = $this->getPlatformCheck($packageMap, $config->get('platform-check'), $devPackageNames); if (null === $platformCheckContent) { $checkPlatform = false; } } if ($checkPlatform) { $filesystem->filePutContentsIfModified($targetDir.'/platform_check.php', $platformCheckContent); } elseif (file_exists($targetDir.'/platform_check.php')) { unlink($targetDir.'/platform_check.php'); } $filesystem->filePutContentsIfModified($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix)); $filesystem->filePutContentsIfModified($targetDir.'/autoload_real.php', $this->getAutoloadRealFile(true, (bool) $includePathFileContents, $targetDirLoader, (bool) $includeFilesFileContents, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath, $prependAutoloader, $checkPlatform)); $filesystem->safeCopy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php'); $filesystem->safeCopy(__DIR__.'/../../../LICENSE', $targetDir.'/LICENSE'); if ($this->runScripts) { $this->eventDispatcher->dispatchScript(ScriptEvents::POST_AUTOLOAD_DUMP, $this->devMode, [], [ 'optimize' => $scanPsrPackages, ]); } return $classMap; } private function buildExclusionRegex(string $dir, ?array $excluded): ?string { if (null === $excluded) { return null; } if (file_exists($dir)) { $dirMatch = preg_quote(strtr(realpath($dir), '\\', '/')); foreach ($excluded as $index => $pattern) { $pattern = Preg::replace('{^(([^.+*?\[^\]$(){}=!<>|:\\\\#-]+|\\\\[.+*?\[^\]$(){}=!<>|:#-])*).*}', '$1', $pattern); if (0 !== strpos($pattern, $dirMatch) && 0 !== strpos($dirMatch, $pattern)) { unset($excluded[$index]); } } } return \count($excluded) > 0 ? '{(' . implode('|', $excluded) . ')}' : null; } public function buildPackageMap(InstallationManager $installationManager, PackageInterface $rootPackage, array $packages) { $packageMap = [[$rootPackage, '']]; foreach ($packages as $package) { if ($package instanceof AliasPackage) { continue; } $this->validatePackage($package); $packageMap[] = [ $package, $installationManager->getInstallPath($package), ]; } return $packageMap; } protected function validatePackage(PackageInterface $package) { $autoload = $package->getAutoload(); if (!empty($autoload['psr-4']) && null !== $package->getTargetDir()) { $name = $package->getName(); $package->getTargetDir(); throw new \InvalidArgumentException("PSR-4 autoloading is incompatible with the target-dir property, remove the target-dir in package '$name'."); } if (!empty($autoload['psr-4'])) { foreach ($autoload['psr-4'] as $namespace => $dirs) { if ($namespace !== '' && '\\' !== substr($namespace, -1)) { throw new \InvalidArgumentException("psr-4 namespaces must end with a namespace separator, '$namespace' does not, use '$namespace\\'."); } } } } public function parseAutoloads(array $packageMap, PackageInterface $rootPackage, $filteredDevPackages = false) { $rootPackageMap = array_shift($packageMap); if (is_array($filteredDevPackages)) { $packageMap = array_filter($packageMap, static function ($item) use ($filteredDevPackages): bool { return !in_array($item[0]->getName(), $filteredDevPackages, true); }); } elseif ($filteredDevPackages) { $packageMap = $this->filterPackageMap($packageMap, $rootPackage); } $sortedPackageMap = $this->sortPackageMap($packageMap); $sortedPackageMap[] = $rootPackageMap; array_unshift($packageMap, $rootPackageMap); $psr0 = $this->parseAutoloadsType($packageMap, 'psr-0', $rootPackage); $psr4 = $this->parseAutoloadsType($packageMap, 'psr-4', $rootPackage); $classmap = $this->parseAutoloadsType(array_reverse($sortedPackageMap), 'classmap', $rootPackage); $files = $this->parseAutoloadsType($sortedPackageMap, 'files', $rootPackage); $exclude = $this->parseAutoloadsType($sortedPackageMap, 'exclude-from-classmap', $rootPackage); krsort($psr0); krsort($psr4); return [ 'psr-0' => $psr0, 'psr-4' => $psr4, 'classmap' => $classmap, 'files' => $files, 'exclude-from-classmap' => $exclude, ]; } public function createLoader(array $autoloads, ?string $vendorDir = null) { $loader = new ClassLoader($vendorDir); if (isset($autoloads['psr-0'])) { foreach ($autoloads['psr-0'] as $namespace => $path) { $loader->add($namespace, $path); } } if (isset($autoloads['psr-4'])) { foreach ($autoloads['psr-4'] as $namespace => $path) { $loader->addPsr4($namespace, $path); } } if (isset($autoloads['classmap'])) { $excluded = null; if (!empty($autoloads['exclude-from-classmap'])) { $excluded = $autoloads['exclude-from-classmap']; } $classMapGenerator = new ClassMapGenerator(['php', 'inc', 'hh']); $classMapGenerator->avoidDuplicateScans(); foreach ($autoloads['classmap'] as $dir) { try { $classMapGenerator->scanPaths($dir, $this->buildExclusionRegex($dir, $excluded)); } catch (\RuntimeException $e) { $this->io->writeError(''.$e->getMessage().''); } } $loader->addClassMap($classMapGenerator->getClassMap()->getMap()); } return $loader; } protected function getIncludePathsFile(array $packageMap, Filesystem $filesystem, string $basePath, string $vendorPath, string $vendorPathCode, string $appBaseDirCode) { $includePaths = []; foreach ($packageMap as $item) { [$package, $installPath] = $item; if (null !== $package->getTargetDir() && strlen($package->getTargetDir()) > 0) { $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir())); } foreach ($package->getIncludePaths() as $includePath) { $includePath = trim($includePath, '/'); $includePaths[] = empty($installPath) ? $includePath : $installPath.'/'.$includePath; } } if (!$includePaths) { return null; } $includePathsCode = ''; foreach ($includePaths as $path) { $includePathsCode .= " " . $this->getPathCode($filesystem, $basePath, $vendorPath, $path) . ",\n"; } return << $functionFile) { $filesCode .= ' ' . var_export($fileIdentifier, true) . ' => ' . $this->getPathCode($filesystem, $basePath, $vendorPath, $functionFile) . ",\n"; } if (!$filesCode) { return null; } return <<isAbsolutePath($path)) { $path = $basePath . '/' . $path; } $path = $filesystem->normalizePath($path); $baseDir = ''; if (strpos($path.'/', $vendorPath.'/') === 0) { $path = (string) substr($path, strlen($vendorPath)); $baseDir = '$vendorDir . '; } else { $path = $filesystem->normalizePath($filesystem->findShortestPath($basePath, $path, true)); if (!$filesystem->isAbsolutePath($path)) { $baseDir = '$baseDir . '; $path = '/' . $path; } } if (strpos($path, '.phar') !== false) { $baseDir = "'phar://' . " . $baseDir; } return $baseDir . var_export($path, true); } protected function getPlatformCheck(array $packageMap, $checkPlatform, array $devPackageNames) { $lowestPhpVersion = Bound::zero(); $requiredExtensions = []; $extensionProviders = []; foreach ($packageMap as $item) { $package = $item[0]; foreach (array_merge($package->getReplaces(), $package->getProvides()) as $link) { if (Preg::isMatch('{^ext-(.+)$}iD', $link->getTarget(), $match)) { $extensionProviders[$match[1]][] = $link->getConstraint(); } } } foreach ($packageMap as $item) { $package = $item[0]; if (in_array($package->getName(), $devPackageNames, true)) { continue; } foreach ($package->getRequires() as $link) { if ($this->platformRequirementFilter->isIgnored($link->getTarget())) { continue; } if ('php' === $link->getTarget()) { $constraint = $link->getConstraint(); if ($constraint->getLowerBound()->compareTo($lowestPhpVersion, '>')) { $lowestPhpVersion = $constraint->getLowerBound(); } } if ($checkPlatform === true && Preg::isMatch('{^ext-(.+)$}iD', $link->getTarget(), $match)) { if (isset($extensionProviders[$match[1]])) { foreach ($extensionProviders[$match[1]] as $provided) { if ($provided->matches($link->getConstraint())) { continue 2; } } } if ($match[1] === 'zend-opcache') { $match[1] = 'zend opcache'; } $extension = var_export($match[1], true); if ($match[1] === 'pcntl' || $match[1] === 'readline') { $requiredExtensions[$extension] = "PHP_SAPI !== 'cli' || extension_loaded($extension) || \$missingExtensions[] = $extension;\n"; } else { $requiredExtensions[$extension] = "extension_loaded($extension) || \$missingExtensions[] = $extension;\n"; } } } } ksort($requiredExtensions); $formatToPhpVersionId = static function (Bound $bound): int { if ($bound->isZero()) { return 0; } if ($bound->isPositiveInfinity()) { return 99999; } $version = str_replace('-', '.', $bound->getVersion()); $chunks = array_map('intval', explode('.', $version)); return $chunks[0] * 10000 + $chunks[1] * 100 + $chunks[2]; }; $formatToHumanReadable = static function (Bound $bound) { if ($bound->isZero()) { return 0; } if ($bound->isPositiveInfinity()) { return 99999; } $version = str_replace('-', '.', $bound->getVersion()); $chunks = explode('.', $version); $chunks = array_slice($chunks, 0, 3); return implode('.', $chunks); }; $requiredPhp = ''; $requiredPhpError = ''; if (!$lowestPhpVersion->isZero()) { $operator = $lowestPhpVersion->isInclusive() ? '>=' : '>'; $requiredPhp = 'PHP_VERSION_ID '.$operator.' '.$formatToPhpVersionId($lowestPhpVersion); $requiredPhpError = '"'.$operator.' '.$formatToHumanReadable($lowestPhpVersion).'"'; } if ($requiredPhp) { $requiredPhp = <<classMapAuthoritative) { $file .= <<<'CLASSMAPAUTHORITATIVE' $loader->setClassMapAuthoritative(true); CLASSMAPAUTHORITATIVE; } if ($this->apcu) { $apcuPrefix = var_export(($this->apcuPrefix !== null ? $this->apcuPrefix : substr(base64_encode(md5(uniqid('', true), true)), 0, -3)), true); $file .= <<setApcuPrefix($apcuPrefix); APCU; } if ($useGlobalIncludePath) { $file .= <<<'INCLUDEPATH' $loader->setUseIncludePath(true); INCLUDEPATH; } if ($targetDirLoader) { $file .= <<register($prependAutoloader); REGISTER_LOADER; if ($useIncludeFiles) { $file .= << \$file) { composerRequire$suffix(\$fileIdentifier, \$file); } INCLUDE_FILES; } $file .= << $path) { $loader->set($namespace, $path); } $map = require $targetDir . '/autoload_psr4.php'; foreach ($map as $namespace => $path) { $loader->setPsr4($namespace, $path); } $classMap = require $targetDir . '/autoload_classmap.php'; if ($classMap) { $loader->addClassMap($classMap); } $filesystem = new Filesystem(); $vendorPathCode = ' => ' . $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true, true) . " . '/"; $vendorPharPathCode = ' => \'phar://\' . ' . $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true, true) . " . '/"; $appBaseDirCode = ' => ' . $filesystem->findShortestPathCode(realpath($targetDir), $basePath, true, true) . " . '/"; $appBaseDirPharCode = ' => \'phar://\' . ' . $filesystem->findShortestPathCode(realpath($targetDir), $basePath, true, true) . " . '/"; $absoluteVendorPathCode = ' => ' . substr(var_export(rtrim($vendorDir, '\\/') . '/', true), 0, -1); $absoluteVendorPharPathCode = ' => ' . substr(var_export(rtrim('phar://' . $vendorDir, '\\/') . '/', true), 0, -1); $absoluteAppBaseDirCode = ' => ' . substr(var_export(rtrim($baseDir, '\\/') . '/', true), 0, -1); $absoluteAppBaseDirPharCode = ' => ' . substr(var_export(rtrim('phar://' . $baseDir, '\\/') . '/', true), 0, -1); $initializer = ''; $prefix = "\0Composer\Autoload\ClassLoader\0"; $prefixLen = strlen($prefix); if (file_exists($targetDir . '/autoload_files.php')) { $maps = ['files' => require $targetDir . '/autoload_files.php']; } else { $maps = []; } foreach ((array) $loader as $prop => $value) { if (!is_array($value) || \count($value) === 0 || !str_starts_with($prop, $prefix)) { continue; } $maps[substr($prop, $prefixLen)] = $value; } foreach ($maps as $prop => $value) { $value = strtr( var_export($value, true), [ $absoluteVendorPathCode => $vendorPathCode, $absoluteVendorPharPathCode => $vendorPharPathCode, $absoluteAppBaseDirCode => $appBaseDirCode, $absoluteAppBaseDirPharCode => $appBaseDirPharCode, ] ); $value = ltrim(Preg::replace('/^ */m', ' $0$0', $value)); $file .= sprintf(" public static $%s = %s;\n\n", $prop, $value); if ('files' !== $prop) { $initializer .= " \$loader->$prop = ComposerStaticInit$suffix::\$$prop;\n"; } } return $file . <<getAutoload(); if ($this->devMode && $package === $rootPackage) { $autoload = array_merge_recursive($autoload, $package->getDevAutoload()); } if (!isset($autoload[$type]) || !is_array($autoload[$type])) { continue; } if (null !== $package->getTargetDir() && $package !== $rootPackage) { $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir())); } foreach ($autoload[$type] as $namespace => $paths) { foreach ((array) $paths as $path) { if (($type === 'files' || $type === 'classmap' || $type === 'exclude-from-classmap') && $package->getTargetDir() && !Filesystem::isReadable($installPath.'/'.$path)) { if ($package === $rootPackage) { $targetDir = str_replace('\\', '[\\\\/]', preg_quote(str_replace(['/', '\\'], '', $package->getTargetDir()))); $path = ltrim(Preg::replace('{^'.$targetDir.'}', '', ltrim($path, '\\/')), '\\/'); } else { $path = $package->getTargetDir() . '/' . $path; } } if ($type === 'exclude-from-classmap') { $path = Preg::replace('{/+}', '/', preg_quote(trim(strtr($path, '\\', '/'), '/'))); $path = strtr($path, ['\\*\\*' => '.+?', '\\*' => '[^/]+?']); $updir = null; $path = Preg::replaceCallback( '{^((?:(?:\\\\\\.){1,2}+/)+)}', static function ($matches) use (&$updir): string { if (isset($matches[1])) { $updir = str_replace('\\.', '.', $matches[1]); } return ''; }, $path ); if (empty($installPath)) { $installPath = strtr(Platform::getCwd(), '\\', '/'); } $resolvedPath = realpath($installPath . '/' . $updir); if (false === $resolvedPath) { continue; } $autoloads[] = preg_quote(strtr($resolvedPath, '\\', '/')) . '/' . $path . '($|/)'; continue; } $relativePath = empty($installPath) ? (empty($path) ? '.' : $path) : $installPath.'/'.$path; if ($type === 'files') { $autoloads[$this->getFileIdentifier($package, $path)] = $relativePath; continue; } if ($type === 'classmap') { $autoloads[] = $relativePath; continue; } $autoloads[$namespace][] = $relativePath; } } } return $autoloads; } protected function getFileIdentifier(PackageInterface $package, string $path) { return md5($package->getName() . ':' . $path); } protected function filterPackageMap(array $packageMap, RootPackageInterface $rootPackage) { $packages = []; $include = []; $replacedBy = []; foreach ($packageMap as $item) { $package = $item[0]; $name = $package->getName(); $packages[$name] = $package; foreach ($package->getReplaces() as $replace) { $replacedBy[$replace->getTarget()] = $name; } } $add = static function (PackageInterface $package) use (&$add, $packages, &$include, $replacedBy): void { foreach ($package->getRequires() as $link) { $target = $link->getTarget(); if (isset($replacedBy[$target])) { $target = $replacedBy[$target]; } if (!isset($include[$target])) { $include[$target] = true; if (isset($packages[$target])) { $add($packages[$target]); } } } }; $add($rootPackage); return array_filter( $packageMap, static function ($item) use ($include): bool { $package = $item[0]; foreach ($package->getNames() as $name) { if (isset($include[$name])) { return true; } } return false; } ); } protected function sortPackageMap(array $packageMap) { $packages = []; $paths = []; foreach ($packageMap as $item) { [$package, $path] = $item; $name = $package->getName(); $packages[$name] = $package; $paths[$name] = $path; } $sortedPackages = PackageSorter::sortPackages($packages); $sortedPackageMap = []; foreach ($sortedPackages as $package) { $name = $package->getName(); $sortedPackageMap[] = [$packages[$name], $paths[$name]]; } return $sortedPackageMap; } } function composerRequire(string $fileIdentifier, string $file): void { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; require $file; } } files = $scannedFiles; $generator->avoidDuplicateScans($fileList); $generator->scanPaths($path, $excluded, $autoloadType ?? 'classmap', $namespace); $classMap = $generator->getClassMap(); $scannedFiles = $fileList->files; if ($io !== null) { foreach ($classMap->getPsrViolations() as $msg) { $io->writeError("$msg"); } foreach ($classMap->getAmbiguousClasses() as $class => $paths) { if (count($paths) > 1) { $io->writeError( 'Warning: Ambiguous class resolution, "'.$class.'"'. ' was found '. (count($paths) + 1) .'x: in "'.$classMap->getClassPath($class).'" and "'. implode('", "', $paths) .'", the first will be used.' ); } else { $io->writeError( 'Warning: Ambiguous class resolution, "'.$class.'"'. ' was found in both "'.$classMap->getClassPath($class).'" and "'. implode('", "', $paths) .'", the first will be used.' ); } } } return $classMap->getMap(); } } $type, 'length' => \strlen($type), 'pattern' => '{.\b(?])'.$type.'\s++[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*+}Ais', ]; } self::$restPattern = '{[^?"\'contents = $contents; $this->len = \strlen($this->contents); $this->maxMatches = $maxMatches; } public function clean(): string { $clean = ''; while ($this->index < $this->len) { $this->skipToPhp(); $clean .= 'index < $this->len) { $char = $this->contents[$this->index]; if ($char === '?' && $this->peek('>')) { $clean .= '?>'; $this->index += 2; continue 2; } if ($char === '"') { $this->skipString('"'); $clean .= 'null'; continue; } if ($char === "'") { $this->skipString("'"); $clean .= 'null'; continue; } if ($char === "<" && $this->peek('<') && $this->match('{<<<[ \t]*+([\'"]?)([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*+)\\1(?:\r\n|\n|\r)}A', $match)) { $this->index += \strlen($match[0]); $this->skipHeredoc($match[2]); $clean .= 'null'; continue; } if ($char === '/') { if ($this->peek('/')) { $this->skipToNewline(); continue; } if ($this->peek('*')) { $this->skipComment(); continue; } } if ($this->maxMatches === 1 && isset(self::$typeConfig[$char])) { $type = self::$typeConfig[$char]; if ( \substr($this->contents, $this->index, $type['length']) === $type['name'] && Preg::isMatch($type['pattern'], $this->contents, $match, 0, $this->index - 1) ) { $clean .= $match[0]; return $clean; } } $this->index += 1; if ($this->match(self::$restPattern, $match)) { $clean .= $char . $match[0]; $this->index += \strlen($match[0]); } else { $clean .= $char; } } } return $clean; } private function skipToPhp(): void { while ($this->index < $this->len) { if ($this->contents[$this->index] === '<' && $this->peek('?')) { $this->index += 2; break; } $this->index += 1; } } private function skipString(string $delimiter): void { $this->index += 1; while ($this->index < $this->len) { if ($this->contents[$this->index] === '\\' && ($this->peek('\\') || $this->peek($delimiter))) { $this->index += 2; continue; } if ($this->contents[$this->index] === $delimiter) { $this->index += 1; break; } $this->index += 1; } } private function skipComment(): void { $this->index += 2; while ($this->index < $this->len) { if ($this->contents[$this->index] === '*' && $this->peek('/')) { $this->index += 2; break; } $this->index += 1; } } private function skipToNewline(): void { while ($this->index < $this->len) { if ($this->contents[$this->index] === "\r" || $this->contents[$this->index] === "\n") { return; } $this->index += 1; } } private function skipHeredoc(string $delimiter): void { $firstDelimiterChar = $delimiter[0]; $delimiterLength = \strlen($delimiter); $delimiterPattern = '{'.preg_quote($delimiter).'(?![a-zA-Z0-9_\x80-\xff])}A'; while ($this->index < $this->len) { switch ($this->contents[$this->index]) { case "\t": case " ": $this->index += 1; continue 2; case $firstDelimiterChar: if ( \substr($this->contents, $this->index, $delimiterLength) === $delimiter && $this->match($delimiterPattern) ) { $this->index += $delimiterLength; return; } break; } while ($this->index < $this->len) { $this->skipToNewline(); while ($this->index < $this->len && ($this->contents[$this->index] === "\r" || $this->contents[$this->index] === "\n")) { $this->index += 1; } break; } } } private function peek(string $char): bool { return $this->index + 1 < $this->len && $this->contents[$this->index + 1] === $char; } private function match($regex, ?array &$match = null): bool { return Preg::isMatch($regex, $this->contents, $match, 0, $this->index); } } io = $io; $this->root = rtrim($cacheDir, '/\\') . '/'; $this->allowlist = $allowlist; $this->filesystem = $filesystem ?: new Filesystem(); $this->readOnly = (bool) $readOnly; if (!self::isUsable($cacheDir)) { $this->enabled = false; } } public function setReadOnly(bool $readOnly) { $this->readOnly = (bool) $readOnly; } public function isReadOnly() { return $this->readOnly; } public static function isUsable(string $path) { return !Preg::isMatch('{(^|[\\\\/])(\$null|nul|NUL|/dev/null)([\\\\/]|$)}', $path); } public function isEnabled() { if ($this->enabled === null) { $this->enabled = true; if ( !$this->readOnly && ( (!is_dir($this->root) && !Silencer::call('mkdir', $this->root, 0777, true)) || !is_writable($this->root) ) ) { $this->io->writeError('Cannot create cache directory ' . $this->root . ', or directory is not writable. Proceeding without cache. See also cache-read-only config if your filesystem is read-only.'); $this->enabled = false; } } return $this->enabled; } public function getRoot() { return $this->root; } public function read(string $file) { if ($this->isEnabled()) { $file = Preg::replace('{[^'.$this->allowlist.']}i', '-', $file); if (file_exists($this->root . $file)) { $this->io->writeError('Reading '.$this->root . $file.' from cache', true, IOInterface::DEBUG); return file_get_contents($this->root . $file); } } return false; } public function write(string $file, string $contents) { if ($this->isEnabled() && !$this->readOnly) { $file = Preg::replace('{[^'.$this->allowlist.']}i', '-', $file); $this->io->writeError('Writing '.$this->root . $file.' into cache', true, IOInterface::DEBUG); $tempFileName = $this->root . $file . uniqid('.', true) . '.tmp'; try { return file_put_contents($tempFileName, $contents) !== false && rename($tempFileName, $this->root . $file); } catch (\ErrorException $e) { $this->io->writeError('Failed to write into cache: '.$e->getMessage().'', true, IOInterface::DEBUG); if (Preg::isMatch('{^file_put_contents\(\): Only ([0-9]+) of ([0-9]+) bytes written}', $e->getMessage(), $m)) { unlink($tempFileName); $message = sprintf( 'Writing %1$s into cache failed after %2$u of %3$u bytes written, only %4$s bytes of free space available', $tempFileName, $m[1], $m[2], function_exists('disk_free_space') ? @disk_free_space(dirname($tempFileName)) : 'unknown' ); $this->io->writeError($message); return false; } throw $e; } } return false; } public function copyFrom(string $file, string $source) { if ($this->isEnabled() && !$this->readOnly) { $file = Preg::replace('{[^'.$this->allowlist.']}i', '-', $file); $this->filesystem->ensureDirectoryExists(dirname($this->root . $file)); if (!file_exists($source)) { $this->io->writeError(''.$source.' does not exist, can not write into cache'); } elseif ($this->io->isDebug()) { $this->io->writeError('Writing '.$this->root . $file.' into cache from '.$source); } return copy($source, $this->root . $file); } return false; } public function copyTo(string $file, string $target) { if ($this->isEnabled()) { $file = Preg::replace('{[^'.$this->allowlist.']}i', '-', $file); if (file_exists($this->root . $file)) { try { touch($this->root . $file, (int) filemtime($this->root . $file), time()); } catch (\ErrorException $e) { Silencer::call('touch', $this->root . $file); } $this->io->writeError('Reading '.$this->root . $file.' from cache', true, IOInterface::DEBUG); return copy($this->root . $file, $target); } } return false; } public function gcIsNecessary() { if (self::$cacheCollected) { return false; } self::$cacheCollected = true; if (Platform::getEnv('COMPOSER_TEST_SUITE')) { return false; } if (Platform::isInputCompletionProcess()) { return false; } return !random_int(0, 50); } public function remove(string $file) { if ($this->isEnabled() && !$this->readOnly) { $file = Preg::replace('{[^'.$this->allowlist.']}i', '-', $file); if (file_exists($this->root . $file)) { return $this->filesystem->unlink($this->root . $file); } } return false; } public function clear() { if ($this->isEnabled() && !$this->readOnly) { $this->filesystem->emptyDirectory($this->root); return true; } return false; } public function getAge(string $file) { if ($this->isEnabled()) { $file = Preg::replace('{[^'.$this->allowlist.']}i', '-', $file); if (file_exists($this->root . $file) && ($mtime = filemtime($this->root . $file)) !== false) { return abs(time() - $mtime); } } return false; } public function gc(int $ttl, int $maxSize) { if ($this->isEnabled() && !$this->readOnly) { $expire = new \DateTime(); $expire->modify('-'.$ttl.' seconds'); $finder = $this->getFinder()->date('until '.$expire->format('Y-m-d H:i:s')); foreach ($finder as $file) { $this->filesystem->unlink($file->getPathname()); } $totalSize = $this->filesystem->size($this->root); if ($totalSize > $maxSize) { $iterator = $this->getFinder()->sortByAccessedTime()->getIterator(); while ($totalSize > $maxSize && $iterator->valid()) { $filepath = $iterator->current()->getPathname(); $totalSize -= $this->filesystem->size($filepath); $this->filesystem->unlink($filepath); $iterator->next(); } } self::$cacheCollected = true; return true; } return false; } public function gcVcsCache(int $ttl): bool { if ($this->isEnabled()) { $expire = new \DateTime(); $expire->modify('-'.$ttl.' seconds'); $finder = Finder::create()->in($this->root)->directories()->depth(0)->date('until '.$expire->format('Y-m-d H:i:s')); foreach ($finder as $file) { $this->filesystem->removeDirectory($file->getPathname()); } self::$cacheCollected = true; return true; } return false; } public function sha1(string $file) { if ($this->isEnabled()) { $file = Preg::replace('{[^'.$this->allowlist.']}i', '-', $file); if (file_exists($this->root . $file)) { return sha1_file($this->root . $file); } } return false; } public function sha256(string $file) { if ($this->isEnabled()) { $file = Preg::replace('{[^'.$this->allowlist.']}i', '-', $file); if (file_exists($this->root . $file)) { return hash_file('sha256', $this->root . $file); } } return false; } protected function getFinder() { return Finder::create()->in($this->root)->files(); } } setName('about') ->setDescription('Shows a short information about Composer') ->setHelp( <<php composer.phar about EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { $composerVersion = Composer::getVersion(); $this->getIO()->write( <<Composer - Dependency Manager for PHP - version $composerVersion Composer is a dependency manager tracking local dependencies of your projects and libraries. See https://getcomposer.org/ for more information. EOT ); return 0; } } setName('archive') ->setDescription('Creates an archive of this composer package') ->setDefinition([ new InputArgument('package', InputArgument::OPTIONAL, 'The package to archive instead of the current project', null, $this->suggestAvailablePackage()), new InputArgument('version', InputArgument::OPTIONAL, 'A version constraint to find the package to archive'), new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the resulting archive: tar, tar.gz, tar.bz2 or zip (default tar)', null, self::FORMATS), new InputOption('dir', null, InputOption::VALUE_REQUIRED, 'Write the archive to this directory'), new InputOption('file', null, InputOption::VALUE_REQUIRED, 'Write the archive with the given file name.' .' Note that the format will be appended.'), new InputOption('ignore-filters', null, InputOption::VALUE_NONE, 'Ignore filters when saving package'), ]) ->setHelp( <<archive command creates an archive of the specified format containing the files and directories of the Composer project or the specified package in the specified version and writes it to the specified directory. php composer.phar archive [--format=zip] [--dir=/foo] [--file=filename] [package [version]] Read more at https://getcomposer.org/doc/03-cli.md#archive EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { $composer = $this->tryComposer(); $config = null; if ($composer) { $config = $composer->getConfig(); $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'archive', $input, $output); $eventDispatcher = $composer->getEventDispatcher(); $eventDispatcher->dispatch($commandEvent->getName(), $commandEvent); $eventDispatcher->dispatchScript(ScriptEvents::PRE_ARCHIVE_CMD); } if (!$config) { $config = Factory::createConfig(); } $format = $input->getOption('format') ?? $config->get('archive-format'); $dir = $input->getOption('dir') ?? $config->get('archive-dir'); $returnCode = $this->archive( $this->getIO(), $config, $input->getArgument('package'), $input->getArgument('version'), $format, $dir, $input->getOption('file'), $input->getOption('ignore-filters'), $composer ); if (0 === $returnCode && $composer) { $composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_ARCHIVE_CMD); } return $returnCode; } protected function archive(IOInterface $io, Config $config, ?string $packageName, ?string $version, string $format, string $dest, ?string $fileName, bool $ignoreFilters, ?Composer $composer): int { if ($composer) { $archiveManager = $composer->getArchiveManager(); } else { $factory = new Factory; $process = new ProcessExecutor(); $httpDownloader = Factory::createHttpDownloader($io, $config); $downloadManager = $factory->createDownloadManager($io, $config, $httpDownloader, $process); $archiveManager = $factory->createArchiveManager($config, $downloadManager, new Loop($httpDownloader, $process)); } if ($packageName) { $package = $this->selectPackage($io, $packageName, $version); if (!$package) { return 1; } } else { $package = $this->requireComposer()->getPackage(); } $io->writeError('Creating the archive into "'.$dest.'".'); $packagePath = $archiveManager->archive($package, $format, $dest, $fileName, $ignoreFilters); $fs = new Filesystem; $shortPath = $fs->findShortestPath(Platform::getCwd(), $packagePath, true); $io->writeError('Created: ', false); $io->write(strlen($shortPath) < strlen($packagePath) ? $shortPath : $packagePath); return 0; } protected function selectPackage(IOInterface $io, string $packageName, ?string $version = null) { $io->writeError('Searching for the specified package.'); if ($composer = $this->tryComposer()) { $localRepo = $composer->getRepositoryManager()->getLocalRepository(); $repo = new CompositeRepository(array_merge([$localRepo], $composer->getRepositoryManager()->getRepositories())); } else { $defaultRepos = RepositoryFactory::defaultReposWithDefaultManager($io); $io->writeError('No composer.json found in the current directory, searching packages from ' . implode(', ', array_keys($defaultRepos))); $repo = new CompositeRepository($defaultRepos); } $packages = $repo->findPackages($packageName, $version); if (count($packages) > 1) { $package = reset($packages); $io->writeError('Found multiple matches, selected '.$package->getPrettyString().'.'); $io->writeError('Alternatives were '.implode(', ', array_map(static function ($p): string { return $p->getPrettyString(); }, $packages)).'.'); $io->writeError('Please use a more specific constraint to pick a different package.'); } elseif ($packages) { $package = reset($packages); $io->writeError('Found an exact match '.$package->getPrettyString().'.'); } else { $io->writeError('Could not find a package matching '.$packageName.'.'); return false; } if (!$package instanceof CompletePackageInterface) { throw new \LogicException('Expected a CompletePackageInterface instance but found '.get_class($package)); } return $package; } } setName('audit') ->setDescription('Checks for security vulnerability advisories for installed packages') ->setDefinition([ new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables auditing of require-dev packages.'), new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Output format. Must be "table", "plain", "json", or "summary".', Auditor::FORMAT_TABLE, Auditor::FORMATS), new InputOption('locked', null, InputOption::VALUE_NONE, 'Audit based on the lock file instead of the installed packages.'), ]) ->setHelp( <<audit command checks for security vulnerability advisories for installed packages. If you do not want to include dev dependencies in the audit you can omit them with --no-dev Read more at https://getcomposer.org/doc/03-cli.md#audit EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $composer = $this->requireComposer(); $packages = $this->getPackages($composer, $input); if (count($packages) === 0) { $this->getIO()->writeError('No packages - skipping audit.'); return 0; } $auditor = new Auditor(); $repoSet = new RepositorySet(); foreach ($composer->getRepositoryManager()->getRepositories() as $repo) { $repoSet->addRepository($repo); } return min(255, $auditor->audit($this->getIO(), $repoSet, $packages, $this->getAuditFormat($input, 'format'), false)); } private function getPackages(Composer $composer, InputInterface $input): array { if ($input->getOption('locked')) { if (!$composer->getLocker()->isLocked()) { throw new \UnexpectedValueException('Valid composer.json and composer.lock files are required to run this command with --locked'); } $locker = $composer->getLocker(); return $locker->getLockedRepository(!$input->getOption('no-dev'))->getPackages(); } $rootPkg = $composer->getPackage(); $installedRepo = new InstalledRepository([$composer->getRepositoryManager()->getLocalRepository()]); if ($input->getOption('no-dev')) { return RepositoryUtils::filterRequiredPackages($installedRepo->getPackages(), $rootPkg); } return $installedRepo->getPackages(); } } requireComposer($disablePlugins, $disableScripts); } return $this->tryComposer($disablePlugins, $disableScripts); } public function requireComposer(?bool $disablePlugins = null, ?bool $disableScripts = null): Composer { if (null === $this->composer) { $application = parent::getApplication(); if ($application instanceof Application) { $this->composer = $application->getComposer(true, $disablePlugins, $disableScripts); assert($this->composer instanceof Composer); } else { throw new \RuntimeException( 'Could not create a Composer\Composer instance, you must inject '. 'one if this command is not used with a Composer\Console\Application instance' ); } } return $this->composer; } public function tryComposer(?bool $disablePlugins = null, ?bool $disableScripts = null): ?Composer { if (null === $this->composer) { $application = parent::getApplication(); if ($application instanceof Application) { $this->composer = $application->getComposer(false, $disablePlugins, $disableScripts); } } return $this->composer; } public function setComposer(Composer $composer) { $this->composer = $composer; } public function resetComposer() { $this->composer = null; $this->getApplication()->resetComposer(); } public function isProxyCommand() { return false; } public function getIO() { if (null === $this->io) { $application = parent::getApplication(); if ($application instanceof Application) { $this->io = $application->getIO(); } else { $this->io = new NullIO(); } } return $this->io; } public function setIO(IOInterface $io) { $this->io = $io; } public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void { $definition = $this->getDefinition(); $name = (string) $input->getCompletionName(); if (CompletionInput::TYPE_OPTION_VALUE === $input->getCompletionType() && $definition->hasOption($name) && ($option = $definition->getOption($name)) instanceof InputOption ) { $option->complete($input, $suggestions); } elseif (CompletionInput::TYPE_ARGUMENT_VALUE === $input->getCompletionType() && $definition->hasArgument($name) && ($argument = $definition->getArgument($name)) instanceof InputArgument ) { $argument->complete($input, $suggestions); } else { parent::complete($input, $suggestions); } } protected function initialize(InputInterface $input, OutputInterface $output) { $disablePlugins = $input->hasParameterOption('--no-plugins'); $disableScripts = $input->hasParameterOption('--no-scripts'); if ($this instanceof SelfUpdateCommand) { $disablePlugins = true; $disableScripts = true; } $composer = $this->tryComposer($disablePlugins, $disableScripts); $io = $this->getIO(); if (null === $composer) { $composer = Factory::createGlobal($this->getIO(), $disablePlugins, $disableScripts); } if ($composer) { $preCommandRunEvent = new PreCommandRunEvent(PluginEvents::PRE_COMMAND_RUN, $input, $this->getName()); $composer->getEventDispatcher()->dispatch($preCommandRunEvent->getName(), $preCommandRunEvent); } if (true === $input->hasParameterOption(['--no-ansi']) && $input->hasOption('no-progress')) { $input->setOption('no-progress', true); } $envOptions = [ 'COMPOSER_NO_AUDIT' => ['no-audit'], 'COMPOSER_NO_DEV' => ['no-dev', 'update-no-dev'], 'COMPOSER_PREFER_STABLE' => ['prefer-stable'], 'COMPOSER_PREFER_LOWEST' => ['prefer-lowest'], ]; foreach ($envOptions as $envName => $optionNames) { foreach ($optionNames as $optionName) { if (true === $input->hasOption($optionName)) { if (false === $input->getOption($optionName) && (bool) Platform::getEnv($envName)) { $input->setOption($optionName, true); } } } } if (true === $input->hasOption('ignore-platform-reqs')) { if (!$input->getOption('ignore-platform-reqs') && (bool) Platform::getEnv('COMPOSER_IGNORE_PLATFORM_REQS')) { $input->setOption('ignore-platform-reqs', true); $io->writeError('COMPOSER_IGNORE_PLATFORM_REQS is set. You may experience unexpected errors.'); } } if (true === $input->hasOption('ignore-platform-req') && (!$input->hasOption('ignore-platform-reqs') || !$input->getOption('ignore-platform-reqs'))) { $ignorePlatformReqEnv = Platform::getEnv('COMPOSER_IGNORE_PLATFORM_REQ'); if (0 === count($input->getOption('ignore-platform-req')) && is_string($ignorePlatformReqEnv) && '' !== $ignorePlatformReqEnv) { $input->setOption('ignore-platform-req', explode(',', $ignorePlatformReqEnv)); $io->writeError('COMPOSER_IGNORE_PLATFORM_REQ is set to ignore '.$ignorePlatformReqEnv.'. You may experience unexpected errors.'); } } parent::initialize($input, $output); } protected function getPreferredInstallOptions(Config $config, InputInterface $input, bool $keepVcsRequiresPreferSource = false) { $preferSource = false; $preferDist = false; switch ($config->get('preferred-install')) { case 'source': $preferSource = true; break; case 'dist': $preferDist = true; break; case 'auto': default: break; } if (!$input->hasOption('prefer-dist') || !$input->hasOption('prefer-source')) { return [$preferSource, $preferDist]; } if ($input->hasOption('prefer-install') && is_string($input->getOption('prefer-install'))) { if ($input->getOption('prefer-source')) { throw new \InvalidArgumentException('--prefer-source can not be used together with --prefer-install'); } if ($input->getOption('prefer-dist')) { throw new \InvalidArgumentException('--prefer-dist can not be used together with --prefer-install'); } switch ($input->getOption('prefer-install')) { case 'dist': $input->setOption('prefer-dist', true); break; case 'source': $input->setOption('prefer-source', true); break; case 'auto': $preferDist = false; $preferSource = false; break; default: throw new \UnexpectedValueException('--prefer-install accepts one of "dist", "source" or "auto", got '.$input->getOption('prefer-install')); } } if ($input->getOption('prefer-source') || $input->getOption('prefer-dist') || ($keepVcsRequiresPreferSource && $input->hasOption('keep-vcs') && $input->getOption('keep-vcs'))) { $preferSource = $input->getOption('prefer-source') || ($keepVcsRequiresPreferSource && $input->hasOption('keep-vcs') && $input->getOption('keep-vcs')); $preferDist = $input->getOption('prefer-dist'); } return [$preferSource, $preferDist]; } protected function getPlatformRequirementFilter(InputInterface $input): PlatformRequirementFilterInterface { if (!$input->hasOption('ignore-platform-reqs') || !$input->hasOption('ignore-platform-req')) { throw new \LogicException('Calling getPlatformRequirementFilter from a command which does not define the --ignore-platform-req[s] flags is not permitted.'); } if (true === $input->getOption('ignore-platform-reqs')) { return PlatformRequirementFilterFactory::ignoreAll(); } $ignores = $input->getOption('ignore-platform-req'); if (count($ignores) > 0) { return PlatformRequirementFilterFactory::fromBoolOrList($ignores); } return PlatformRequirementFilterFactory::ignoreNothing(); } protected function formatRequirements(array $requirements) { $requires = []; $requirements = $this->normalizeRequirements($requirements); foreach ($requirements as $requirement) { if (!isset($requirement['version'])) { throw new \UnexpectedValueException('Option '.$requirement['name'] .' is missing a version constraint, use e.g. '.$requirement['name'].':^1.0'); } $requires[$requirement['name']] = $requirement['version']; } return $requires; } protected function normalizeRequirements(array $requirements) { $parser = new VersionParser(); return $parser->parseNameVersionPairs($requirements); } protected function renderTable(array $table, OutputInterface $output) { $renderer = new Table($output); $renderer->setStyle('compact'); $renderer->setRows($table)->render(); } protected function getTerminalWidth() { $terminal = new Terminal(); $width = $terminal->getWidth(); if (Platform::isWindows()) { $width--; } else { $width = max(80, $width); } return $width; } protected function getAuditFormat(InputInterface $input, string $optName = 'audit-format'): string { if (!$input->hasOption($optName)) { throw new \LogicException('This should not be called on a Command which has no '.$optName.' option defined.'); } $val = $input->getOption($optName); if (!in_array($val, Auditor::FORMATS, true)) { throw new \InvalidArgumentException('--'.$optName.' must be one of '.implode(', ', Auditor::FORMATS).'.'); } return $val; } } requireComposer(); $commandEvent = new CommandEvent(PluginEvents::COMMAND, $this->getName(), $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); $repos = []; $repos[] = new RootPackageRepository(clone $composer->getPackage()); if ($input->getOption('locked')) { $locker = $composer->getLocker(); if (!$locker->isLocked()) { throw new \UnexpectedValueException('A valid composer.lock file is required to run this command with --locked'); } $repos[] = $locker->getLockedRepository(true); $repos[] = new PlatformRepository([], $locker->getPlatformOverrides()); } else { $localRepo = $composer->getRepositoryManager()->getLocalRepository(); $rootPkg = $composer->getPackage(); if (count($localRepo->getPackages()) === 0 && (count($rootPkg->getRequires()) > 0 || count($rootPkg->getDevRequires()) > 0)) { $output->writeln('No dependencies installed. Try running composer install or update, or use --locked.'); return 1; } $repos[] = $localRepo; $platformOverrides = $composer->getConfig()->get('platform') ?: []; $repos[] = new PlatformRepository([], $platformOverrides); } $installedRepo = new InstalledRepository($repos); [$needle, $textConstraint] = array_pad( explode(':', $input->getArgument(self::ARGUMENT_PACKAGE)), 2, $input->hasArgument(self::ARGUMENT_CONSTRAINT) ? $input->getArgument(self::ARGUMENT_CONSTRAINT) : '*' ); $packages = $installedRepo->findPackagesWithReplacersAndProviders($needle); if (empty($packages)) { throw new \InvalidArgumentException(sprintf('Could not find package "%s" in your project', $needle)); } if (!$installedRepo->findPackage($needle, $textConstraint)) { $defaultRepos = new CompositeRepository(RepositoryFactory::defaultRepos($this->getIO(), $composer->getConfig(), $composer->getRepositoryManager())); if ($match = $defaultRepos->findPackage($needle, $textConstraint)) { $installedRepo->addRepository(new InstalledArrayRepository([clone $match])); } } $needles = [$needle]; if ($inverted) { foreach ($packages as $package) { $needles = array_merge($needles, array_map(static function (Link $link): string { return $link->getTarget(); }, $package->getReplaces())); } } if ('*' !== $textConstraint) { $versionParser = new VersionParser(); $constraint = $versionParser->parseConstraints($textConstraint); } else { $constraint = null; } $renderTree = $input->getOption(self::OPTION_TREE); $recursive = $renderTree || $input->getOption(self::OPTION_RECURSIVE); $results = $installedRepo->getDependents($needles, $constraint, $inverted, $recursive); if (empty($results)) { $extra = (null !== $constraint) ? sprintf(' in versions %smatching %s', $inverted ? 'not ' : '', $textConstraint) : ''; $this->getIO()->writeError(sprintf( 'There is no installed package depending on "%s"%s', $needle, $extra )); } elseif ($renderTree) { $this->initStyles($output); $root = $packages[0]; $this->getIO()->write(sprintf('%s %s %s', $root->getPrettyName(), $root->getPrettyVersion(), $root instanceof CompletePackageInterface ? $root->getDescription() : '')); $this->printTree($results); } else { $this->printTable($output, $results); } if ($inverted && $input->hasArgument(self::ARGUMENT_CONSTRAINT)) { $this->getIO()->writeError('Not finding what you were looking for? Try calling `composer update "'.$input->getArgument(self::ARGUMENT_PACKAGE).':'.$input->getArgument(self::ARGUMENT_CONSTRAINT).'" --dry-run` to get another view on the problem.'); } return 0; } protected function printTable(OutputInterface $output, $results): void { $table = []; $doubles = []; do { $queue = []; $rows = []; foreach ($results as $result) { [$package, $link, $children] = $result; $unique = (string) $link; if (isset($doubles[$unique])) { continue; } $doubles[$unique] = true; $version = $package->getPrettyVersion() === RootPackage::DEFAULT_PRETTY_VERSION ? '-' : $package->getPrettyVersion(); $rows[] = [$package->getPrettyName(), $version, $link->getDescription(), sprintf('%s (%s)', $link->getTarget(), $link->getPrettyConstraint())]; if ($children) { $queue = array_merge($queue, $children); } } $results = $queue; $table = array_merge($rows, $table); } while (!empty($results)); $this->renderTable($table, $output); } protected function initStyles(OutputInterface $output): void { $this->colors = [ 'green', 'yellow', 'cyan', 'magenta', 'blue', ]; foreach ($this->colors as $color) { $style = new OutputFormatterStyle($color); $output->getFormatter()->setStyle($color, $style); } } protected function printTree(array $results, string $prefix = '', int $level = 1): void { $count = count($results); $idx = 0; foreach ($results as $result) { [$package, $link, $children] = $result; $color = $this->colors[$level % count($this->colors)]; $prevColor = $this->colors[($level - 1) % count($this->colors)]; $isLast = (++$idx === $count); $versionText = $package->getPrettyVersion() === RootPackage::DEFAULT_PRETTY_VERSION ? '' : $package->getPrettyVersion(); $packageText = rtrim(sprintf('<%s>%s %s', $color, $package->getPrettyName(), $versionText)); $linkText = sprintf('%s <%s>%s %s', $link->getDescription(), $prevColor, $link->getTarget(), $link->getPrettyConstraint()); $circularWarn = $children === false ? '(circular dependency aborted here)' : ''; $this->writeTreeLine(rtrim(sprintf("%s%s%s (%s) %s", $prefix, $isLast ? '└──' : '├──', $packageText, $linkText, $circularWarn))); if ($children) { $this->printTree($children, $prefix . ($isLast ? ' ' : '│ '), $level + 1); } } } private function writeTreeLine(string $line): void { $io = $this->getIO(); if (!$io->isDecorated()) { $line = str_replace(['└', '├', '──', '│'], ['`-', '|-', '-', '|'], $line); } $io->write($line); } } setName('bump') ->setDescription('Increases the lower limit of your composer.json requirements to the currently installed versions') ->setDefinition([ new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Optional package name(s) to restrict which packages are bumped.', null, $this->suggestRootRequirement()), new InputOption('dev-only', 'D', InputOption::VALUE_NONE, 'Only bump requirements in "require-dev".'), new InputOption('no-dev-only', 'R', InputOption::VALUE_NONE, 'Only bump requirements in "require".'), ]) ->setHelp( <<bump command increases the lower limit of your composer.json requirements to the currently installed versions. This helps to ensure your dependencies do not accidentally get downgraded due to some other conflict, and can slightly improve dependency resolution performance as it limits the amount of package versions Composer has to look at. Running this blindly on libraries is **NOT** recommended as it will narrow down your allowed dependencies, which may cause dependency hell for your users. Running it with --dev-only on libraries may be fine however as dev requirements are local to the library and do not affect consumers of the package. EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $composerJsonPath = Factory::getComposerFile(); $io = $this->getIO(); if (!Filesystem::isReadable($composerJsonPath)) { $io->writeError(''.$composerJsonPath.' is not readable.'); return self::ERROR_GENERIC; } $composerJson = new JsonFile($composerJsonPath); $contents = file_get_contents($composerJson->getPath()); if (false === $contents) { $io->writeError(''.$composerJsonPath.' is not readable.'); return self::ERROR_GENERIC; } if (!is_writable($composerJsonPath) && false === Silencer::call('file_put_contents', $composerJsonPath, $contents)) { $io->writeError(''.$composerJsonPath.' is not writable.'); return self::ERROR_GENERIC; } unset($contents); $composer = $this->requireComposer(); if ($composer->getLocker()->isLocked()) { if (!$composer->getLocker()->isFresh()) { $io->writeError('The lock file is not up to date with the latest changes in composer.json. Run the appropriate `update` to fix that before you use the `bump` command.'); return self::ERROR_LOCK_OUTDATED; } $repo = $composer->getLocker()->getLockedRepository(true); } else { $repo = $composer->getRepositoryManager()->getLocalRepository(); } if ($composer->getPackage()->getType() !== 'project' && !$input->getOption('dev-only')) { $io->writeError('Warning: Bumping dependency constraints is not recommended for libraries as it will narrow down your dependencies and may cause problems for your users.'); $contents = $composerJson->read(); if (!isset($contents['type'])) { $io->writeError('If your package is not a library, you can explicitly specify the "type" by using "composer config type project".'); $io->writeError('Alternatively you can use --dev-only to only bump dependencies within "require-dev".'); } unset($contents); } $bumper = new VersionBumper(); $tasks = []; if (!$input->getOption('no-dev-only')) { $tasks['require-dev'] = $composer->getPackage()->getDevRequires(); } if (!$input->getOption('dev-only')) { $tasks['require'] = $composer->getPackage()->getRequires(); } $packagesFilter = $input->getArgument('packages'); if (count($packagesFilter) > 0) { $pattern = BasePackage::packageNamesToRegexp(array_unique(array_map('strtolower', $packagesFilter))); foreach ($tasks as $key => $reqs) { foreach ($reqs as $pkgName => $link) { if (!Preg::isMatch($pattern, $pkgName)) { unset($tasks[$key][$pkgName]); } } } } $updates = []; foreach ($tasks as $key => $reqs) { foreach ($reqs as $pkgName => $link) { if (PlatformRepository::isPlatformPackage($pkgName)) { continue; } $currentConstraint = $link->getPrettyConstraint(); $package = $repo->findPackage($pkgName, '*'); if (null === $package) { continue; } while ($package instanceof AliasPackage) { $package = $package->getAliasOf(); } $bumped = $bumper->bumpRequirement($link->getConstraint(), $package); if ($bumped === $currentConstraint) { continue; } $updates[$key][$pkgName] = $bumped; } } if (!$this->updateFileCleanly($composerJson, $updates)) { $composerDefinition = $composerJson->read(); foreach ($updates as $key => $packages) { foreach ($packages as $package => $version) { $composerDefinition[$key][$package] = $version; } } $composerJson->write($composerDefinition); } $changeCount = array_sum(array_map('count', $updates)); if ($changeCount > 0) { $io->write(''.$composerJsonPath.' has been updated ('.$changeCount.' changes).'); } else { $io->write('No requirements to update in '.$composerJsonPath.'.'); } if ($composer->getLocker()->isLocked() && $changeCount > 0) { $contents = file_get_contents($composerJson->getPath()); if (false === $contents) { throw new \RuntimeException('Unable to read '.$composerJson->getPath().' contents to update the lock file hash.'); } $lock = new JsonFile(Factory::getLockFile($composerJsonPath)); $lockData = $lock->read(); $lockData['content-hash'] = Locker::getContentHash($contents); $lock->write($lockData); } return 0; } private function updateFileCleanly(JsonFile $json, array $updates): bool { $contents = file_get_contents($json->getPath()); if (false === $contents) { throw new \RuntimeException('Unable to read '.$json->getPath().' contents.'); } $manipulator = new JsonManipulator($contents); foreach ($updates as $key => $packages) { foreach ($packages as $package => $version) { if (!$manipulator->addLink($key, $package, $version)) { return false; } } } if (false === file_put_contents($json->getPath(), $manipulator->getContents())) { throw new \RuntimeException('Unable to write new '.$json->getPath().' contents.'); } return true; } } setName('check-platform-reqs') ->setDescription('Check that platform requirements are satisfied') ->setDefinition([ new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables checking of require-dev packages requirements.'), new InputOption('lock', null, InputOption::VALUE_NONE, 'Checks requirements only from the lock file, not from installed packages.'), new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text', ['json', 'text']), ]) ->setHelp( <<php composer.phar check-platform-reqs EOT ); } protected function execute(InputInterface $input, OutputInterface $output): int { $composer = $this->requireComposer(); $requires = []; $removePackages = []; if ($input->getOption('lock')) { $this->getIO()->writeError('Checking '.($input->getOption('no-dev') ? 'non-dev ' : '').'platform requirements using the lock file'); $installedRepo = $composer->getLocker()->getLockedRepository(!$input->getOption('no-dev')); } else { $installedRepo = $composer->getRepositoryManager()->getLocalRepository(); if (!$installedRepo->getPackages()) { $this->getIO()->writeError('No vendor dir present, checking '.($input->getOption('no-dev') ? 'non-dev ' : '').'platform requirements from the lock file'); $installedRepo = $composer->getLocker()->getLockedRepository(!$input->getOption('no-dev')); } else { if ($input->getOption('no-dev')) { $removePackages = $installedRepo->getDevPackageNames(); } $this->getIO()->writeError('Checking '.($input->getOption('no-dev') ? 'non-dev ' : '').'platform requirements for packages in the vendor dir'); } } if (!$input->getOption('no-dev')) { $requires += $composer->getPackage()->getDevRequires(); } foreach ($requires as $require => $link) { $requires[$require] = [$link]; } $installedRepo = new InstalledRepository([$installedRepo, new RootPackageRepository(clone $composer->getPackage())]); foreach ($installedRepo->getPackages() as $package) { if (in_array($package->getName(), $removePackages, true)) { continue; } foreach ($package->getRequires() as $require => $link) { $requires[$require][] = $link; } } ksort($requires); $installedRepo->addRepository(new PlatformRepository([], [])); $results = []; $exitCode = 0; foreach ($requires as $require => $links) { if (PlatformRepository::isPlatformPackage($require)) { $candidates = $installedRepo->findPackagesWithReplacersAndProviders($require); if ($candidates) { $reqResults = []; foreach ($candidates as $candidate) { $candidateConstraint = null; if ($candidate->getName() === $require) { $candidateConstraint = new Constraint('=', $candidate->getVersion()); $candidateConstraint->setPrettyString($candidate->getPrettyVersion()); } else { foreach (array_merge($candidate->getProvides(), $candidate->getReplaces()) as $link) { if ($link->getTarget() === $require) { $candidateConstraint = $link->getConstraint(); break; } } } if (!$candidateConstraint) { continue; } foreach ($links as $link) { if (!$link->getConstraint()->matches($candidateConstraint)) { $reqResults[] = [ $candidate->getName() === $require ? $candidate->getPrettyName() : $require, $candidateConstraint->getPrettyString(), $link, 'failed', $candidate->getName() === $require ? '' : 'provided by '.$candidate->getPrettyName().'', ]; continue 2; } } $results[] = [ $candidate->getName() === $require ? $candidate->getPrettyName() : $require, $candidateConstraint->getPrettyString(), null, 'success', $candidate->getName() === $require ? '' : 'provided by '.$candidate->getPrettyName().'', ]; continue 2; } $results = array_merge($results, $reqResults); $exitCode = max($exitCode, 1); continue; } $results[] = [ $require, 'n/a', $links[0], 'missing', '', ]; $exitCode = max($exitCode, 2); } } $this->printTable($output, $results, $input->getOption('format')); return $exitCode; } protected function printTable(OutputInterface $output, array $results, string $format): void { $rows = []; foreach ($results as $result) { [$platformPackage, $version, $link, $status, $provider] = $result; if ('json' === $format) { $rows[] = [ "name" => $platformPackage, "version" => $version, "status" => strip_tags($status), "failed_requirement" => $link instanceof Link ? [ 'source' => $link->getSource(), 'type' => $link->getDescription(), 'target' => $link->getTarget(), 'constraint' => $link->getPrettyConstraint(), ] : null, "provider" => $provider === '' ? null : strip_tags($provider), ]; } else { $rows[] = [ $platformPackage, $version, $link, $link ? sprintf('%s %s %s (%s)', $link->getSource(), $link->getDescription(), $link->getTarget(), $link->getPrettyConstraint()) : '', rtrim($status.' '.$provider), ]; } } if ('json' === $format) { $this->getIO()->write(JsonFile::encode($rows)); } else { $this->renderTable($rows, $output); } } } setName('clear-cache') ->setAliases(['clearcache', 'cc']) ->setDescription('Clears composer\'s internal package cache') ->setDefinition([ new InputOption('gc', null, InputOption::VALUE_NONE, 'Only run garbage collection, not a full cache clear'), ]) ->setHelp( <<clear-cache deletes all cached packages from composer's cache directory. Read more at https://getcomposer.org/doc/03-cli.md#clear-cache-clearcache-cc EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { $config = Factory::createConfig(); $io = $this->getIO(); $cachePaths = [ 'cache-vcs-dir' => $config->get('cache-vcs-dir'), 'cache-repo-dir' => $config->get('cache-repo-dir'), 'cache-files-dir' => $config->get('cache-files-dir'), 'cache-dir' => $config->get('cache-dir'), ]; foreach ($cachePaths as $key => $cachePath) { if ($key === 'cache-dir' && $input->getOption('gc')) { continue; } $cachePath = realpath($cachePath); if (!$cachePath) { $io->writeError("Cache directory does not exist ($key): $cachePath"); continue; } $cache = new Cache($io, $cachePath); $cache->setReadOnly($config->get('cache-read-only')); if (!$cache->isEnabled()) { $io->writeError("Cache is not enabled ($key): $cachePath"); continue; } if ($input->getOption('gc')) { $io->writeError("Garbage-collecting cache ($key): $cachePath"); if ($key === 'cache-files-dir') { $cache->gc($config->get('cache-files-ttl'), $config->get('cache-files-maxsize')); } elseif ($key === 'cache-repo-dir') { $cache->gc($config->get('cache-ttl'), 1024 * 1024 * 1024 ); } elseif ($key === 'cache-vcs-dir') { $cache->gcVcsCache($config->get('cache-ttl')); } } else { $io->writeError("Clearing cache ($key): $cachePath"); $cache->clear(); } } if ($input->getOption('gc')) { $io->writeError('All caches garbage-collected.'); } else { $io->writeError('All caches cleared.'); } return 0; } } requireComposer(); return array_merge(array_keys($composer->getPackage()->getRequires()), array_keys($composer->getPackage()->getDevRequires())); }; } private function suggestInstalledPackage(bool $includeRootPackage = true, bool $includePlatformPackages = false): \Closure { return function (CompletionInput $input) use ($includeRootPackage, $includePlatformPackages): array { $composer = $this->requireComposer(); $installedRepos = []; if ($includeRootPackage) { $installedRepos[] = new RootPackageRepository(clone $composer->getPackage()); } $locker = $composer->getLocker(); if ($locker->isLocked()) { $installedRepos[] = $locker->getLockedRepository(true); } else { $installedRepos[] = $composer->getRepositoryManager()->getLocalRepository(); } $platformHint = []; if ($includePlatformPackages) { if ($locker->isLocked()) { $platformRepo = new PlatformRepository([], $locker->getPlatformOverrides()); } else { $platformRepo = new PlatformRepository([], $composer->getConfig()->get('platform')); } if ($input->getCompletionValue() === '') { $hintsToFind = ['ext-' => 0, 'lib-' => 0, 'php' => 99, 'composer' => 99]; foreach ($platformRepo->getPackages() as $pkg) { foreach ($hintsToFind as $hintPrefix => $hintCount) { if (str_starts_with($pkg->getName(), $hintPrefix)) { if ($hintCount === 0 || $hintCount >= 99) { $platformHint[] = $pkg->getName(); $hintsToFind[$hintPrefix]++; } elseif ($hintCount === 1) { unset($hintsToFind[$hintPrefix]); $platformHint[] = substr($pkg->getName(), 0, max(strlen($pkg->getName()) - 3, strlen($hintPrefix) + 1)).'...'; } continue 2; } } } } else { $installedRepos[] = $platformRepo; } } $installedRepo = new InstalledRepository($installedRepos); return array_merge( array_map(static function (PackageInterface $package) { return $package->getName(); }, $installedRepo->getPackages()), $platformHint ); }; } private function suggestAvailablePackage(int $max = 99): \Closure { return function (CompletionInput $input) use ($max): array { if ($max < 1) { return []; } $composer = $this->requireComposer(); $repos = new CompositeRepository($composer->getRepositoryManager()->getRepositories()); $results = []; $showVendors = false; if (!str_contains($input->getCompletionValue(), '/')) { $results = $repos->search('^' . preg_quote($input->getCompletionValue()), RepositoryInterface::SEARCH_VENDOR); $showVendors = true; } if (\count($results) <= 1) { $results = $repos->search('^'.preg_quote($input->getCompletionValue()), RepositoryInterface::SEARCH_NAME); $showVendors = false; } $results = array_column($results, 'name'); if ($showVendors) { $results = array_map(static function (string $name): string { return $name.'/'; }, $results); usort($results, static function (string $a, string $b) { $lenA = \strlen($a); $lenB = \strlen($b); if ($lenA === $lenB) { return $a <=> $b; } return $lenA - $lenB; }); $pinned = []; $completionInput = $input->getCompletionValue().'/'; if (false !== ($exactIndex = array_search($completionInput, $results, true))) { $pinned[] = $completionInput; array_splice($results, $exactIndex, 1); } return array_merge($pinned, array_slice($results, 0, $max - \count($pinned))); } return array_slice($results, 0, $max); }; } private function suggestAvailablePackageInclPlatform(): \Closure { return function (CompletionInput $input): array { if (Preg::isMatch('{^(ext|lib|php)(-|$)|^com}', $input->getCompletionValue())) { $matches = $this->suggestPlatformPackage()($input); } else { $matches = []; } return array_merge($matches, $this->suggestAvailablePackage(99 - \count($matches))($input)); }; } private function suggestPlatformPackage(): \Closure { return function (CompletionInput $input): array { $repos = new PlatformRepository([], $this->requireComposer()->getConfig()->get('platform')); $pattern = BasePackage::packageNameToRegexp($input->getCompletionValue().'*'); return array_filter(array_map(static function (PackageInterface $package) { return $package->getName(); }, $repos->getPackages()), static function (string $name) use ($pattern): bool { return Preg::isMatch($pattern, $name); }); }; } } setName('config') ->setDescription('Sets config options') ->setDefinition([ new InputOption('global', 'g', InputOption::VALUE_NONE, 'Apply command to the global config file'), new InputOption('editor', 'e', InputOption::VALUE_NONE, 'Open editor'), new InputOption('auth', 'a', InputOption::VALUE_NONE, 'Affect auth config file (only used for --editor)'), new InputOption('unset', null, InputOption::VALUE_NONE, 'Unset the given setting-key'), new InputOption('list', 'l', InputOption::VALUE_NONE, 'List configuration settings'), new InputOption('file', 'f', InputOption::VALUE_REQUIRED, 'If you want to choose a different composer.json or config.json'), new InputOption('absolute', null, InputOption::VALUE_NONE, 'Returns absolute paths when fetching *-dir config values instead of relative'), new InputOption('json', 'j', InputOption::VALUE_NONE, 'JSON decode the setting value, to be used with extra.* keys'), new InputOption('merge', 'm', InputOption::VALUE_NONE, 'Merge the setting value with the current value, to be used with extra.* keys in combination with --json'), new InputOption('append', null, InputOption::VALUE_NONE, 'When adding a repository, append it (lowest priority) to the existing ones instead of prepending it (highest priority)'), new InputOption('source', null, InputOption::VALUE_NONE, 'Display where the config value is loaded from'), new InputArgument('setting-key', null, 'Setting key'), new InputArgument('setting-value', InputArgument::IS_ARRAY, 'Setting value'), ]) ->setHelp( <<%command.full_name% bin-dir bin/ To read a config setting: %command.full_name% bin-dir Outputs: bin To edit the global config.json file: %command.full_name% --global To add a repository: %command.full_name% repositories.foo vcs https://bar.com To remove a repository (repo is a short alias for repositories): %command.full_name% --unset repo.foo To disable packagist: %command.full_name% repo.packagist false You can alter repositories in the global config.json file by passing in the --global option. To add or edit suggested packages you can use: %command.full_name% suggest.package reason for the suggestion To add or edit extra properties you can use: %command.full_name% extra.property value Or to add a complex value you can use json with: %command.full_name% extra.property --json '{"foo":true, "bar": []}' To edit the file in an external editor: %command.full_name% --editor To choose your editor you can set the "EDITOR" env variable. To get a list of configuration values in the file: %command.full_name% --list You can always pass more than one option. As an example, if you want to edit the global config.json file. %command.full_name% --editor --global Read more at https://getcomposer.org/doc/03-cli.md#config EOT ) ; } protected function initialize(InputInterface $input, OutputInterface $output): void { parent::initialize($input, $output); if ($input->getOption('global') && null !== $input->getOption('file')) { throw new \RuntimeException('--file and --global can not be combined'); } $io = $this->getIO(); $this->config = Factory::createConfig($io); $configFile = $input->getOption('global') ? ($this->config->get('home') . '/config.json') : ($input->getOption('file') ?: Factory::getComposerFile()); if ( ($configFile === 'composer.json' || $configFile === './composer.json') && !file_exists($configFile) && realpath(Platform::getCwd()) === realpath($this->config->get('home')) ) { file_put_contents($configFile, "{\n}\n"); } $this->configFile = new JsonFile($configFile, null, $io); $this->configSource = new JsonConfigSource($this->configFile); $authConfigFile = $input->getOption('global') ? ($this->config->get('home') . '/auth.json') : dirname($configFile) . '/auth.json'; $this->authConfigFile = new JsonFile($authConfigFile, null, $io); $this->authConfigSource = new JsonConfigSource($this->authConfigFile, true); if ($input->getOption('global') && !$this->configFile->exists()) { touch($this->configFile->getPath()); $this->configFile->write(['config' => new \ArrayObject]); Silencer::call('chmod', $this->configFile->getPath(), 0600); } if ($input->getOption('global') && !$this->authConfigFile->exists()) { touch($this->authConfigFile->getPath()); $this->authConfigFile->write(['bitbucket-oauth' => new \ArrayObject, 'github-oauth' => new \ArrayObject, 'gitlab-oauth' => new \ArrayObject, 'gitlab-token' => new \ArrayObject, 'http-basic' => new \ArrayObject, 'bearer' => new \ArrayObject]); Silencer::call('chmod', $this->authConfigFile->getPath(), 0600); } if (!$this->configFile->exists()) { throw new \RuntimeException(sprintf('File "%s" cannot be found in the current directory', $configFile)); } } protected function execute(InputInterface $input, OutputInterface $output): int { if (true === $input->getOption('editor')) { $editor = Platform::getEnv('EDITOR'); if (false === $editor || '' === $editor) { if (Platform::isWindows()) { $editor = 'notepad'; } else { foreach (['editor', 'vim', 'vi', 'nano', 'pico', 'ed'] as $candidate) { if (exec('which '.$candidate)) { $editor = $candidate; break; } } } } else { $editor = escapeshellcmd($editor); } $file = $input->getOption('auth') ? $this->authConfigFile->getPath() : $this->configFile->getPath(); system($editor . ' ' . $file . (Platform::isWindows() ? '' : ' > `tty`')); return 0; } if (false === $input->getOption('global')) { $this->config->merge($this->configFile->read(), $this->configFile->getPath()); $this->config->merge(['config' => $this->authConfigFile->exists() ? $this->authConfigFile->read() : []], $this->authConfigFile->getPath()); } if (true === $input->getOption('list')) { $this->listConfiguration($this->config->all(), $this->config->raw(), $output, null, $input->getOption('source')); return 0; } $settingKey = $input->getArgument('setting-key'); if (!is_string($settingKey)) { return 0; } if ([] !== $input->getArgument('setting-value') && $input->getOption('unset')) { throw new \RuntimeException('You can not combine a setting value with --unset'); } if ([] === $input->getArgument('setting-value') && !$input->getOption('unset')) { $properties = ['name', 'type', 'description', 'homepage', 'version', 'minimum-stability', 'prefer-stable', 'keywords', 'license', 'extra']; $rawData = $this->configFile->read(); $data = $this->config->all(); if (Preg::isMatch('/^repos?(?:itories)?(?:\.(.+))?/', $settingKey, $matches)) { if (!isset($matches[1]) || $matches[1] === '') { $value = $data['repositories'] ?? []; } else { if (!isset($data['repositories'][$matches[1]])) { throw new \InvalidArgumentException('There is no '.$matches[1].' repository defined'); } $value = $data['repositories'][$matches[1]]; } } elseif (strpos($settingKey, '.')) { $bits = explode('.', $settingKey); if ($bits[0] === 'extra') { $data = $rawData; } else { $data = $data['config']; } $match = false; foreach ($bits as $bit) { $key = isset($key) ? $key.'.'.$bit : $bit; $match = false; if (isset($data[$key])) { $match = true; $data = $data[$key]; unset($key); } } if (!$match) { throw new \RuntimeException($settingKey.' is not defined.'); } $value = $data; } elseif (isset($data['config'][$settingKey])) { $value = $this->config->get($settingKey, $input->getOption('absolute') ? 0 : Config::RELATIVE_PATHS); } elseif (isset($rawData[$settingKey]) && in_array($settingKey, $properties, true)) { $value = $rawData[$settingKey]; } else { throw new \RuntimeException($settingKey.' is not defined'); } if (is_array($value)) { $value = JsonFile::encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); } $sourceOfConfigValue = ''; if ($input->getOption('source')) { $sourceOfConfigValue = ' (' . $this->config->getSourceOfValue($settingKey) . ')'; } $this->getIO()->write($value . $sourceOfConfigValue, true, IOInterface::QUIET); return 0; } $values = $input->getArgument('setting-value'); $booleanValidator = static function ($val): bool { return in_array($val, ['true', 'false', '1', '0'], true); }; $booleanNormalizer = static function ($val): bool { return $val !== 'false' && (bool) $val; }; $uniqueConfigValues = [ 'process-timeout' => ['is_numeric', 'intval'], 'use-include-path' => [$booleanValidator, $booleanNormalizer], 'use-github-api' => [$booleanValidator, $booleanNormalizer], 'preferred-install' => [ static function ($val): bool { return in_array($val, ['auto', 'source', 'dist'], true); }, static function ($val) { return $val; }, ], 'gitlab-protocol' => [ static function ($val): bool { return in_array($val, ['git', 'http', 'https'], true); }, static function ($val) { return $val; }, ], 'store-auths' => [ static function ($val): bool { return in_array($val, ['true', 'false', 'prompt'], true); }, static function ($val) { if ('prompt' === $val) { return 'prompt'; } return $val !== 'false' && (bool) $val; }, ], 'notify-on-install' => [$booleanValidator, $booleanNormalizer], 'vendor-dir' => ['is_string', static function ($val) { return $val; }], 'bin-dir' => ['is_string', static function ($val) { return $val; }], 'archive-dir' => ['is_string', static function ($val) { return $val; }], 'archive-format' => ['is_string', static function ($val) { return $val; }], 'data-dir' => ['is_string', static function ($val) { return $val; }], 'cache-dir' => ['is_string', static function ($val) { return $val; }], 'cache-files-dir' => ['is_string', static function ($val) { return $val; }], 'cache-repo-dir' => ['is_string', static function ($val) { return $val; }], 'cache-vcs-dir' => ['is_string', static function ($val) { return $val; }], 'cache-ttl' => ['is_numeric', 'intval'], 'cache-files-ttl' => ['is_numeric', 'intval'], 'cache-files-maxsize' => [ static function ($val): bool { return Preg::isMatch('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', $val); }, static function ($val) { return $val; }, ], 'bin-compat' => [ static function ($val): bool { return in_array($val, ['auto', 'full', 'symlink']); }, static function ($val) { return $val; }, ], 'discard-changes' => [ static function ($val): bool { return in_array($val, ['stash', 'true', 'false', '1', '0'], true); }, static function ($val) { if ('stash' === $val) { return 'stash'; } return $val !== 'false' && (bool) $val; }, ], 'autoloader-suffix' => ['is_string', static function ($val) { return $val === 'null' ? null : $val; }], 'sort-packages' => [$booleanValidator, $booleanNormalizer], 'optimize-autoloader' => [$booleanValidator, $booleanNormalizer], 'classmap-authoritative' => [$booleanValidator, $booleanNormalizer], 'apcu-autoloader' => [$booleanValidator, $booleanNormalizer], 'prepend-autoloader' => [$booleanValidator, $booleanNormalizer], 'disable-tls' => [$booleanValidator, $booleanNormalizer], 'secure-http' => [$booleanValidator, $booleanNormalizer], 'cafile' => [ static function ($val): bool { return file_exists($val) && Filesystem::isReadable($val); }, static function ($val) { return $val === 'null' ? null : $val; }, ], 'capath' => [ static function ($val): bool { return is_dir($val) && Filesystem::isReadable($val); }, static function ($val) { return $val === 'null' ? null : $val; }, ], 'github-expose-hostname' => [$booleanValidator, $booleanNormalizer], 'htaccess-protect' => [$booleanValidator, $booleanNormalizer], 'lock' => [$booleanValidator, $booleanNormalizer], 'allow-plugins' => [$booleanValidator, $booleanNormalizer], 'platform-check' => [ static function ($val): bool { return in_array($val, ['php-only', 'true', 'false', '1', '0'], true); }, static function ($val) { if ('php-only' === $val) { return 'php-only'; } return $val !== 'false' && (bool) $val; }, ], 'use-parent-dir' => [ static function ($val): bool { return in_array($val, ['true', 'false', 'prompt'], true); }, static function ($val) { if ('prompt' === $val) { return 'prompt'; } return $val !== 'false' && (bool) $val; }, ], ]; $multiConfigValues = [ 'github-protocols' => [ static function ($vals) { if (!is_array($vals)) { return 'array expected'; } foreach ($vals as $val) { if (!in_array($val, ['git', 'https', 'ssh'])) { return 'valid protocols include: git, https, ssh'; } } return true; }, static function ($vals) { return $vals; }, ], 'github-domains' => [ static function ($vals) { if (!is_array($vals)) { return 'array expected'; } return true; }, static function ($vals) { return $vals; }, ], 'gitlab-domains' => [ static function ($vals) { if (!is_array($vals)) { return 'array expected'; } return true; }, static function ($vals) { return $vals; }, ], ]; if ($input->getOption('unset') && (isset($uniqueConfigValues[$settingKey]) || isset($multiConfigValues[$settingKey]))) { if ($settingKey === 'disable-tls' && $this->config->get('disable-tls')) { $this->getIO()->writeError('You are now running Composer with SSL/TLS protection enabled.'); } $this->configSource->removeConfigSetting($settingKey); return 0; } if (isset($uniqueConfigValues[$settingKey])) { $this->handleSingleValue($settingKey, $uniqueConfigValues[$settingKey], $values, 'addConfigSetting'); return 0; } if (isset($multiConfigValues[$settingKey])) { $this->handleMultiValue($settingKey, $multiConfigValues[$settingKey], $values, 'addConfigSetting'); return 0; } if (Preg::isMatch('/^preferred-install\.(.+)/', $settingKey, $matches)) { if ($input->getOption('unset')) { $this->configSource->removeConfigSetting($settingKey); return 0; } [$validator] = $uniqueConfigValues['preferred-install']; if (!$validator($values[0])) { throw new \RuntimeException('Invalid value for '.$settingKey.'. Should be one of: auto, source, or dist'); } $this->configSource->addConfigSetting($settingKey, $values[0]); return 0; } if (Preg::isMatch('{^allow-plugins\.([a-zA-Z0-9/*-]+)}', $settingKey, $matches)) { if ($input->getOption('unset')) { $this->configSource->removeConfigSetting($settingKey); return 0; } if (true !== $booleanValidator($values[0])) { throw new \RuntimeException(sprintf( '"%s" is an invalid value', $values[0] )); } $normalizedValue = $booleanNormalizer($values[0]); $this->configSource->addConfigSetting($settingKey, $normalizedValue); return 0; } $uniqueProps = [ 'name' => ['is_string', static function ($val) { return $val; }], 'type' => ['is_string', static function ($val) { return $val; }], 'description' => ['is_string', static function ($val) { return $val; }], 'homepage' => ['is_string', static function ($val) { return $val; }], 'version' => ['is_string', static function ($val) { return $val; }], 'minimum-stability' => [ static function ($val): bool { return isset(BasePackage::$stabilities[VersionParser::normalizeStability($val)]); }, static function ($val): string { return VersionParser::normalizeStability($val); }, ], 'prefer-stable' => [$booleanValidator, $booleanNormalizer], ]; $multiProps = [ 'keywords' => [ static function ($vals) { if (!is_array($vals)) { return 'array expected'; } return true; }, static function ($vals) { return $vals; }, ], 'license' => [ static function ($vals) { if (!is_array($vals)) { return 'array expected'; } return true; }, static function ($vals) { return $vals; }, ], ]; if ($input->getOption('global') && (isset($uniqueProps[$settingKey]) || isset($multiProps[$settingKey]) || strpos($settingKey, 'extra.') === 0)) { throw new \InvalidArgumentException('The ' . $settingKey . ' property can not be set in the global config.json file. Use `composer global config` to apply changes to the global composer.json'); } if ($input->getOption('unset') && (isset($uniqueProps[$settingKey]) || isset($multiProps[$settingKey]))) { $this->configSource->removeProperty($settingKey); return 0; } if (isset($uniqueProps[$settingKey])) { $this->handleSingleValue($settingKey, $uniqueProps[$settingKey], $values, 'addProperty'); return 0; } if (isset($multiProps[$settingKey])) { $this->handleMultiValue($settingKey, $multiProps[$settingKey], $values, 'addProperty'); return 0; } if (Preg::isMatch('/^repos?(?:itories)?\.(.+)/', $settingKey, $matches)) { if ($input->getOption('unset')) { $this->configSource->removeRepository($matches[1]); return 0; } if (2 === count($values)) { $this->configSource->addRepository($matches[1], [ 'type' => $values[0], 'url' => $values[1], ], $input->getOption('append')); return 0; } if (1 === count($values)) { $value = strtolower($values[0]); if (true === $booleanValidator($value)) { if (false === $booleanNormalizer($value)) { $this->configSource->addRepository($matches[1], false, $input->getOption('append')); return 0; } } else { $value = JsonFile::parseJson($values[0]); $this->configSource->addRepository($matches[1], $value, $input->getOption('append')); return 0; } } throw new \RuntimeException('You must pass the type and a url. Example: php composer.phar config repositories.foo vcs https://bar.com'); } if (Preg::isMatch('/^extra\.(.+)/', $settingKey, $matches)) { if ($input->getOption('unset')) { $this->configSource->removeProperty($settingKey); return 0; } $value = $values[0]; if ($input->getOption('json')) { $value = JsonFile::parseJson($value); if ($input->getOption('merge')) { $currentValue = $this->configFile->read(); $bits = explode('.', $settingKey); foreach ($bits as $bit) { $currentValue = $currentValue[$bit] ?? null; } if (is_array($currentValue)) { $value = array_merge($currentValue, $value); } } } $this->configSource->addProperty($settingKey, $value); return 0; } if (Preg::isMatch('/^suggest\.(.+)/', $settingKey, $matches)) { if ($input->getOption('unset')) { $this->configSource->removeProperty($settingKey); return 0; } $this->configSource->addProperty($settingKey, implode(' ', $values)); return 0; } if (in_array($settingKey, ['suggest', 'extra'], true) && $input->getOption('unset')) { $this->configSource->removeProperty($settingKey); return 0; } if (Preg::isMatch('/^platform\.(.+)/', $settingKey, $matches)) { if ($input->getOption('unset')) { $this->configSource->removeConfigSetting($settingKey); return 0; } $this->configSource->addConfigSetting($settingKey, $values[0] === 'false' ? false : $values[0]); return 0; } if ($settingKey === 'platform' && $input->getOption('unset')) { $this->configSource->removeConfigSetting($settingKey); return 0; } if (Preg::isMatch('/^(bitbucket-oauth|github-oauth|gitlab-oauth|gitlab-token|http-basic|bearer)\.(.+)/', $settingKey, $matches)) { if ($input->getOption('unset')) { $this->authConfigSource->removeConfigSetting($matches[1].'.'.$matches[2]); $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]); return 0; } if ($matches[1] === 'bitbucket-oauth') { if (2 !== count($values)) { throw new \RuntimeException('Expected two arguments (consumer-key, consumer-secret), got '.count($values)); } $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]); $this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], ['consumer-key' => $values[0], 'consumer-secret' => $values[1]]); } elseif ($matches[1] === 'gitlab-token' && 2 === count($values)) { $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]); $this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], ['username' => $values[0], 'token' => $values[1]]); } elseif (in_array($matches[1], ['github-oauth', 'gitlab-oauth', 'gitlab-token', 'bearer'], true)) { if (1 !== count($values)) { throw new \RuntimeException('Too many arguments, expected only one token'); } $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]); $this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], $values[0]); } elseif ($matches[1] === 'http-basic') { if (2 !== count($values)) { throw new \RuntimeException('Expected two arguments (username, password), got '.count($values)); } $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]); $this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], ['username' => $values[0], 'password' => $values[1]]); } return 0; } if (Preg::isMatch('/^scripts\.(.+)/', $settingKey, $matches)) { if ($input->getOption('unset')) { $this->configSource->removeProperty($settingKey); return 0; } $this->configSource->addProperty($settingKey, count($values) > 1 ? $values : $values[0]); return 0; } if ($input->getOption('unset')) { $this->configSource->removeProperty($settingKey); return 0; } throw new \InvalidArgumentException('Setting '.$settingKey.' does not exist or is not supported by this command'); } protected function handleSingleValue(string $key, array $callbacks, array $values, string $method): void { [$validator, $normalizer] = $callbacks; if (1 !== count($values)) { throw new \RuntimeException('You can only pass one value. Example: php composer.phar config process-timeout 300'); } if (true !== $validation = $validator($values[0])) { throw new \RuntimeException(sprintf( '"%s" is an invalid value'.($validation ? ' ('.$validation.')' : ''), $values[0] )); } $normalizedValue = $normalizer($values[0]); if ($key === 'disable-tls') { if (!$normalizedValue && $this->config->get('disable-tls')) { $this->getIO()->writeError('You are now running Composer with SSL/TLS protection enabled.'); } elseif ($normalizedValue && !$this->config->get('disable-tls')) { $this->getIO()->writeError('You are now running Composer with SSL/TLS protection disabled.'); } } call_user_func([$this->configSource, $method], $key, $normalizedValue); } protected function handleMultiValue(string $key, array $callbacks, array $values, string $method): void { [$validator, $normalizer] = $callbacks; if (true !== $validation = $validator($values)) { throw new \RuntimeException(sprintf( '%s is an invalid value'.($validation ? ' ('.$validation.')' : ''), json_encode($values) )); } call_user_func([$this->configSource, $method], $key, $normalizer($values)); } protected function listConfiguration(array $contents, array $rawContents, OutputInterface $output, ?string $k = null, bool $showSource = false): void { $origK = $k; $io = $this->getIO(); foreach ($contents as $key => $value) { if ($k === null && !in_array($key, ['config', 'repositories'])) { continue; } $rawVal = $rawContents[$key] ?? null; if (is_array($value) && (!is_numeric(key($value)) || ($key === 'repositories' && null === $k))) { $k .= Preg::replace('{^config\.}', '', $key . '.'); $this->listConfiguration($value, $rawVal, $output, $k, $showSource); $k = $origK; continue; } if (is_array($value)) { $value = array_map(static function ($val) { return is_array($val) ? json_encode($val) : $val; }, $value); $value = '['.implode(', ', $value).']'; } if (is_bool($value)) { $value = var_export($value, true); } $source = ''; if ($showSource) { $source = ' (' . $this->config->getSourceOfValue($k . $key) . ')'; } if (null !== $k && 0 === strpos($k, 'repositories')) { $link = 'https://getcomposer.org/doc/05-repositories.md'; } else { $id = Preg::replace('{\..*$}', '', $k === '' || $k === null ? (string) $key : $k); $id = Preg::replace('{[^a-z0-9]}i', '-', strtolower(trim($id))); $id = Preg::replace('{-+}', '-', $id); $link = 'https://getcomposer.org/doc/06-config.md#' . $id; } if (is_string($rawVal) && $rawVal !== $value) { $io->write('[' . $k . $key . '] ' . $rawVal . ' (' . $value . ')' . $source, true, IOInterface::QUIET); } else { $io->write('[' . $k . $key . '] ' . $value . '' . $source, true, IOInterface::QUIET); } } } } setName('create-project') ->setDescription('Creates new project from a package into given directory') ->setDefinition([ new InputArgument('package', InputArgument::OPTIONAL, 'Package name to be installed', null, $this->suggestAvailablePackage()), new InputArgument('directory', InputArgument::OPTIONAL, 'Directory where the files should be created'), new InputArgument('version', InputArgument::OPTIONAL, 'Version, will default to latest'), new InputOption('stability', 's', InputOption::VALUE_REQUIRED, 'Minimum-stability allowed (unless a version is specified).'), new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), new InputOption('prefer-dist', null, InputOption::VALUE_NONE, 'Forces installation from package dist (default behavior).'), new InputOption('prefer-install', null, InputOption::VALUE_REQUIRED, 'Forces installation from package dist|source|auto (auto chooses source for dev versions, dist for the rest).', null, $this->suggestPreferInstall()), new InputOption('repository', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Add custom repositories to look the package up, either by URL or using JSON arrays'), new InputOption('repository-url', null, InputOption::VALUE_REQUIRED, 'DEPRECATED: Use --repository instead.'), new InputOption('add-repository', null, InputOption::VALUE_NONE, 'Add the custom repository in the composer.json. If a lock file is present it will be deleted and an update will be run instead of install.'), new InputOption('dev', null, InputOption::VALUE_NONE, 'Enables installation of require-dev packages (enabled by default, only present for BC).'), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables installation of require-dev packages.'), new InputOption('no-custom-installers', null, InputOption::VALUE_NONE, 'DEPRECATED: Use no-plugins instead.'), new InputOption('no-scripts', null, InputOption::VALUE_NONE, 'Whether to prevent execution of all defined scripts in the root package.'), new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'), new InputOption('no-secure-http', null, InputOption::VALUE_NONE, 'Disable the secure-http config option temporarily while installing the root package. Use at your own risk. Using this flag is a bad idea.'), new InputOption('keep-vcs', null, InputOption::VALUE_NONE, 'Whether to prevent deleting the vcs folder.'), new InputOption('remove-vcs', null, InputOption::VALUE_NONE, 'Whether to force deletion of the vcs folder without prompting.'), new InputOption('no-install', null, InputOption::VALUE_NONE, 'Whether to skip installation of the package dependencies.'), new InputOption('no-audit', null, InputOption::VALUE_NONE, 'Whether to skip auditing of the installed package dependencies (can also be set via the COMPOSER_NO_AUDIT=1 env var).'), new InputOption('audit-format', null, InputOption::VALUE_REQUIRED, 'Audit output format. Must be "table", "plain", "json" or "summary".', Auditor::FORMAT_SUMMARY, Auditor::FORMATS), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), new InputOption('ask', null, InputOption::VALUE_NONE, 'Whether to ask for project directory.'), ]) ->setHelp( <<create-project command creates a new project from a given package into a new directory. If executed without params and in a directory with a composer.json file it installs the packages for the current project. You can use this command to bootstrap new projects or setup a clean version-controlled installation for developers of your project. php composer.phar create-project vendor/project target-directory [version] You can also specify the version with the package name using = or : as separator. php composer.phar create-project vendor/project:version target-directory To install unstable packages, either specify the version you want, or use the --stability=dev (where dev can be one of RC, beta, alpha or dev). To setup a developer workable version you should create the project using the source controlled code by appending the '--prefer-source' flag. To install a package from another repository than the default one you can pass the '--repository=https://myrepository.org' flag. Read more at https://getcomposer.org/doc/03-cli.md#create-project EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { $config = Factory::createConfig(); $io = $this->getIO(); [$preferSource, $preferDist] = $this->getPreferredInstallOptions($config, $input, true); if ($input->getOption('dev')) { $io->writeError('You are using the deprecated option "dev". Dev packages are installed by default now.'); } if ($input->getOption('no-custom-installers')) { $io->writeError('You are using the deprecated option "no-custom-installers". Use "no-plugins" instead.'); $input->setOption('no-plugins', true); } if ($input->isInteractive() && $input->getOption('ask')) { $package = $input->getArgument('package'); if (null === $package) { throw new \RuntimeException('Not enough arguments (missing: "package").'); } $parts = explode("/", strtolower($package), 2); $input->setArgument('directory', $io->ask('New project directory ['.array_pop($parts).']: ')); } return $this->installProject( $io, $config, $input, $input->getArgument('package'), $input->getArgument('directory'), $input->getArgument('version'), $input->getOption('stability'), $preferSource, $preferDist, !$input->getOption('no-dev'), \count($input->getOption('repository')) > 0 ? $input->getOption('repository') : $input->getOption('repository-url'), $input->getOption('no-plugins'), $input->getOption('no-scripts'), $input->getOption('no-progress'), $input->getOption('no-install'), $this->getPlatformRequirementFilter($input), !$input->getOption('no-secure-http'), $input->getOption('add-repository') ); } public function installProject(IOInterface $io, Config $config, InputInterface $input, ?string $packageName = null, ?string $directory = null, ?string $packageVersion = null, ?string $stability = 'stable', bool $preferSource = false, bool $preferDist = false, bool $installDevPackages = false, $repositories = null, bool $disablePlugins = false, bool $disableScripts = false, bool $noProgress = false, bool $noInstall = false, ?PlatformRequirementFilterInterface $platformRequirementFilter = null, bool $secureHttp = true, bool $addRepository = false): int { $oldCwd = Platform::getCwd(); if ($repositories !== null && !is_array($repositories)) { $repositories = (array) $repositories; } $platformRequirementFilter = $platformRequirementFilter ?? PlatformRequirementFilterFactory::ignoreNothing(); $io->loadConfiguration($config); $this->suggestedPackagesReporter = new SuggestedPackagesReporter($io); if ($packageName !== null) { $installedFromVcs = $this->installRootPackage($io, $config, $packageName, $platformRequirementFilter, $directory, $packageVersion, $stability, $preferSource, $preferDist, $installDevPackages, $repositories, $disablePlugins, $disableScripts, $noProgress, $secureHttp); } else { $installedFromVcs = false; } if ($repositories !== null && $addRepository && is_file('composer.lock')) { unlink('composer.lock'); } $composer = Factory::create($io, null, $disablePlugins, $disableScripts); if ($repositories !== null && $addRepository) { foreach ($repositories as $index => $repo) { $repoConfig = RepositoryFactory::configFromString($io, $composer->getConfig(), $repo, true); $composerJsonRepositoriesConfig = $composer->getConfig()->getRepositories(); $name = RepositoryFactory::generateRepositoryName($index, $repoConfig, $composerJsonRepositoriesConfig); $configSource = new JsonConfigSource(new JsonFile('composer.json')); if ( (isset($repoConfig['packagist']) && $repoConfig === ['packagist' => false]) || (isset($repoConfig['packagist.org']) && $repoConfig === ['packagist.org' => false]) ) { $configSource->addRepository('packagist.org', false); } else { $configSource->addRepository($name, $repoConfig, false); } $composer = Factory::create($io, null, $disablePlugins); } } $process = $composer->getLoop()->getProcessExecutor(); $fs = new Filesystem($process); $composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_ROOT_PACKAGE_INSTALL, $installDevPackages); $config = $composer->getConfig(); [$preferSource, $preferDist] = $this->getPreferredInstallOptions($config, $input); if ($noInstall === false) { $composer->getInstallationManager()->setOutputProgress(!$noProgress); $installer = Installer::create($io, $composer); $installer->setPreferSource($preferSource) ->setPreferDist($preferDist) ->setDevMode($installDevPackages) ->setPlatformRequirementFilter($platformRequirementFilter) ->setSuggestedPackagesReporter($this->suggestedPackagesReporter) ->setOptimizeAutoloader($config->get('optimize-autoloader')) ->setClassMapAuthoritative($config->get('classmap-authoritative')) ->setApcuAutoloader($config->get('apcu-autoloader')) ->setAudit(!$input->getOption('no-audit')) ->setAuditFormat($this->getAuditFormat($input)); if (!$composer->getLocker()->isLocked()) { $installer->setUpdate(true); } if ($disablePlugins) { $installer->disablePlugins(); } try { $status = $installer->run(); if (0 !== $status) { return $status; } } catch (PluginBlockedException $e) { $io->writeError('Hint: To allow running the config command recommended below before dependencies are installed, run create-project with --no-install.'); $io->writeError('You can then cd into '.getcwd().', configure allow-plugins, and finally run a composer install to complete the process.'); throw $e; } } $hasVcs = $installedFromVcs; if ( !$input->getOption('keep-vcs') && $installedFromVcs && ( $input->getOption('remove-vcs') || !$io->isInteractive() || $io->askConfirmation('Do you want to remove the existing VCS (.git, .svn..) history? [Y,n]? ') ) ) { $finder = new Finder(); $finder->depth(0)->directories()->in(Platform::getCwd())->ignoreVCS(false)->ignoreDotFiles(false); foreach (['.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg', '.fslckout', '_FOSSIL_'] as $vcsName) { $finder->name($vcsName); } try { $dirs = iterator_to_array($finder); unset($finder); foreach ($dirs as $dir) { if (!$fs->removeDirectory((string) $dir)) { throw new \RuntimeException('Could not remove '.$dir); } } } catch (\Exception $e) { $io->writeError('An error occurred while removing the VCS metadata: '.$e->getMessage().''); } $hasVcs = false; } if (!$hasVcs) { $package = $composer->getPackage(); $configSource = new JsonConfigSource(new JsonFile('composer.json')); foreach (BasePackage::$supportedLinkTypes as $type => $meta) { foreach ($package->{'get'.$meta['method']}() as $link) { if ($link->getPrettyConstraint() === 'self.version') { $configSource->addLink($type, $link->getTarget(), $package->getPrettyVersion()); } } } } $composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_CREATE_PROJECT_CMD, $installDevPackages); chdir($oldCwd); $vendorComposerDir = $config->get('vendor-dir').'/composer'; if (is_dir($vendorComposerDir) && $fs->isDirEmpty($vendorComposerDir)) { Silencer::call('rmdir', $vendorComposerDir); $vendorDir = $config->get('vendor-dir'); if (is_dir($vendorDir) && $fs->isDirEmpty($vendorDir)) { Silencer::call('rmdir', $vendorDir); } } return 0; } protected function installRootPackage(IOInterface $io, Config $config, string $packageName, PlatformRequirementFilterInterface $platformRequirementFilter, ?string $directory = null, ?string $packageVersion = null, ?string $stability = 'stable', bool $preferSource = false, bool $preferDist = false, bool $installDevPackages = false, ?array $repositories = null, bool $disablePlugins = false, bool $disableScripts = false, bool $noProgress = false, bool $secureHttp = true): bool { if (!$secureHttp) { $config->merge(['config' => ['secure-http' => false]], Config::SOURCE_COMMAND); } $parser = new VersionParser(); $requirements = $parser->parseNameVersionPairs([$packageName]); $name = strtolower($requirements[0]['name']); if (!$packageVersion && isset($requirements[0]['version'])) { $packageVersion = $requirements[0]['version']; } if (null === $directory) { $parts = explode("/", $name, 2); $directory = Platform::getCwd() . DIRECTORY_SEPARATOR . array_pop($parts); } $process = new ProcessExecutor($io); $fs = new Filesystem($process); if (!$fs->isAbsolutePath($directory)) { $directory = Platform::getCwd() . DIRECTORY_SEPARATOR . $directory; } $io->writeError('Creating a "' . $packageName . '" project at "' . $fs->findShortestPath(Platform::getCwd(), $directory, true) . '"'); if (file_exists($directory)) { if (!is_dir($directory)) { throw new \InvalidArgumentException('Cannot create project directory at "'.$directory.'", it exists as a file.'); } if (!$fs->isDirEmpty($directory)) { throw new \InvalidArgumentException('Project directory "'.$directory.'" is not empty.'); } } if (null === $stability) { if (null === $packageVersion) { $stability = 'stable'; } elseif (Preg::isMatch('{^[^,\s]*?@('.implode('|', array_keys(BasePackage::$stabilities)).')$}i', $packageVersion, $match)) { $stability = $match[1]; } else { $stability = VersionParser::parseStability($packageVersion); } } $stability = VersionParser::normalizeStability($stability); if (!isset(BasePackage::$stabilities[$stability])) { throw new \InvalidArgumentException('Invalid stability provided ('.$stability.'), must be one of: '.implode(', ', array_keys(BasePackage::$stabilities))); } $composer = Factory::create($io, $config->all(), $disablePlugins, $disableScripts); $config = $composer->getConfig(); $rm = $composer->getRepositoryManager(); $repositorySet = new RepositorySet($stability); if (null === $repositories) { $repositorySet->addRepository(new CompositeRepository(RepositoryFactory::defaultRepos($io, $config, $rm))); } else { foreach ($repositories as $repo) { $repoConfig = RepositoryFactory::configFromString($io, $config, $repo, true); if ( (isset($repoConfig['packagist']) && $repoConfig === ['packagist' => false]) || (isset($repoConfig['packagist.org']) && $repoConfig === ['packagist.org' => false]) ) { continue; } $repositorySet->addRepository(RepositoryFactory::createRepo($io, $config, $repoConfig, $rm)); } } $platformOverrides = $config->get('platform'); $platformRepo = new PlatformRepository([], $platformOverrides); $versionSelector = new VersionSelector($repositorySet, $platformRepo); $package = $versionSelector->findBestCandidate($name, $packageVersion, $stability, $platformRequirementFilter, 0, $io); if (!$package) { $errorMessage = "Could not find package $name with " . ($packageVersion ? "version $packageVersion" : "stability $stability"); if (!($platformRequirementFilter instanceof IgnoreAllPlatformRequirementFilter) && $versionSelector->findBestCandidate($name, $packageVersion, $stability, PlatformRequirementFilterFactory::ignoreAll())) { throw new \InvalidArgumentException($errorMessage .' in a version installable using your PHP version, PHP extensions and Composer version.'); } throw new \InvalidArgumentException($errorMessage .'.'); } @mkdir($directory, 0777, true); if (false !== ($realDir = realpath($directory))) { $signalHandler = SignalHandler::create([SignalHandler::SIGINT, SignalHandler::SIGTERM, SignalHandler::SIGHUP], function (string $signal, SignalHandler $handler) use ($realDir) { $this->getIO()->writeError('Received '.$signal.', aborting', true, IOInterface::DEBUG); $fs = new Filesystem(); $fs->removeDirectory($realDir); $handler->exitWithLastSignal(); }); } if ($package instanceof AliasPackage && $package->getPrettyVersion() === VersionParser::DEFAULT_BRANCH_ALIAS) { $package = $package->getAliasOf(); } $io->writeError('Installing ' . $package->getName() . ' (' . $package->getFullPrettyVersion(false) . ')'); if ($disablePlugins) { $io->writeError('Plugins have been disabled.'); } if ($package instanceof AliasPackage) { $package = $package->getAliasOf(); } $dm = $composer->getDownloadManager(); $dm->setPreferSource($preferSource) ->setPreferDist($preferDist); $projectInstaller = new ProjectInstaller($directory, $dm, $fs); $im = $composer->getInstallationManager(); $im->setOutputProgress(!$noProgress); $im->addInstaller($projectInstaller); $im->execute(new InstalledArrayRepository(), [new InstallOperation($package)]); $im->notifyInstalls($io); $this->suggestedPackagesReporter->addSuggestionsFromPackage($package); $installedFromVcs = 'source' === $package->getInstallationSource(); $io->writeError('Created project in ' . $directory . ''); chdir($directory); Platform::putEnv('COMPOSER_ROOT_VERSION', $package->getPrettyVersion()); if (isset($signalHandler)) { $signalHandler->unregister(); } return $installedFromVcs; } } setName('depends') ->setAliases(['why']) ->setDescription('Shows which packages cause the given package to be installed') ->setDefinition([ new InputArgument(self::ARGUMENT_PACKAGE, InputArgument::REQUIRED, 'Package to inspect', null, $this->suggestInstalledPackage(true, true)), new InputOption(self::OPTION_RECURSIVE, 'r', InputOption::VALUE_NONE, 'Recursively resolves up to the root package'), new InputOption(self::OPTION_TREE, 't', InputOption::VALUE_NONE, 'Prints the results as a nested tree'), new InputOption('locked', null, InputOption::VALUE_NONE, 'Read dependency information from composer.lock'), ]) ->setHelp( <<php composer.phar depends composer/composer Read more at https://getcomposer.org/doc/03-cli.md#depends-why- EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { return parent::doExecute($input, $output); } } setName('diagnose') ->setDescription('Diagnoses the system to identify common errors') ->setHelp( <<diagnose command checks common errors to help debugging problems. The process exit code will be 1 in case of warnings and 2 for errors. Read more at https://getcomposer.org/doc/03-cli.md#diagnose EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $composer = $this->tryComposer(); $io = $this->getIO(); if ($composer) { $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'diagnose', $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); $io->write('Checking composer.json: ', false); $this->outputResult($this->checkComposerSchema()); $this->process = $composer->getLoop()->getProcessExecutor() ?? new ProcessExecutor($io); } else { $this->process = new ProcessExecutor($io); } if ($composer) { $config = $composer->getConfig(); } else { $config = Factory::createConfig(); } $config->merge(['config' => ['secure-http' => false]], Config::SOURCE_COMMAND); $config->prohibitUrlByConfig('http://repo.packagist.org', new NullIO); $this->httpDownloader = Factory::createHttpDownloader($io, $config); $io->write('Checking platform settings: ', false); $this->outputResult($this->checkPlatform()); $io->write('Checking git settings: ', false); $this->outputResult($this->checkGit()); $io->write('Checking http connectivity to packagist: ', false); $this->outputResult($this->checkHttp('http', $config)); $io->write('Checking https connectivity to packagist: ', false); $this->outputResult($this->checkHttp('https', $config)); $opts = stream_context_get_options(StreamContextFactory::getContext('http://example.org')); if (!empty($opts['http']['proxy'])) { $io->write('Checking HTTP proxy: ', false); $this->outputResult($this->checkHttpProxy()); } if (count($oauth = $config->get('github-oauth')) > 0) { foreach ($oauth as $domain => $token) { $io->write('Checking '.$domain.' oauth access: ', false); $this->outputResult($this->checkGithubOauth($domain, $token)); } } else { $io->write('Checking github.com rate limit: ', false); try { $rate = $this->getGithubRateLimit('github.com'); if (!is_array($rate)) { $this->outputResult($rate); } elseif (10 > $rate['remaining']) { $io->write('WARNING'); $io->write(sprintf( 'Github has a rate limit on their API. ' . 'You currently have %u ' . 'out of %u requests left.' . PHP_EOL . 'See https://developer.github.com/v3/#rate-limiting and also' . PHP_EOL . ' https://getcomposer.org/doc/articles/troubleshooting.md#api-rate-limit-and-oauth-tokens', $rate['remaining'], $rate['limit'] )); } else { $this->outputResult(true); } } catch (\Exception $e) { if ($e instanceof TransportException && $e->getCode() === 401) { $this->outputResult('The oauth token for github.com seems invalid, run "composer config --global --unset github-oauth.github.com" to remove it'); } else { $this->outputResult($e); } } } $io->write('Checking disk free space: ', false); $this->outputResult($this->checkDiskSpace($config)); if (strpos(__FILE__, 'phar:') === 0) { $io->write('Checking pubkeys: ', false); $this->outputResult($this->checkPubKeys($config)); $io->write('Checking composer version: ', false); $this->outputResult($this->checkVersion($config)); } $io->write(sprintf('Composer version: %s', Composer::getVersion())); $platformOverrides = $config->get('platform') ?: []; $platformRepo = new PlatformRepository([], $platformOverrides); $phpPkg = $platformRepo->findPackage('php', '*'); $phpVersion = $phpPkg->getPrettyVersion(); if ($phpPkg instanceof CompletePackageInterface && false !== strpos($phpPkg->getDescription(), 'overridden')) { $phpVersion .= ' - ' . $phpPkg->getDescription(); } $io->write(sprintf('PHP version: %s', $phpVersion)); if (defined('PHP_BINARY')) { $io->write(sprintf('PHP binary path: %s', PHP_BINARY)); } $io->write('OpenSSL version: ' . (defined('OPENSSL_VERSION_TEXT') ? ''.OPENSSL_VERSION_TEXT.'' : 'missing')); $io->write('cURL version: ' . $this->getCurlVersion()); $finder = new ExecutableFinder; $hasSystemUnzip = (bool) $finder->find('unzip'); $bin7zip = ''; if ($hasSystem7zip = (bool) $finder->find('7z', null, ['C:\Program Files\7-Zip'])) { $bin7zip = '7z'; } if (!Platform::isWindows() && !$hasSystem7zip && $hasSystem7zip = (bool) $finder->find('7zz')) { $bin7zip = '7zz'; } $io->write( 'zip: ' . (extension_loaded('zip') ? 'extension present' : 'extension not loaded') . ', ' . ($hasSystemUnzip ? 'unzip present' : 'unzip not available') . ', ' . ($hasSystem7zip ? '7-Zip present ('.$bin7zip.')' : '7-Zip not available') . (($hasSystem7zip || $hasSystemUnzip) && !function_exists('proc_open') ? ', proc_open is disabled or not present, unzip/7-z will not be usable' : '') ); return $this->exitCode; } private function checkComposerSchema() { $validator = new ConfigValidator($this->getIO()); [$errors, , $warnings] = $validator->validate(Factory::getComposerFile()); if ($errors || $warnings) { $messages = [ 'error' => $errors, 'warning' => $warnings, ]; $output = ''; foreach ($messages as $style => $msgs) { foreach ($msgs as $msg) { $output .= '<' . $style . '>' . $msg . '' . PHP_EOL; } } return rtrim($output); } return true; } private function checkGit(): string { if (!function_exists('proc_open')) { return 'proc_open is not available, git cannot be used'; } $this->process->execute('git config color.ui', $output); if (strtolower(trim($output)) === 'always') { return 'Your git color.ui setting is set to always, this is known to create issues. Use "git config --global color.ui true" to set it correctly.'; } $gitVersion = Git::getVersion($this->process); if (null === $gitVersion) { return 'No git process found'; } if (version_compare('2.24.0', $gitVersion, '>')) { return 'Your git version ('.$gitVersion.') is too old and possibly will cause issues. Please upgrade to git 2.24 or above'; } return 'OK git version '.$gitVersion.''; } private function checkHttp(string $proto, Config $config) { $result = $this->checkConnectivity(); if ($result !== true) { return $result; } $result = []; if ($proto === 'https' && $config->get('disable-tls') === true) { $tlsWarning = 'Composer is configured to disable SSL/TLS protection. This will leave remote HTTPS requests vulnerable to Man-In-The-Middle attacks.'; } try { $this->httpDownloader->get($proto . '://repo.packagist.org/packages.json'); } catch (TransportException $e) { $hints = HttpDownloader::getExceptionHints($e); if (null !== $hints && count($hints) > 0) { foreach ($hints as $hint) { $result[] = $hint; } } $result[] = '[' . get_class($e) . '] ' . $e->getMessage() . ''; } if (isset($tlsWarning)) { $result[] = $tlsWarning; } if (count($result) > 0) { return $result; } return true; } private function checkHttpProxy() { $result = $this->checkConnectivity(); if ($result !== true) { return $result; } $protocol = extension_loaded('openssl') ? 'https' : 'http'; try { $json = $this->httpDownloader->get($protocol . '://repo.packagist.org/packages.json')->decodeJson(); $hash = reset($json['provider-includes']); $hash = $hash['sha256']; $path = str_replace('%hash%', $hash, key($json['provider-includes'])); $provider = $this->httpDownloader->get($protocol . '://repo.packagist.org/'.$path)->getBody(); if (hash('sha256', $provider) !== $hash) { return 'It seems that your proxy is modifying http traffic on the fly'; } } catch (\Exception $e) { return $e; } return true; } private function checkGithubOauth(string $domain, string $token) { $result = $this->checkConnectivity(); if ($result !== true) { return $result; } $this->getIO()->setAuthentication($domain, $token, 'x-oauth-basic'); try { $url = $domain === 'github.com' ? 'https://api.'.$domain.'/' : 'https://'.$domain.'/api/v3/'; $this->httpDownloader->get($url, [ 'retry-auth-failure' => false, ]); return true; } catch (\Exception $e) { if ($e instanceof TransportException && $e->getCode() === 401) { return 'The oauth token for '.$domain.' seems invalid, run "composer config --global --unset github-oauth.'.$domain.'" to remove it'; } return $e; } } private function getGithubRateLimit(string $domain, ?string $token = null) { $result = $this->checkConnectivity(); if ($result !== true) { return $result; } if ($token) { $this->getIO()->setAuthentication($domain, $token, 'x-oauth-basic'); } $url = $domain === 'github.com' ? 'https://api.'.$domain.'/rate_limit' : 'https://'.$domain.'/api/rate_limit'; $data = $this->httpDownloader->get($url, ['retry-auth-failure' => false])->decodeJson(); return $data['resources']['core']; } private function checkDiskSpace(Config $config) { if (!function_exists('disk_free_space')) { return true; } $minSpaceFree = 1024 * 1024; if ((($df = @disk_free_space($dir = $config->get('home'))) !== false && $df < $minSpaceFree) || (($df = @disk_free_space($dir = $config->get('vendor-dir'))) !== false && $df < $minSpaceFree) ) { return 'The disk hosting '.$dir.' is full'; } return true; } private function checkPubKeys(Config $config) { $home = $config->get('home'); $errors = []; $io = $this->getIO(); if (file_exists($home.'/keys.tags.pub') && file_exists($home.'/keys.dev.pub')) { $io->write(''); } if (file_exists($home.'/keys.tags.pub')) { $io->write('Tags Public Key Fingerprint: ' . Keys::fingerprint($home.'/keys.tags.pub')); } else { $errors[] = 'Missing pubkey for tags verification'; } if (file_exists($home.'/keys.dev.pub')) { $io->write('Dev Public Key Fingerprint: ' . Keys::fingerprint($home.'/keys.dev.pub')); } else { $errors[] = 'Missing pubkey for dev verification'; } if ($errors) { $errors[] = 'Run composer self-update --update-keys to set them up'; } return $errors ?: true; } private function checkVersion(Config $config) { $result = $this->checkConnectivity(); if ($result !== true) { return $result; } $versionsUtil = new Versions($config, $this->httpDownloader); try { $latest = $versionsUtil->getLatest(); } catch (\Exception $e) { return $e; } if (Composer::VERSION !== $latest['version'] && Composer::VERSION !== '@package_version@') { return 'You are not running the latest '.$versionsUtil->getChannel().' version, run `composer self-update` to update ('.Composer::VERSION.' => '.$latest['version'].')'; } return true; } private function getCurlVersion(): string { if (extension_loaded('curl')) { if (!HttpDownloader::isCurlEnabled()) { return 'disabled via disable_functions, using php streams fallback, which reduces performance'; } $version = curl_version(); return ''.$version['version'].' '. 'libz '.(!empty($version['libz_version']) ? $version['libz_version'] : 'missing').' '. 'ssl '.($version['ssl_version'] ?? 'missing').''; } return 'missing, using php streams fallback, which reduces performance'; } private function outputResult($result): void { $io = $this->getIO(); if (true === $result) { $io->write('OK'); return; } $hadError = false; $hadWarning = false; if ($result instanceof \Exception) { $result = '['.get_class($result).'] '.$result->getMessage().''; } if (!$result) { $hadError = true; } else { if (!is_array($result)) { $result = [$result]; } foreach ($result as $message) { if (false !== strpos($message, '')) { $hadError = true; } elseif (false !== strpos($message, '')) { $hadWarning = true; } } } if ($hadError) { $io->write('FAIL'); $this->exitCode = max($this->exitCode, 2); } elseif ($hadWarning) { $io->write('WARNING'); $this->exitCode = max($this->exitCode, 1); } if ($result) { foreach ($result as $message) { $io->write($message); } } } private function checkPlatform() { $output = ''; $out = static function ($msg, $style) use (&$output): void { $output .= '<'.$style.'>'.$msg.''.PHP_EOL; }; $errors = []; $warnings = []; $displayIniMessage = false; $iniMessage = PHP_EOL.PHP_EOL.IniHelper::getMessage(); $iniMessage .= PHP_EOL.'If you can not modify the ini file, you can also run `php -d option=value` to modify ini values on the fly. You can use -d multiple times.'; if (!function_exists('json_decode')) { $errors['json'] = true; } if (!extension_loaded('Phar')) { $errors['phar'] = true; } if (!extension_loaded('filter')) { $errors['filter'] = true; } if (!extension_loaded('hash')) { $errors['hash'] = true; } if (!extension_loaded('iconv') && !extension_loaded('mbstring')) { $errors['iconv_mbstring'] = true; } if (!filter_var(ini_get('allow_url_fopen'), FILTER_VALIDATE_BOOLEAN)) { $errors['allow_url_fopen'] = true; } if (extension_loaded('ionCube Loader') && ioncube_loader_iversion() < 40009) { $errors['ioncube'] = ioncube_loader_version(); } if (PHP_VERSION_ID < 70205) { $errors['php'] = PHP_VERSION; } if (!extension_loaded('openssl')) { $errors['openssl'] = true; } if (extension_loaded('openssl') && OPENSSL_VERSION_NUMBER < 0x1000100f) { $warnings['openssl_version'] = true; } if (!defined('HHVM_VERSION') && !extension_loaded('apcu') && filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN)) { $warnings['apc_cli'] = true; } if (!extension_loaded('zlib')) { $warnings['zlib'] = true; } ob_start(); phpinfo(INFO_GENERAL); $phpinfo = ob_get_clean(); if (Preg::isMatch('{Configure Command(?: *| *=> *)(.*?)(?:|$)}m', $phpinfo, $match)) { $configure = $match[1]; if (false !== strpos($configure, '--enable-sigchild')) { $warnings['sigchild'] = true; } if (false !== strpos($configure, '--with-curlwrappers')) { $warnings['curlwrappers'] = true; } } if (filter_var(ini_get('xdebug.profiler_enabled'), FILTER_VALIDATE_BOOLEAN)) { $warnings['xdebug_profile'] = true; } elseif (XdebugHandler::isXdebugActive()) { $warnings['xdebug_loaded'] = true; } if (defined('PHP_WINDOWS_VERSION_BUILD') && (version_compare(PHP_VERSION, '7.2.23', '<') || (version_compare(PHP_VERSION, '7.3.0', '>=') && version_compare(PHP_VERSION, '7.3.10', '<')))) { $warnings['onedrive'] = PHP_VERSION; } if (!empty($errors)) { foreach ($errors as $error => $current) { switch ($error) { case 'json': $text = PHP_EOL."The json extension is missing.".PHP_EOL; $text .= "Install it or recompile php without --disable-json"; break; case 'phar': $text = PHP_EOL."The phar extension is missing.".PHP_EOL; $text .= "Install it or recompile php without --disable-phar"; break; case 'filter': $text = PHP_EOL."The filter extension is missing.".PHP_EOL; $text .= "Install it or recompile php without --disable-filter"; break; case 'hash': $text = PHP_EOL."The hash extension is missing.".PHP_EOL; $text .= "Install it or recompile php without --disable-hash"; break; case 'iconv_mbstring': $text = PHP_EOL."The iconv OR mbstring extension is required and both are missing.".PHP_EOL; $text .= "Install either of them or recompile php without --disable-iconv"; break; case 'php': $text = PHP_EOL."Your PHP ({$current}) is too old, you must upgrade to PHP 7.2.5 or higher."; break; case 'allow_url_fopen': $text = PHP_EOL."The allow_url_fopen setting is incorrect.".PHP_EOL; $text .= "Add the following to the end of your `php.ini`:".PHP_EOL; $text .= " allow_url_fopen = On"; $displayIniMessage = true; break; case 'ioncube': $text = PHP_EOL."Your ionCube Loader extension ($current) is incompatible with Phar files.".PHP_EOL; $text .= "Upgrade to ionCube 4.0.9 or higher or remove this line (path may be different) from your `php.ini` to disable it:".PHP_EOL; $text .= " zend_extension = /usr/lib/php5/20090626+lfs/ioncube_loader_lin_5.3.so"; $displayIniMessage = true; break; case 'openssl': $text = PHP_EOL."The openssl extension is missing, which means that secure HTTPS transfers are impossible.".PHP_EOL; $text .= "If possible you should enable it or recompile php with --with-openssl"; break; default: throw new \InvalidArgumentException(sprintf("DiagnoseCommand: Unknown error type \"%s\". Please report at https://github.com/composer/composer/issues/new.", $error)); } $out($text, 'error'); } $output .= PHP_EOL; } if (!empty($warnings)) { foreach ($warnings as $warning => $current) { switch ($warning) { case 'apc_cli': $text = "The apc.enable_cli setting is incorrect.".PHP_EOL; $text .= "Add the following to the end of your `php.ini`:".PHP_EOL; $text .= " apc.enable_cli = Off"; $displayIniMessage = true; break; case 'zlib': $text = 'The zlib extension is not loaded, this can slow down Composer a lot.'.PHP_EOL; $text .= 'If possible, enable it or recompile php with --with-zlib'.PHP_EOL; $displayIniMessage = true; break; case 'sigchild': $text = "PHP was compiled with --enable-sigchild which can cause issues on some platforms.".PHP_EOL; $text .= "Recompile it without this flag if possible, see also:".PHP_EOL; $text .= " https://bugs.php.net/bug.php?id=22999"; break; case 'curlwrappers': $text = "PHP was compiled with --with-curlwrappers which will cause issues with HTTP authentication and GitHub.".PHP_EOL; $text .= " Recompile it without this flag if possible"; break; case 'openssl_version': $opensslVersion = strstr(trim(strstr(OPENSSL_VERSION_TEXT, ' ')), ' ', true); $opensslVersion = $opensslVersion ?: OPENSSL_VERSION_TEXT; $text = "The OpenSSL library ({$opensslVersion}) used by PHP does not support TLSv1.2 or TLSv1.1.".PHP_EOL; $text .= "If possible you should upgrade OpenSSL to version 1.0.1 or above."; break; case 'xdebug_loaded': $text = "The xdebug extension is loaded, this can slow down Composer a little.".PHP_EOL; $text .= " Disabling it when using Composer is recommended."; break; case 'xdebug_profile': $text = "The xdebug.profiler_enabled setting is enabled, this can slow down Composer a lot.".PHP_EOL; $text .= "Add the following to the end of your `php.ini` to disable it:".PHP_EOL; $text .= " xdebug.profiler_enabled = 0"; $displayIniMessage = true; break; case 'onedrive': $text = "The Windows OneDrive folder is not supported on PHP versions below 7.2.23 and 7.3.10.".PHP_EOL; $text .= "Upgrade your PHP ({$current}) to use this location with Composer.".PHP_EOL; break; default: throw new \InvalidArgumentException(sprintf("DiagnoseCommand: Unknown warning type \"%s\". Please report at https://github.com/composer/composer/issues/new.", $warning)); } $out($text, 'comment'); } } if ($displayIniMessage) { $out($iniMessage, 'comment'); } return !$warnings && !$errors ? true : $output; } private function checkConnectivity() { if (!ini_get('allow_url_fopen')) { return 'Skipped because allow_url_fopen is missing.'; } return true; } } setName('dump-autoload') ->setAliases(['dumpautoload']) ->setDescription('Dumps the autoloader') ->setDefinition([ new InputOption('optimize', 'o', InputOption::VALUE_NONE, 'Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production.'), new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize`.'), new InputOption('apcu', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'), new InputOption('apcu-prefix', null, InputOption::VALUE_REQUIRED, 'Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu'), new InputOption('dev', null, InputOption::VALUE_NONE, 'Enables autoload-dev rules. Composer will by default infer this automatically according to the last install or update --no-dev state.'), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables autoload-dev rules. Composer will by default infer this automatically according to the last install or update --no-dev state.'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), new InputOption('strict-psr', null, InputOption::VALUE_NONE, 'Return a failed status code (1) if PSR-4 or PSR-0 mapping errors are present. Requires --optimize to work.'), ]) ->setHelp( <<php composer.phar dump-autoload Read more at https://getcomposer.org/doc/03-cli.md#dump-autoload-dumpautoload- EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $composer = $this->requireComposer(); $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'dump-autoload', $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); $installationManager = $composer->getInstallationManager(); $localRepo = $composer->getRepositoryManager()->getLocalRepository(); $package = $composer->getPackage(); $config = $composer->getConfig(); $optimize = $input->getOption('optimize') || $config->get('optimize-autoloader'); $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative'); $apcuPrefix = $input->getOption('apcu-prefix'); $apcu = $apcuPrefix !== null || $input->getOption('apcu') || $config->get('apcu-autoloader'); if ($input->getOption('strict-psr') && !$optimize) { throw new \InvalidArgumentException('--strict-psr mode only works with optimized autoloader, use --optimize if you want a strict return value.'); } if ($authoritative) { $this->getIO()->write('Generating optimized autoload files (authoritative)'); } elseif ($optimize) { $this->getIO()->write('Generating optimized autoload files'); } else { $this->getIO()->write('Generating autoload files'); } $generator = $composer->getAutoloadGenerator(); if ($input->getOption('no-dev')) { $generator->setDevMode(false); } if ($input->getOption('dev')) { if ($input->getOption('no-dev')) { throw new \InvalidArgumentException('You can not use both --no-dev and --dev as they conflict with each other.'); } $generator->setDevMode(true); } $generator->setClassMapAuthoritative($authoritative); $generator->setRunScripts(true); $generator->setApcu($apcu, $apcuPrefix); $generator->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input)); $classMap = $generator->dump($config, $localRepo, $package, $installationManager, 'composer', $optimize); $numberOfClasses = count($classMap); if ($authoritative) { $this->getIO()->write('Generated optimized autoload files (authoritative) containing '. $numberOfClasses .' classes'); } elseif ($optimize) { $this->getIO()->write('Generated optimized autoload files containing '. $numberOfClasses .' classes'); } else { $this->getIO()->write('Generated autoload files'); } if ($input->getOption('strict-psr') && count($classMap->getPsrViolations()) > 0) { return 1; } return 0; } } setName('exec') ->setDescription('Executes a vendored binary/script') ->setDefinition([ new InputOption('list', 'l', InputOption::VALUE_NONE), new InputArgument('binary', InputArgument::OPTIONAL, 'The binary to run, e.g. phpunit', null, function () { return $this->getBinaries(false); }), new InputArgument( 'args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Arguments to pass to the binary. Use -- to separate from composer arguments' ), ]) ->setHelp( <<requireComposer(); if ($input->getOption('list') || null === $input->getArgument('binary')) { $bins = $this->getBinaries(true); if ([] === $bins) { $binDir = $composer->getConfig()->get('bin-dir'); throw new \RuntimeException("No binaries found in composer.json or in bin-dir ($binDir)"); } $this->getIO()->write( <<Available binaries: EOT ); foreach ($bins as $bin) { $this->getIO()->write( <<- $bin EOT ); } return 0; } $binary = $input->getArgument('binary'); $dispatcher = $composer->getEventDispatcher(); $dispatcher->addListener('__exec_command', $binary); if (getcwd() !== $this->getApplication()->getInitialWorkingDirectory() && $this->getApplication()->getInitialWorkingDirectory() !== false) { try { chdir($this->getApplication()->getInitialWorkingDirectory()); } catch (\Exception $e) { throw new \RuntimeException('Could not switch back to working directory "'.$this->getApplication()->getInitialWorkingDirectory().'"', 0, $e); } } return $dispatcher->dispatchScript('__exec_command', true, $input->getArgument('args')); } private function getBinaries(bool $forDisplay): array { $composer = $this->requireComposer(); $binDir = $composer->getConfig()->get('bin-dir'); $bins = glob($binDir . '/*'); $localBins = $composer->getPackage()->getBinaries(); if ($forDisplay) { $localBins = array_map(static function ($e) { return "$e (local)"; }, $localBins); } $binaries = []; foreach (array_merge($bins, $localBins) as $bin) { if (isset($previousBin) && $bin === $previousBin.'.bat') { continue; } $previousBin = $bin; $binaries[] = basename($bin); } return $binaries; } } setName('fund') ->setDescription('Discover how to help fund the maintenance of your dependencies') ->setDefinition([ new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text', ['text', 'json']), ]) ; } protected function execute(InputInterface $input, OutputInterface $output): int { $composer = $this->requireComposer(); $repo = $composer->getRepositoryManager()->getLocalRepository(); $remoteRepos = new CompositeRepository($composer->getRepositoryManager()->getRepositories()); $fundings = []; $packagesToLoad = []; foreach ($repo->getPackages() as $package) { if ($package instanceof AliasPackage) { continue; } $packagesToLoad[$package->getName()] = new MatchAllConstraint(); } $result = $remoteRepos->loadPackages($packagesToLoad, ['dev' => BasePackage::STABILITY_DEV], []); foreach ($result['packages'] as $package) { if ( !$package instanceof AliasPackage && $package instanceof CompletePackageInterface && $package->isDefaultBranch() && $package->getFunding() && isset($packagesToLoad[$package->getName()]) ) { $fundings = $this->insertFundingData($fundings, $package); unset($packagesToLoad[$package->getName()]); } } foreach ($repo->getPackages() as $package) { if ($package instanceof AliasPackage || !isset($packagesToLoad[$package->getName()])) { continue; } if ($package instanceof CompletePackageInterface && $package->getFunding()) { $fundings = $this->insertFundingData($fundings, $package); } } ksort($fundings); $io = $this->getIO(); $format = $input->getOption('format'); if (!in_array($format, ['text', 'json'])) { $io->writeError(sprintf('Unsupported format "%s". See help for supported formats.', $format)); return 1; } if ($fundings && $format === 'text') { $prev = null; $io->write('The following packages were found in your dependencies which publish funding information:'); foreach ($fundings as $vendor => $links) { $io->write(''); $io->write(sprintf("%s", $vendor)); foreach ($links as $url => $packages) { $line = sprintf(' %s', implode(', ', $packages)); if ($prev !== $line) { $io->write($line); $prev = $line; } $io->write(sprintf(' %s', OutputFormatter::escape($url), $url)); } } $io->write(""); $io->write("Please consider following these links and sponsoring the work of package authors!"); $io->write("Thank you!"); } elseif ($format === 'json') { $io->write(JsonFile::encode($fundings)); } else { $io->write("No funding links were found in your package dependencies. This doesn't mean they don't need your support!"); } return 0; } private function insertFundingData(array $fundings, CompletePackageInterface $package): array { foreach ($package->getFunding() as $fundingOption) { [$vendor, $packageName] = explode('/', $package->getPrettyName()); if (empty($fundingOption['url'])) { continue; } $url = $fundingOption['url']; if (!empty($fundingOption['type']) && $fundingOption['type'] === 'github' && Preg::isMatch('{^https://github.com/([^/]+)$}', $url, $match)) { $url = 'https://github.com/sponsors/'.$match[1]; } $fundings[$vendor][$url][] = $packageName; } return $fundings; } } getApplication(); if ($input->mustSuggestArgumentValuesFor('command-name')) { $suggestions->suggestValues(array_filter(array_map(static function (Command $command) { return $command->isHidden() ? null : $command->getName(); }, $application->all()))); return; } if ($application->has($commandName = $input->getArgument('command-name'))) { $input = $this->prepareSubcommandInput($input, true); $input = CompletionInput::fromString($input->__toString(), 2); $command = $application->find($commandName); $command->mergeApplicationDefinition(); $input->bind($command->getDefinition()); $command->complete($input, $suggestions); } } protected function configure(): void { $this ->setName('global') ->setDescription('Allows running commands in the global composer dir ($COMPOSER_HOME)') ->setDefinition([ new InputArgument('command-name', InputArgument::REQUIRED, ''), new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''), ]) ->setHelp( <<\AppData\Roaming\Composer on Windows and /home//.composer on unix systems. If your system uses freedesktop.org standards, then it will first check XDG_CONFIG_HOME or default to /home//.config/composer Note: This path may vary depending on customizations to bin-dir in composer.json or the environmental variable COMPOSER_BIN_DIR. Read more at https://getcomposer.org/doc/03-cli.md#global EOT ) ; } public function run(InputInterface $input, OutputInterface $output): int { if (!method_exists($input, '__toString')) { throw new \LogicException('Expected an Input instance that is stringable, got '.get_class($input)); } $tokens = Preg::split('{\s+}', $input->__toString()); $args = []; foreach ($tokens as $token) { if ($token && $token[0] !== '-') { $args[] = $token; if (count($args) >= 2) { break; } } } if (count($args) < 2) { return parent::run($input, $output); } $input = $this->prepareSubcommandInput($input); return $this->getApplication()->run($input, $output); } private function prepareSubcommandInput(InputInterface $input, bool $quiet = false): StringInput { if (!method_exists($input, '__toString')) { throw new \LogicException('Expected an Input instance that is stringable, got '.get_class($input)); } if (Platform::getEnv('COMPOSER')) { Platform::clearEnv('COMPOSER'); } $config = Factory::createConfig(); $home = $config->get('home'); if (!is_dir($home)) { $fs = new Filesystem(); $fs->ensureDirectoryExists($home); if (!is_dir($home)) { throw new \RuntimeException('Could not create home directory'); } } try { chdir($home); } catch (\Exception $e) { throw new \RuntimeException('Could not switch to home directory "'.$home.'"', 0, $e); } if (!$quiet) { $this->getIO()->writeError('Changed current directory to '.$home.''); } $input = new StringInput(Preg::replace('{\bg(?:l(?:o(?:b(?:a(?:l)?)?)?)?)?\b}', '', $input->__toString(), 1)); $this->getApplication()->resetComposer(); return $input; } public function isProxyCommand(): bool { return true; } } setName('browse') ->setAliases(['home']) ->setDescription('Opens the package\'s repository URL or homepage in your browser') ->setDefinition([ new InputArgument('packages', InputArgument::IS_ARRAY, 'Package(s) to browse to.', null, $this->suggestInstalledPackage()), new InputOption('homepage', 'H', InputOption::VALUE_NONE, 'Open the homepage instead of the repository URL.'), new InputOption('show', 's', InputOption::VALUE_NONE, 'Only show the homepage or repository URL.'), ]) ->setHelp( <<initializeRepos(); $io = $this->getIO(); $return = 0; $packages = $input->getArgument('packages'); if (count($packages) === 0) { $io->writeError('No package specified, opening homepage for the root package'); $packages = [$this->requireComposer()->getPackage()->getName()]; } foreach ($packages as $packageName) { $handled = false; $packageExists = false; foreach ($repos as $repo) { foreach ($repo->findPackages($packageName) as $package) { $packageExists = true; if ($package instanceof CompletePackageInterface && $this->handlePackage($package, $input->getOption('homepage'), $input->getOption('show'))) { $handled = true; break 2; } } } if (!$packageExists) { $return = 1; $io->writeError('Package '.$packageName.' not found'); } if (!$handled) { $return = 1; $io->writeError(''.($input->getOption('homepage') ? 'Invalid or missing homepage' : 'Invalid or missing repository URL').' for '.$packageName.''); } } return $return; } private function handlePackage(CompletePackageInterface $package, bool $showHomepage, bool $showOnly): bool { $support = $package->getSupport(); $url = $support['source'] ?? $package->getSourceUrl(); if (!$url || $showHomepage) { $url = $package->getHomepage(); } if (!$url || !filter_var($url, FILTER_VALIDATE_URL)) { return false; } if ($showOnly) { $this->getIO()->write(sprintf('%s', $url)); } else { $this->openBrowser($url); } return true; } private function openBrowser(string $url): void { $url = ProcessExecutor::escape($url); $process = new ProcessExecutor($this->getIO()); if (Platform::isWindows()) { $process->execute('start "web" explorer ' . $url, $output); return; } $linux = $process->execute('which xdg-open', $output); $osx = $process->execute('which open', $output); if (0 === $linux) { $process->execute('xdg-open ' . $url, $output); } elseif (0 === $osx) { $process->execute('open ' . $url, $output); } else { $this->getIO()->writeError('No suitable browser opening command found, open yourself: ' . $url); } } private function initializeRepos(): array { $composer = $this->tryComposer(); if ($composer) { return array_merge( [new RootPackageRepository(clone $composer->getPackage())], [$composer->getRepositoryManager()->getLocalRepository()], $composer->getRepositoryManager()->getRepositories() ); } return RepositoryFactory::defaultReposWithDefaultManager($this->getIO()); } } setName('init') ->setDescription('Creates a basic composer.json file in current directory') ->setDefinition([ new InputOption('name', null, InputOption::VALUE_REQUIRED, 'Name of the package'), new InputOption('description', null, InputOption::VALUE_REQUIRED, 'Description of package'), new InputOption('author', null, InputOption::VALUE_REQUIRED, 'Author name of package'), new InputOption('type', null, InputOption::VALUE_OPTIONAL, 'Type of package (e.g. library, project, metapackage, composer-plugin)'), new InputOption('homepage', null, InputOption::VALUE_REQUIRED, 'Homepage of package'), new InputOption('require', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Package to require with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or "foo/bar 1.0.0"', null, $this->suggestAvailablePackageInclPlatform()), new InputOption('require-dev', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Package to require for development with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or "foo/bar 1.0.0"', null, $this->suggestAvailablePackageInclPlatform()), new InputOption('stability', 's', InputOption::VALUE_REQUIRED, 'Minimum stability (empty or one of: '.implode(', ', array_keys(BasePackage::$stabilities)).')'), new InputOption('license', 'l', InputOption::VALUE_REQUIRED, 'License of package'), new InputOption('repository', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Add custom repositories, either by URL or using JSON arrays'), new InputOption('autoload', 'a', InputOption::VALUE_REQUIRED, 'Add PSR-4 autoload mapping. Maps your package\'s namespace to the provided directory. (Expects a relative path, e.g. src/)'), ]) ->setHelp( <<init command creates a basic composer.json file in the current directory. php composer.phar init Read more at https://getcomposer.org/doc/03-cli.md#init EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $io = $this->getIO(); $allowlist = ['name', 'description', 'author', 'type', 'homepage', 'require', 'require-dev', 'stability', 'license', 'autoload']; $options = array_filter(array_intersect_key($input->getOptions(), array_flip($allowlist))); if (isset($options['name']) && !Preg::isMatch('{^[a-z0-9_.-]+/[a-z0-9_.-]+$}D', $options['name'])) { throw new \InvalidArgumentException( 'The package name '.$options['name'].' is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+' ); } if (isset($options['author'])) { $options['authors'] = $this->formatAuthors($options['author']); unset($options['author']); } $repositories = $input->getOption('repository'); if (count($repositories) > 0) { $config = Factory::createConfig($io); foreach ($repositories as $repo) { $options['repositories'][] = RepositoryFactory::configFromString($io, $config, $repo, true); } } if (isset($options['stability'])) { $options['minimum-stability'] = $options['stability']; unset($options['stability']); } $options['require'] = isset($options['require']) ? $this->formatRequirements($options['require']) : new \stdClass; if ([] === $options['require']) { $options['require'] = new \stdClass; } if (isset($options['require-dev'])) { $options['require-dev'] = $this->formatRequirements($options['require-dev']); if ([] === $options['require-dev']) { $options['require-dev'] = new \stdClass; } } $autoloadPath = null; if (isset($options['autoload'])) { $autoloadPath = $options['autoload']; $namespace = $this->namespaceFromPackageName((string) $input->getOption('name')); $options['autoload'] = (object) [ 'psr-4' => [ $namespace . '\\' => $autoloadPath, ], ]; } $file = new JsonFile(Factory::getComposerFile()); $json = JsonFile::encode($options); if ($input->isInteractive()) { $io->writeError(['', $json, '']); if (!$io->askConfirmation('Do you confirm generation [yes]? ')) { $io->writeError('Command aborted'); return 1; } } else { if (json_encode($options) === '{"require":{}}') { throw new \RuntimeException('You have to run this command in interactive mode, or specify at least some data using --name, --require, etc.'); } $io->writeError('Writing '.$file->getPath()); } $file->write($options); try { $file->validateSchema(JsonFile::LAX_SCHEMA); } catch (JsonValidationException $e) { $io->writeError('Schema validation error, aborting'); $errors = ' - ' . implode(PHP_EOL . ' - ', $e->getErrors()); $io->writeError($e->getMessage() . ':' . PHP_EOL . $errors); Silencer::call('unlink', $file->getPath()); return 1; } if ($autoloadPath) { $filesystem = new Filesystem(); $filesystem->ensureDirectoryExists($autoloadPath); if (!$this->hasDependencies($options)) { $this->runDumpAutoloadCommand($output); } } if ($input->isInteractive() && is_dir('.git')) { $ignoreFile = realpath('.gitignore'); if (false === $ignoreFile) { $ignoreFile = realpath('.') . '/.gitignore'; } if (!$this->hasVendorIgnore($ignoreFile)) { $question = 'Would you like the vendor directory added to your .gitignore [yes]? '; if ($io->askConfirmation($question)) { $this->addVendorIgnore($ignoreFile); } } } $question = 'Would you like to install dependencies now [yes]? '; if ($input->isInteractive() && $this->hasDependencies($options) && $io->askConfirmation($question)) { $this->updateDependencies($output); } if ($autoloadPath) { $namespace = $this->namespaceFromPackageName((string) $input->getOption('name')); $io->writeError('PSR-4 autoloading configured. Use "namespace '.$namespace.';" in '.$autoloadPath); $io->writeError('Include the Composer autoloader with: require \'vendor/autoload.php\';'); } return 0; } protected function interact(InputInterface $input, OutputInterface $output) { $git = $this->getGitConfig(); $io = $this->getIO(); $formatter = $this->getHelperSet()->get('formatter'); $repositories = $input->getOption('repository'); if (count($repositories) > 0) { $config = Factory::createConfig($io); $io->loadConfiguration($config); $repoManager = RepositoryFactory::manager($io, $config); $repos = [new PlatformRepository]; $createDefaultPackagistRepo = true; foreach ($repositories as $repo) { $repoConfig = RepositoryFactory::configFromString($io, $config, $repo, true); if ( (isset($repoConfig['packagist']) && $repoConfig === ['packagist' => false]) || (isset($repoConfig['packagist.org']) && $repoConfig === ['packagist.org' => false]) ) { $createDefaultPackagistRepo = false; continue; } $repos[] = RepositoryFactory::createRepo($io, $config, $repoConfig, $repoManager); } if ($createDefaultPackagistRepo) { $repos[] = RepositoryFactory::createRepo($io, $config, [ 'type' => 'composer', 'url' => 'https://repo.packagist.org', ], $repoManager); } $this->repos = new CompositeRepository($repos); unset($repos, $config, $repositories); } $io->writeError([ '', $formatter->formatBlock('Welcome to the Composer config generator', 'bg=blue;fg=white', true), '', ]); $io->writeError([ '', 'This command will guide you through creating your composer.json config.', '', ]); $cwd = realpath("."); $name = $input->getOption('name'); if (null === $name) { $name = basename($cwd); $name = Preg::replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $name); $name = strtolower($name); if (!empty($_SERVER['COMPOSER_DEFAULT_VENDOR'])) { $name = $_SERVER['COMPOSER_DEFAULT_VENDOR'] . '/' . $name; } elseif (isset($git['github.user'])) { $name = $git['github.user'] . '/' . $name; } elseif (!empty($_SERVER['USERNAME'])) { $name = $_SERVER['USERNAME'] . '/' . $name; } elseif (!empty($_SERVER['USER'])) { $name = $_SERVER['USER'] . '/' . $name; } elseif (get_current_user()) { $name = get_current_user() . '/' . $name; } else { $name .= '/' . $name; } $name = strtolower($name); } $name = $io->askAndValidate( 'Package name (/) ['.$name.']: ', static function ($value) use ($name) { if (null === $value) { return $name; } if (!Preg::isMatch('{^[a-z0-9_.-]+/[a-z0-9_.-]+$}D', $value)) { throw new \InvalidArgumentException( 'The package name '.$value.' is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+' ); } return $value; }, null, $name ); $input->setOption('name', $name); $description = $input->getOption('description') ?: null; $description = $io->ask( 'Description ['.$description.']: ', $description ); $input->setOption('description', $description); if (null === $author = $input->getOption('author')) { if (!empty($_SERVER['COMPOSER_DEFAULT_AUTHOR'])) { $author_name = $_SERVER['COMPOSER_DEFAULT_AUTHOR']; } elseif (isset($git['user.name'])) { $author_name = $git['user.name']; } if (!empty($_SERVER['COMPOSER_DEFAULT_EMAIL'])) { $author_email = $_SERVER['COMPOSER_DEFAULT_EMAIL']; } elseif (isset($git['user.email'])) { $author_email = $git['user.email']; } if (isset($author_name, $author_email)) { $author = sprintf('%s <%s>', $author_name, $author_email); } } $author = $io->askAndValidate( 'Author ['.(is_string($author) ? ''.$author.', ' : '') . 'n to skip]: ', function ($value) use ($author) { if ($value === 'n' || $value === 'no') { return; } $value = $value ?: $author; $author = $this->parseAuthorString($value ?? ''); if ($author['email'] === null) { return $author['name']; } return sprintf('%s <%s>', $author['name'], $author['email']); }, null, $author ); $input->setOption('author', $author); $minimumStability = $input->getOption('stability') ?: null; $minimumStability = $io->askAndValidate( 'Minimum Stability ['.$minimumStability.']: ', static function ($value) use ($minimumStability) { if (null === $value) { return $minimumStability; } if (!isset(BasePackage::$stabilities[$value])) { throw new \InvalidArgumentException( 'Invalid minimum stability "'.$value.'". Must be empty or one of: '. implode(', ', array_keys(BasePackage::$stabilities)) ); } return $value; }, null, $minimumStability ); $input->setOption('stability', $minimumStability); $type = $input->getOption('type') ?: false; $type = $io->ask( 'Package Type (e.g. library, project, metapackage, composer-plugin) ['.$type.']: ', $type ); $input->setOption('type', $type); if (null === $license = $input->getOption('license')) { if (!empty($_SERVER['COMPOSER_DEFAULT_LICENSE'])) { $license = $_SERVER['COMPOSER_DEFAULT_LICENSE']; } } $license = $io->ask( 'License ['.$license.']: ', $license ); $input->setOption('license', $license); $io->writeError(['', 'Define your dependencies.', '']); $repos = $this->getRepos(); $preferredStability = $minimumStability ?: 'stable'; $platformRepo = null; if ($repos instanceof CompositeRepository) { foreach ($repos->getRepositories() as $candidateRepo) { if ($candidateRepo instanceof PlatformRepository) { $platformRepo = $candidateRepo; break; } } } $question = 'Would you like to define your dependencies (require) interactively [yes]? '; $require = $input->getOption('require'); $requirements = []; if (count($require) > 0 || $io->askConfirmation($question)) { $requirements = $this->determineRequirements($input, $output, $require, $platformRepo, $preferredStability); } $input->setOption('require', $requirements); $question = 'Would you like to define your dev dependencies (require-dev) interactively [yes]? '; $requireDev = $input->getOption('require-dev'); $devRequirements = []; if (count($requireDev) > 0 || $io->askConfirmation($question)) { $devRequirements = $this->determineRequirements($input, $output, $requireDev, $platformRepo, $preferredStability); } $input->setOption('require-dev', $devRequirements); $autoload = $input->getOption('autoload') ?: 'src/'; $namespace = $this->namespaceFromPackageName((string) $input->getOption('name')); $autoload = $io->askAndValidate( 'Add PSR-4 autoload mapping? Maps namespace "'.$namespace.'" to the entered relative path. ['.$autoload.', n to skip]: ', static function ($value) use ($autoload) { if (null === $value) { return $autoload; } if ($value === 'n' || $value === 'no') { return; } $value = $value ?: $autoload; if (!Preg::isMatch('{^[^/][A-Za-z0-9\-_/]+/$}', $value)) { throw new \InvalidArgumentException(sprintf( 'The src folder name "%s" is invalid. Please add a relative path with tailing forward slash. [A-Za-z0-9_-/]+/', $value )); } return $value; }, null, $autoload ); $input->setOption('autoload', $autoload); } private function parseAuthorString(string $author): array { if (Preg::isMatch('/^(?P[- .,\p{L}\p{N}\p{Mn}\'’"()]+)(?:\s+<(?P.+?)>)?$/u', $author, $match)) { $hasEmail = isset($match['email']) && '' !== $match['email']; if ($hasEmail && !$this->isValidEmail($match['email'])) { throw new \InvalidArgumentException('Invalid email "'.$match['email'].'"'); } return [ 'name' => trim($match['name']), 'email' => $hasEmail ? $match['email'] : null, ]; } throw new \InvalidArgumentException( 'Invalid author string. Must be in the formats: '. 'Jane Doe or John Smith ' ); } protected function formatAuthors(string $author): array { $author = $this->parseAuthorString($author); if (null === $author['email']) { unset($author['email']); } return [$author]; } public function namespaceFromPackageName(string $packageName): ?string { if (!$packageName || strpos($packageName, '/') === false) { return null; } $namespace = array_map( static function ($part): string { $part = Preg::replace('/[^a-z0-9]/i', ' ', $part); $part = ucwords($part); return str_replace(' ', '', $part); }, explode('/', $packageName) ); return implode('\\', $namespace); } protected function getGitConfig(): array { if (null !== $this->gitConfig) { return $this->gitConfig; } $finder = new ExecutableFinder(); $gitBin = $finder->find('git'); $cmd = new Process([$gitBin, 'config', '-l']); $cmd->run(); if ($cmd->isSuccessful()) { $this->gitConfig = []; Preg::matchAll('{^([^=]+)=(.*)$}m', $cmd->getOutput(), $matches); foreach ($matches[1] as $key => $match) { $this->gitConfig[$match] = $matches[2][$key]; } return $this->gitConfig; } return $this->gitConfig = []; } protected function hasVendorIgnore(string $ignoreFile, string $vendor = 'vendor'): bool { if (!file_exists($ignoreFile)) { return false; } $pattern = sprintf('{^/?%s(/\*?)?$}', preg_quote($vendor)); $lines = file($ignoreFile, FILE_IGNORE_NEW_LINES); foreach ($lines as $line) { if (Preg::isMatch($pattern, $line)) { return true; } } return false; } protected function addVendorIgnore(string $ignoreFile, string $vendor = '/vendor/'): void { $contents = ""; if (file_exists($ignoreFile)) { $contents = file_get_contents($ignoreFile); if (strpos($contents, "\n") !== 0) { $contents .= "\n"; } } file_put_contents($ignoreFile, $contents . $vendor. "\n"); } protected function isValidEmail(string $email): bool { if (!function_exists('filter_var')) { return true; } return false !== filter_var($email, FILTER_VALIDATE_EMAIL); } private function updateDependencies(OutputInterface $output): void { try { $updateCommand = $this->getApplication()->find('update'); $this->getApplication()->resetComposer(); $updateCommand->run(new ArrayInput([]), $output); } catch (\Exception $e) { $this->getIO()->writeError('Could not update dependencies. Run `composer update` to see more information.'); } } private function runDumpAutoloadCommand(OutputInterface $output): void { try { $command = $this->getApplication()->find('dump-autoload'); $this->getApplication()->resetComposer(); $command->run(new ArrayInput([]), $output); } catch (\Exception $e) { $this->getIO()->writeError('Could not run dump-autoload.'); } } private function hasDependencies(array $options): bool { $requires = (array) $options['require']; $devRequires = isset($options['require-dev']) ? (array) $options['require-dev'] : []; return !empty($requires) || !empty($devRequires); } } setName('install') ->setAliases(['i']) ->setDescription('Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json') ->setDefinition([ new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), new InputOption('prefer-dist', null, InputOption::VALUE_NONE, 'Forces installation from package dist (default behavior).'), new InputOption('prefer-install', null, InputOption::VALUE_REQUIRED, 'Forces installation from package dist|source|auto (auto chooses source for dev versions, dist for the rest).', null, $this->suggestPreferInstall()), new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'), new InputOption('dev', null, InputOption::VALUE_NONE, 'DEPRECATED: Enables installation of require-dev packages (enabled by default, only present for BC).'), new InputOption('no-suggest', null, InputOption::VALUE_NONE, 'DEPRECATED: This flag does not exist anymore.'), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables installation of require-dev packages.'), new InputOption('no-autoloader', null, InputOption::VALUE_NONE, 'Skips autoloader generation'), new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'), new InputOption('no-install', null, InputOption::VALUE_NONE, 'Do not use, only defined here to catch misuse of the install command.'), new InputOption('audit', null, InputOption::VALUE_NONE, 'Run an audit after installation is complete.'), new InputOption('audit-format', null, InputOption::VALUE_REQUIRED, 'Audit output format. Must be "table", "plain", "json", or "summary".', Auditor::FORMAT_SUMMARY, Auditor::FORMATS), new InputOption('verbose', 'v|vv|vvv', InputOption::VALUE_NONE, 'Shows more details including new commits pulled in when updating packages.'), new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump'), new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.'), new InputOption('apcu-autoloader', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'), new InputOption('apcu-autoloader-prefix', null, InputOption::VALUE_REQUIRED, 'Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu-autoloader'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Should not be provided, use composer require instead to add a given package to composer.json.'), ]) ->setHelp( <<install command reads the composer.lock file from the current directory, processes it, and downloads and installs all the libraries and dependencies outlined in that file. If the file does not exist it will look for composer.json and do the same. php composer.phar install Read more at https://getcomposer.org/doc/03-cli.md#install-i EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $io = $this->getIO(); if ($input->getOption('dev')) { $io->writeError('You are using the deprecated option "--dev". It has no effect and will break in Composer 3.'); } if ($input->getOption('no-suggest')) { $io->writeError('You are using the deprecated option "--no-suggest". It has no effect and will break in Composer 3.'); } $args = $input->getArgument('packages'); if (count($args) > 0) { $io->writeError('Invalid argument '.implode(' ', $args).'. Use "composer require '.implode(' ', $args).'" instead to add packages to your composer.json.'); return 1; } if ($input->getOption('no-install')) { $io->writeError('Invalid option "--no-install". Use "composer update --no-install" instead if you are trying to update the composer.lock file.'); return 1; } $composer = $this->requireComposer(); if (!$composer->getLocker()->isLocked() && !HttpDownloader::isCurlEnabled()) { $io->writeError('Composer is operating significantly slower than normal because you do not have the PHP curl extension enabled.'); } $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'install', $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); $install = Installer::create($io, $composer); $config = $composer->getConfig(); [$preferSource, $preferDist] = $this->getPreferredInstallOptions($config, $input); $optimize = $input->getOption('optimize-autoloader') || $config->get('optimize-autoloader'); $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative'); $apcuPrefix = $input->getOption('apcu-autoloader-prefix'); $apcu = $apcuPrefix !== null || $input->getOption('apcu-autoloader') || $config->get('apcu-autoloader'); $composer->getInstallationManager()->setOutputProgress(!$input->getOption('no-progress')); $install ->setDryRun($input->getOption('dry-run')) ->setVerbose($input->getOption('verbose')) ->setPreferSource($preferSource) ->setPreferDist($preferDist) ->setDevMode(!$input->getOption('no-dev')) ->setDumpAutoloader(!$input->getOption('no-autoloader')) ->setOptimizeAutoloader($optimize) ->setClassMapAuthoritative($authoritative) ->setApcuAutoloader($apcu, $apcuPrefix) ->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input)) ->setAudit($input->getOption('audit')) ->setAuditFormat($this->getAuditFormat($input)) ; if ($input->getOption('no-plugins')) { $install->disablePlugins(); } return $install->run(); } } setName('licenses') ->setDescription('Shows information about licenses of dependencies') ->setDefinition([ new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text, json or summary', 'text', ['text', 'json', 'summary']), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables search in require-dev packages.'), ]) ->setHelp( <<requireComposer(); $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'licenses', $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); $root = $composer->getPackage(); $repo = $composer->getRepositoryManager()->getLocalRepository(); if ($input->getOption('no-dev')) { $packages = RepositoryUtils::filterRequiredPackages($repo->getPackages(), $root); } else { $packages = $repo->getPackages(); } $packages = PackageSorter::sortPackagesAlphabetically($packages); $io = $this->getIO(); switch ($format = $input->getOption('format')) { case 'text': $io->write('Name: '.$root->getPrettyName().''); $io->write('Version: '.$root->getFullPrettyVersion().''); $io->write('Licenses: '.(implode(', ', $root->getLicense()) ?: 'none').''); $io->write('Dependencies:'); $io->write(''); $table = new Table($output); $table->setStyle('compact'); $table->setHeaders(['Name', 'Version', 'Licenses']); foreach ($packages as $package) { $link = PackageInfo::getViewSourceOrHomepageUrl($package); if ($link !== null) { $name = ''.$package->getPrettyName().''; } else { $name = $package->getPrettyName(); } $table->addRow([ $name, $package->getFullPrettyVersion(), implode(', ', $package instanceof CompletePackageInterface ? $package->getLicense() : []) ?: 'none', ]); } $table->render(); break; case 'json': $dependencies = []; foreach ($packages as $package) { $dependencies[$package->getPrettyName()] = [ 'version' => $package->getFullPrettyVersion(), 'license' => $package instanceof CompletePackageInterface ? $package->getLicense() : [], ]; } $io->write(JsonFile::encode([ 'name' => $root->getPrettyName(), 'version' => $root->getFullPrettyVersion(), 'license' => $root->getLicense(), 'dependencies' => $dependencies, ])); break; case 'summary': $usedLicenses = []; foreach ($packages as $package) { $licenses = $package instanceof CompletePackageInterface ? $package->getLicense() : []; if (count($licenses) === 0) { $licenses[] = 'none'; } foreach ($licenses as $licenseName) { if (!isset($usedLicenses[$licenseName])) { $usedLicenses[$licenseName] = 0; } $usedLicenses[$licenseName]++; } } arsort($usedLicenses, SORT_NUMERIC); $rows = []; foreach ($usedLicenses as $usedLicense => $numberOfDependencies) { $rows[] = [$usedLicense, $numberOfDependencies]; } $symfonyIo = new SymfonyStyle($input, $output); $symfonyIo->table( ['License', 'Number of dependencies'], $rows ); break; default: throw new \RuntimeException(sprintf('Unsupported format "%s". See help for supported formats.', $format)); } return 0; } } setName('outdated') ->setDescription('Shows a list of installed packages that have updates available, including their latest version') ->setDefinition([ new InputArgument('package', InputArgument::OPTIONAL, 'Package to inspect. Or a name including a wildcard (*) to filter lists of packages instead.', null, $this->suggestInstalledPackage(false)), new InputOption('outdated', 'o', InputOption::VALUE_NONE, 'Show only packages that are outdated (this is the default, but present here for compat with `show`'), new InputOption('all', 'a', InputOption::VALUE_NONE, 'Show all installed packages with their latest versions'), new InputOption('locked', null, InputOption::VALUE_NONE, 'Shows updates for packages from the lock file, regardless of what is currently in vendor dir'), new InputOption('direct', 'D', InputOption::VALUE_NONE, 'Shows only packages that are directly required by the root package'), new InputOption('strict', null, InputOption::VALUE_NONE, 'Return a non-zero exit code when there are outdated packages'), new InputOption('major-only', 'M', InputOption::VALUE_NONE, 'Show only packages that have major SemVer-compatible updates.'), new InputOption('minor-only', 'm', InputOption::VALUE_NONE, 'Show only packages that have minor SemVer-compatible updates.'), new InputOption('patch-only', 'p', InputOption::VALUE_NONE, 'Show only packages that have patch SemVer-compatible updates.'), new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text', ['json', 'text']), new InputOption('ignore', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore specified package(s). Use it if you don\'t want to be informed about new versions of some packages.', null, $this->suggestInstalledPackage(false)), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables search in require-dev packages.'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages). Use with the --outdated option'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages). Use with the --outdated option'), ]) ->setHelp( <<green (=): Dependency is in the latest version and is up to date. - yellow (~): Dependency has a new version available that includes backwards compatibility breaks according to semver, so upgrade when you can but it may involve work. - red (!): Dependency has a new version that is semver-compatible and you should upgrade it. Read more at https://getcomposer.org/doc/03-cli.md#outdated EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { $args = [ 'command' => 'show', '--latest' => true, ]; if (!$input->getOption('all')) { $args['--outdated'] = true; } if ($input->getOption('direct')) { $args['--direct'] = true; } if (null !== $input->getArgument('package')) { $args['package'] = $input->getArgument('package'); } if ($input->getOption('strict')) { $args['--strict'] = true; } if ($input->getOption('major-only')) { $args['--major-only'] = true; } if ($input->getOption('minor-only')) { $args['--minor-only'] = true; } if ($input->getOption('patch-only')) { $args['--patch-only'] = true; } if ($input->getOption('locked')) { $args['--locked'] = true; } if ($input->getOption('no-dev')) { $args['--no-dev'] = true; } $args['--ignore-platform-req'] = $input->getOption('ignore-platform-req'); if ($input->getOption('ignore-platform-reqs')) { $args['--ignore-platform-reqs'] = true; } $args['--format'] = $input->getOption('format'); $args['--ignore'] = $input->getOption('ignore'); $input = new ArrayInput($args); return $this->getApplication()->run($input, $output); } public function isProxyCommand(): bool { return true; } } repos) { $this->repos = new CompositeRepository(array_merge( [new PlatformRepository], RepositoryFactory::defaultReposWithDefaultManager($this->getIO()) )); } return $this->repos; } private function getRepositorySet(InputInterface $input, ?string $minimumStability = null): RepositorySet { $key = $minimumStability ?? 'default'; if (!isset($this->repositorySets[$key])) { $this->repositorySets[$key] = $repositorySet = new RepositorySet($minimumStability ?? $this->getMinimumStability($input)); $repositorySet->addRepository($this->getRepos()); } return $this->repositorySets[$key]; } private function getMinimumStability(InputInterface $input): string { if ($input->hasOption('stability')) { return VersionParser::normalizeStability($input->getOption('stability') ?? 'stable'); } $file = Factory::getComposerFile(); if (is_file($file) && Filesystem::isReadable($file) && is_array($composer = json_decode((string) file_get_contents($file), true))) { if (!empty($composer['minimum-stability'])) { return VersionParser::normalizeStability($composer['minimum-stability']); } } return 'stable'; } final protected function determineRequirements(InputInterface $input, OutputInterface $output, array $requires = [], ?PlatformRepository $platformRepo = null, string $preferredStability = 'stable', bool $checkProvidedVersions = true, bool $fixed = false): array { if (count($requires) > 0) { $requires = $this->normalizeRequirements($requires); $result = []; $io = $this->getIO(); foreach ($requires as $requirement) { if (isset($requirement['version']) && Preg::isMatch('{^\d+(\.\d+)?$}', $requirement['version'])) { $io->writeError('The "'.$requirement['version'].'" constraint for "'.$requirement['name'].'" appears too strict and will likely not match what you want. See https://getcomposer.org/constraints'); } if (!isset($requirement['version'])) { [$name, $version] = $this->findBestVersionAndNameForPackage($input, $requirement['name'], $platformRepo, $preferredStability, $fixed); $requirement['version'] = $version; $requirement['name'] = $name; $io->writeError(sprintf( 'Using version %s for %s', $requirement['version'], $requirement['name'] )); } $result[] = $requirement['name'] . ' ' . $requirement['version']; } return $result; } $versionParser = new VersionParser(); $composer = $this->tryComposer(); $installedRepo = null; if (null !== $composer) { $installedRepo = $composer->getRepositoryManager()->getLocalRepository(); } $existingPackages = []; if (null !== $installedRepo) { foreach ($installedRepo->getPackages() as $package) { $existingPackages[] = $package->getName(); } } unset($composer, $installedRepo); $io = $this->getIO(); while (null !== $package = $io->ask('Search for a package: ')) { $matches = $this->getRepos()->search($package); if (count($matches) > 0) { foreach ($matches as $position => $foundPackage) { if (in_array($foundPackage['name'], $existingPackages, true)) { unset($matches[$position]); } } $matches = array_values($matches); $exactMatch = false; foreach ($matches as $match) { if ($match['name'] === $package) { $exactMatch = true; break; } } if (!$exactMatch) { $providers = $this->getRepos()->getProviders($package); if (count($providers) > 0) { array_unshift($matches, ['name' => $package, 'description' => '']); } $choices = []; foreach ($matches as $position => $foundPackage) { $abandoned = ''; if (isset($foundPackage['abandoned'])) { if (is_string($foundPackage['abandoned'])) { $replacement = sprintf('Use %s instead', $foundPackage['abandoned']); } else { $replacement = 'No replacement was suggested'; } $abandoned = sprintf('Abandoned. %s.', $replacement); } $choices[] = sprintf(' %5s %s %s', "[$position]", $foundPackage['name'], $abandoned); } $io->writeError([ '', sprintf('Found %s packages matching %s', count($matches), $package), '', ]); $io->writeError($choices); $io->writeError(''); $validator = static function (string $selection) use ($matches, $versionParser) { if ('' === $selection) { return false; } if (is_numeric($selection) && isset($matches[(int) $selection])) { $package = $matches[(int) $selection]; return $package['name']; } if (Preg::isMatch('{^\s*(?P[\S/]+)(?:\s+(?P\S+))?\s*$}', $selection, $packageMatches)) { if (isset($packageMatches['version'])) { $versionParser->parseConstraints($packageMatches['version']); return $packageMatches['name'].' '.$packageMatches['version']; } return $packageMatches['name']; } throw new \Exception('Not a valid selection'); }; $package = $io->askAndValidate( 'Enter package # to add, or the complete package name if it is not listed: ', $validator, 3, '' ); } if (false !== $package && false === strpos($package, ' ')) { $validator = static function (string $input) { $input = trim($input); return strlen($input) > 0 ? $input : false; }; $constraint = $io->askAndValidate( 'Enter the version constraint to require (or leave blank to use the latest version): ', $validator, 3, '' ); if (false === $constraint) { [, $constraint] = $this->findBestVersionAndNameForPackage($input, $package, $platformRepo, $preferredStability); $io->writeError(sprintf( 'Using version %s for %s', $constraint, $package )); } $package .= ' '.$constraint; } if (false !== $package) { $requires[] = $package; $existingPackages[] = explode(' ', $package)[0]; } } } return $requires; } private function findBestVersionAndNameForPackage(InputInterface $input, string $name, ?PlatformRepository $platformRepo = null, string $preferredStability = 'stable', bool $fixed = false): array { if ($input->hasOption('ignore-platform-reqs') && $input->hasOption('ignore-platform-req')) { $platformRequirementFilter = $this->getPlatformRequirementFilter($input); } else { $platformRequirementFilter = PlatformRequirementFilterFactory::ignoreNothing(); } $repoSet = $this->getRepositorySet($input); $versionSelector = new VersionSelector($repoSet, $platformRepo); $effectiveMinimumStability = $this->getMinimumStability($input); $package = $versionSelector->findBestCandidate($name, null, $preferredStability, $platformRequirementFilter, 0, $this->getIO()); if (false === $package) { if ($platformRequirementFilter->isIgnored($name)) { return [$name, '*']; } $providers = $repoSet->getProviders($name); if (count($providers) > 0) { $constraint = '*'; if ($input->isInteractive()) { $constraint = $this->getIO()->askAndValidate('Package "'.$name.'" does not exist but is provided by '.count($providers).' packages. Which version constraint would you like to use? [*] ', static function ($value) { $parser = new VersionParser(); $parser->parseConstraints($value); return $value; }, 3, '*'); } return [$name, $constraint]; } if (!($platformRequirementFilter instanceof IgnoreAllPlatformRequirementFilter) && false !== ($candidate = $versionSelector->findBestCandidate($name, null, $preferredStability, PlatformRequirementFilterFactory::ignoreAll()))) { throw new \InvalidArgumentException(sprintf( 'Package %s has requirements incompatible with your PHP version, PHP extensions and Composer version' . $this->getPlatformExceptionDetails($candidate, $platformRepo), $name )); } if (false !== ($package = $versionSelector->findBestCandidate($name, null, $preferredStability, $platformRequirementFilter, RepositorySet::ALLOW_UNACCEPTABLE_STABILITIES))) { if (false !== ($allReposPackage = $versionSelector->findBestCandidate($name, null, $preferredStability, $platformRequirementFilter, RepositorySet::ALLOW_SHADOWED_REPOSITORIES))) { throw new \InvalidArgumentException( 'Package '.$name.' exists in '.$allReposPackage->getRepository()->getRepoName().' and '.$package->getRepository()->getRepoName().' which has a higher repository priority. The packages from the higher priority repository do not match your minimum-stability and are therefore not installable. That repository is canonical so the lower priority repo\'s packages are not installable. See https://getcomposer.org/repoprio for details and assistance.' ); } throw new \InvalidArgumentException(sprintf( 'Could not find a version of package %s matching your minimum-stability (%s). Require it with an explicit version constraint allowing its desired stability.', $name, $effectiveMinimumStability )); } if (!$platformRequirementFilter instanceof IgnoreAllPlatformRequirementFilter && false !== ($candidate = $versionSelector->findBestCandidate($name, null, $preferredStability, PlatformRequirementFilterFactory::ignoreAll(), RepositorySet::ALLOW_UNACCEPTABLE_STABILITIES))) { $additional = ''; if (false === $versionSelector->findBestCandidate($name, null, $preferredStability, PlatformRequirementFilterFactory::ignoreAll())) { $additional = PHP_EOL.PHP_EOL.'Additionally, the package was only found with a stability of "'.$candidate->getStability().'" while your minimum stability is "'.$effectiveMinimumStability.'".'; } throw new \InvalidArgumentException(sprintf( 'Could not find package %s in any version matching your PHP version, PHP extensions and Composer version' . $this->getPlatformExceptionDetails($candidate, $platformRepo) . '%s', $name, $additional )); } $similar = $this->findSimilar($name); if (count($similar) > 0) { if (in_array($name, $similar, true)) { throw new \InvalidArgumentException(sprintf( "Could not find package %s. It was however found via repository search, which indicates a consistency issue with the repository.", $name )); } throw new \InvalidArgumentException(sprintf( "Could not find package %s.\n\nDid you mean " . (count($similar) > 1 ? 'one of these' : 'this') . "?\n %s", $name, implode("\n ", $similar) )); } throw new \InvalidArgumentException(sprintf( 'Could not find a matching version of package %s. Check the package spelling, your version constraint and that the package is available in a stability which matches your minimum-stability (%s).', $name, $effectiveMinimumStability )); } return [ $package->getPrettyName(), $fixed ? $package->getPrettyVersion() : $versionSelector->findRecommendedRequireVersion($package), ]; } private function findSimilar(string $package): array { try { if (null === $this->repos) { throw new \LogicException('findSimilar was called before $this->repos was initialized'); } $results = $this->repos->search($package); } catch (\Throwable $e) { if ($e instanceof \LogicException) { throw $e; } return []; } $similarPackages = []; $installedRepo = $this->requireComposer()->getRepositoryManager()->getLocalRepository(); foreach ($results as $result) { if (null !== $installedRepo->findPackage($result['name'], '*')) { continue; } $similarPackages[$result['name']] = levenshtein($package, $result['name']); } asort($similarPackages); return array_keys(array_slice($similarPackages, 0, 5)); } private function getPlatformExceptionDetails(PackageInterface $candidate, ?PlatformRepository $platformRepo = null): string { $details = []; if (null === $platformRepo) { return ''; } foreach ($candidate->getRequires() as $link) { if (!PlatformRepository::isPlatformPackage($link->getTarget())) { continue; } $platformPkg = $platformRepo->findPackage($link->getTarget(), '*'); if (null === $platformPkg) { if ($platformRepo->isPlatformPackageDisabled($link->getTarget())) { $details[] = $candidate->getPrettyName().' '.$candidate->getPrettyVersion().' requires '.$link->getTarget().' '.$link->getPrettyConstraint().' but it is disabled by your platform config. Enable it again with "composer config platform.'.$link->getTarget().' --unset".'; } else { $details[] = $candidate->getPrettyName().' '.$candidate->getPrettyVersion().' requires '.$link->getTarget().' '.$link->getPrettyConstraint().' but it is not present.'; } continue; } if (!$link->getConstraint()->matches(new Constraint('==', $platformPkg->getVersion()))) { $platformPkgVersion = $platformPkg->getPrettyVersion(); $platformExtra = $platformPkg->getExtra(); if (isset($platformExtra['config.platform']) && $platformPkg instanceof CompletePackageInterface) { $platformPkgVersion .= ' ('.$platformPkg->getDescription().')'; } $details[] = $candidate->getPrettyName().' '.$candidate->getPrettyVersion().' requires '.$link->getTarget().' '.$link->getPrettyConstraint().' which does not match your installed version '.$platformPkgVersion.'.'; } } if (count($details) === 0) { return ''; } return ':'.PHP_EOL.' - ' . implode(PHP_EOL.' - ', $details); } } setName('prohibits') ->setAliases(['why-not']) ->setDescription('Shows which packages prevent the given package from being installed') ->setDefinition([ new InputArgument(self::ARGUMENT_PACKAGE, InputArgument::REQUIRED, 'Package to inspect', null, $this->suggestAvailablePackage()), new InputArgument(self::ARGUMENT_CONSTRAINT, InputArgument::REQUIRED, 'Version constraint, which version you expected to be installed'), new InputOption(self::OPTION_RECURSIVE, 'r', InputOption::VALUE_NONE, 'Recursively resolves up to the root package'), new InputOption(self::OPTION_TREE, 't', InputOption::VALUE_NONE, 'Prints the results as a nested tree'), new InputOption('locked', null, InputOption::VALUE_NONE, 'Read dependency information from composer.lock'), ]) ->setHelp( <<php composer.phar prohibits composer/composer Read more at https://getcomposer.org/doc/03-cli.md#prohibits-why-not- EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { return parent::doExecute($input, $output, true); } } setName('reinstall') ->setDescription('Uninstalls and reinstalls the given package names') ->setDefinition([ new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), new InputOption('prefer-dist', null, InputOption::VALUE_NONE, 'Forces installation from package dist (default behavior).'), new InputOption('prefer-install', null, InputOption::VALUE_REQUIRED, 'Forces installation from package dist|source|auto (auto chooses source for dev versions, dist for the rest).', null, $this->suggestPreferInstall()), new InputOption('no-autoloader', null, InputOption::VALUE_NONE, 'Skips autoloader generation'), new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'), new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump'), new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.'), new InputOption('apcu-autoloader', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'), new InputOption('apcu-autoloader-prefix', null, InputOption::VALUE_REQUIRED, 'Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu-autoloader'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'List of package names to reinstall, can include a wildcard (*) to match any substring.', null, $this->suggestInstalledPackage(false)), ]) ->setHelp( <<reinstall command looks up installed packages by name, uninstalls them and reinstalls them. This lets you do a clean install of a package if you messed with its files, or if you wish to change the installation type using --prefer-install. php composer.phar reinstall acme/foo "acme/bar-*" Read more at https://getcomposer.org/doc/03-cli.md#reinstall EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { $io = $this->getIO(); $composer = $this->requireComposer(); $localRepo = $composer->getRepositoryManager()->getLocalRepository(); $packagesToReinstall = []; $packageNamesToReinstall = []; foreach ($input->getArgument('packages') as $pattern) { $patternRegexp = BasePackage::packageNameToRegexp($pattern); $matched = false; foreach ($localRepo->getCanonicalPackages() as $package) { if (Preg::isMatch($patternRegexp, $package->getName())) { $matched = true; $packagesToReinstall[] = $package; $packageNamesToReinstall[] = $package->getName(); } } if (!$matched) { $io->writeError('Pattern "' . $pattern . '" does not match any currently installed packages.'); } } if (!$packagesToReinstall) { $io->writeError('Found no packages to reinstall, aborting.'); return 1; } $uninstallOperations = []; foreach ($packagesToReinstall as $package) { $uninstallOperations[] = new UninstallOperation($package); } $presentPackages = $localRepo->getPackages(); $resultPackages = $presentPackages; foreach ($presentPackages as $index => $package) { if (in_array($package->getName(), $packageNamesToReinstall, true)) { unset($presentPackages[$index]); } } $transaction = new Transaction($presentPackages, $resultPackages); $installOperations = $transaction->getOperations(); $installOrder = []; foreach ($installOperations as $index => $op) { if ($op instanceof InstallOperation && !$op->getPackage() instanceof AliasPackage) { $installOrder[$op->getPackage()->getName()] = $index; } } usort($uninstallOperations, static function ($a, $b) use ($installOrder): int { return $installOrder[$b->getPackage()->getName()] - $installOrder[$a->getPackage()->getName()]; }); $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'reinstall', $input, $output); $eventDispatcher = $composer->getEventDispatcher(); $eventDispatcher->dispatch($commandEvent->getName(), $commandEvent); $config = $composer->getConfig(); [$preferSource, $preferDist] = $this->getPreferredInstallOptions($config, $input); $installationManager = $composer->getInstallationManager(); $downloadManager = $composer->getDownloadManager(); $package = $composer->getPackage(); $installationManager->setOutputProgress(!$input->getOption('no-progress')); if ($input->getOption('no-plugins')) { $installationManager->disablePlugins(); } $downloadManager->setPreferSource($preferSource); $downloadManager->setPreferDist($preferDist); $devMode = $localRepo->getDevMode() !== null ? $localRepo->getDevMode() : true; Platform::putEnv('COMPOSER_DEV_MODE', $devMode ? '1' : '0'); $eventDispatcher->dispatchScript(ScriptEvents::PRE_INSTALL_CMD, $devMode); $installationManager->execute($localRepo, $uninstallOperations, $devMode); $installationManager->execute($localRepo, $installOperations, $devMode); if (!$input->getOption('no-autoloader')) { $optimize = $input->getOption('optimize-autoloader') || $config->get('optimize-autoloader'); $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative'); $apcuPrefix = $input->getOption('apcu-autoloader-prefix'); $apcu = $apcuPrefix !== null || $input->getOption('apcu-autoloader') || $config->get('apcu-autoloader'); $generator = $composer->getAutoloadGenerator(); $generator->setClassMapAuthoritative($authoritative); $generator->setApcu($apcu, $apcuPrefix); $generator->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input)); $generator->dump($config, $localRepo, $package, $installationManager, 'composer', $optimize); } $eventDispatcher->dispatchScript(ScriptEvents::POST_INSTALL_CMD, $devMode); return 0; } } setName('remove') ->setDescription('Removes a package from the require or require-dev') ->setDefinition([ new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Packages that should be removed.', null, $this->suggestRootRequirement()), new InputOption('dev', null, InputOption::VALUE_NONE, 'Removes a package from the require-dev section.'), new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'), new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'), new InputOption('no-update', null, InputOption::VALUE_NONE, 'Disables the automatic update of the dependencies (implies --no-install).'), new InputOption('no-install', null, InputOption::VALUE_NONE, 'Skip the install step after updating the composer.lock file.'), new InputOption('no-audit', null, InputOption::VALUE_NONE, 'Skip the audit step after updating the composer.lock file (can also be set via the COMPOSER_NO_AUDIT=1 env var).'), new InputOption('audit-format', null, InputOption::VALUE_REQUIRED, 'Audit output format. Must be "table", "plain", "json", or "summary".', Auditor::FORMAT_SUMMARY, Auditor::FORMATS), new InputOption('update-no-dev', null, InputOption::VALUE_NONE, 'Run the dependency update with the --no-dev option.'), new InputOption('update-with-dependencies', 'w', InputOption::VALUE_NONE, 'Allows inherited dependencies to be updated with explicit dependencies. (Deprecated, is now default behavior)'), new InputOption('update-with-all-dependencies', 'W', InputOption::VALUE_NONE, 'Allows all inherited dependencies to be updated, including those that are root requirements.'), new InputOption('with-all-dependencies', null, InputOption::VALUE_NONE, 'Alias for --update-with-all-dependencies'), new InputOption('no-update-with-dependencies', null, InputOption::VALUE_NONE, 'Does not allow inherited dependencies to be updated with explicit dependencies.'), new InputOption('unused', null, InputOption::VALUE_NONE, 'Remove all packages which are locked but not required by any other package.'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump'), new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.'), new InputOption('apcu-autoloader', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'), new InputOption('apcu-autoloader-prefix', null, InputOption::VALUE_REQUIRED, 'Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu-autoloader'), ]) ->setHelp( <<remove command removes a package from the current list of installed packages php composer.phar remove Read more at https://getcomposer.org/doc/03-cli.md#remove EOT ) ; } protected function interact(InputInterface $input, OutputInterface $output) { if ($input->getOption('unused')) { $composer = $this->requireComposer(); $locker = $composer->getLocker(); if (!$locker->isLocked()) { throw new \UnexpectedValueException('A valid composer.lock file is required to run this command with --unused'); } $lockedPackages = $locker->getLockedRepository()->getPackages(); $required = []; foreach (array_merge($composer->getPackage()->getRequires(), $composer->getPackage()->getDevRequires()) as $link) { $required[$link->getTarget()] = true; } do { $found = false; foreach ($lockedPackages as $index => $package) { foreach ($package->getNames() as $name) { if (isset($required[$name])) { foreach ($package->getRequires() as $link) { $required[$link->getTarget()] = true; } $found = true; unset($lockedPackages[$index]); break; } } } } while ($found); $unused = []; foreach ($lockedPackages as $package) { $unused[] = $package->getName(); } $input->setArgument('packages', array_merge($input->getArgument('packages'), $unused)); if (count($input->getArgument('packages')) === 0) { $this->getIO()->writeError('No unused packages to remove'); $this->setCode(static function (): int { return 0; }); } } } protected function execute(InputInterface $input, OutputInterface $output) { $packages = $input->getArgument('packages'); $packages = array_map('strtolower', $packages); $file = Factory::getComposerFile(); $jsonFile = new JsonFile($file); $composer = $jsonFile->read(); $composerBackup = file_get_contents($jsonFile->getPath()); $json = new JsonConfigSource($jsonFile); $type = $input->getOption('dev') ? 'require-dev' : 'require'; $altType = !$input->getOption('dev') ? 'require-dev' : 'require'; $io = $this->getIO(); if ($input->getOption('update-with-dependencies')) { $io->writeError('You are using the deprecated option "update-with-dependencies". This is now default behaviour. The --no-update-with-dependencies option can be used to remove a package without its dependencies.'); } foreach (['require', 'require-dev'] as $linkType) { if (isset($composer[$linkType])) { foreach ($composer[$linkType] as $name => $version) { $composer[$linkType][strtolower($name)] = $name; } } } $dryRun = $input->getOption('dry-run'); $toRemove = []; foreach ($packages as $package) { if (isset($composer[$type][$package])) { if ($dryRun) { $toRemove[$type][] = $composer[$type][$package]; } else { $json->removeLink($type, $composer[$type][$package]); } } elseif (isset($composer[$altType][$package])) { $io->writeError('' . $composer[$altType][$package] . ' could not be found in ' . $type . ' but it is present in ' . $altType . ''); if ($io->isInteractive()) { if ($io->askConfirmation('Do you want to remove it from ' . $altType . ' [yes]? ')) { if ($dryRun) { $toRemove[$altType][] = $composer[$altType][$package]; } else { $json->removeLink($altType, $composer[$altType][$package]); } } } } elseif (isset($composer[$type]) && count($matches = Preg::grep(BasePackage::packageNameToRegexp($package), array_keys($composer[$type]))) > 0) { foreach ($matches as $matchedPackage) { if ($dryRun) { $toRemove[$type][] = $matchedPackage; } else { $json->removeLink($type, $matchedPackage); } } } elseif (isset($composer[$altType]) && count($matches = Preg::grep(BasePackage::packageNameToRegexp($package), array_keys($composer[$altType]))) > 0) { foreach ($matches as $matchedPackage) { $io->writeError('' . $matchedPackage . ' could not be found in ' . $type . ' but it is present in ' . $altType . ''); if ($io->isInteractive()) { if ($io->askConfirmation('Do you want to remove it from ' . $altType . ' [yes]? ')) { if ($dryRun) { $toRemove[$altType][] = $matchedPackage; } else { $json->removeLink($altType, $matchedPackage); } } } } } else { $io->writeError(''.$package.' is not required in your composer.json and has not been removed'); } } $io->writeError(''.$file.' has been updated'); if ($input->getOption('no-update')) { return 0; } if ($composer = $this->tryComposer()) { $composer->getPluginManager()->deactivateInstalledPlugins(); } $this->resetComposer(); $composer = $this->requireComposer(); if ($dryRun) { $rootPackage = $composer->getPackage(); $links = [ 'require' => $rootPackage->getRequires(), 'require-dev' => $rootPackage->getDevRequires(), ]; foreach ($toRemove as $type => $names) { foreach ($names as $name) { unset($links[$type][$name]); } } $rootPackage->setRequires($links['require']); $rootPackage->setDevRequires($links['require-dev']); } $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'remove', $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); $allowPlugins = $composer->getConfig()->get('allow-plugins'); $removedPlugins = is_array($allowPlugins) ? array_intersect(array_keys($allowPlugins), $packages) : []; if (!$dryRun && is_array($allowPlugins) && count($removedPlugins) > 0) { if (count($allowPlugins) === count($removedPlugins)) { $json->removeConfigSetting('allow-plugins'); } else { foreach ($removedPlugins as $plugin) { $json->removeConfigSetting('allow-plugins.'.$plugin); } } } $composer->getInstallationManager()->setOutputProgress(!$input->getOption('no-progress')); $install = Installer::create($io, $composer); $updateDevMode = !$input->getOption('update-no-dev'); $optimize = $input->getOption('optimize-autoloader') || $composer->getConfig()->get('optimize-autoloader'); $authoritative = $input->getOption('classmap-authoritative') || $composer->getConfig()->get('classmap-authoritative'); $apcuPrefix = $input->getOption('apcu-autoloader-prefix'); $apcu = $apcuPrefix !== null || $input->getOption('apcu-autoloader') || $composer->getConfig()->get('apcu-autoloader'); $updateAllowTransitiveDependencies = Request::UPDATE_LISTED_WITH_TRANSITIVE_DEPS_NO_ROOT_REQUIRE; $flags = ''; if ($input->getOption('update-with-all-dependencies') || $input->getOption('with-all-dependencies')) { $updateAllowTransitiveDependencies = Request::UPDATE_LISTED_WITH_TRANSITIVE_DEPS; $flags .= ' --with-all-dependencies'; } elseif ($input->getOption('no-update-with-dependencies')) { $updateAllowTransitiveDependencies = Request::UPDATE_ONLY_LISTED; $flags .= ' --with-dependencies'; } $io->writeError('Running composer update '.implode(' ', $packages).$flags.''); $install ->setVerbose($input->getOption('verbose')) ->setDevMode($updateDevMode) ->setOptimizeAutoloader($optimize) ->setClassMapAuthoritative($authoritative) ->setApcuAutoloader($apcu, $apcuPrefix) ->setUpdate(true) ->setInstall(!$input->getOption('no-install')) ->setUpdateAllowTransitiveDependencies($updateAllowTransitiveDependencies) ->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input)) ->setDryRun($dryRun) ->setAudit(!$input->getOption('no-audit')) ->setAuditFormat($this->getAuditFormat($input)) ; if ($composer->getLocker()->isLocked()) { $install->setUpdateAllowList($packages); } $status = $install->run(); if ($status !== 0) { $io->writeError("\n".'Removal failed, reverting '.$file.' to its original content.'); file_put_contents($jsonFile->getPath(), $composerBackup); } if (!$dryRun) { foreach ($packages as $package) { if ($composer->getRepositoryManager()->getLocalRepository()->findPackages($package)) { $io->writeError('Removal failed, '.$package.' is still present, it may be required by another package. See `composer why '.$package.'`.'); return 2; } } } return $status; } } setName('require') ->setAliases(['r']) ->setDescription('Adds required packages to your composer.json and installs them') ->setDefinition([ new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Optional package name can also include a version constraint, e.g. foo/bar or foo/bar:1.0.0 or foo/bar=1.0.0 or "foo/bar 1.0.0"', null, $this->suggestAvailablePackageInclPlatform()), new InputOption('dev', null, InputOption::VALUE_NONE, 'Add requirement to require-dev.'), new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'), new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), new InputOption('prefer-dist', null, InputOption::VALUE_NONE, 'Forces installation from package dist (default behavior).'), new InputOption('prefer-install', null, InputOption::VALUE_REQUIRED, 'Forces installation from package dist|source|auto (auto chooses source for dev versions, dist for the rest).', null, $this->suggestPreferInstall()), new InputOption('fixed', null, InputOption::VALUE_NONE, 'Write fixed version to the composer.json.'), new InputOption('no-suggest', null, InputOption::VALUE_NONE, 'DEPRECATED: This flag does not exist anymore.'), new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'), new InputOption('no-update', null, InputOption::VALUE_NONE, 'Disables the automatic update of the dependencies (implies --no-install).'), new InputOption('no-install', null, InputOption::VALUE_NONE, 'Skip the install step after updating the composer.lock file.'), new InputOption('no-audit', null, InputOption::VALUE_NONE, 'Skip the audit step after updating the composer.lock file (can also be set via the COMPOSER_NO_AUDIT=1 env var).'), new InputOption('audit-format', null, InputOption::VALUE_REQUIRED, 'Audit output format. Must be "table", "plain", "json", or "summary".', Auditor::FORMAT_SUMMARY, Auditor::FORMATS), new InputOption('update-no-dev', null, InputOption::VALUE_NONE, 'Run the dependency update with the --no-dev option.'), new InputOption('update-with-dependencies', 'w', InputOption::VALUE_NONE, 'Allows inherited dependencies to be updated, except those that are root requirements.'), new InputOption('update-with-all-dependencies', 'W', InputOption::VALUE_NONE, 'Allows all inherited dependencies to be updated, including those that are root requirements.'), new InputOption('with-dependencies', null, InputOption::VALUE_NONE, 'Alias for --update-with-dependencies'), new InputOption('with-all-dependencies', null, InputOption::VALUE_NONE, 'Alias for --update-with-all-dependencies'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), new InputOption('prefer-stable', null, InputOption::VALUE_NONE, 'Prefer stable versions of dependencies (can also be set via the COMPOSER_PREFER_STABLE=1 env var).'), new InputOption('prefer-lowest', null, InputOption::VALUE_NONE, 'Prefer lowest versions of dependencies (can also be set via the COMPOSER_PREFER_LOWEST=1 env var).'), new InputOption('sort-packages', null, InputOption::VALUE_NONE, 'Sorts packages when adding/updating a new dependency'), new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump'), new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.'), new InputOption('apcu-autoloader', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'), new InputOption('apcu-autoloader-prefix', null, InputOption::VALUE_REQUIRED, 'Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu-autoloader'), ]) ->setHelp( <<file = Factory::getComposerFile(); $io = $this->getIO(); if ($input->getOption('no-suggest')) { $io->writeError('You are using the deprecated option "--no-suggest". It has no effect and will break in Composer 3.'); } $this->newlyCreated = !file_exists($this->file); if ($this->newlyCreated && !file_put_contents($this->file, "{\n}\n")) { $io->writeError(''.$this->file.' could not be created.'); return 1; } if (!Filesystem::isReadable($this->file)) { $io->writeError(''.$this->file.' is not readable.'); return 1; } if (filesize($this->file) === 0) { file_put_contents($this->file, "{\n}\n"); } $this->json = new JsonFile($this->file); $this->lock = Factory::getLockFile($this->file); $this->composerBackup = file_get_contents($this->json->getPath()); $this->lockBackup = file_exists($this->lock) ? file_get_contents($this->lock) : null; $signalHandler = SignalHandler::create([SignalHandler::SIGINT, SignalHandler::SIGTERM, SignalHandler::SIGHUP], function (string $signal, SignalHandler $handler) { $this->getIO()->writeError('Received '.$signal.', aborting', true, IOInterface::DEBUG); $this->revertComposerFile(); $handler->exitWithLastSignal(); }); if (!is_writable($this->file) && false === Silencer::call('file_put_contents', $this->file, $this->composerBackup)) { $io->writeError(''.$this->file.' is not writable.'); return 1; } if ($input->getOption('fixed') === true) { $config = $this->json->read(); $packageType = empty($config['type']) ? 'library' : $config['type']; if ($packageType !== 'project' && !$input->getOption('dev')) { $io->writeError('The "--fixed" option is only allowed for packages with a "project" type or for dev dependencies to prevent possible misuses.'); if (!isset($config['type'])) { $io->writeError('If your package is not a library, you can explicitly specify the "type" by using "composer config type project".'); } return 1; } } $composer = $this->requireComposer(); $repos = $composer->getRepositoryManager()->getRepositories(); $platformOverrides = $composer->getConfig()->get('platform'); $this->repos = new CompositeRepository(array_merge( [$platformRepo = new PlatformRepository([], $platformOverrides)], $repos )); if ($composer->getPackage()->getPreferStable()) { $preferredStability = 'stable'; } else { $preferredStability = $composer->getPackage()->getMinimumStability(); } try { $requirements = $this->determineRequirements( $input, $output, $input->getArgument('packages'), $platformRepo, $preferredStability, !$input->getOption('no-update'), $input->getOption('fixed') ); } catch (\Exception $e) { if ($this->newlyCreated) { $this->revertComposerFile(); throw new \RuntimeException('No composer.json present in the current directory ('.$this->file.'), this may be the cause of the following exception.', 0, $e); } throw $e; } $requirements = $this->formatRequirements($requirements); if (!$input->getOption('dev') && $io->isInteractive()) { $devPackages = []; $devTags = ['dev', 'testing', 'static analysis']; $currentRequiresByKey = $this->getPackagesByRequireKey(); foreach ($requirements as $name => $version) { if (isset($currentRequiresByKey[$name])) { continue; } $pkg = PackageSorter::getMostCurrentVersion($this->getRepos()->findPackages($name)); if ($pkg instanceof CompletePackageInterface) { $pkgDevTags = array_intersect($devTags, array_map('strtolower', $pkg->getKeywords())); if (count($pkgDevTags) > 0) { $devPackages[] = $pkgDevTags; } } } if (count($devPackages) === count($requirements)) { $plural = count($requirements) > 1 ? 's' : ''; $plural2 = count($requirements) > 1 ? 'are' : 'is'; $plural3 = count($requirements) > 1 ? 'they are' : 'it is'; $pkgDevTags = array_unique(array_merge(...$devPackages)); $io->warning('The package'.$plural.' you required '.$plural2.' recommended to be placed in require-dev (because '.$plural3.' tagged as "'.implode('", "', $pkgDevTags).'") but you did not use --dev.'); if ($io->askConfirmation('Do you want to re-run the command with --dev? [yes]? ')) { $input->setOption('dev', true); } } unset($devPackages, $pkgDevTags); } $requireKey = $input->getOption('dev') ? 'require-dev' : 'require'; $removeKey = $input->getOption('dev') ? 'require' : 'require-dev'; $versionParser = new VersionParser(); foreach ($requirements as $package => $constraint) { if (strtolower($package) === $composer->getPackage()->getName()) { $io->writeError(sprintf('Root package \'%s\' cannot require itself in its composer.json', $package)); return 1; } if ($constraint === 'self.version') { continue; } $versionParser->parseConstraints($constraint); } $inconsistentRequireKeys = $this->getInconsistentRequireKeys($requirements, $requireKey); if (count($inconsistentRequireKeys) > 0) { foreach ($inconsistentRequireKeys as $package) { $io->warning(sprintf( '%s is currently present in the %s key and you ran the command %s the --dev flag, which will move it to the %s key.', $package, $removeKey, $input->getOption('dev') ? 'with' : 'without', $requireKey )); } if ($io->isInteractive()) { if (!$io->askConfirmation(sprintf('Do you want to move %s? [no]? ', count($inconsistentRequireKeys) > 1 ? 'these requirements' : 'this requirement'), false)) { if (!$io->askConfirmation(sprintf('Do you want to re-run the command %s --dev? [yes]? ', $input->getOption('dev') ? 'without' : 'with'), true)) { return 0; } $input->setOption('dev', true); [$requireKey, $removeKey] = [$removeKey, $requireKey]; } } } $sortPackages = $input->getOption('sort-packages') || $composer->getConfig()->get('sort-packages'); $this->firstRequire = $this->newlyCreated; if (!$this->firstRequire) { $composerDefinition = $this->json->read(); if (count($composerDefinition['require'] ?? []) === 0 && count($composerDefinition['require-dev'] ?? []) === 0) { $this->firstRequire = true; } } if (!$input->getOption('dry-run') && !$this->updateFileCleanly($this->json, $requirements, $requireKey, $removeKey, $sortPackages)) { $composerDefinition = $this->json->read(); foreach ($requirements as $package => $version) { $composerDefinition[$requireKey][$package] = $version; unset($composerDefinition[$removeKey][$package]); if (isset($composerDefinition[$removeKey]) && count($composerDefinition[$removeKey]) === 0) { unset($composerDefinition[$removeKey]); } } $this->json->write($composerDefinition); } $io->writeError(''.$this->file.' has been '.($this->newlyCreated ? 'created' : 'updated').''); if ($input->getOption('no-update')) { return 0; } $composer->getPluginManager()->deactivateInstalledPlugins(); try { return $this->doUpdate($input, $output, $io, $requirements, $requireKey, $removeKey); } catch (\Exception $e) { if (!$this->dependencyResolutionCompleted) { $this->revertComposerFile(); } throw $e; } finally { $signalHandler->unregister(); } } private function getInconsistentRequireKeys(array $newRequirements, string $requireKey): array { $requireKeys = $this->getPackagesByRequireKey(); $inconsistentRequirements = []; foreach ($requireKeys as $package => $packageRequireKey) { if (!isset($newRequirements[$package])) { continue; } if ($requireKey !== $packageRequireKey) { $inconsistentRequirements[] = $package; } } return $inconsistentRequirements; } private function getPackagesByRequireKey(): array { $composerDefinition = $this->json->read(); $require = []; $requireDev = []; if (isset($composerDefinition['require'])) { $require = $composerDefinition['require']; } if (isset($composerDefinition['require-dev'])) { $requireDev = $composerDefinition['require-dev']; } return array_merge( array_fill_keys(array_keys($require), 'require'), array_fill_keys(array_keys($requireDev), 'require-dev') ); } private function doUpdate(InputInterface $input, OutputInterface $output, IOInterface $io, array $requirements, string $requireKey, string $removeKey): int { $this->resetComposer(); $composer = $this->requireComposer(); $this->dependencyResolutionCompleted = false; $composer->getEventDispatcher()->addListener(InstallerEvents::PRE_OPERATIONS_EXEC, function (): void { $this->dependencyResolutionCompleted = true; }, 10000); if ($input->getOption('dry-run')) { $rootPackage = $composer->getPackage(); $links = [ 'require' => $rootPackage->getRequires(), 'require-dev' => $rootPackage->getDevRequires(), ]; $loader = new ArrayLoader(); $newLinks = $loader->parseLinks($rootPackage->getName(), $rootPackage->getPrettyVersion(), BasePackage::$supportedLinkTypes[$requireKey]['method'], $requirements); $links[$requireKey] = array_merge($links[$requireKey], $newLinks); foreach ($requirements as $package => $constraint) { unset($links[$removeKey][$package]); } $rootPackage->setRequires($links['require']); $rootPackage->setDevRequires($links['require-dev']); } $updateDevMode = !$input->getOption('update-no-dev'); $optimize = $input->getOption('optimize-autoloader') || $composer->getConfig()->get('optimize-autoloader'); $authoritative = $input->getOption('classmap-authoritative') || $composer->getConfig()->get('classmap-authoritative'); $apcuPrefix = $input->getOption('apcu-autoloader-prefix'); $apcu = $apcuPrefix !== null || $input->getOption('apcu-autoloader') || $composer->getConfig()->get('apcu-autoloader'); $updateAllowTransitiveDependencies = Request::UPDATE_ONLY_LISTED; $flags = ''; if ($input->getOption('update-with-all-dependencies') || $input->getOption('with-all-dependencies')) { $updateAllowTransitiveDependencies = Request::UPDATE_LISTED_WITH_TRANSITIVE_DEPS; $flags .= ' --with-all-dependencies'; } elseif ($input->getOption('update-with-dependencies') || $input->getOption('with-dependencies')) { $updateAllowTransitiveDependencies = Request::UPDATE_LISTED_WITH_TRANSITIVE_DEPS_NO_ROOT_REQUIRE; $flags .= ' --with-dependencies'; } $io->writeError('Running composer update '.implode(' ', array_keys($requirements)).$flags.''); $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'require', $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); $composer->getInstallationManager()->setOutputProgress(!$input->getOption('no-progress')); $install = Installer::create($io, $composer); [$preferSource, $preferDist] = $this->getPreferredInstallOptions($composer->getConfig(), $input); $install ->setDryRun($input->getOption('dry-run')) ->setVerbose($input->getOption('verbose')) ->setPreferSource($preferSource) ->setPreferDist($preferDist) ->setDevMode($updateDevMode) ->setOptimizeAutoloader($optimize) ->setClassMapAuthoritative($authoritative) ->setApcuAutoloader($apcu, $apcuPrefix) ->setUpdate(true) ->setInstall(!$input->getOption('no-install')) ->setUpdateAllowTransitiveDependencies($updateAllowTransitiveDependencies) ->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input)) ->setPreferStable($input->getOption('prefer-stable')) ->setPreferLowest($input->getOption('prefer-lowest')) ->setAudit(!$input->getOption('no-audit')) ->setAuditFormat($this->getAuditFormat($input)) ; if (!$this->firstRequire && $composer->getLocker()->isLocked()) { $install->setUpdateAllowList(array_keys($requirements)); } $status = $install->run(); if ($status !== 0) { if ($status === Installer::ERROR_DEPENDENCY_RESOLUTION_FAILED) { foreach ($this->normalizeRequirements($input->getArgument('packages')) as $req) { if (!isset($req['version'])) { $io->writeError('You can also try re-running composer require with an explicit version constraint, e.g. "composer require '.$req['name'].':*" to figure out if any version is installable, or "composer require '.$req['name'].':^2.1" if you know which you need.'); break; } } } $this->revertComposerFile(); } return $status; } private function updateFileCleanly(JsonFile $json, array $new, string $requireKey, string $removeKey, bool $sortPackages): bool { $contents = file_get_contents($json->getPath()); $manipulator = new JsonManipulator($contents); foreach ($new as $package => $constraint) { if (!$manipulator->addLink($requireKey, $package, $constraint, $sortPackages)) { return false; } if (!$manipulator->removeSubNode($removeKey, $package)) { return false; } } $manipulator->removeMainKeyIfEmpty($removeKey); file_put_contents($json->getPath(), $manipulator->getContents()); return true; } protected function interact(InputInterface $input, OutputInterface $output): void { } private function revertComposerFile(): void { $io = $this->getIO(); if ($this->newlyCreated) { $io->writeError("\n".'Installation failed, deleting '.$this->file.'.'); unlink($this->json->getPath()); if (file_exists($this->lock)) { unlink($this->lock); } } else { $msg = ' to its '; if ($this->lockBackup) { $msg = ' and '.$this->lock.' to their '; } $io->writeError("\n".'Installation failed, reverting '.$this->file.$msg.'original content.'); file_put_contents($this->json->getPath(), $this->composerBackup); if ($this->lockBackup) { file_put_contents($this->lock, $this->lockBackup); } } } } setName('run-script') ->setAliases(['run']) ->setDescription('Runs the scripts defined in composer.json') ->setDefinition([ new InputArgument('script', InputArgument::OPTIONAL, 'Script name to run.', null, function () { return array_keys($this->requireComposer()->getPackage()->getScripts()); }), new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''), new InputOption('timeout', null, InputOption::VALUE_REQUIRED, 'Sets script timeout in seconds, or 0 for never.'), new InputOption('dev', null, InputOption::VALUE_NONE, 'Sets the dev mode.'), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables the dev mode.'), new InputOption('list', 'l', InputOption::VALUE_NONE, 'List scripts.'), ]) ->setHelp( <<run-script command runs scripts defined in composer.json: php composer.phar run-script post-update-cmd Read more at https://getcomposer.org/doc/03-cli.md#run-script EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { if ($input->getOption('list')) { return $this->listScripts($output); } $script = $input->getArgument('script'); if ($script === null) { throw new \RuntimeException('Missing required argument "script"'); } if (!in_array($script, $this->scriptEvents)) { if (defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) { throw new \InvalidArgumentException(sprintf('Script "%s" cannot be run with this command', $script)); } } $composer = $this->requireComposer(); $devMode = $input->getOption('dev') || !$input->getOption('no-dev'); $event = new ScriptEvent($script, $composer, $this->getIO(), $devMode); $hasListeners = $composer->getEventDispatcher()->hasEventListeners($event); if (!$hasListeners) { throw new \InvalidArgumentException(sprintf('Script "%s" is not defined in this package', $script)); } $args = $input->getArgument('args'); if (null !== $timeout = $input->getOption('timeout')) { if (!ctype_digit($timeout)) { throw new \RuntimeException('Timeout value must be numeric and positive if defined, or 0 for forever'); } ProcessExecutor::setTimeout((int) $timeout); } Platform::putEnv('COMPOSER_DEV_MODE', $devMode ? '1' : '0'); return $composer->getEventDispatcher()->dispatchScript($script, $devMode, $args); } protected function listScripts(OutputInterface $output): int { $scripts = $this->requireComposer()->getPackage()->getScripts(); if (!count($scripts)) { return 0; } $io = $this->getIO(); $io->writeError('scripts:'); $table = []; foreach ($scripts as $name => $script) { $description = ''; try { $cmd = $this->getApplication()->find($name); if ($cmd instanceof ScriptAliasCommand) { $description = $cmd->getDescription(); } } catch (\Symfony\Component\Console\Exception\CommandNotFoundException $e) { } $table[] = [' '.$name, $description]; } $this->renderTable($table, $output); return 0; } } script = $script; $this->description = $description ?? 'Runs the '.$script.' script as defined in composer.json'; parent::__construct(); } protected function configure(): void { $this ->setName($this->script) ->setDescription($this->description) ->setDefinition([ new InputOption('dev', null, InputOption::VALUE_NONE, 'Sets the dev mode.'), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables the dev mode.'), new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''), ]) ->setHelp( <<run-script command runs scripts defined in composer.json: php composer.phar run-script post-update-cmd Read more at https://getcomposer.org/doc/03-cli.md#run-script EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { $composer = $this->requireComposer(); $args = $input->getArguments(); return $composer->getEventDispatcher()->dispatchScript($this->script, $input->getOption('dev') || !$input->getOption('no-dev'), $args['args']); } } setName('search') ->setDescription('Searches for packages') ->setDefinition([ new InputOption('only-name', 'N', InputOption::VALUE_NONE, 'Search only in package names'), new InputOption('only-vendor', 'O', InputOption::VALUE_NONE, 'Search only for vendor / organization names, returns only "vendor" as result'), new InputOption('type', 't', InputOption::VALUE_REQUIRED, 'Search for a specific package type'), new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text', ['json', 'text']), new InputArgument('tokens', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'tokens to search for'), ]) ->setHelp( <<php composer.phar search symfony composer Read more at https://getcomposer.org/doc/03-cli.md#search EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { $platformRepo = new PlatformRepository; $io = $this->getIO(); $format = $input->getOption('format'); if (!in_array($format, ['text', 'json'])) { $io->writeError(sprintf('Unsupported format "%s". See help for supported formats.', $format)); return 1; } if (!($composer = $this->tryComposer())) { $composer = Factory::create($this->getIO(), [], $input->hasParameterOption('--no-plugins')); } $localRepo = $composer->getRepositoryManager()->getLocalRepository(); $installedRepo = new CompositeRepository([$localRepo, $platformRepo]); $repos = new CompositeRepository(array_merge([$installedRepo], $composer->getRepositoryManager()->getRepositories())); $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'search', $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); $mode = RepositoryInterface::SEARCH_FULLTEXT; if ($input->getOption('only-name') === true) { if ($input->getOption('only-vendor') === true) { throw new \InvalidArgumentException('--only-name and --only-vendor cannot be used together'); } $mode = RepositoryInterface::SEARCH_NAME; } elseif ($input->getOption('only-vendor') === true) { $mode = RepositoryInterface::SEARCH_VENDOR; } $type = $input->getOption('type'); $query = implode(' ', $input->getArgument('tokens')); if ($mode !== RepositoryInterface::SEARCH_FULLTEXT) { $query = preg_quote($query); } $results = $repos->search($query, $mode, $type); if ($results && $format === 'text') { $width = $this->getTerminalWidth(); $nameLength = 0; foreach ($results as $result) { $nameLength = max(strlen($result['name']), $nameLength); } $nameLength += 1; foreach ($results as $result) { $description = $result['description'] ?? ''; $warning = !empty($result['abandoned']) ? '! Abandoned ! ' : ''; $remaining = $width - $nameLength - strlen($warning) - 2; if (strlen($description) > $remaining) { $description = substr($description, 0, $remaining - 3) . '...'; } $link = $result['url'] ?? null; if ($link !== null) { $io->write(''.$result['name'].''. str_repeat(' ', $nameLength - strlen($result['name'])) . $warning . $description); } else { $io->write(str_pad($result['name'], $nameLength, ' ') . $warning . $description); } } } elseif ($format === 'json') { $io->write(JsonFile::encode($results)); } return 0; } } setName('self-update') ->setAliases(['selfupdate']) ->setDescription('Updates composer.phar to the latest version') ->setDefinition([ new InputOption('rollback', 'r', InputOption::VALUE_NONE, 'Revert to an older installation of composer'), new InputOption('clean-backups', null, InputOption::VALUE_NONE, 'Delete old backups during an update. This makes the current version of composer the only backup available after the update'), new InputArgument('version', InputArgument::OPTIONAL, 'The version to update to'), new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'), new InputOption('update-keys', null, InputOption::VALUE_NONE, 'Prompt user for a key update'), new InputOption('stable', null, InputOption::VALUE_NONE, 'Force an update to the stable channel'), new InputOption('preview', null, InputOption::VALUE_NONE, 'Force an update to the preview channel'), new InputOption('snapshot', null, InputOption::VALUE_NONE, 'Force an update to the snapshot channel'), new InputOption('1', null, InputOption::VALUE_NONE, 'Force an update to the stable channel, but only use 1.x versions'), new InputOption('2', null, InputOption::VALUE_NONE, 'Force an update to the stable channel, but only use 2.x versions'), new InputOption('2.2', null, InputOption::VALUE_NONE, 'Force an update to the stable channel, but only use 2.2.x LTS versions'), new InputOption('set-channel-only', null, InputOption::VALUE_NONE, 'Only store the channel as the default one and then exit'), ]) ->setHelp( <<self-update command checks getcomposer.org for newer versions of composer and if found, installs the latest. php composer.phar self-update Read more at https://getcomposer.org/doc/03-cli.md#self-update-selfupdate- EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { if ($_SERVER['argv'][0] === 'Standard input code') { return 1; } class_exists('Composer\Util\Platform'); class_exists('Composer\Downloader\FilesystemException'); $config = Factory::createConfig(); if ($config->get('disable-tls') === true) { $baseUrl = 'http://' . self::HOMEPAGE; } else { $baseUrl = 'https://' . self::HOMEPAGE; } $io = $this->getIO(); $httpDownloader = Factory::createHttpDownloader($io, $config); $versionsUtil = new Versions($config, $httpDownloader); $requestedChannel = null; foreach (Versions::CHANNELS as $channel) { if ($input->getOption($channel)) { $requestedChannel = $channel; $versionsUtil->setChannel($channel, $io); break; } } if ($input->getOption('set-channel-only')) { return 0; } $cacheDir = $config->get('cache-dir'); $rollbackDir = $config->get('data-dir'); $home = $config->get('home'); $localFilename = realpath($_SERVER['argv'][0]); if (false === $localFilename) { $localFilename = $_SERVER['argv'][0]; } if ($input->getOption('update-keys')) { $this->fetchKeys($io, $config); return 0; } if (!file_exists($localFilename)) { throw new FilesystemException('Composer update failed: the "'.$localFilename.'" is not accessible'); } $tmpDir = is_writable(dirname($localFilename)) ? dirname($localFilename) : $cacheDir; if (!is_writable($tmpDir)) { throw new FilesystemException('Composer update failed: the "'.$tmpDir.'" directory used to download the temp file could not be written'); } if (function_exists('posix_getpwuid') && function_exists('posix_geteuid')) { $composerUser = posix_getpwuid(posix_geteuid()); $homeDirOwnerId = fileowner($home); if (is_array($composerUser) && $homeDirOwnerId !== false) { $homeOwner = posix_getpwuid($homeDirOwnerId); if (is_array($homeOwner) && isset($composerUser['name'], $homeOwner['name']) && $composerUser['name'] !== $homeOwner['name']) { $io->writeError('You are running Composer as "'.$composerUser['name'].'", while "'.$home.'" is owned by "'.$homeOwner['name'].'"'); } } } if ($input->getOption('rollback')) { return $this->rollback($output, $rollbackDir, $localFilename); } $latest = $versionsUtil->getLatest(); $latestStable = $versionsUtil->getLatest('stable'); try { $latestPreview = $versionsUtil->getLatest('preview'); } catch (\UnexpectedValueException $e) { $latestPreview = $latestStable; } $latestVersion = $latest['version']; $updateVersion = $input->getArgument('version') ?? $latestVersion; $currentMajorVersion = Preg::replace('{^(\d+).*}', '$1', Composer::getVersion()); $updateMajorVersion = Preg::replace('{^(\d+).*}', '$1', $updateVersion); $previewMajorVersion = Preg::replace('{^(\d+).*}', '$1', $latestPreview['version']); if ($versionsUtil->getChannel() === 'stable' && null === $input->getArgument('version')) { if ($currentMajorVersion < $updateMajorVersion) { $skippedVersion = $updateVersion; $versionsUtil->setChannel($currentMajorVersion); $latest = $versionsUtil->getLatest(); $latestStable = $versionsUtil->getLatest('stable'); $latestVersion = $latest['version']; $updateVersion = $latestVersion; $io->writeError('A new stable major version of Composer is available ('.$skippedVersion.'), run "composer self-update --'.$updateMajorVersion.'" to update to it. See also https://getcomposer.org/'.$updateMajorVersion.''); } elseif ($currentMajorVersion < $previewMajorVersion) { $io->writeError('A preview release of the next major version of Composer is available ('.$latestPreview['version'].'), run "composer self-update --preview" to give it a try. See also https://github.com/composer/composer/releases for changelogs.'); } } $effectiveChannel = $requestedChannel === null ? $versionsUtil->getChannel() : $requestedChannel; if (is_numeric($effectiveChannel) && strpos($latestStable['version'], $effectiveChannel) !== 0) { $io->writeError('Warning: You forced the install of '.$latestVersion.' via --'.$effectiveChannel.', but '.$latestStable['version'].' is the latest stable version. Updating to it via composer self-update --stable is recommended.'); } if (isset($latest['eol'])) { $io->writeError('Warning: Version '.$latestVersion.' is EOL / End of Life. '.$latestStable['version'].' is the latest stable version. Updating to it via composer self-update --stable is recommended.'); } if (Preg::isMatch('{^[0-9a-f]{40}$}', $updateVersion) && $updateVersion !== $latestVersion) { $io->writeError('You can not update to a specific SHA-1 as those phars are not available for download'); return 1; } $channelString = $versionsUtil->getChannel(); if (is_numeric($channelString)) { $channelString .= '.x'; } if (Composer::VERSION === $updateVersion) { $io->writeError( sprintf( 'You are already using the latest available Composer version %s (%s channel).', $updateVersion, $channelString ) ); if ($input->getOption('clean-backups')) { $this->cleanBackups($rollbackDir, $this->getLastBackupVersion($rollbackDir)); } return 0; } $tempFilename = $tmpDir . '/' . basename($localFilename, '.phar').'-temp'.random_int(0, 10000000).'.phar'; $backupFile = sprintf( '%s/%s-%s%s', $rollbackDir, strtr(Composer::RELEASE_DATE, ' :', '_-'), Preg::replace('{^([0-9a-f]{7})[0-9a-f]{33}$}', '$1', Composer::VERSION), self::OLD_INSTALL_EXT ); $updatingToTag = !Preg::isMatch('{^[0-9a-f]{40}$}', $updateVersion); $io->write(sprintf("Upgrading to version %s (%s channel).", $updateVersion, $channelString)); $remoteFilename = $baseUrl . ($updatingToTag ? "/download/{$updateVersion}/composer.phar" : '/composer.phar'); try { $signature = $httpDownloader->get($remoteFilename.'.sig')->getBody(); } catch (TransportException $e) { if ($e->getStatusCode() === 404) { throw new \InvalidArgumentException('Version "'.$updateVersion.'" could not be found.', 0, $e); } throw $e; } $io->writeError(' ', false); $httpDownloader->copy($remoteFilename, $tempFilename); $io->writeError(''); if (!file_exists($tempFilename) || null === $signature || '' === $signature) { $io->writeError('The download of the new composer version failed for an unexpected reason'); return 1; } if (!extension_loaded('openssl') && $config->get('disable-tls')) { $io->writeError('Skipping phar signature verification as you have disabled OpenSSL via config.disable-tls'); } else { if (!extension_loaded('openssl')) { throw new \RuntimeException('The openssl extension is required for phar signatures to be verified but it is not available. ' . 'If you can not enable the openssl extension, you can disable this error, at your own risk, by setting the \'disable-tls\' option to true.'); } $sigFile = 'file://'.$home.'/' . ($updatingToTag ? 'keys.tags.pub' : 'keys.dev.pub'); if (!file_exists($sigFile)) { file_put_contents( $home.'/keys.dev.pub', <<getOption('clean-backups')) { $this->cleanBackups($rollbackDir); } if (!$this->setLocalPhar($localFilename, $tempFilename, $backupFile)) { @unlink($tempFilename); return 1; } if (file_exists($backupFile)) { $io->writeError(sprintf( 'Use composer self-update --rollback to return to version %s', Composer::VERSION )); } else { $io->writeError('A backup of the current version could not be written to '.$backupFile.', no rollback possible'); } return 0; } protected function fetchKeys(IOInterface $io, Config $config): void { if (!$io->isInteractive()) { throw new \RuntimeException('Public keys can not be fetched in non-interactive mode, please run Composer interactively'); } $io->write('Open https://composer.github.io/pubkeys.html to find the latest keys'); $validator = static function ($value): string { if (!Preg::isMatch('{^-----BEGIN PUBLIC KEY-----$}', trim($value))) { throw new \UnexpectedValueException('Invalid input'); } return trim($value)."\n"; }; $devKey = ''; while (!Preg::isMatch('{(-----BEGIN PUBLIC KEY-----.+?-----END PUBLIC KEY-----)}s', $devKey, $match)) { $devKey = $io->askAndValidate('Enter Dev / Snapshot Public Key (including lines with -----): ', $validator); while ($line = $io->ask('')) { $devKey .= trim($line)."\n"; if (trim($line) === '-----END PUBLIC KEY-----') { break; } } } file_put_contents($keyPath = $config->get('home').'/keys.dev.pub', $match[0]); $io->write('Stored key with fingerprint: ' . Keys::fingerprint($keyPath)); $tagsKey = ''; while (!Preg::isMatch('{(-----BEGIN PUBLIC KEY-----.+?-----END PUBLIC KEY-----)}s', $tagsKey, $match)) { $tagsKey = $io->askAndValidate('Enter Tags Public Key (including lines with -----): ', $validator); while ($line = $io->ask('')) { $tagsKey .= trim($line)."\n"; if (trim($line) === '-----END PUBLIC KEY-----') { break; } } } file_put_contents($keyPath = $config->get('home').'/keys.tags.pub', $match[0]); $io->write('Stored key with fingerprint: ' . Keys::fingerprint($keyPath)); $io->write('Public keys stored in '.$config->get('home')); } protected function rollback(OutputInterface $output, string $rollbackDir, string $localFilename): int { $rollbackVersion = $this->getLastBackupVersion($rollbackDir); if (null === $rollbackVersion) { throw new \UnexpectedValueException('Composer rollback failed: no installation to roll back to in "'.$rollbackDir.'"'); } $oldFile = $rollbackDir . '/' . $rollbackVersion . self::OLD_INSTALL_EXT; if (!is_file($oldFile)) { throw new FilesystemException('Composer rollback failed: "'.$oldFile.'" could not be found'); } if (!Filesystem::isReadable($oldFile)) { throw new FilesystemException('Composer rollback failed: "'.$oldFile.'" could not be read'); } $io = $this->getIO(); $io->writeError(sprintf("Rolling back to version %s.", $rollbackVersion)); if (!$this->setLocalPhar($localFilename, $oldFile)) { return 1; } return 0; } protected function setLocalPhar(string $localFilename, string $newFilename, ?string $backupTarget = null): bool { $io = $this->getIO(); $perms = @fileperms($localFilename); if ($perms !== false) { @chmod($newFilename, $perms); } if (!$this->validatePhar($newFilename, $error)) { $io->writeError('The '.($backupTarget !== null ? 'update' : 'backup').' file is corrupted ('.$error.')'); if ($backupTarget !== null) { $io->writeError('Please re-run the self-update command to try again.'); } return false; } if ($backupTarget !== null) { @copy($localFilename, $backupTarget); } try { if (Platform::isWindows()) { copy($newFilename, $localFilename); @unlink($newFilename); } else { rename($newFilename, $localFilename); } return true; } catch (\Exception $e) { if (!is_writable(dirname($localFilename)) && $io->isInteractive() && $this->isWindowsNonAdminUser()) { return $this->tryAsWindowsAdmin($localFilename, $newFilename); } @unlink($newFilename); $action = 'Composer '.($backupTarget !== null ? 'update' : 'rollback'); throw new FilesystemException($action.' failed: "'.$localFilename.'" could not be written.'.PHP_EOL.$e->getMessage()); } } protected function cleanBackups(string $rollbackDir, ?string $except = null): void { $finder = $this->getOldInstallationFinder($rollbackDir); $io = $this->getIO(); $fs = new Filesystem; foreach ($finder as $file) { if ($file->getBasename(self::OLD_INSTALL_EXT) === $except) { continue; } $file = (string) $file; $io->writeError('Removing: '.$file.''); $fs->remove($file); } } protected function getLastBackupVersion(string $rollbackDir): ?string { $finder = $this->getOldInstallationFinder($rollbackDir); $finder->sortByName(); $files = iterator_to_array($finder); if (count($files) > 0) { return end($files)->getBasename(self::OLD_INSTALL_EXT); } return null; } protected function getOldInstallationFinder(string $rollbackDir): Finder { return Finder::create() ->depth(0) ->files() ->name('*' . self::OLD_INSTALL_EXT) ->in($rollbackDir); } protected function validatePhar(string $pharFile, ?string &$error): bool { if ((bool) ini_get('phar.readonly')) { return true; } try { $phar = new \Phar($pharFile); unset($phar); $result = true; } catch (\Exception $e) { if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) { throw $e; } $error = $e->getMessage(); $result = false; } return $result; } protected function isWindowsNonAdminUser(): bool { if (!Platform::isWindows()) { return false; } exec('fltmc.exe filters', $output, $exitCode); return $exitCode !== 0; } protected function tryAsWindowsAdmin(string $localFilename, string $newFilename): bool { $io = $this->getIO(); $io->writeError('Unable to write "'.$localFilename.'". Access is denied.'); $helpMessage = 'Please run the self-update command as an Administrator.'; $question = 'Complete this operation with Administrator privileges [Y,n]? '; if (!$io->askConfirmation($question, true)) { $io->writeError('Operation cancelled. '.$helpMessage.''); return false; } $tmpFile = tempnam(sys_get_temp_dir(), ''); if (false === $tmpFile) { $io->writeError('Operation failed.'.$helpMessage.''); return false; } $script = $tmpFile.'.vbs'; rename($tmpFile, $script); $checksum = hash_file('sha256', $newFilename); $source = str_replace('/', '\\', $newFilename); $destination = str_replace('/', '\\', $localFilename); $vbs = <<writeError('Operation succeeded.'); @unlink($newFilename); } else { $io->writeError('Operation failed.'.$helpMessage.''); } return $result; } } setName('show') ->setAliases(['info']) ->setDescription('Shows information about packages') ->setDefinition([ new InputArgument('package', InputArgument::OPTIONAL, 'Package to inspect. Or a name including a wildcard (*) to filter lists of packages instead.', null, $this->suggestPackageBasedOnMode()), new InputArgument('version', InputArgument::OPTIONAL, 'Version or version constraint to inspect'), new InputOption('all', null, InputOption::VALUE_NONE, 'List all packages'), new InputOption('locked', null, InputOption::VALUE_NONE, 'List all locked packages'), new InputOption('installed', 'i', InputOption::VALUE_NONE, 'List installed packages only (enabled by default, only present for BC).'), new InputOption('platform', 'p', InputOption::VALUE_NONE, 'List platform packages only'), new InputOption('available', 'a', InputOption::VALUE_NONE, 'List available packages only'), new InputOption('self', 's', InputOption::VALUE_NONE, 'Show the root package information'), new InputOption('name-only', 'N', InputOption::VALUE_NONE, 'List package names only'), new InputOption('path', 'P', InputOption::VALUE_NONE, 'Show package paths'), new InputOption('tree', 't', InputOption::VALUE_NONE, 'List the dependencies as a tree'), new InputOption('latest', 'l', InputOption::VALUE_NONE, 'Show the latest version'), new InputOption('outdated', 'o', InputOption::VALUE_NONE, 'Show the latest version but only for packages that are outdated'), new InputOption('ignore', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore specified package(s). Use it with the --outdated option if you don\'t want to be informed about new versions of some packages.', null, $this->suggestInstalledPackage(false)), new InputOption('major-only', 'M', InputOption::VALUE_NONE, 'Show only packages that have major SemVer-compatible updates. Use with the --latest or --outdated option.'), new InputOption('minor-only', 'm', InputOption::VALUE_NONE, 'Show only packages that have minor SemVer-compatible updates. Use with the --latest or --outdated option.'), new InputOption('patch-only', null, InputOption::VALUE_NONE, 'Show only packages that have patch SemVer-compatible updates. Use with the --latest or --outdated option.'), new InputOption('direct', 'D', InputOption::VALUE_NONE, 'Shows only packages that are directly required by the root package'), new InputOption('strict', null, InputOption::VALUE_NONE, 'Return a non-zero exit code when there are outdated packages'), new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text', ['json', 'text']), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables search in require-dev packages.'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages). Use with the --outdated option'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages). Use with the --outdated option'), ]) ->setHelp( <<getOption('available') || $input->getOption('all')) { return $this->suggestAvailablePackageInclPlatform()($input); } if ($input->getOption('platform')) { return $this->suggestPlatformPackage()($input); } return $this->suggestInstalledPackage(false)($input); }; } protected function execute(InputInterface $input, OutputInterface $output) { $this->versionParser = new VersionParser; if ($input->getOption('tree')) { $this->initStyles($output); } $composer = $this->tryComposer(); $io = $this->getIO(); if ($input->getOption('installed')) { $io->writeError('You are using the deprecated option "installed". Only installed packages are shown by default now. The --all option can be used to show all packages.'); } if ($input->getOption('outdated')) { $input->setOption('latest', true); } elseif (count($input->getOption('ignore')) > 0) { $io->writeError('You are using the option "ignore" for action other than "outdated", it will be ignored.'); } if ($input->getOption('direct') && ($input->getOption('all') || $input->getOption('available') || $input->getOption('platform'))) { $io->writeError('The --direct (-D) option is not usable in combination with --all, --platform (-p) or --available (-a)'); return 1; } if ($input->getOption('tree') && ($input->getOption('all') || $input->getOption('available'))) { $io->writeError('The --tree (-t) option is not usable in combination with --all or --available (-a)'); return 1; } if (count(array_filter([$input->getOption('patch-only'), $input->getOption('minor-only'), $input->getOption('major-only')])) > 1) { $io->writeError('Only one of --major-only, --minor-only or --patch-only can be used at once'); return 1; } if ($input->getOption('tree') && $input->getOption('latest')) { $io->writeError('The --tree (-t) option is not usable in combination with --latest (-l)'); return 1; } if ($input->getOption('tree') && $input->getOption('path')) { $io->writeError('The --tree (-t) option is not usable in combination with --path (-P)'); return 1; } $format = $input->getOption('format'); if (!in_array($format, ['text', 'json'])) { $io->writeError(sprintf('Unsupported format "%s". See help for supported formats.', $format)); return 1; } $platformReqFilter = $this->getPlatformRequirementFilter($input); $platformOverrides = []; if ($composer) { $platformOverrides = $composer->getConfig()->get('platform'); } $platformRepo = new PlatformRepository([], $platformOverrides); $lockedRepo = null; if ($input->getOption('self')) { $package = clone $this->requireComposer()->getPackage(); if ($input->getOption('name-only')) { $io->write($package->getName()); return 0; } if ($input->getArgument('package')) { throw new \InvalidArgumentException('You cannot use --self together with a package name'); } $repos = $installedRepo = new InstalledRepository([new RootPackageRepository($package)]); } elseif ($input->getOption('platform')) { $repos = $installedRepo = new InstalledRepository([$platformRepo]); } elseif ($input->getOption('available')) { $installedRepo = new InstalledRepository([$platformRepo]); if ($composer) { $repos = new CompositeRepository($composer->getRepositoryManager()->getRepositories()); $installedRepo->addRepository($composer->getRepositoryManager()->getLocalRepository()); } else { $defaultRepos = RepositoryFactory::defaultReposWithDefaultManager($io); $repos = new CompositeRepository($defaultRepos); $io->writeError('No composer.json found in the current directory, showing available packages from ' . implode(', ', array_keys($defaultRepos))); } } elseif ($input->getOption('all') && $composer) { $localRepo = $composer->getRepositoryManager()->getLocalRepository(); $locker = $composer->getLocker(); if ($locker->isLocked()) { $lockedRepo = $locker->getLockedRepository(true); $installedRepo = new InstalledRepository([$lockedRepo, $localRepo, $platformRepo]); } else { $installedRepo = new InstalledRepository([$localRepo, $platformRepo]); } $repos = new CompositeRepository(array_merge([new FilterRepository($installedRepo, ['canonical' => false])], $composer->getRepositoryManager()->getRepositories())); } elseif ($input->getOption('all')) { $defaultRepos = RepositoryFactory::defaultReposWithDefaultManager($io); $io->writeError('No composer.json found in the current directory, showing available packages from ' . implode(', ', array_keys($defaultRepos))); $installedRepo = new InstalledRepository([$platformRepo]); $repos = new CompositeRepository(array_merge([$installedRepo], $defaultRepos)); } elseif ($input->getOption('locked')) { if (!$composer || !$composer->getLocker()->isLocked()) { throw new \UnexpectedValueException('A valid composer.json and composer.lock files is required to run this command with --locked'); } $locker = $composer->getLocker(); $lockedRepo = $locker->getLockedRepository(!$input->getOption('no-dev')); $repos = $installedRepo = new InstalledRepository([$lockedRepo]); } else { if (!$composer) { $composer = $this->requireComposer(); } $rootPkg = $composer->getPackage(); $repos = $installedRepo = new InstalledRepository([$composer->getRepositoryManager()->getLocalRepository()]); if ($input->getOption('no-dev')) { $packages = RepositoryUtils::filterRequiredPackages($installedRepo->getPackages(), $rootPkg); $repos = $installedRepo = new InstalledRepository([new InstalledArrayRepository(array_map(static function ($pkg): PackageInterface { return clone $pkg; }, $packages))]); } if (!$installedRepo->getPackages() && ($rootPkg->getRequires() || $rootPkg->getDevRequires())) { $io->writeError('No dependencies installed. Try running composer install or update.'); } } if ($composer) { $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'show', $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); } if ($input->getOption('latest') && null === $composer) { $io->writeError('No composer.json found in the current directory, disabling "latest" option'); $input->setOption('latest', false); } $packageFilter = $input->getArgument('package'); if (isset($package)) { $versions = [$package->getPrettyVersion() => $package->getVersion()]; } elseif (null !== $packageFilter && !str_contains($packageFilter, '*')) { [$package, $versions] = $this->getPackage($installedRepo, $repos, $packageFilter, $input->getArgument('version')); if (!isset($package)) { $options = $input->getOptions(); $hint = ''; if ($input->getOption('locked')) { $hint .= ' in lock file'; } if (isset($options['working-dir'])) { $hint .= ' in ' . $options['working-dir'] . '/composer.json'; } if (PlatformRepository::isPlatformPackage($packageFilter) && !$input->getOption('platform')) { $hint .= ', try using --platform (-p) to show platform packages'; } if (!$input->getOption('all')) { $hint .= ', try using --available (-a) to show all available packages'; } throw new \InvalidArgumentException('Package "' . $packageFilter . '" not found'.$hint.'.'); } } if (isset($package)) { assert(isset($versions)); $exitCode = 0; if ($input->getOption('tree')) { $arrayTree = $this->generatePackageTree($package, $installedRepo, $repos); if ('json' === $format) { $io->write(JsonFile::encode(['installed' => [$arrayTree]])); } else { $this->displayPackageTree([$arrayTree]); } return $exitCode; } $latestPackage = null; if ($input->getOption('latest')) { $latestPackage = $this->findLatestPackage($package, $composer, $platformRepo, $input->getOption('major-only'), $input->getOption('minor-only'), $input->getOption('patch-only'), $platformReqFilter); } if ( $input->getOption('outdated') && $input->getOption('strict') && null !== $latestPackage && $latestPackage->getFullPrettyVersion() !== $package->getFullPrettyVersion() && (!$latestPackage instanceof CompletePackageInterface || !$latestPackage->isAbandoned()) ) { $exitCode = 1; } if ($input->getOption('path')) { $io->write($package->getName(), false); $io->write(' ' . strtok(realpath($composer->getInstallationManager()->getInstallPath($package)), "\r\n")); return $exitCode; } if ('json' === $format) { $this->printPackageInfoAsJson($package, $versions, $installedRepo, $latestPackage ?: null); } else { $this->printPackageInfo($package, $versions, $installedRepo, $latestPackage ?: null); } return $exitCode; } if ($input->getOption('tree')) { $rootRequires = $this->getRootRequires(); $packages = $installedRepo->getPackages(); usort($packages, static function (BasePackage $a, BasePackage $b): int { return strcmp((string) $a, (string) $b); }); $arrayTree = []; foreach ($packages as $package) { if (in_array($package->getName(), $rootRequires, true)) { $arrayTree[] = $this->generatePackageTree($package, $installedRepo, $repos); } } if ('json' === $format) { $io->write(JsonFile::encode(['installed' => $arrayTree])); } else { $this->displayPackageTree($arrayTree); } return 0; } $packages = []; $packageFilterRegex = null; if (null !== $packageFilter) { $packageFilterRegex = '{^'.str_replace('\\*', '.*?', preg_quote($packageFilter)).'$}i'; } $packageListFilter = null; if ($input->getOption('direct')) { $packageListFilter = $this->getRootRequires(); } if ($input->getOption('path') && null === $composer) { $io->writeError('No composer.json found in the current directory, disabling "path" option'); $input->setOption('path', false); } foreach (RepositoryUtils::flattenRepositories($repos) as $repo) { if ($repo === $platformRepo) { $type = 'platform'; } elseif ($lockedRepo !== null && $repo === $lockedRepo) { $type = 'locked'; } elseif ($repo === $installedRepo || in_array($repo, $installedRepo->getRepositories(), true)) { $type = 'installed'; } else { $type = 'available'; } if ($repo instanceof ComposerRepository) { foreach ($repo->getPackageNames($packageFilter) as $name) { $packages[$type][$name] = $name; } } else { foreach ($repo->getPackages() as $package) { if (!isset($packages[$type][$package->getName()]) || !is_object($packages[$type][$package->getName()]) || version_compare($packages[$type][$package->getName()]->getVersion(), $package->getVersion(), '<') ) { while ($package instanceof AliasPackage) { $package = $package->getAliasOf(); } if (!$packageFilterRegex || Preg::isMatch($packageFilterRegex, $package->getName())) { if (null === $packageListFilter || in_array($package->getName(), $packageListFilter, true)) { $packages[$type][$package->getName()] = $package; } } } } if ($repo === $platformRepo) { foreach ($platformRepo->getDisabledPackages() as $name => $package) { $packages[$type][$name] = $package; } } } } $showAllTypes = $input->getOption('all'); $showLatest = $input->getOption('latest'); $showMajorOnly = $input->getOption('major-only'); $showMinorOnly = $input->getOption('minor-only'); $showPatchOnly = $input->getOption('patch-only'); $ignoredPackages = array_map('strtolower', $input->getOption('ignore')); $indent = $showAllTypes ? ' ' : ''; $latestPackages = []; $exitCode = 0; $viewData = []; $viewMetaData = []; foreach (['platform' => true, 'locked' => true, 'available' => false, 'installed' => true] as $type => $showVersion) { if (isset($packages[$type])) { ksort($packages[$type]); $nameLength = $versionLength = $latestLength = 0; if ($showLatest && $showVersion) { foreach ($packages[$type] as $package) { if (is_object($package)) { $latestPackage = $this->findLatestPackage($package, $composer, $platformRepo, $showMajorOnly, $showMinorOnly, $showPatchOnly, $platformReqFilter); if ($latestPackage === null) { continue; } $latestPackages[$package->getPrettyName()] = $latestPackage; } } } $writePath = !$input->getOption('name-only') && $input->getOption('path'); $writeVersion = !$input->getOption('name-only') && !$input->getOption('path') && $showVersion; $writeLatest = $writeVersion && $showLatest; $writeDescription = !$input->getOption('name-only') && !$input->getOption('path'); $hasOutdatedPackages = false; $viewData[$type] = []; foreach ($packages[$type] as $package) { $packageViewData = []; if (is_object($package)) { $latestPackage = null; if ($showLatest && isset($latestPackages[$package->getPrettyName()])) { $latestPackage = $latestPackages[$package->getPrettyName()]; } $packageIsUpToDate = $latestPackage && $latestPackage->getFullPrettyVersion() === $package->getFullPrettyVersion() && (!$latestPackage instanceof CompletePackageInterface || !$latestPackage->isAbandoned()); $packageIsUpToDate = $packageIsUpToDate || ($latestPackage === null && $showMajorOnly); $packageIsIgnored = \in_array($package->getPrettyName(), $ignoredPackages, true); if ($input->getOption('outdated') && ($packageIsUpToDate || $packageIsIgnored)) { continue; } if ($input->getOption('outdated') || $input->getOption('strict')) { $hasOutdatedPackages = true; } $packageViewData['name'] = $package->getPrettyName(); $packageViewData['direct-dependency'] = in_array($package->getName(), $this->getRootRequires(), true); if ($format !== 'json' || true !== $input->getOption('name-only')) { $packageViewData['homepage'] = $package instanceof CompletePackageInterface ? $package->getHomepage() : null; $packageViewData['source'] = PackageInfo::getViewSourceUrl($package); } $nameLength = max($nameLength, strlen($package->getPrettyName())); if ($writeVersion) { $packageViewData['version'] = $package->getFullPrettyVersion(); $versionLength = max($versionLength, strlen($package->getFullPrettyVersion())); } if ($writeLatest && $latestPackage) { $packageViewData['latest'] = $latestPackage->getFullPrettyVersion(); $packageViewData['latest-status'] = $this->getUpdateStatus($latestPackage, $package); $latestLength = max($latestLength, strlen($packageViewData['latest'])); } elseif ($writeLatest) { $packageViewData['latest'] = '[none matched]'; $packageViewData['latest-status'] = 'up-to-date'; $latestLength = max($latestLength, strlen($packageViewData['latest'])); } if ($writeDescription && $package instanceof CompletePackageInterface) { $packageViewData['description'] = $package->getDescription(); } if ($writePath) { $packageViewData['path'] = strtok(realpath($composer->getInstallationManager()->getInstallPath($package)), "\r\n"); } $packageIsAbandoned = false; if ($latestPackage instanceof CompletePackageInterface && $latestPackage->isAbandoned()) { $replacementPackageName = $latestPackage->getReplacementPackage(); $replacement = $replacementPackageName !== null ? 'Use ' . $latestPackage->getReplacementPackage() . ' instead' : 'No replacement was suggested'; $packageWarning = sprintf( 'Package %s is abandoned, you should avoid using it. %s.', $package->getPrettyName(), $replacement ); $packageViewData['warning'] = $packageWarning; $packageIsAbandoned = $replacementPackageName ?? true; } $packageViewData['abandoned'] = $packageIsAbandoned; } else { $packageViewData['name'] = $package; $nameLength = max($nameLength, strlen($package)); } $viewData[$type][] = $packageViewData; } $viewMetaData[$type] = [ 'nameLength' => $nameLength, 'versionLength' => $versionLength, 'latestLength' => $latestLength, 'writeLatest' => $writeLatest, ]; if ($input->getOption('strict') && $hasOutdatedPackages) { $exitCode = 1; break; } } } if ('json' === $format) { $io->write(JsonFile::encode($viewData)); } else { if ($input->getOption('latest') && array_filter($viewData)) { if (!$io->isDecorated()) { $io->writeError('Legend:'); $io->writeError('! patch or minor release available - update recommended'); $io->writeError('~ major release available - update possible'); if (!$input->getOption('outdated')) { $io->writeError('= up to date version'); } } else { $io->writeError('Color legend:'); $io->writeError('- patch or minor release available - update recommended'); $io->writeError('- major release available - update possible'); if (!$input->getOption('outdated')) { $io->writeError('- up to date version'); } } } $width = $this->getTerminalWidth(); foreach ($viewData as $type => $packages) { $nameLength = $viewMetaData[$type]['nameLength']; $versionLength = $viewMetaData[$type]['versionLength']; $latestLength = $viewMetaData[$type]['latestLength']; $writeLatest = $viewMetaData[$type]['writeLatest']; $versionFits = $nameLength + $versionLength + 3 <= $width; $latestFits = $nameLength + $versionLength + $latestLength + 3 <= $width; $descriptionFits = $nameLength + $versionLength + $latestLength + 24 <= $width; if ($latestFits && !$io->isDecorated()) { $latestLength += 2; } if ($showAllTypes) { if ('available' === $type) { $io->write('' . $type . ':'); } else { $io->write('' . $type . ':'); } } if ($writeLatest && !$input->getOption('direct')) { $directDeps = []; $transitiveDeps = []; foreach ($packages as $pkg) { if ($pkg['direct-dependency'] ?? false) { $directDeps[] = $pkg; } else { $transitiveDeps[] = $pkg; } } $io->write(''); $io->write('Direct dependencies required in composer.json:'); if (\count($directDeps) > 0) { $this->printPackages($io, $directDeps, $indent, $versionFits, $latestFits, $descriptionFits, $width, $versionLength, $nameLength, $latestLength); } else { $io->write('Everything up to date'); } $io->write(''); $io->write('Transitive dependencies not required in composer.json:'); if (\count($transitiveDeps) > 0) { $this->printPackages($io, $transitiveDeps, $indent, $versionFits, $latestFits, $descriptionFits, $width, $versionLength, $nameLength, $latestLength); } else { $io->write('Everything up to date'); } } else { $this->printPackages($io, $packages, $indent, $versionFits, $latestFits, $descriptionFits, $width, $versionLength, $nameLength, $latestLength); } if ($showAllTypes) { $io->write(''); } } } return $exitCode; } private function printPackages(IOInterface $io, array $packages, string $indent, bool $writeVersion, bool $writeLatest, bool $writeDescription, int $width, int $versionLength, int $nameLength, int $latestLength): void { foreach ($packages as $package) { $link = $package['source'] ?? $package['homepage'] ?? ''; if ($link !== '') { $io->write($indent . ''.$package['name'].''. str_repeat(' ', $nameLength - strlen($package['name'])), false); } else { $io->write($indent . str_pad($package['name'], $nameLength, ' '), false); } if (isset($package['version']) && $writeVersion) { $io->write(' ' . str_pad($package['version'], $versionLength, ' '), false); } if (isset($package['latest']) && isset($package['latest-status']) && $writeLatest) { $latestVersion = $package['latest']; $updateStatus = $package['latest-status']; $style = $this->updateStatusToVersionStyle($updateStatus); if (!$io->isDecorated()) { $latestVersion = str_replace(['up-to-date', 'semver-safe-update', 'update-possible'], ['=', '!', '~'], $updateStatus) . ' ' . $latestVersion; } $io->write(' <' . $style . '>' . str_pad($latestVersion, $latestLength, ' ') . '', false); } if (isset($package['description']) && $writeDescription) { $description = strtok($package['description'], "\r\n"); $remaining = $width - $nameLength - $versionLength - 4; if ($writeLatest) { $remaining -= $latestLength; } if (strlen($description) > $remaining) { $description = substr($description, 0, $remaining - 3) . '...'; } $io->write(' ' . $description, false); } if (isset($package['path'])) { $io->write(' ' . $package['path'], false); } $io->write(''); if (isset($package['warning'])) { $io->write('' . $package['warning'] . ''); } } } protected function getRootRequires(): array { $composer = $this->tryComposer(); if ($composer === null) { return []; } $rootPackage = $composer->getPackage(); return array_map( 'strtolower', array_keys(array_merge($rootPackage->getRequires(), $rootPackage->getDevRequires())) ); } protected function getVersionStyle(PackageInterface $latestPackage, PackageInterface $package) { return $this->updateStatusToVersionStyle($this->getUpdateStatus($latestPackage, $package)); } protected function getPackage(InstalledRepository $installedRepo, RepositoryInterface $repos, string $name, $version = null): array { $name = strtolower($name); $constraint = is_string($version) ? $this->versionParser->parseConstraints($version) : $version; $policy = new DefaultPolicy(); $repositorySet = new RepositorySet('dev'); $repositorySet->allowInstalledRepositories(); $repositorySet->addRepository($repos); $matchedPackage = null; $versions = []; if (PlatformRepository::isPlatformPackage($name)) { $pool = $repositorySet->createPoolWithAllPackages(); } else { $pool = $repositorySet->createPoolForPackage($name); } $matches = $pool->whatProvides($name, $constraint); foreach ($matches as $index => $package) { if ($package instanceof AliasPackage && $package->getVersion() === VersionParser::DEFAULT_BRANCH_ALIAS) { $package = $package->getAliasOf(); } if (null === $version && $installedRepo->hasPackage($package)) { $matchedPackage = $package; } $versions[$package->getPrettyVersion()] = $package->getVersion(); $matches[$index] = $package->getId(); } if (!$matchedPackage && $matches && $preferred = $policy->selectPreferredPackages($pool, $matches)) { $matchedPackage = $pool->literalToPackage($preferred[0]); } return [$matchedPackage, $versions]; } protected function printPackageInfo(CompletePackageInterface $package, array $versions, InstalledRepository $installedRepo, ?PackageInterface $latestPackage = null): void { $io = $this->getIO(); $this->printMeta($package, $versions, $installedRepo, $latestPackage ?: null); $this->printLinks($package, Link::TYPE_REQUIRE); $this->printLinks($package, Link::TYPE_DEV_REQUIRE, 'requires (dev)'); if ($package->getSuggests()) { $io->write("\nsuggests"); foreach ($package->getSuggests() as $suggested => $reason) { $io->write($suggested . ' ' . $reason . ''); } } $this->printLinks($package, Link::TYPE_PROVIDE); $this->printLinks($package, Link::TYPE_CONFLICT); $this->printLinks($package, Link::TYPE_REPLACE); } protected function printMeta(CompletePackageInterface $package, array $versions, InstalledRepository $installedRepo, ?PackageInterface $latestPackage = null): void { $io = $this->getIO(); $io->write('name : ' . $package->getPrettyName()); $io->write('descrip. : ' . $package->getDescription()); $io->write('keywords : ' . implode(', ', $package->getKeywords() ?: [])); $this->printVersions($package, $versions, $installedRepo); if ($latestPackage) { $style = $this->getVersionStyle($latestPackage, $package); $io->write('latest : <'.$style.'>' . $latestPackage->getPrettyVersion() . ''); } else { $latestPackage = $package; } $io->write('type : ' . $package->getType()); $this->printLicenses($package); $io->write('homepage : ' . $package->getHomepage()); $io->write('source : ' . sprintf('[%s] %s %s', $package->getSourceType(), $package->getSourceUrl(), $package->getSourceReference())); $io->write('dist : ' . sprintf('[%s] %s %s', $package->getDistType(), $package->getDistUrl(), $package->getDistReference())); if ($installedRepo->hasPackage($package)) { $io->write('path : ' . sprintf('%s', realpath($this->requireComposer()->getInstallationManager()->getInstallPath($package)))); } $io->write('names : ' . implode(', ', $package->getNames())); if ($latestPackage instanceof CompletePackageInterface && $latestPackage->isAbandoned()) { $replacement = ($latestPackage->getReplacementPackage() !== null) ? ' The author suggests using the ' . $latestPackage->getReplacementPackage(). ' package instead.' : null; $io->writeError( sprintf('Attention: This package is abandoned and no longer maintained.%s', $replacement) ); } if ($package->getSupport()) { $io->write("\nsupport"); foreach ($package->getSupport() as $type => $value) { $io->write('' . $type . ' : '.$value); } } if ($package->getAutoload()) { $io->write("\nautoload"); $autoloadConfig = $package->getAutoload(); foreach ($autoloadConfig as $type => $autoloads) { $io->write('' . $type . ''); if ($type === 'psr-0' || $type === 'psr-4') { foreach ($autoloads as $name => $path) { $io->write(($name ?: '*') . ' => ' . (is_array($path) ? implode(', ', $path) : ($path ?: '.'))); } } elseif ($type === 'classmap') { $io->write(implode(', ', $autoloadConfig[$type])); } } if ($package->getIncludePaths()) { $io->write('include-path'); $io->write(implode(', ', $package->getIncludePaths())); } } } protected function printVersions(CompletePackageInterface $package, array $versions, InstalledRepository $installedRepo): void { $versions = array_keys($versions); $versions = Semver::rsort($versions); if ($installedPackages = $installedRepo->findPackages($package->getName())) { foreach ($installedPackages as $installedPackage) { $installedVersion = $installedPackage->getPrettyVersion(); $key = array_search($installedVersion, $versions); if (false !== $key) { $versions[$key] = '* ' . $installedVersion . ''; } } } $versions = implode(', ', $versions); $this->getIO()->write('versions : ' . $versions); } protected function printLinks(CompletePackageInterface $package, string $linkType, ?string $title = null): void { $title = $title ?: $linkType; $io = $this->getIO(); if ($links = $package->{'get'.ucfirst($linkType)}()) { $io->write("\n" . $title . ""); foreach ($links as $link) { $io->write($link->getTarget() . ' ' . $link->getPrettyConstraint() . ''); } } } protected function printLicenses(CompletePackageInterface $package): void { $spdxLicenses = new SpdxLicenses(); $licenses = $package->getLicense(); $io = $this->getIO(); foreach ($licenses as $licenseId) { $license = $spdxLicenses->getLicenseByIdentifier($licenseId); if (!$license) { $out = $licenseId; } else { if ($license[1] === true) { $out = sprintf('%s (%s) (OSI approved) %s', $license[0], $licenseId, $license[2]); } else { $out = sprintf('%s (%s) %s', $license[0], $licenseId, $license[2]); } } $io->write('license : ' . $out); } } protected function printPackageInfoAsJson(CompletePackageInterface $package, array $versions, InstalledRepository $installedRepo, ?PackageInterface $latestPackage = null): void { $json = [ 'name' => $package->getPrettyName(), 'description' => $package->getDescription(), 'keywords' => $package->getKeywords() ?: [], 'type' => $package->getType(), 'homepage' => $package->getHomepage(), 'names' => $package->getNames(), ]; $json = $this->appendVersions($json, $versions); $json = $this->appendLicenses($json, $package); if ($latestPackage) { $json['latest'] = $latestPackage->getPrettyVersion(); } else { $latestPackage = $package; } if (null !== $package->getSourceType()) { $json['source'] = [ 'type' => $package->getSourceType(), 'url' => $package->getSourceUrl(), 'reference' => $package->getSourceReference(), ]; } if (null !== $package->getDistType()) { $json['dist'] = [ 'type' => $package->getDistType(), 'url' => $package->getDistUrl(), 'reference' => $package->getDistReference(), ]; } if ($installedRepo->hasPackage($package)) { $json['path'] = realpath($this->requireComposer()->getInstallationManager()->getInstallPath($package)); if ($json['path'] === false) { unset($json['path']); } } if ($latestPackage instanceof CompletePackageInterface && $latestPackage->isAbandoned()) { $json['replacement'] = $latestPackage->getReplacementPackage(); } if ($package->getSuggests()) { $json['suggests'] = $package->getSuggests(); } if ($package->getSupport()) { $json['support'] = $package->getSupport(); } $json = $this->appendAutoload($json, $package); if ($package->getIncludePaths()) { $json['include_path'] = $package->getIncludePaths(); } $json = $this->appendLinks($json, $package); $this->getIO()->write(JsonFile::encode($json)); } private function appendVersions(array $json, array $versions): array { uasort($versions, 'version_compare'); $versions = array_keys(array_reverse($versions)); $json['versions'] = $versions; return $json; } private function appendLicenses(array $json, CompletePackageInterface $package): array { if ($licenses = $package->getLicense()) { $spdxLicenses = new SpdxLicenses(); $json['licenses'] = array_map(static function ($licenseId) use ($spdxLicenses) { $license = $spdxLicenses->getLicenseByIdentifier($licenseId); if (!$license) { return $licenseId; } return [ 'name' => $license[0], 'osi' => $licenseId, 'url' => $license[2], ]; }, $licenses); } return $json; } private function appendAutoload(array $json, CompletePackageInterface $package): array { if ($package->getAutoload()) { $autoload = []; foreach ($package->getAutoload() as $type => $autoloads) { if ($type === 'psr-0' || $type === 'psr-4') { $psr = []; foreach ($autoloads as $name => $path) { if (!$path) { $path = '.'; } $psr[$name ?: '*'] = $path; } $autoload[$type] = $psr; } elseif ($type === 'classmap') { $autoload['classmap'] = $autoloads; } } $json['autoload'] = $autoload; } return $json; } private function appendLinks(array $json, CompletePackageInterface $package): array { foreach (Link::$TYPES as $linkType) { $json = $this->appendLink($json, $package, $linkType); } return $json; } private function appendLink(array $json, CompletePackageInterface $package, string $linkType): array { $links = $package->{'get' . ucfirst($linkType)}(); if ($links) { $json[$linkType] = []; foreach ($links as $link) { $json[$linkType][$link->getTarget()] = $link->getPrettyConstraint(); } } return $json; } protected function initStyles(OutputInterface $output): void { $this->colors = [ 'green', 'yellow', 'cyan', 'magenta', 'blue', ]; foreach ($this->colors as $color) { $style = new OutputFormatterStyle($color); $output->getFormatter()->setStyle($color, $style); } } protected function displayPackageTree(array $arrayTree): void { $io = $this->getIO(); foreach ($arrayTree as $package) { $io->write(sprintf('%s', $package['name']), false); $io->write(' ' . $package['version'], false); if (isset($package['description'])) { $io->write(' ' . strtok($package['description'], "\r\n")); } else { $io->write(''); } if (isset($package['requires'])) { $requires = $package['requires']; $treeBar = '├'; $j = 0; $total = count($requires); foreach ($requires as $require) { $requireName = $require['name']; $j++; if ($j === $total) { $treeBar = '└'; } $level = 1; $color = $this->colors[$level]; $info = sprintf( '%s──<%s>%s %s', $treeBar, $color, $requireName, $color, $require['version'] ); $this->writeTreeLine($info); $treeBar = str_replace('└', ' ', $treeBar); $packagesInTree = [$package['name'], $requireName]; $this->displayTree($require, $packagesInTree, $treeBar, $level + 1); } } } } protected function generatePackageTree( PackageInterface $package, InstalledRepository $installedRepo, RepositoryInterface $remoteRepos ): array { $requires = $package->getRequires(); ksort($requires); $children = []; foreach ($requires as $requireName => $require) { $packagesInTree = [$package->getName(), $requireName]; $treeChildDesc = [ 'name' => $requireName, 'version' => $require->getPrettyConstraint(), ]; $deepChildren = $this->addTree($requireName, $require, $installedRepo, $remoteRepos, $packagesInTree); if ($deepChildren) { $treeChildDesc['requires'] = $deepChildren; } $children[] = $treeChildDesc; } $tree = [ 'name' => $package->getPrettyName(), 'version' => $package->getPrettyVersion(), 'description' => $package instanceof CompletePackageInterface ? $package->getDescription() : '', ]; if ($children) { $tree['requires'] = $children; } return $tree; } protected function displayTree( $package, array $packagesInTree, string $previousTreeBar = '├', int $level = 1 ): void { $previousTreeBar = str_replace('├', '│', $previousTreeBar); if (is_array($package) && isset($package['requires'])) { $requires = $package['requires']; $treeBar = $previousTreeBar . ' ├'; $i = 0; $total = count($requires); foreach ($requires as $require) { $currentTree = $packagesInTree; $i++; if ($i === $total) { $treeBar = $previousTreeBar . ' └'; } $colorIdent = $level % count($this->colors); $color = $this->colors[$colorIdent]; $circularWarn = in_array( $require['name'], $currentTree, true ) ? '(circular dependency aborted here)' : ''; $info = rtrim(sprintf( '%s──<%s>%s %s %s', $treeBar, $color, $require['name'], $color, $require['version'], $circularWarn )); $this->writeTreeLine($info); $treeBar = str_replace('└', ' ', $treeBar); $currentTree[] = $require['name']; $this->displayTree($require, $currentTree, $treeBar, $level + 1); } } } protected function addTree( string $name, Link $link, InstalledRepository $installedRepo, RepositoryInterface $remoteRepos, array $packagesInTree ): array { $children = []; [$package] = $this->getPackage( $installedRepo, $remoteRepos, $name, $link->getPrettyConstraint() === 'self.version' ? $link->getConstraint() : $link->getPrettyConstraint() ); if (is_object($package)) { $requires = $package->getRequires(); ksort($requires); foreach ($requires as $requireName => $require) { $currentTree = $packagesInTree; $treeChildDesc = [ 'name' => $requireName, 'version' => $require->getPrettyConstraint(), ]; if (!in_array($requireName, $currentTree, true)) { $currentTree[] = $requireName; $deepChildren = $this->addTree($requireName, $require, $installedRepo, $remoteRepos, $currentTree); if ($deepChildren) { $treeChildDesc['requires'] = $deepChildren; } } $children[] = $treeChildDesc; } } return $children; } private function updateStatusToVersionStyle(string $updateStatus): string { return str_replace(['up-to-date', 'semver-safe-update', 'update-possible'], ['info', 'highlight', 'comment'], $updateStatus); } private function getUpdateStatus(PackageInterface $latestPackage, PackageInterface $package): string { if ($latestPackage->getFullPrettyVersion() === $package->getFullPrettyVersion()) { return 'up-to-date'; } $constraint = $package->getVersion(); if (0 !== strpos($constraint, 'dev-')) { $constraint = '^'.$constraint; } if ($latestPackage->getVersion() && Semver::satisfies($latestPackage->getVersion(), $constraint)) { return 'semver-safe-update'; } return 'update-possible'; } private function writeTreeLine(string $line): void { $io = $this->getIO(); if (!$io->isDecorated()) { $line = str_replace(['└', '├', '──', '│'], ['`-', '|-', '-', '|'], $line); } $io->write($line); } private function findLatestPackage(PackageInterface $package, Composer $composer, PlatformRepository $platformRepo, bool $majorOnly, bool $minorOnly, bool $patchOnly, PlatformRequirementFilterInterface $platformReqFilter): ?PackageInterface { $name = $package->getName(); $versionSelector = new VersionSelector($this->getRepositorySet($composer), $platformRepo); $stability = $composer->getPackage()->getMinimumStability(); $flags = $composer->getPackage()->getStabilityFlags(); if (isset($flags[$name])) { $stability = array_search($flags[$name], BasePackage::$stabilities, true); } $bestStability = $stability; if ($composer->getPackage()->getPreferStable()) { $bestStability = $package->getStability(); } $targetVersion = null; if (0 === strpos($package->getVersion(), 'dev-')) { $targetVersion = $package->getVersion(); if ($majorOnly) { return null; } } if ($targetVersion === null) { if ($majorOnly && Preg::isMatch('{^(?P(?:0\.)+)?(?P\d+)\.}', $package->getVersion(), $match)) { $targetVersion = '>='.$match['zero_major'].($match['first_meaningful'] + 1).',<9999999-dev'; } if ($minorOnly) { $targetVersion = '^'.$package->getVersion(); } if ($patchOnly) { $trimmedVersion = Preg::replace('{(\.0)+$}D', '', $package->getVersion()); $partsNeeded = substr($trimmedVersion, 0, 1) === '0' ? 4 : 3; while (substr_count($trimmedVersion, '.') + 1 < $partsNeeded) { $trimmedVersion .= '.0'; } $targetVersion = '~'.$trimmedVersion; } } $candidate = $versionSelector->findBestCandidate($name, $targetVersion, $bestStability, $platformReqFilter); while ($candidate instanceof AliasPackage) { $candidate = $candidate->getAliasOf(); } return $candidate !== false ? $candidate : null; } private function getRepositorySet(Composer $composer): RepositorySet { if (!$this->repositorySet) { $this->repositorySet = new RepositorySet($composer->getPackage()->getMinimumStability(), $composer->getPackage()->getStabilityFlags()); $this->repositorySet->addRepository(new CompositeRepository($composer->getRepositoryManager()->getRepositories())); } return $this->repositorySet; } } setName('status') ->setDescription('Shows a list of locally modified packages') ->setDefinition([ new InputOption('verbose', 'v|vv|vvv', InputOption::VALUE_NONE, 'Show modified files for each directory that contains changes.'), ]) ->setHelp( <<requireComposer(); $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'status', $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); $composer->getEventDispatcher()->dispatchScript(ScriptEvents::PRE_STATUS_CMD, true); $exitCode = $this->doExecute($input); $composer->getEventDispatcher()->dispatchScript(ScriptEvents::POST_STATUS_CMD, true); return $exitCode; } private function doExecute(InputInterface $input): int { $composer = $this->requireComposer(); $installedRepo = $composer->getRepositoryManager()->getLocalRepository(); $dm = $composer->getDownloadManager(); $im = $composer->getInstallationManager(); $errors = []; $io = $this->getIO(); $unpushedChanges = []; $vcsVersionChanges = []; $parser = new VersionParser; $guesser = new VersionGuesser($composer->getConfig(), $composer->getLoop()->getProcessExecutor() ?? new ProcessExecutor($io), $parser); $dumper = new ArrayDumper; foreach ($installedRepo->getCanonicalPackages() as $package) { $downloader = $dm->getDownloaderForPackage($package); $targetDir = $im->getInstallPath($package); if ($downloader instanceof ChangeReportInterface) { if (is_link($targetDir)) { $errors[$targetDir] = $targetDir . ' is a symbolic link.'; } if (null !== ($changes = $downloader->getLocalChanges($package, $targetDir))) { $errors[$targetDir] = $changes; } } if ($downloader instanceof VcsCapableDownloaderInterface) { if ($downloader->getVcsReference($package, $targetDir)) { switch ($package->getInstallationSource()) { case 'source': $previousRef = $package->getSourceReference(); break; case 'dist': $previousRef = $package->getDistReference(); break; default: $previousRef = null; } $currentVersion = $guesser->guessVersion($dumper->dump($package), $targetDir); if ($previousRef && $currentVersion && $currentVersion['commit'] !== $previousRef) { $vcsVersionChanges[$targetDir] = [ 'previous' => [ 'version' => $package->getPrettyVersion(), 'ref' => $previousRef, ], 'current' => [ 'version' => $currentVersion['pretty_version'], 'ref' => $currentVersion['commit'], ], ]; } } } if ($downloader instanceof DvcsDownloaderInterface) { if ($unpushed = $downloader->getUnpushedChanges($package, $targetDir)) { $unpushedChanges[$targetDir] = $unpushed; } } } if (!$errors && !$unpushedChanges && !$vcsVersionChanges) { $io->writeError('No local changes'); return 0; } if ($errors) { $io->writeError('You have changes in the following dependencies:'); foreach ($errors as $path => $changes) { if ($input->getOption('verbose')) { $indentedChanges = implode("\n", array_map(static function ($line): string { return ' ' . ltrim($line); }, explode("\n", $changes))); $io->write(''.$path.':'); $io->write($indentedChanges); } else { $io->write($path); } } } if ($unpushedChanges) { $io->writeError('You have unpushed changes on the current branch in the following dependencies:'); foreach ($unpushedChanges as $path => $changes) { if ($input->getOption('verbose')) { $indentedChanges = implode("\n", array_map(static function ($line): string { return ' ' . ltrim($line); }, explode("\n", $changes))); $io->write(''.$path.':'); $io->write($indentedChanges); } else { $io->write($path); } } } if ($vcsVersionChanges) { $io->writeError('You have version variations in the following dependencies:'); foreach ($vcsVersionChanges as $path => $changes) { if ($input->getOption('verbose')) { $currentVersion = $changes['current']['version'] ?: $changes['current']['ref']; $previousVersion = $changes['previous']['version'] ?: $changes['previous']['ref']; if ($io->isVeryVerbose()) { $currentVersion .= sprintf(' (%s)', $changes['current']['ref']); $previousVersion .= sprintf(' (%s)', $changes['previous']['ref']); } $io->write(''.$path.':'); $io->write(sprintf(' From %s to %s', $previousVersion, $currentVersion)); } else { $io->write($path); } } } if (($errors || $unpushedChanges || $vcsVersionChanges) && !$input->getOption('verbose')) { $io->writeError('Use --verbose (-v) to see a list of files'); } return ($errors ? self::EXIT_CODE_ERRORS : 0) + ($unpushedChanges ? self::EXIT_CODE_UNPUSHED_CHANGES : 0) + ($vcsVersionChanges ? self::EXIT_CODE_VERSION_CHANGES : 0); } } setName('suggests') ->setDescription('Shows package suggestions') ->setDefinition([ new InputOption('by-package', null, InputOption::VALUE_NONE, 'Groups output by suggesting package (default)'), new InputOption('by-suggestion', null, InputOption::VALUE_NONE, 'Groups output by suggested package'), new InputOption('all', 'a', InputOption::VALUE_NONE, 'Show suggestions from all dependencies, including transitive ones'), new InputOption('list', null, InputOption::VALUE_NONE, 'Show only list of suggested package names'), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Exclude suggestions from require-dev packages'), new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Packages that you want to list suggestions from.', null, $this->suggestInstalledPackage()), ]) ->setHelp( <<%command.name% command shows a sorted list of suggested packages. Read more at https://getcomposer.org/doc/03-cli.md#suggests EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output): int { $composer = $this->requireComposer(); $installedRepos = [ new RootPackageRepository(clone $composer->getPackage()), ]; $locker = $composer->getLocker(); if ($locker->isLocked()) { $installedRepos[] = new PlatformRepository([], $locker->getPlatformOverrides()); $installedRepos[] = $locker->getLockedRepository(!$input->getOption('no-dev')); } else { $installedRepos[] = new PlatformRepository([], $composer->getConfig()->get('platform')); $installedRepos[] = $composer->getRepositoryManager()->getLocalRepository(); } $installedRepo = new InstalledRepository($installedRepos); $reporter = new SuggestedPackagesReporter($this->getIO()); $filter = $input->getArgument('packages'); $packages = $installedRepo->getPackages(); $packages[] = $composer->getPackage(); foreach ($packages as $package) { if (!empty($filter) && !in_array($package->getName(), $filter)) { continue; } $reporter->addSuggestionsFromPackage($package); } $mode = SuggestedPackagesReporter::MODE_BY_PACKAGE; if ($input->getOption('by-suggestion')) { $mode = SuggestedPackagesReporter::MODE_BY_SUGGESTION; } if ($input->getOption('by-package')) { $mode |= SuggestedPackagesReporter::MODE_BY_PACKAGE; } if ($input->getOption('list')) { $mode = SuggestedPackagesReporter::MODE_LIST; } $reporter->output($mode, $installedRepo, empty($filter) && !$input->getOption('all') ? $composer->getPackage() : null); return 0; } } setName('update') ->setAliases(['u', 'upgrade']) ->setDescription('Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file') ->setDefinition([ new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Packages that should be updated, if not provided all packages are.', null, $this->suggestInstalledPackage(false)), new InputOption('with', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Temporary version constraint to add, e.g. foo/bar:1.0.0 or foo/bar=1.0.0'), new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), new InputOption('prefer-dist', null, InputOption::VALUE_NONE, 'Forces installation from package dist (default behavior).'), new InputOption('prefer-install', null, InputOption::VALUE_REQUIRED, 'Forces installation from package dist|source|auto (auto chooses source for dev versions, dist for the rest).', null, $this->suggestPreferInstall()), new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'), new InputOption('dev', null, InputOption::VALUE_NONE, 'DEPRECATED: Enables installation of require-dev packages (enabled by default, only present for BC).'), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables installation of require-dev packages.'), new InputOption('lock', null, InputOption::VALUE_NONE, 'Overwrites the lock file hash to suppress warning about the lock file being out of date without updating package versions. Package metadata like mirrors and URLs are updated if they changed.'), new InputOption('no-install', null, InputOption::VALUE_NONE, 'Skip the install step after updating the composer.lock file.'), new InputOption('no-audit', null, InputOption::VALUE_NONE, 'Skip the audit step after updating the composer.lock file (can also be set via the COMPOSER_NO_AUDIT=1 env var).'), new InputOption('audit-format', null, InputOption::VALUE_REQUIRED, 'Audit output format. Must be "table", "plain", "json", or "summary".', Auditor::FORMAT_SUMMARY, Auditor::FORMATS), new InputOption('no-autoloader', null, InputOption::VALUE_NONE, 'Skips autoloader generation'), new InputOption('no-suggest', null, InputOption::VALUE_NONE, 'DEPRECATED: This flag does not exist anymore.'), new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'), new InputOption('with-dependencies', 'w', InputOption::VALUE_NONE, 'Update also dependencies of packages in the argument list, except those which are root requirements.'), new InputOption('with-all-dependencies', 'W', InputOption::VALUE_NONE, 'Update also dependencies of packages in the argument list, including those which are root requirements.'), new InputOption('verbose', 'v|vv|vvv', InputOption::VALUE_NONE, 'Shows more details including new commits pulled in when updating packages.'), new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump.'), new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.'), new InputOption('apcu-autoloader', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'), new InputOption('apcu-autoloader-prefix', null, InputOption::VALUE_REQUIRED, 'Use a custom prefix for the APCu autoloader cache. Implicitly enables --apcu-autoloader'), new InputOption('ignore-platform-req', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Ignore a specific platform requirement (php & ext- packages).'), new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore all platform requirements (php & ext- packages).'), new InputOption('prefer-stable', null, InputOption::VALUE_NONE, 'Prefer stable versions of dependencies (can also be set via the COMPOSER_PREFER_STABLE=1 env var).'), new InputOption('prefer-lowest', null, InputOption::VALUE_NONE, 'Prefer lowest versions of dependencies (can also be set via the COMPOSER_PREFER_LOWEST=1 env var).'), new InputOption('interactive', 'i', InputOption::VALUE_NONE, 'Interactive interface with autocompletion to select the packages to update.'), new InputOption('root-reqs', null, InputOption::VALUE_NONE, 'Restricts the update to your first degree dependencies.'), ]) ->setHelp( <<update command reads the composer.json file from the current directory, processes it, and updates, removes or installs all the dependencies. php composer.phar update To limit the update operation to a few packages, you can list the package(s) you want to update as such: php composer.phar update vendor/package1 foo/mypackage [...] You may also use an asterisk (*) pattern to limit the update operation to package(s) from a specific vendor: php composer.phar update vendor/package1 foo/* [...] To run an update with more restrictive constraints you can use: php composer.phar update --with vendor/package:1.0.* To run a partial update with more restrictive constraints you can use the shorthand: php composer.phar update vendor/package:1.0.* To select packages names interactively with auto-completion use -i. Read more at https://getcomposer.org/doc/03-cli.md#update-u EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $io = $this->getIO(); if ($input->getOption('dev')) { $io->writeError('You are using the deprecated option "--dev". It has no effect and will break in Composer 3.'); } if ($input->getOption('no-suggest')) { $io->writeError('You are using the deprecated option "--no-suggest". It has no effect and will break in Composer 3.'); } $composer = $this->requireComposer(); if (!HttpDownloader::isCurlEnabled()) { $io->writeError('Composer is operating significantly slower than normal because you do not have the PHP curl extension enabled.'); } $packages = $input->getArgument('packages'); $reqs = $this->formatRequirements($input->getOption('with')); if (count($packages) > 0) { $allowlistPackagesWithRequirements = array_filter($packages, static function ($pkg): bool { return Preg::isMatch('{\S+[ =:]\S+}', $pkg); }); foreach ($this->formatRequirements($allowlistPackagesWithRequirements) as $package => $constraint) { $reqs[$package] = $constraint; } foreach ($allowlistPackagesWithRequirements as $package) { $packageName = Preg::replace('{^([^ =:]+)[ =:].*$}', '$1', $package); $index = array_search($package, $packages); $packages[$index] = $packageName; } } $parser = new VersionParser; $temporaryConstraints = []; foreach ($reqs as $package => $constraint) { $temporaryConstraints[strtolower($package)] = $parser->parseConstraints($constraint); } $rootPackage = $composer->getPackage(); $rootPackage->setReferences(RootPackageLoader::extractReferences($reqs, $rootPackage->getReferences())); $rootPackage->setStabilityFlags(RootPackageLoader::extractStabilityFlags($reqs, $rootPackage->getMinimumStability(), $rootPackage->getStabilityFlags())); if ($input->getOption('interactive')) { $packages = $this->getPackagesInteractively($io, $input, $output, $composer, $packages); } if ($input->getOption('root-reqs')) { $requires = array_keys($rootPackage->getRequires()); if (!$input->getOption('no-dev')) { $requires = array_merge($requires, array_keys($rootPackage->getDevRequires())); } if (!empty($packages)) { $packages = array_intersect($packages, $requires); } else { $packages = $requires; } } $filteredPackages = array_filter($packages, static function ($package): bool { return !in_array($package, ['lock', 'nothing', 'mirrors'], true); }); $updateMirrors = $input->getOption('lock') || count($filteredPackages) !== count($packages); $packages = $filteredPackages; if ($updateMirrors && !empty($packages)) { $io->writeError('You cannot simultaneously update only a selection of packages and regenerate the lock file metadata.'); return -1; } $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'update', $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); $composer->getInstallationManager()->setOutputProgress(!$input->getOption('no-progress')); $install = Installer::create($io, $composer); $config = $composer->getConfig(); [$preferSource, $preferDist] = $this->getPreferredInstallOptions($config, $input); $optimize = $input->getOption('optimize-autoloader') || $config->get('optimize-autoloader'); $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative'); $apcuPrefix = $input->getOption('apcu-autoloader-prefix'); $apcu = $apcuPrefix !== null || $input->getOption('apcu-autoloader') || $config->get('apcu-autoloader'); $updateAllowTransitiveDependencies = Request::UPDATE_ONLY_LISTED; if ($input->getOption('with-all-dependencies')) { $updateAllowTransitiveDependencies = Request::UPDATE_LISTED_WITH_TRANSITIVE_DEPS; } elseif ($input->getOption('with-dependencies')) { $updateAllowTransitiveDependencies = Request::UPDATE_LISTED_WITH_TRANSITIVE_DEPS_NO_ROOT_REQUIRE; } $install ->setDryRun($input->getOption('dry-run')) ->setVerbose($input->getOption('verbose')) ->setPreferSource($preferSource) ->setPreferDist($preferDist) ->setDevMode(!$input->getOption('no-dev')) ->setDumpAutoloader(!$input->getOption('no-autoloader')) ->setOptimizeAutoloader($optimize) ->setClassMapAuthoritative($authoritative) ->setApcuAutoloader($apcu, $apcuPrefix) ->setUpdate(true) ->setInstall(!$input->getOption('no-install')) ->setUpdateMirrors($updateMirrors) ->setUpdateAllowList($packages) ->setUpdateAllowTransitiveDependencies($updateAllowTransitiveDependencies) ->setPlatformRequirementFilter($this->getPlatformRequirementFilter($input)) ->setPreferStable($input->getOption('prefer-stable')) ->setPreferLowest($input->getOption('prefer-lowest')) ->setTemporaryConstraints($temporaryConstraints) ->setAudit(!$input->getOption('no-audit')) ->setAuditFormat($this->getAuditFormat($input)) ; if ($input->getOption('no-plugins')) { $install->disablePlugins(); } return $install->run(); } private function getPackagesInteractively(IOInterface $io, InputInterface $input, OutputInterface $output, Composer $composer, array $packages): array { if (!$input->isInteractive()) { throw new \InvalidArgumentException('--interactive cannot be used in non-interactive terminals.'); } $requires = array_merge( $composer->getPackage()->getRequires(), $composer->getPackage()->getDevRequires() ); $autocompleterValues = []; foreach ($requires as $require) { $target = $require->getTarget(); $autocompleterValues[strtolower($target)] = $target; } $installedPackages = $composer->getRepositoryManager()->getLocalRepository()->getPackages(); foreach ($installedPackages as $package) { $autocompleterValues[$package->getName()] = $package->getPrettyName(); } $helper = $this->getHelper('question'); $question = new Question('Enter package name: ', null); $io->writeError('Press enter without value to end submission'); do { $autocompleterValues = array_diff($autocompleterValues, $packages); $question->setAutocompleterValues($autocompleterValues); $addedPackage = $helper->ask($input, $output, $question); if (!is_string($addedPackage) || empty($addedPackage)) { break; } $addedPackage = strtolower($addedPackage); if (!in_array($addedPackage, $packages)) { $packages[] = $addedPackage; } } while (true); $packages = array_filter($packages); if (!$packages) { throw new \InvalidArgumentException('You must enter minimum one package.'); } $table = new Table($output); $table->setHeaders(['Selected packages']); foreach ($packages as $package) { $table->addRow([$package]); } $table->render(); if ($io->askConfirmation(sprintf( 'Would you like to continue and update the above package%s [yes]? ', 1 === count($packages) ? '' : 's' ))) { return $packages; } throw new \RuntimeException('Installation aborted.'); } } setName('validate') ->setDescription('Validates a composer.json and composer.lock') ->setDefinition([ new InputOption('no-check-all', null, InputOption::VALUE_NONE, 'Do not validate requires for overly strict/loose constraints'), new InputOption('check-lock', null, InputOption::VALUE_NONE, 'Check if lock file is up to date (even when config.lock is false)'), new InputOption('no-check-lock', null, InputOption::VALUE_NONE, 'Do not check if lock file is up to date'), new InputOption('no-check-publish', null, InputOption::VALUE_NONE, 'Do not check for publish errors'), new InputOption('no-check-version', null, InputOption::VALUE_NONE, 'Do not report a warning if the version field is present'), new InputOption('with-dependencies', 'A', InputOption::VALUE_NONE, 'Also validate the composer.json of all installed dependencies'), new InputOption('strict', null, InputOption::VALUE_NONE, 'Return a non-zero exit code for warnings as well as errors'), new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file'), ]) ->setHelp( <<getArgument('file') ?: Factory::getComposerFile(); $io = $this->getIO(); if (!file_exists($file)) { $io->writeError('' . $file . ' not found.'); return 3; } if (!Filesystem::isReadable($file)) { $io->writeError('' . $file . ' is not readable.'); return 3; } $validator = new ConfigValidator($io); $checkAll = $input->getOption('no-check-all') ? 0 : ValidatingArrayLoader::CHECK_ALL; $checkPublish = !$input->getOption('no-check-publish'); $checkLock = !$input->getOption('no-check-lock'); $checkVersion = $input->getOption('no-check-version') ? 0 : ConfigValidator::CHECK_VERSION; $isStrict = $input->getOption('strict'); [$errors, $publishErrors, $warnings] = $validator->validate($file, $checkAll, $checkVersion); $lockErrors = []; $composer = Factory::create($io, $file, $input->hasParameterOption('--no-plugins')); $checkLock = ($checkLock && $composer->getConfig()->get('lock')) || $input->getOption('check-lock'); $locker = $composer->getLocker(); if ($locker->isLocked() && !$locker->isFresh()) { $lockErrors[] = '- The lock file is not up to date with the latest changes in composer.json, it is recommended that you run `composer update` or `composer update `.'; } if ($locker->isLocked()) { $missingRequirements = false; $sets = [ ['repo' => $locker->getLockedRepository(false), 'method' => 'getRequires', 'description' => 'Required'], ['repo' => $locker->getLockedRepository(true), 'method' => 'getDevRequires', 'description' => 'Required (in require-dev)'], ]; foreach ($sets as $set) { $installedRepo = new InstalledRepository([$set['repo']]); foreach (call_user_func([$composer->getPackage(), $set['method']]) as $link) { if (PlatformRepository::isPlatformPackage($link->getTarget())) { continue; } if (!$installedRepo->findPackagesWithReplacersAndProviders($link->getTarget(), $link->getConstraint())) { if ($results = $installedRepo->findPackagesWithReplacersAndProviders($link->getTarget())) { $provider = reset($results); $lockErrors[] = '- ' . $set['description'].' package "' . $link->getTarget() . '" is in the lock file as "'.$provider->getPrettyVersion().'" but that does not satisfy your constraint "'.$link->getPrettyConstraint().'".'; } else { $lockErrors[] = '- ' . $set['description'].' package "' . $link->getTarget() . '" is not present in the lock file.'; } $missingRequirements = true; } } } if ($missingRequirements) { $lockErrors[] = 'This usually happens when composer files are incorrectly merged or the composer.json file is manually edited.'; $lockErrors[] = 'Read more about correctly resolving merge conflicts https://getcomposer.org/doc/articles/resolving-merge-conflicts.md'; $lockErrors[] = 'and prefer using the "require" command over editing the composer.json file directly https://getcomposer.org/doc/03-cli.md#require'; } } $this->outputResult($io, $file, $errors, $warnings, $checkPublish, $publishErrors, $checkLock, $lockErrors, true); $exitCode = $errors ? 2 : ($isStrict && $warnings ? 1 : 0); if ($input->getOption('with-dependencies')) { $localRepo = $composer->getRepositoryManager()->getLocalRepository(); foreach ($localRepo->getPackages() as $package) { $path = $composer->getInstallationManager()->getInstallPath($package); $file = $path . '/composer.json'; if (is_dir($path) && file_exists($file)) { [$errors, $publishErrors, $warnings] = $validator->validate($file, $checkAll, $checkVersion); $this->outputResult($io, $package->getPrettyName(), $errors, $warnings, $checkPublish, $publishErrors); $depCode = $errors ? 2 : ($isStrict && $warnings ? 1 : 0); $exitCode = max($depCode, $exitCode); } } } $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'validate', $input, $output); $eventCode = $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); return max($eventCode, $exitCode); } private function outputResult(IOInterface $io, string $name, array &$errors, array &$warnings, bool $checkPublish = false, array $publishErrors = [], bool $checkLock = false, array $lockErrors = [], bool $printSchemaUrl = false): void { $doPrintSchemaUrl = false; if ($errors) { $io->writeError('' . $name . ' is invalid, the following errors/warnings were found:'); } elseif ($publishErrors) { $io->writeError('' . $name . ' is valid for simple usage with Composer but has'); $io->writeError('strict errors that make it unable to be published as a package'); $doPrintSchemaUrl = $printSchemaUrl; } elseif ($warnings) { $io->writeError('' . $name . ' is valid, but with a few warnings'); $doPrintSchemaUrl = $printSchemaUrl; } elseif ($lockErrors) { $io->write('' . $name . ' is valid but your composer.lock has some '.($checkLock ? 'errors' : 'warnings').''); } else { $io->write('' . $name . ' is valid'); } if ($doPrintSchemaUrl) { $io->writeError('See https://getcomposer.org/doc/04-schema.md for details on the schema'); } if ($errors) { $errors = array_map(static function ($err): string { return '- ' . $err; }, $errors); array_unshift($errors, '# General errors'); } if ($warnings) { $warnings = array_map(static function ($err): string { return '- ' . $err; }, $warnings); array_unshift($warnings, '# General warnings'); } $extraWarnings = []; if ($publishErrors) { $publishErrors = array_map(static function ($err): string { return '- ' . $err; }, $publishErrors); if ($checkPublish) { array_unshift($publishErrors, '# Publish errors'); $errors = array_merge($errors, $publishErrors); } else { array_unshift($publishErrors, '# Publish warnings'); $extraWarnings = array_merge($extraWarnings, $publishErrors); } } if ($lockErrors) { if ($checkLock) { array_unshift($lockErrors, '# Lock file errors'); $errors = array_merge($errors, $lockErrors); } else { array_unshift($lockErrors, '# Lock file warnings'); $extraWarnings = array_merge($extraWarnings, $lockErrors); } } $messages = [ 'error' => $errors, 'warning' => array_merge($warnings, $extraWarnings), ]; foreach ($messages as $style => $msgs) { foreach ($msgs as $msg) { if (strpos($msg, '#') === 0) { $io->writeError('<' . $style . '>' . $msg . ''); } else { $io->writeError($msg); } } } } } locker = $locker; } public function getLocker(): Locker { return $this->locker; } public function setDownloadManager(DownloadManager $manager): void { $this->downloadManager = $manager; } public function getDownloadManager(): DownloadManager { return $this->downloadManager; } public function setArchiveManager(ArchiveManager $manager): void { $this->archiveManager = $manager; } public function getArchiveManager(): ArchiveManager { return $this->archiveManager; } public function setPluginManager(PluginManager $manager): void { $this->pluginManager = $manager; } public function getPluginManager(): PluginManager { return $this->pluginManager; } public function setAutoloadGenerator(AutoloadGenerator $autoloadGenerator): void { $this->autoloadGenerator = $autoloadGenerator; } public function getAutoloadGenerator(): AutoloadGenerator { return $this->autoloadGenerator; } } 300, 'use-include-path' => false, 'allow-plugins' => [], 'use-parent-dir' => 'prompt', 'preferred-install' => 'dist', 'notify-on-install' => true, 'github-protocols' => ['https', 'ssh', 'git'], 'gitlab-protocol' => null, 'vendor-dir' => 'vendor', 'bin-dir' => '{$vendor-dir}/bin', 'cache-dir' => '{$home}/cache', 'data-dir' => '{$home}', 'cache-files-dir' => '{$cache-dir}/files', 'cache-repo-dir' => '{$cache-dir}/repo', 'cache-vcs-dir' => '{$cache-dir}/vcs', 'cache-ttl' => 15552000, 'cache-files-ttl' => null, 'cache-files-maxsize' => '300MiB', 'cache-read-only' => false, 'bin-compat' => 'auto', 'discard-changes' => false, 'autoloader-suffix' => null, 'sort-packages' => false, 'optimize-autoloader' => false, 'classmap-authoritative' => false, 'apcu-autoloader' => false, 'prepend-autoloader' => true, 'github-domains' => ['github.com'], 'bitbucket-expose-hostname' => true, 'disable-tls' => false, 'secure-http' => true, 'secure-svn-domains' => [], 'cafile' => null, 'capath' => null, 'github-expose-hostname' => true, 'gitlab-domains' => ['gitlab.com'], 'store-auths' => 'prompt', 'platform' => [], 'archive-format' => 'tar', 'archive-dir' => '.', 'htaccess-protect' => true, 'use-github-api' => true, 'lock' => true, 'platform-check' => 'php-only', 'bitbucket-oauth' => [], 'github-oauth' => [], 'gitlab-oauth' => [], 'gitlab-token' => [], 'http-basic' => [], 'bearer' => [], ]; public static $defaultRepositories = [ 'packagist.org' => [ 'type' => 'composer', 'url' => 'https://repo.packagist.org', ], ]; private $config; private $baseDir; private $repositories; private $configSource; private $authConfigSource; private $useEnvironment; private $warnedHosts = []; private $sslVerifyWarnedHosts = []; private $sourceOfConfigValue = []; public function __construct(bool $useEnvironment = true, ?string $baseDir = null) { $this->config = static::$defaultConfig; $this->repositories = static::$defaultRepositories; $this->useEnvironment = (bool) $useEnvironment; $this->baseDir = is_string($baseDir) && '' !== $baseDir ? $baseDir : null; foreach ($this->config as $configKey => $configValue) { $this->setSourceOfConfigValue($configValue, $configKey, self::SOURCE_DEFAULT); } foreach ($this->repositories as $configKey => $configValue) { $this->setSourceOfConfigValue($configValue, 'repositories.' . $configKey, self::SOURCE_DEFAULT); } } public function setConfigSource(ConfigSourceInterface $source): void { $this->configSource = $source; } public function getConfigSource(): ConfigSourceInterface { return $this->configSource; } public function setAuthConfigSource(ConfigSourceInterface $source): void { $this->authConfigSource = $source; } public function getAuthConfigSource(): ConfigSourceInterface { return $this->authConfigSource; } public function merge(array $config, string $source = self::SOURCE_UNKNOWN): void { if (!empty($config['config']) && is_array($config['config'])) { foreach ($config['config'] as $key => $val) { if (in_array($key, ['bitbucket-oauth', 'github-oauth', 'gitlab-oauth', 'gitlab-token', 'http-basic', 'bearer'], true) && isset($this->config[$key])) { $this->config[$key] = array_merge($this->config[$key], $val); $this->setSourceOfConfigValue($val, $key, $source); } elseif (in_array($key, ['allow-plugins'], true) && isset($this->config[$key]) && is_array($this->config[$key]) && is_array($val)) { $this->config[$key] = array_merge($val, $this->config[$key], $val); $this->setSourceOfConfigValue($val, $key, $source); } elseif (in_array($key, ['gitlab-domains', 'github-domains'], true) && isset($this->config[$key])) { $this->config[$key] = array_unique(array_merge($this->config[$key], $val)); $this->setSourceOfConfigValue($val, $key, $source); } elseif ('preferred-install' === $key && isset($this->config[$key])) { if (is_array($val) || is_array($this->config[$key])) { if (is_string($val)) { $val = ['*' => $val]; } if (is_string($this->config[$key])) { $this->config[$key] = ['*' => $this->config[$key]]; $this->sourceOfConfigValue[$key . '*'] = $source; } $this->config[$key] = array_merge($this->config[$key], $val); $this->setSourceOfConfigValue($val, $key, $source); if (isset($this->config[$key]['*'])) { $wildcard = $this->config[$key]['*']; unset($this->config[$key]['*']); $this->config[$key]['*'] = $wildcard; } } else { $this->config[$key] = $val; $this->setSourceOfConfigValue($val, $key, $source); } } else { $this->config[$key] = $val; $this->setSourceOfConfigValue($val, $key, $source); } } } if (!empty($config['repositories']) && is_array($config['repositories'])) { $this->repositories = array_reverse($this->repositories, true); $newRepos = array_reverse($config['repositories'], true); foreach ($newRepos as $name => $repository) { if (false === $repository) { $this->disableRepoByName((string) $name); continue; } if (is_array($repository) && 1 === count($repository) && false === current($repository)) { $this->disableRepoByName((string) key($repository)); continue; } if (isset($repository['type'], $repository['url']) && $repository['type'] === 'composer' && Preg::isMatch('{^https?://(?:[a-z0-9-.]+\.)?packagist.org(/|$)}', $repository['url'])) { $this->disableRepoByName('packagist.org'); } if (is_int($name)) { $this->repositories[] = $repository; $this->setSourceOfConfigValue($repository, 'repositories.' . array_search($repository, $this->repositories, true), $source); } else { if ($name === 'packagist') { $this->repositories[$name . '.org'] = $repository; $this->setSourceOfConfigValue($repository, 'repositories.' . $name . '.org', $source); } else { $this->repositories[$name] = $repository; $this->setSourceOfConfigValue($repository, 'repositories.' . $name, $source); } } } $this->repositories = array_reverse($this->repositories, true); } } public function getRepositories(): array { return $this->repositories; } public function get(string $key, int $flags = 0) { switch ($key) { case 'vendor-dir': case 'bin-dir': case 'process-timeout': case 'data-dir': case 'cache-dir': case 'cache-files-dir': case 'cache-repo-dir': case 'cache-vcs-dir': case 'cafile': case 'capath': $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_')); $val = $this->getComposerEnv($env); if ($val !== false) { $this->setSourceOfConfigValue($val, $key, $env); } if ($key === 'process-timeout') { return max(0, false !== $val ? (int) $val : $this->config[$key]); } $val = rtrim((string) $this->process(false !== $val ? $val : $this->config[$key], $flags), '/\\'); $val = Platform::expandPath($val); if (substr($key, -4) !== '-dir') { return $val; } return (($flags & self::RELATIVE_PATHS) === self::RELATIVE_PATHS) ? $val : $this->realpath($val); case 'cache-read-only': case 'htaccess-protect': $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_')); $val = $this->getComposerEnv($env); if (false === $val) { $val = $this->config[$key]; } else { $this->setSourceOfConfigValue($val, $key, $env); } return $val !== 'false' && (bool) $val; case 'disable-tls': case 'secure-http': case 'use-github-api': case 'lock': if ($key === 'secure-http' && $this->get('disable-tls') === true) { return false; } return $this->config[$key] !== 'false' && (bool) $this->config[$key]; case 'cache-ttl': return max(0, (int) $this->config[$key]); case 'cache-files-maxsize': if (!Preg::isMatch('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', (string) $this->config[$key], $matches)) { throw new \RuntimeException( "Could not parse the value of '$key': {$this->config[$key]}" ); } $size = (float) $matches[1]; if (isset($matches[2])) { switch (strtolower($matches[2])) { case 'g': $size *= 1024; case 'm': $size *= 1024; case 'k': $size *= 1024; break; } } return max(0, (int) $size); case 'cache-files-ttl': if (isset($this->config[$key])) { return max(0, (int) $this->config[$key]); } return $this->get('cache-ttl'); case 'home': return rtrim($this->process(Platform::expandPath($this->config[$key]), $flags), '/\\'); case 'bin-compat': $value = $this->getComposerEnv('COMPOSER_BIN_COMPAT') ?: $this->config[$key]; if (!in_array($value, ['auto', 'full', 'proxy', 'symlink'])) { throw new \RuntimeException( "Invalid value for 'bin-compat': {$value}. Expected auto, full or proxy" ); } if ($value === 'symlink') { trigger_error('config.bin-compat "symlink" is deprecated since Composer 2.2, use auto, full (for Windows compatibility) or proxy instead.', E_USER_DEPRECATED); } return $value; case 'discard-changes': $env = $this->getComposerEnv('COMPOSER_DISCARD_CHANGES'); if ($env !== false) { if (!in_array($env, ['stash', 'true', 'false', '1', '0'], true)) { throw new \RuntimeException( "Invalid value for COMPOSER_DISCARD_CHANGES: {$env}. Expected 1, 0, true, false or stash" ); } if ('stash' === $env) { return 'stash'; } return $env !== 'false' && (bool) $env; } if (!in_array($this->config[$key], [true, false, 'stash'], true)) { throw new \RuntimeException( "Invalid value for 'discard-changes': {$this->config[$key]}. Expected true, false or stash" ); } return $this->config[$key]; case 'github-protocols': $protos = $this->config['github-protocols']; if ($this->config['secure-http'] && false !== ($index = array_search('git', $protos))) { unset($protos[$index]); } if (reset($protos) === 'http') { throw new \RuntimeException('The http protocol for github is not available anymore, update your config\'s github-protocols to use "https", "git" or "ssh"'); } return $protos; case 'autoloader-suffix': if ($this->config[$key] === '') { return null; } return $this->process($this->config[$key], $flags); default: if (!isset($this->config[$key])) { return null; } return $this->process($this->config[$key], $flags); } } public function all(int $flags = 0): array { $all = [ 'repositories' => $this->getRepositories(), ]; foreach (array_keys($this->config) as $key) { $all['config'][$key] = $this->get($key, $flags); } return $all; } public function getSourceOfValue(string $key): string { $this->get($key); return $this->sourceOfConfigValue[$key] ?? self::SOURCE_UNKNOWN; } private function setSourceOfConfigValue($configValue, string $path, string $source): void { $this->sourceOfConfigValue[$path] = $source; if (is_array($configValue)) { foreach ($configValue as $key => $value) { $this->setSourceOfConfigValue($value, $path . '.' . $key, $source); } } } public function raw(): array { return [ 'repositories' => $this->getRepositories(), 'config' => $this->config, ]; } public function has(string $key): bool { return array_key_exists($key, $this->config); } private function process($value, int $flags) { if (!is_string($value)) { return $value; } return Preg::replaceCallback('#\{\$(.+)\}#', function ($match) use ($flags) { return $this->get($match[1], $flags); }, $value); } private function realpath(string $path): string { if (Preg::isMatch('{^(?:/|[a-z]:|[a-z0-9.]+://|\\\\\\\\)}i', $path)) { return $path; } return $this->baseDir ? $this->baseDir . '/' . $path : $path; } private function getComposerEnv(string $var) { if ($this->useEnvironment) { return Platform::getEnv($var); } return false; } private function disableRepoByName(string $name): void { if (isset($this->repositories[$name])) { unset($this->repositories[$name]); } elseif ($name === 'packagist') { unset($this->repositories['packagist.org']); } } public function prohibitUrlByConfig(string $url, ?IOInterface $io = null, array $repoOptions = []): void { if (false === filter_var($url, FILTER_VALIDATE_URL)) { return; } $scheme = parse_url($url, PHP_URL_SCHEME); $hostname = parse_url($url, PHP_URL_HOST); if (in_array($scheme, ['http', 'git', 'ftp', 'svn'])) { if ($this->get('secure-http')) { if ($scheme === 'svn') { if (in_array($hostname, $this->get('secure-svn-domains'), true)) { return; } throw new TransportException("Your configuration does not allow connections to $url. See https://getcomposer.org/doc/06-config.md#secure-svn-domains for details."); } throw new TransportException("Your configuration does not allow connections to $url. See https://getcomposer.org/doc/06-config.md#secure-http for details."); } if ($io !== null) { if (is_string($hostname)) { if (!isset($this->warnedHosts[$hostname])) { $io->writeError("Warning: Accessing $hostname over $scheme which is an insecure protocol."); } $this->warnedHosts[$hostname] = true; } } } if ($io !== null && is_string($hostname) && !isset($this->sslVerifyWarnedHosts[$hostname])) { $warning = null; if (isset($repoOptions['ssl']['verify_peer']) && !(bool) $repoOptions['ssl']['verify_peer']) { $warning = 'verify_peer'; } if (isset($repoOptions['ssl']['verify_peer_name']) && !(bool) $repoOptions['ssl']['verify_peer_name']) { $warning = $warning === null ? 'verify_peer_name' : $warning . ' and verify_peer_name'; } if ($warning !== null) { $io->writeError("Warning: Accessing $hostname with $warning disabled."); $this->sslVerifyWarnedHosts[$hostname] = true; } } } public static function disableProcessTimeout(): void { ProcessExecutor::setTimeout(0); } } file = $file; $this->authConfig = $authConfig; } public function getName(): string { return $this->file->getPath(); } public function addRepository(string $name, $config, bool $append = true): void { $this->manipulateJson('addRepository', static function (&$config, $repo, $repoConfig) use ($append): void { if (isset($config['repositories'])) { foreach ($config['repositories'] as $index => $val) { if ($index === $repo) { continue; } if (is_numeric($index) && ($val === ['packagist' => false] || $val === ['packagist.org' => false])) { unset($config['repositories'][$index]); $config['repositories']['packagist.org'] = false; break; } } } if ($append) { $config['repositories'][$repo] = $repoConfig; } else { $config['repositories'] = [$repo => $repoConfig] + $config['repositories']; } }, $name, $config, $append); } public function removeRepository(string $name): void { $this->manipulateJson('removeRepository', static function (&$config, $repo): void { unset($config['repositories'][$repo]); }, $name); } public function addConfigSetting(string $name, $value): void { $authConfig = $this->authConfig; $this->manipulateJson('addConfigSetting', static function (&$config, $key, $val) use ($authConfig): void { if (Preg::isMatch('{^(bitbucket-oauth|github-oauth|gitlab-oauth|gitlab-token|bearer|http-basic|platform)\.}', $key)) { [$key, $host] = explode('.', $key, 2); if ($authConfig) { $config[$key][$host] = $val; } else { $config['config'][$key][$host] = $val; } } else { $config['config'][$key] = $val; } }, $name, $value); } public function removeConfigSetting(string $name): void { $authConfig = $this->authConfig; $this->manipulateJson('removeConfigSetting', static function (&$config, $key) use ($authConfig): void { if (Preg::isMatch('{^(bitbucket-oauth|github-oauth|gitlab-oauth|gitlab-token|bearer|http-basic|platform)\.}', $key)) { [$key, $host] = explode('.', $key, 2); if ($authConfig) { unset($config[$key][$host]); } else { unset($config['config'][$key][$host]); } } else { unset($config['config'][$key]); } }, $name); } public function addProperty(string $name, $value): void { $this->manipulateJson('addProperty', static function (&$config, $key, $val): void { if (strpos($key, 'extra.') === 0 || strpos($key, 'scripts.') === 0) { $bits = explode('.', $key); $last = array_pop($bits); $arr = &$config[reset($bits)]; foreach ($bits as $bit) { if (!isset($arr[$bit])) { $arr[$bit] = []; } $arr = &$arr[$bit]; } $arr[$last] = $val; } else { $config[$key] = $val; } }, $name, $value); } public function removeProperty(string $name): void { $this->manipulateJson('removeProperty', static function (&$config, $key): void { if (strpos($key, 'extra.') === 0 || strpos($key, 'scripts.') === 0) { $bits = explode('.', $key); $last = array_pop($bits); $arr = &$config[reset($bits)]; foreach ($bits as $bit) { if (!isset($arr[$bit])) { return; } $arr = &$arr[$bit]; } unset($arr[$last]); } else { unset($config[$key]); } }, $name); } public function addLink(string $type, string $name, string $value): void { $this->manipulateJson('addLink', static function (&$config, $type, $name, $value): void { $config[$type][$name] = $value; }, $type, $name, $value); } public function removeLink(string $type, string $name): void { $this->manipulateJson('removeSubNode', static function (&$config, $type, $name): void { unset($config[$type][$name]); }, $type, $name); $this->manipulateJson('removeMainKeyIfEmpty', static function (&$config, $type): void { if (0 === count($config[$type])) { unset($config[$type]); } }, $type); } private function manipulateJson(string $method, callable $fallback, ...$args): void { if ($this->file->exists()) { if (!is_writable($this->file->getPath())) { throw new \RuntimeException(sprintf('The file "%s" is not writable.', $this->file->getPath())); } if (!Filesystem::isReadable($this->file->getPath())) { throw new \RuntimeException(sprintf('The file "%s" is not readable.', $this->file->getPath())); } $contents = file_get_contents($this->file->getPath()); } elseif ($this->authConfig) { $contents = "{\n}\n"; } else { $contents = "{\n \"config\": {\n }\n}\n"; } $manipulator = new JsonManipulator($contents); $newFile = !$this->file->exists(); if ($this->authConfig && $method === 'addConfigSetting') { $method = 'addSubNode'; [$mainNode, $name] = explode('.', $args[0], 2); $args = [$mainNode, $name, $args[1]]; } elseif ($this->authConfig && $method === 'removeConfigSetting') { $method = 'removeSubNode'; [$mainNode, $name] = explode('.', $args[0], 2); $args = [$mainNode, $name]; } if (call_user_func_array([$manipulator, $method], $args)) { file_put_contents($this->file->getPath(), $manipulator->getContents()); } else { $config = $this->file->read(); $this->arrayUnshiftRef($args, $config); $fallback(...$args); foreach (['require', 'require-dev', 'conflict', 'provide', 'replace', 'suggest', 'config', 'autoload', 'autoload-dev', 'scripts', 'scripts-descriptions', 'support'] as $prop) { if (isset($config[$prop]) && $config[$prop] === []) { $config[$prop] = new \stdClass; } } foreach (['psr-0', 'psr-4'] as $prop) { if (isset($config['autoload'][$prop]) && $config['autoload'][$prop] === []) { $config['autoload'][$prop] = new \stdClass; } if (isset($config['autoload-dev'][$prop]) && $config['autoload-dev'][$prop] === []) { $config['autoload-dev'][$prop] = new \stdClass; } } foreach (['platform', 'http-basic', 'bearer', 'gitlab-token', 'gitlab-oauth', 'github-oauth', 'preferred-install'] as $prop) { if (isset($config['config'][$prop]) && $config['config'][$prop] === []) { $config['config'][$prop] = new \stdClass; } } $this->file->write($config); } try { $this->file->validateSchema(JsonFile::LAX_SCHEMA); } catch (JsonValidationException $e) { file_put_contents($this->file->getPath(), $contents); throw new \RuntimeException('Failed to update composer.json with a valid format, reverting to the original content. Please report an issue to us with details (command you run and a copy of your composer.json). '.PHP_EOL.implode(PHP_EOL, $e->getErrors()), 0, $e); } if ($newFile) { Silencer::call('chmod', $this->file->getPath(), 0600); } } private function arrayUnshiftRef(array &$array, &$value): int { $return = array_unshift($array, ''); $array[0] = &$value; return $return; } } io = new NullIO(); $this->signalHandler = SignalHandler::create([SignalHandler::SIGINT, SignalHandler::SIGTERM, SignalHandler::SIGHUP], function (string $signal, SignalHandler $handler) { $this->io->writeError('Received '.$signal.', aborting', true, IOInterface::DEBUG); $handler->exitWithLastSignal(); }); if (!$shutdownRegistered) { $shutdownRegistered = true; register_shutdown_function(static function (): void { $lastError = error_get_last(); if ($lastError && $lastError['message'] && (strpos($lastError['message'], 'Allowed memory') !== false || strpos($lastError['message'], 'exceeded memory') !== false )) { echo "\n". 'Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.'; } }); } $this->initialWorkingDirectory = getcwd(); parent::__construct('Composer', Composer::getVersion()); } public function __destruct() { $this->signalHandler->unregister(); } public function run(?InputInterface $input = null, ?OutputInterface $output = null): int { if (null === $output) { $output = Factory::createOutput(); } return parent::run($input, $output); } public function doRun(InputInterface $input, OutputInterface $output): int { $this->disablePluginsByDefault = $input->hasParameterOption('--no-plugins'); $this->disableScriptsByDefault = $input->hasParameterOption('--no-scripts'); $stdin = defined('STDIN') ? STDIN : fopen('php://stdin', 'r'); if (Platform::getEnv('COMPOSER_NO_INTERACTION') || $stdin === false || !Platform::isTty($stdin)) { $input->setInteractive(false); } $io = $this->io = new ConsoleIO($input, $output, new HelperSet([ new QuestionHelper(), ])); ErrorHandler::register($io); if ($input->hasParameterOption('--no-cache')) { $io->writeError('Disabling cache usage', true, IOInterface::DEBUG); Platform::putEnv('COMPOSER_CACHE_DIR', Platform::isWindows() ? 'nul' : '/dev/null'); } $newWorkDir = $this->getNewWorkingDir($input); if (null !== $newWorkDir) { $oldWorkingDir = Platform::getCwd(true); chdir($newWorkDir); $this->initialWorkingDirectory = $newWorkDir; $cwd = Platform::getCwd(true); $io->writeError('Changed CWD to ' . ($cwd !== '' ? $cwd : $newWorkDir), true, IOInterface::DEBUG); } $commandName = ''; if ($name = $this->getCommandName($input)) { try { $commandName = $this->find($name)->getName(); } catch (CommandNotFoundException $e) { $commandName = false; } catch (\InvalidArgumentException $e) { } } if ($io->isInteractive() && null === $newWorkDir && !in_array($commandName, ['', 'list', 'init', 'about', 'help', 'diagnose', 'self-update', 'global', 'create-project', 'outdated'], true) && !file_exists(Factory::getComposerFile()) && ($useParentDirIfNoJsonAvailable = $this->getUseParentDirConfigValue()) !== false) { $dir = dirname(Platform::getCwd(true)); $home = realpath(Platform::getEnv('HOME') ?: Platform::getEnv('USERPROFILE') ?: '/'); while (dirname($dir) !== $dir && $dir !== $home) { if (file_exists($dir.'/'.Factory::getComposerFile())) { if ($useParentDirIfNoJsonAvailable === true || $io->askConfirmation('No composer.json in current directory, do you want to use the one at '.$dir.'? [Y,n]? ')) { if ($useParentDirIfNoJsonAvailable === true) { $io->writeError('No composer.json in current directory, changing working directory to '.$dir.''); } else { $io->writeError('Always want to use the parent dir? Use "composer config --global use-parent-dir true" to change the default.'); } $oldWorkingDir = Platform::getCwd(true); chdir($dir); } break; } $dir = dirname($dir); } } $needsSudoCheck = !Platform::isWindows() && function_exists('exec') && !Platform::getEnv('COMPOSER_ALLOW_SUPERUSER') && (ini_get('open_basedir') || !file_exists('/.dockerenv')); $isNonAllowedRoot = false; if ($needsSudoCheck) { $isNonAllowedRoot = function_exists('posix_getuid') && posix_getuid() === 0; if ($isNonAllowedRoot) { if ($uid = (int) Platform::getEnv('SUDO_UID')) { Silencer::call('exec', "sudo -u \\#{$uid} sudo -K > /dev/null 2>&1"); } } Silencer::call('exec', 'sudo -K > /dev/null 2>&1'); } $mayNeedPluginCommand = false === $input->hasParameterOption(['--version', '-V']) && ( false === $commandName || in_array($commandName, ['', 'list', 'help'], true) ); if ($mayNeedPluginCommand && !$this->disablePluginsByDefault && !$this->hasPluginCommands) { if ($isNonAllowedRoot) { $io->writeError('Do not run Composer as root/super user! See https://getcomposer.org/root for details'); if ($io->isInteractive() && $io->askConfirmation('Continue as root/super user [yes]? ')) { $isNonAllowedRoot = false; } else { $io->writeError('Aborting as no plugin should be loaded if running as super user is not explicitly allowed'); return 1; } } try { foreach ($this->getPluginCommands() as $command) { if ($this->has($command->getName())) { $io->writeError('Plugin command '.$command->getName().' ('.get_class($command).') would override a Composer command and has been skipped'); } else { $this->add($command); } } } catch (NoSslException $e) { } catch (ParsingException $e) { $details = $e->getDetails(); $file = realpath(Factory::getComposerFile()); $line = null; if ($details && isset($details['line'])) { $line = $details['line']; } $ghe = new GithubActionError($this->io); $ghe->emit($e->getMessage(), $file, $line); throw $e; } $this->hasPluginCommands = true; } if ($isNonAllowedRoot && !$io->isInteractive()) { $this->disablePluginsByDefault = true; } $isProxyCommand = false; if ($name = $this->getCommandName($input)) { try { $command = $this->find($name); $commandName = $command->getName(); $isProxyCommand = ($command instanceof Command\BaseCommand && $command->isProxyCommand()); } catch (\InvalidArgumentException $e) { } } if (!$isProxyCommand) { $io->writeError(sprintf( 'Running %s (%s) with %s on %s', Composer::getVersion(), Composer::RELEASE_DATE, defined('HHVM_VERSION') ? 'HHVM '.HHVM_VERSION : 'PHP '.PHP_VERSION, function_exists('php_uname') ? php_uname('s') . ' / ' . php_uname('r') : 'Unknown OS' ), true, IOInterface::DEBUG); if (PHP_VERSION_ID < 70205) { $io->writeError('Composer supports PHP 7.2.5 and above, you will most likely encounter problems with your PHP '.PHP_VERSION.'. Upgrading is strongly recommended but you can use Composer 2.2.x LTS as a fallback.'); } if (XdebugHandler::isXdebugActive() && !Platform::getEnv('COMPOSER_DISABLE_XDEBUG_WARN')) { $io->writeError('Composer is operating slower than normal because you have Xdebug enabled. See https://getcomposer.org/xdebug'); } if (defined('COMPOSER_DEV_WARNING_TIME') && $commandName !== 'self-update' && $commandName !== 'selfupdate' && time() > COMPOSER_DEV_WARNING_TIME) { $io->writeError(sprintf('Warning: This development build of Composer is over 60 days old. It is recommended to update it by running "%s self-update" to get the latest version.', $_SERVER['PHP_SELF'])); } if ($isNonAllowedRoot) { if ($commandName !== 'self-update' && $commandName !== 'selfupdate' && $commandName !== '_complete') { $io->writeError('Do not run Composer as root/super user! See https://getcomposer.org/root for details'); if ($io->isInteractive()) { if (!$io->askConfirmation('Continue as root/super user [yes]? ')) { return 1; } } } } Silencer::call(static function () use ($io): void { $tempfile = sys_get_temp_dir() . '/temp-' . md5(microtime()); if (!(file_put_contents($tempfile, __FILE__) && (file_get_contents($tempfile) === __FILE__) && unlink($tempfile) && !file_exists($tempfile))) { $io->writeError(sprintf('PHP temp directory (%s) does not exist or is not writable to Composer. Set sys_temp_dir in your php.ini', sys_get_temp_dir())); } }); $file = Factory::getComposerFile(); if (is_file($file) && Filesystem::isReadable($file) && is_array($composer = json_decode(file_get_contents($file), true))) { if (isset($composer['scripts']) && is_array($composer['scripts'])) { foreach ($composer['scripts'] as $script => $dummy) { if (!defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) { if ($this->has($script)) { $io->writeError('A script named '.$script.' would override a Composer command and has been skipped'); } else { $description = null; if (isset($composer['scripts-descriptions'][$script])) { $description = $composer['scripts-descriptions'][$script]; } $this->add(new Command\ScriptAliasCommand($script, $description)); } } } } } } try { if ($input->hasParameterOption('--profile')) { $startTime = microtime(true); $this->io->enableDebugging($startTime); } $result = parent::doRun($input, $output); if (isset($oldWorkingDir) && '' !== $oldWorkingDir) { Silencer::call('chdir', $oldWorkingDir); } if (isset($startTime)) { $io->writeError('Memory usage: '.round(memory_get_usage() / 1024 / 1024, 2).'MiB (peak: '.round(memory_get_peak_usage() / 1024 / 1024, 2).'MiB), time: '.round(microtime(true) - $startTime, 2).'s'); } return $result; } catch (ScriptExecutionException $e) { return $e->getCode(); } catch (\Throwable $e) { $ghe = new GithubActionError($this->io); $ghe->emit($e->getMessage()); $this->hintCommonErrors($e, $output); if (!$e instanceof \Exception) { if ($output instanceof ConsoleOutputInterface) { $this->renderThrowable($e, $output->getErrorOutput()); } else { $this->renderThrowable($e, $output); } $exitCode = $e->getCode(); if (is_numeric($exitCode)) { $exitCode = (int) $exitCode; if (0 === $exitCode) { $exitCode = 1; } } else { $exitCode = 1; } return $exitCode; } throw $e; } finally { restore_error_handler(); } } private function getNewWorkingDir(InputInterface $input): ?string { $workingDir = $input->getParameterOption(['--working-dir', '-d'], null, true); if (null !== $workingDir && !is_dir($workingDir)) { throw new \RuntimeException('Invalid working directory specified, '.$workingDir.' does not exist.'); } return $workingDir; } private function hintCommonErrors(\Throwable $exception, OutputInterface $output): void { $io = $this->getIO(); if ((get_class($exception) === LogicException::class || $exception instanceof \Error) && $output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) { $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); } Silencer::suppress(); try { $composer = $this->getComposer(false, true); if (null !== $composer && function_exists('disk_free_space')) { $config = $composer->getConfig(); $minSpaceFree = 1024 * 1024; if ((($df = disk_free_space($dir = $config->get('home'))) !== false && $df < $minSpaceFree) || (($df = disk_free_space($dir = $config->get('vendor-dir'))) !== false && $df < $minSpaceFree) || (($df = disk_free_space($dir = sys_get_temp_dir())) !== false && $df < $minSpaceFree) ) { $io->writeError('The disk hosting '.$dir.' is full, this may be the cause of the following exception', true, IOInterface::QUIET); } } } catch (\Exception $e) { } Silencer::restore(); if (Platform::isWindows() && false !== strpos($exception->getMessage(), 'The system cannot find the path specified')) { $io->writeError('The following exception may be caused by a stale entry in your cmd.exe AutoRun', true, IOInterface::QUIET); $io->writeError('Check https://getcomposer.org/doc/articles/troubleshooting.md#-the-system-cannot-find-the-path-specified-windows- for details', true, IOInterface::QUIET); } if (false !== strpos($exception->getMessage(), 'fork failed - Cannot allocate memory')) { $io->writeError('The following exception is caused by a lack of memory or swap, or not having swap configured', true, IOInterface::QUIET); $io->writeError('Check https://getcomposer.org/doc/articles/troubleshooting.md#proc-open-fork-failed-errors for details', true, IOInterface::QUIET); } if ($exception instanceof ProcessTimedOutException) { $io->writeError('The following exception is caused by a process timeout', true, IOInterface::QUIET); $io->writeError('Check https://getcomposer.org/doc/06-config.md#process-timeout for details', true, IOInterface::QUIET); } $hints = HttpDownloader::getExceptionHints($exception); if (null !== $hints && count($hints) > 0) { foreach ($hints as $hint) { $io->writeError($hint, true, IOInterface::QUIET); } } } public function getComposer(bool $required = true, ?bool $disablePlugins = null, ?bool $disableScripts = null): ?Composer { if (null === $disablePlugins) { $disablePlugins = $this->disablePluginsByDefault; } if (null === $disableScripts) { $disableScripts = $this->disableScriptsByDefault; } if (null === $this->composer) { try { $this->composer = Factory::create(Platform::isInputCompletionProcess() ? new NullIO() : $this->io, null, $disablePlugins, $disableScripts); } catch (\InvalidArgumentException $e) { if ($required) { $this->io->writeError($e->getMessage()); if ($this->areExceptionsCaught()) { exit(1); } throw $e; } } catch (JsonValidationException $e) { if ($required) { throw $e; } } } return $this->composer; } public function resetComposer(): void { $this->composer = null; if (method_exists($this->getIO(), 'resetAuthentications')) { $this->getIO()->resetAuthentications(); } } public function getIO(): IOInterface { return $this->io; } public function getHelp(): string { return self::$logo . parent::getHelp(); } protected function getDefaultCommands(): array { $commands = array_merge(parent::getDefaultCommands(), [ new Command\AboutCommand(), new Command\ConfigCommand(), new Command\DependsCommand(), new Command\ProhibitsCommand(), new Command\InitCommand(), new Command\InstallCommand(), new Command\CreateProjectCommand(), new Command\UpdateCommand(), new Command\SearchCommand(), new Command\ValidateCommand(), new Command\AuditCommand(), new Command\ShowCommand(), new Command\SuggestsCommand(), new Command\RequireCommand(), new Command\DumpAutoloadCommand(), new Command\StatusCommand(), new Command\ArchiveCommand(), new Command\DiagnoseCommand(), new Command\RunScriptCommand(), new Command\LicensesCommand(), new Command\GlobalCommand(), new Command\ClearCacheCommand(), new Command\RemoveCommand(), new Command\HomeCommand(), new Command\ExecCommand(), new Command\OutdatedCommand(), new Command\CheckPlatformReqsCommand(), new Command\FundCommand(), new Command\ReinstallCommand(), new Command\BumpCommand(), ]); if (strpos(__FILE__, 'phar:') === 0 || '1' === Platform::getEnv('COMPOSER_TESTS_ARE_RUNNING')) { $commands[] = new Command\SelfUpdateCommand(); } return $commands; } public function getLongVersion(): string { $branchAliasString = ''; if (Composer::BRANCH_ALIAS_VERSION && Composer::BRANCH_ALIAS_VERSION !== '@package_branch_alias_version'.'@') { $branchAliasString = sprintf(' (%s)', Composer::BRANCH_ALIAS_VERSION); } return sprintf( '%s version %s%s %s', $this->getName(), $this->getVersion(), $branchAliasString, Composer::RELEASE_DATE ); } protected function getDefaultInputDefinition(): InputDefinition { $definition = parent::getDefaultInputDefinition(); $definition->addOption(new InputOption('--profile', null, InputOption::VALUE_NONE, 'Display timing and memory usage information')); $definition->addOption(new InputOption('--no-plugins', null, InputOption::VALUE_NONE, 'Whether to disable plugins.')); $definition->addOption(new InputOption('--no-scripts', null, InputOption::VALUE_NONE, 'Skips the execution of all scripts defined in composer.json file.')); $definition->addOption(new InputOption('--working-dir', '-d', InputOption::VALUE_REQUIRED, 'If specified, use the given directory as working directory.')); $definition->addOption(new InputOption('--no-cache', null, InputOption::VALUE_NONE, 'Prevent use of the cache')); return $definition; } private function getPluginCommands(): array { $commands = []; $composer = $this->getComposer(false, false); if (null === $composer) { $composer = Factory::createGlobal($this->io, $this->disablePluginsByDefault, $this->disableScriptsByDefault); } if (null !== $composer) { $pm = $composer->getPluginManager(); foreach ($pm->getPluginCapabilities('Composer\Plugin\Capability\CommandProvider', ['composer' => $composer, 'io' => $this->io]) as $capability) { $newCommands = $capability->getCommands(); if (!is_array($newCommands)) { throw new \UnexpectedValueException('Plugin capability '.get_class($capability).' failed to return an array from getCommands'); } foreach ($newCommands as $command) { if (!$command instanceof Command\BaseCommand) { throw new \UnexpectedValueException('Plugin capability '.get_class($capability).' returned an invalid value, we expected an array of Composer\Command\BaseCommand objects'); } } $commands = array_merge($commands, $newCommands); } } return $commands; } public function getInitialWorkingDirectory() { return $this->initialWorkingDirectory; } private function getUseParentDirConfigValue() { $config = Factory::createConfig($this->io); return $config->get('use-parent-dir'); } } io = $io; } public function emit(string $message, ?string $file = null, ?int $line = null): void { if (Platform::getEnv('GITHUB_ACTIONS') && !Platform::getEnv('COMPOSER_TESTS_ARE_RUNNING')) { $message = $this->escapeData($message); if ($file && $line) { $file = $this->escapeProperty($file); $this->io->write("::error file=". $file .",line=". $line ."::". $message); } elseif ($file) { $file = $this->escapeProperty($file); $this->io->write("::error file=". $file ."::". $message); } else { $this->io->write("::error ::". $message); } } } private function escapeData(string $data): string { $data = str_replace("%", '%25', $data); $data = str_replace("\r", '%0D', $data); $data = str_replace("\n", '%0A', $data); return $data; } private function escapeProperty(string $property): string { $property = str_replace("%", '%25', $property); $property = str_replace("\r", '%0D', $property); $property = str_replace("\n", '%0A', $property); $property = str_replace(":", '%3A', $property); $property = str_replace(",", '%2C', $property); return $property; } } 'black', 31 => 'red', 32 => 'green', 33 => 'yellow', 34 => 'blue', 35 => 'magenta', 36 => 'cyan', 37 => 'white', ]; private static $availableBackgroundColors = [ 40 => 'black', 41 => 'red', 42 => 'green', 43 => 'yellow', 44 => 'blue', 45 => 'magenta', 46 => 'cyan', 47 => 'white', ]; private static $availableOptions = [ 1 => 'bold', 4 => 'underscore', ]; public function __construct(array $styles = []) { parent::__construct(true, $styles); } public function format(?string $message): ?string { $formatted = parent::format($message); if ($formatted === null) { return null; } $clearEscapeCodes = '(?:39|49|0|22|24|25|27|28)'; return Preg::replaceCallback("{\033\[([0-9;]+)m(.*?)\033\[(?:".$clearEscapeCodes.";)*?".$clearEscapeCodes."m}s", Closure::fromCallable([$this, 'formatHtml']), $formatted); } private function formatHtml(array $matches): string { $out = ''.$matches[2].''; } } suggestedValues = $suggestedValues; } public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void { $values = $this->suggestedValues; if ($values instanceof \Closure && !\is_array($values = $values($input, $suggestions))) { throw new LogicException(sprintf('Closure for option "%s" must return an array. Got "%s".', $this->getName(), get_debug_type($values))); } if ([] !== $values) { $suggestions->suggestValues($values); } } } suggestedValues = $suggestedValues; if ([] !== $suggestedValues && !$this->acceptValue()) { throw new LogicException('Cannot set suggested values if the option does not accept a value.'); } } public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void { $values = $this->suggestedValues; if ($values instanceof \Closure && !\is_array($values = $values($input, $suggestions))) { throw new LogicException(sprintf('Closure for argument "%s" must return an array. Got "%s".', $this->getName(), get_debug_type($values))); } if ([] !== $values) { $suggestions->suggestValues($values); } } } pool = $pool; $this->decisionMap = []; } public function decide(int $literal, int $level, Rule $why): void { $this->addDecision($literal, $level); $this->decisionQueue[] = [ self::DECISION_LITERAL => $literal, self::DECISION_REASON => $why, ]; } public function satisfy(int $literal): bool { $packageId = abs($literal); return ( $literal > 0 && isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] > 0 || $literal < 0 && isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] < 0 ); } public function conflict(int $literal): bool { $packageId = abs($literal); return ( (isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] > 0 && $literal < 0) || (isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] < 0 && $literal > 0) ); } public function decided(int $literalOrPackageId): bool { return !empty($this->decisionMap[abs($literalOrPackageId)]); } public function undecided(int $literalOrPackageId): bool { return empty($this->decisionMap[abs($literalOrPackageId)]); } public function decidedInstall(int $literalOrPackageId): bool { $packageId = abs($literalOrPackageId); return isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] > 0; } public function decisionLevel(int $literalOrPackageId): int { $packageId = abs($literalOrPackageId); if (isset($this->decisionMap[$packageId])) { return abs($this->decisionMap[$packageId]); } return 0; } public function decisionRule(int $literalOrPackageId): ?Rule { $packageId = abs($literalOrPackageId); foreach ($this->decisionQueue as $decision) { if ($packageId === abs($decision[self::DECISION_LITERAL])) { return $decision[self::DECISION_REASON]; } } return null; } public function atOffset(int $queueOffset): array { return $this->decisionQueue[$queueOffset]; } public function validOffset(int $queueOffset): bool { return $queueOffset >= 0 && $queueOffset < \count($this->decisionQueue); } public function lastReason(): Rule { return $this->decisionQueue[\count($this->decisionQueue) - 1][self::DECISION_REASON]; } public function lastLiteral(): int { return $this->decisionQueue[\count($this->decisionQueue) - 1][self::DECISION_LITERAL]; } public function reset(): void { while ($decision = array_pop($this->decisionQueue)) { $this->decisionMap[abs($decision[self::DECISION_LITERAL])] = 0; } } public function resetToOffset(int $offset): void { while (\count($this->decisionQueue) > $offset + 1) { $decision = array_pop($this->decisionQueue); $this->decisionMap[abs($decision[self::DECISION_LITERAL])] = 0; } } public function revertLast(): void { $this->decisionMap[abs($this->lastLiteral())] = 0; array_pop($this->decisionQueue); } public function count(): int { return \count($this->decisionQueue); } public function rewind(): void { end($this->decisionQueue); } #[\ReturnTypeWillChange] public function current() { return current($this->decisionQueue); } public function key(): ?int { return key($this->decisionQueue); } public function next(): void { prev($this->decisionQueue); } public function valid(): bool { return false !== current($this->decisionQueue); } public function isEmpty(): bool { return \count($this->decisionQueue) === 0; } protected function addDecision(int $literal, int $level): void { $packageId = abs($literal); $previousDecision = $this->decisionMap[$packageId] ?? 0; if ($previousDecision !== 0) { $literalString = $this->pool->literalToPrettyString($literal, []); $package = $this->pool->literalToPackage($literal); throw new SolverBugException( "Trying to decide $literalString on level $level, even though $package was previously decided as ".$previousDecision."." ); } if ($literal > 0) { $this->decisionMap[$packageId] = $level; } else { $this->decisionMap[$packageId] = -$level; } } public function toString(?Pool $pool = null): string { $decisionMap = $this->decisionMap; ksort($decisionMap); $str = '['; foreach ($decisionMap as $packageId => $level) { $str .= (($pool) ? $pool->literalToPackage($packageId) : $packageId).':'.$level.','; } $str .= ']'; return $str; } public function __toString(): string { return $this->toString(); } } preferStable = $preferStable; $this->preferLowest = $preferLowest; } public function versionCompare(PackageInterface $a, PackageInterface $b, string $operator): bool { if ($this->preferStable && ($stabA = $a->getStability()) !== ($stabB = $b->getStability())) { return BasePackage::$stabilities[$stabA] < BasePackage::$stabilities[$stabB]; } $constraint = new Constraint($operator, $b->getVersion()); $version = new Constraint('==', $a->getVersion()); return $constraint->matchSpecific($version, true); } public function selectPreferredPackages(Pool $pool, array $literals, ?string $requiredPackage = null): array { sort($literals); $resultCacheKey = implode(',', $literals).$requiredPackage; $poolId = spl_object_id($pool); if (isset($this->preferredPackageResultCachePerPool[$poolId][$resultCacheKey])) { return $this->preferredPackageResultCachePerPool[$poolId][$resultCacheKey]; } $packages = $this->groupLiteralsByName($pool, $literals); foreach ($packages as &$nameLiterals) { usort($nameLiterals, function ($a, $b) use ($pool, $requiredPackage, $poolId): int { $cacheKey = 'i'.$a.'.'.$b.$requiredPackage; if (isset($this->sortingCachePerPool[$poolId][$cacheKey])) { return $this->sortingCachePerPool[$poolId][$cacheKey]; } return $this->sortingCachePerPool[$poolId][$cacheKey] = $this->compareByPriority($pool, $pool->literalToPackage($a), $pool->literalToPackage($b), $requiredPackage, true); }); } foreach ($packages as &$sortedLiterals) { $sortedLiterals = $this->pruneToBestVersion($pool, $sortedLiterals); $sortedLiterals = $this->pruneRemoteAliases($pool, $sortedLiterals); } $selected = array_merge(...array_values($packages)); usort($selected, function ($a, $b) use ($pool, $requiredPackage, $poolId): int { $cacheKey = $a.'.'.$b.$requiredPackage; if (isset($this->sortingCachePerPool[$poolId][$cacheKey])) { return $this->sortingCachePerPool[$poolId][$cacheKey]; } return $this->sortingCachePerPool[$poolId][$cacheKey] = $this->compareByPriority($pool, $pool->literalToPackage($a), $pool->literalToPackage($b), $requiredPackage); }); return $this->preferredPackageResultCachePerPool[$poolId][$resultCacheKey] = $selected; } protected function groupLiteralsByName(Pool $pool, array $literals): array { $packages = []; foreach ($literals as $literal) { $packageName = $pool->literalToPackage($literal)->getName(); if (!isset($packages[$packageName])) { $packages[$packageName] = []; } $packages[$packageName][] = $literal; } return $packages; } public function compareByPriority(Pool $pool, BasePackage $a, BasePackage $b, ?string $requiredPackage = null, bool $ignoreReplace = false): int { if ($a->getName() === $b->getName()) { $aAliased = $a instanceof AliasPackage; $bAliased = $b instanceof AliasPackage; if ($aAliased && !$bAliased) { return -1; } if (!$aAliased && $bAliased) { return 1; } } if (!$ignoreReplace) { if ($this->replaces($a, $b)) { return 1; } if ($this->replaces($b, $a)) { return -1; } if ($requiredPackage && false !== ($pos = strpos($requiredPackage, '/'))) { $requiredVendor = substr($requiredPackage, 0, $pos); $aIsSameVendor = strpos($a->getName(), $requiredVendor) === 0; $bIsSameVendor = strpos($b->getName(), $requiredVendor) === 0; if ($bIsSameVendor !== $aIsSameVendor) { return $aIsSameVendor ? -1 : 1; } } } if ($a->id === $b->id) { return 0; } return ($a->id < $b->id) ? -1 : 1; } protected function replaces(BasePackage $source, BasePackage $target): bool { foreach ($source->getReplaces() as $link) { if ($link->getTarget() === $target->getName() ) { return true; } } return false; } protected function pruneToBestVersion(Pool $pool, array $literals): array { $operator = $this->preferLowest ? '<' : '>'; $bestLiterals = [$literals[0]]; $bestPackage = $pool->literalToPackage($literals[0]); foreach ($literals as $i => $literal) { if (0 === $i) { continue; } $package = $pool->literalToPackage($literal); if ($this->versionCompare($package, $bestPackage, $operator)) { $bestPackage = $package; $bestLiterals = [$literal]; } elseif ($this->versionCompare($package, $bestPackage, '==')) { $bestLiterals[] = $literal; } } return $bestLiterals; } protected function pruneRemoteAliases(Pool $pool, array $literals): array { $hasLocalAlias = false; foreach ($literals as $literal) { $package = $pool->literalToPackage($literal); if ($package instanceof AliasPackage && $package->isRootPackageAlias()) { $hasLocalAlias = true; break; } } if (!$hasLocalAlias) { return $literals; } $selected = []; foreach ($literals as $literal) { $package = $pool->literalToPackage($literal); if ($package instanceof AliasPackage && $package->isRootPackageAlias()) { $selected[] = $literal; } } return $selected; } } literals = $literals; } public function getLiterals(): array { return $this->literals; } public function getHash() { $data = unpack('ihash', md5(implode(',', $this->literals), true)); return $data['hash']; } public function equals(Rule $rule): bool { return $this->literals === $rule->getLiterals(); } public function isAssertion(): bool { return 1 === \count($this->literals); } public function __toString(): string { $result = $this->isDisabled() ? 'disabled(' : '('; foreach ($this->literals as $i => $literal) { if ($i !== 0) { $result .= '|'; } $result .= $literal; } $result .= ')'; return $result; } } getPackages(), $lockedRepository->getPackages() ); } } presentMap = $presentMap; $this->unlockableMap = $unlockableMap; $this->setResultPackages($pool, $decisions); parent::__construct($this->presentMap, $this->resultPackages['all']); } public function setResultPackages(Pool $pool, Decisions $decisions): void { $this->resultPackages = ['all' => [], 'non-dev' => [], 'dev' => []]; foreach ($decisions as $i => $decision) { $literal = $decision[Decisions::DECISION_LITERAL]; if ($literal > 0) { $package = $pool->literalToPackage($literal); $this->resultPackages['all'][] = $package; if (!isset($this->unlockableMap[$package->id])) { $this->resultPackages['non-dev'][] = $package; } } } } public function setNonDevPackages(LockTransaction $extractionResult): void { $packages = $extractionResult->getNewLockPackages(false); $this->resultPackages['dev'] = $this->resultPackages['non-dev']; $this->resultPackages['non-dev'] = []; foreach ($packages as $package) { foreach ($this->resultPackages['dev'] as $i => $resultPackage) { if ($package->getName() === $resultPackage->getName()) { $this->resultPackages['non-dev'][] = $resultPackage; unset($this->resultPackages['dev'][$i]); } } } } public function getNewLockPackages(bool $devMode, bool $updateMirrors = false): array { $packages = []; foreach ($this->resultPackages[$devMode ? 'dev' : 'non-dev'] as $package) { if (!$package instanceof AliasPackage) { if ($updateMirrors && !isset($this->presentMap[spl_object_hash($package)])) { foreach ($this->presentMap as $presentPackage) { if ($package->getName() === $presentPackage->getName() && $package->getVersion() === $presentPackage->getVersion()) { if ($presentPackage->getSourceReference() && $presentPackage->getSourceType() === $package->getSourceType()) { $package->setSourceDistReferences($presentPackage->getSourceReference()); } if ($presentPackage->getReleaseDate() !== null && $package instanceof Package) { $package->setReleaseDate($presentPackage->getReleaseDate()); } } } } $packages[] = $package; } } return $packages; } public function getAliases(array $aliases): array { $usedAliases = []; foreach ($this->resultPackages['all'] as $package) { if ($package instanceof AliasPackage) { foreach ($aliases as $index => $alias) { if ($alias['package'] === $package->getName()) { $usedAliases[] = $alias; unset($aliases[$index]); } } } } usort($usedAliases, static function ($a, $b): int { return strcmp($a['package'], $b['package']); }); return $usedAliases; } } literals = $literals; } public function getLiterals(): array { return $this->literals; } public function getHash() { $data = unpack('ihash', md5('c:'.implode(',', $this->literals), true)); return $data['hash']; } public function equals(Rule $rule): bool { if ($rule instanceof MultiConflictRule) { return $this->literals === $rule->getLiterals(); } return false; } public function isAssertion(): bool { return false; } public function disable(): void { throw new \RuntimeException("Disabling multi conflict rules is not possible. Please contact composer at https://github.com/composer/composer to let us debug what lead to this situation."); } public function __toString(): string { $result = $this->isDisabled() ? 'disabled(multi(' : '(multi('; foreach ($this->literals as $i => $literal) { if ($i !== 0) { $result .= '|'; } $result .= $literal; } $result .= '))'; return $result; } } package = $package; } public function getPackage(): PackageInterface { return $this->package; } public function show($lock): string { return self::format($this->package, $lock); } public static function format(PackageInterface $package, bool $lock = false): string { return ($lock ? 'Locking ' : 'Installing ').''.$package->getPrettyName().' ('.$package->getFullPrettyVersion().')'; } } package = $package; } public function getPackage(): AliasPackage { return $this->package; } public function show($lock): string { return 'Marking '.$this->package->getPrettyName().' ('.$this->package->getFullPrettyVersion().') as installed, alias of '.$this->package->getAliasOf()->getPrettyName().' ('.$this->package->getAliasOf()->getFullPrettyVersion().')'; } } package = $package; } public function getPackage(): AliasPackage { return $this->package; } public function show($lock): string { return 'Marking '.$this->package->getPrettyName().' ('.$this->package->getFullPrettyVersion().') as uninstalled, alias of '.$this->package->getAliasOf()->getPrettyName().' ('.$this->package->getAliasOf()->getFullPrettyVersion().')'; } } show(false); } } package = $package; } public function getPackage(): PackageInterface { return $this->package; } public function show($lock): string { return self::format($this->package, $lock); } public static function format(PackageInterface $package, bool $lock = false): string { return 'Removing '.$package->getPrettyName().' ('.$package->getFullPrettyVersion().')'; } } initialPackage = $initial; $this->targetPackage = $target; } public function getInitialPackage(): PackageInterface { return $this->initialPackage; } public function getTargetPackage(): PackageInterface { return $this->targetPackage; } public function show($lock): string { return self::format($this->initialPackage, $this->targetPackage, $lock); } public static function format(PackageInterface $initialPackage, PackageInterface $targetPackage, bool $lock = false): string { $fromVersion = $initialPackage->getFullPrettyVersion(); $toVersion = $targetPackage->getFullPrettyVersion(); if ($fromVersion === $toVersion && $initialPackage->getSourceReference() !== $targetPackage->getSourceReference()) { $fromVersion = $initialPackage->getFullPrettyVersion(true, PackageInterface::DISPLAY_SOURCE_REF); $toVersion = $targetPackage->getFullPrettyVersion(true, PackageInterface::DISPLAY_SOURCE_REF); } elseif ($fromVersion === $toVersion && $initialPackage->getDistReference() !== $targetPackage->getDistReference()) { $fromVersion = $initialPackage->getFullPrettyVersion(true, PackageInterface::DISPLAY_DIST_REF); $toVersion = $targetPackage->getFullPrettyVersion(true, PackageInterface::DISPLAY_DIST_REF); } $actionName = VersionParser::isUpgrade($initialPackage->getVersion(), $targetPackage->getVersion()) ? 'Upgrading' : 'Downgrading'; return $actionName.' '.$initialPackage->getPrettyName().' ('.$fromVersion.' => '.$toVersion.')'; } } versionParser = new VersionParser; $this->setPackages($packages); $this->unacceptableFixedOrLockedPackages = $unacceptableFixedOrLockedPackages; $this->removedVersions = $removedVersions; $this->removedVersionsByPackage = $removedVersionsByPackage; } public function getRemovedVersions(string $name, ConstraintInterface $constraint): array { if (!isset($this->removedVersions[$name])) { return []; } $result = []; foreach ($this->removedVersions[$name] as $version => $prettyVersion) { if ($constraint->matches(new Constraint('==', $version))) { $result[$version] = $prettyVersion; } } return $result; } public function getRemovedVersionsByPackage(string $objectHash): array { if (!isset($this->removedVersionsByPackage[$objectHash])) { return []; } return $this->removedVersionsByPackage[$objectHash]; } private function setPackages(array $packages): void { $id = 1; foreach ($packages as $package) { $this->packages[] = $package; $package->id = $id++; foreach ($package->getNames() as $provided) { $this->packageByName[$provided][] = $package; } } } public function getPackages(): array { return $this->packages; } public function packageById(int $id): BasePackage { return $this->packages[$id - 1]; } public function count(): int { return \count($this->packages); } public function whatProvides(string $name, ?ConstraintInterface $constraint = null): array { $key = (string) $constraint; if (isset($this->providerCache[$name][$key])) { return $this->providerCache[$name][$key]; } return $this->providerCache[$name][$key] = $this->computeWhatProvides($name, $constraint); } private function computeWhatProvides(string $name, ?ConstraintInterface $constraint = null): array { if (!isset($this->packageByName[$name])) { return []; } $matches = []; foreach ($this->packageByName[$name] as $candidate) { if ($this->match($candidate, $name, $constraint)) { $matches[] = $candidate; } } return $matches; } public function literalToPackage(int $literal): BasePackage { $packageId = abs($literal); return $this->packageById($packageId); } public function literalToPrettyString(int $literal, array $installedMap): string { $package = $this->literalToPackage($literal); if (isset($installedMap[$package->id])) { $prefix = ($literal > 0 ? 'keep' : 'remove'); } else { $prefix = ($literal > 0 ? 'install' : 'don\'t install'); } return $prefix.' '.$package->getPrettyString(); } public function match(BasePackage $candidate, string $name, ?ConstraintInterface $constraint = null): bool { $candidateName = $candidate->getName(); $candidateVersion = $candidate->getVersion(); if ($candidateName === $name) { return $constraint === null || CompilingMatcher::match($constraint, Constraint::OP_EQ, $candidateVersion); } $provides = $candidate->getProvides(); $replaces = $candidate->getReplaces(); if (isset($replaces[0]) || isset($provides[0])) { foreach ($provides as $link) { if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) { return true; } } foreach ($replaces as $link) { if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) { return true; } } return false; } if (isset($provides[$name]) && ($constraint === null || $constraint->matches($provides[$name]->getConstraint()))) { return true; } if (isset($replaces[$name]) && ($constraint === null || $constraint->matches($replaces[$name]->getConstraint()))) { return true; } return false; } public function isUnacceptableFixedOrLockedPackage(BasePackage $package): bool { return \in_array($package, $this->unacceptableFixedOrLockedPackages, true); } public function getUnacceptableFixedOrLockedPackages(): array { return $this->unacceptableFixedOrLockedPackages; } public function __toString(): string { $str = "Pool:\n"; foreach ($this->packages as $package) { $str .= '- '.str_pad((string) $package->id, 6, ' ', STR_PAD_LEFT).': '.$package->getName()."\n"; } return $str; } } acceptableStabilities = $acceptableStabilities; $this->stabilityFlags = $stabilityFlags; $this->rootAliases = $rootAliases; $this->rootReferences = $rootReferences; $this->eventDispatcher = $eventDispatcher; $this->poolOptimizer = $poolOptimizer; $this->io = $io; $this->temporaryConstraints = $temporaryConstraints; } public function buildPool(array $repositories, Request $request): Pool { if ($request->getUpdateAllowList()) { $this->updateAllowList = $request->getUpdateAllowList(); $this->warnAboutNonMatchingUpdateAllowList($request); foreach ($request->getLockedRepository()->getPackages() as $lockedPackage) { if (!$this->isUpdateAllowed($lockedPackage)) { $this->skippedLoad[$lockedPackage->getName()][] = $lockedPackage; foreach ($lockedPackage->getReplaces() as $link) { $this->skippedLoad[$link->getTarget()][] = $lockedPackage; } if ($lockedPackage->getDistType() === 'path') { $transportOptions = $lockedPackage->getTransportOptions(); if (!isset($transportOptions['symlink']) || $transportOptions['symlink'] !== false) { $this->pathRepoUnlocked[$lockedPackage->getName()] = true; continue; } } $request->lockPackage($lockedPackage); } } } foreach ($request->getFixedOrLockedPackages() as $package) { $this->loadedPackages[$package->getName()] = new MatchAllConstraint(); foreach ($package->getReplaces() as $link) { $this->loadedPackages[$link->getTarget()] = new MatchAllConstraint(); } if ( $package->getRepository() instanceof RootPackageRepository || $package->getRepository() instanceof PlatformRepository || StabilityFilter::isPackageAcceptable($this->acceptableStabilities, $this->stabilityFlags, $package->getNames(), $package->getStability()) ) { $this->loadPackage($request, $repositories, $package, false); } else { $this->unacceptableFixedOrLockedPackages[] = $package; } } foreach ($request->getRequires() as $packageName => $constraint) { if (isset($this->loadedPackages[$packageName])) { continue; } $this->packagesToLoad[$packageName] = $constraint; $this->maxExtendedReqs[$packageName] = true; } foreach ($this->packagesToLoad as $name => $constraint) { if (isset($this->loadedPackages[$name])) { unset($this->packagesToLoad[$name]); } } while (!empty($this->packagesToLoad)) { $this->loadPackagesMarkedForLoading($request, $repositories); } if (\count($this->temporaryConstraints) > 0) { foreach ($this->packages as $i => $package) { if (!isset($this->temporaryConstraints[$package->getName()]) || $package instanceof AliasPackage) { continue; } $constraint = $this->temporaryConstraints[$package->getName()]; $packageAndAliases = [$i => $package]; if (isset($this->aliasMap[spl_object_hash($package)])) { $packageAndAliases += $this->aliasMap[spl_object_hash($package)]; } $found = false; foreach ($packageAndAliases as $packageOrAlias) { if (CompilingMatcher::match($constraint, Constraint::OP_EQ, $packageOrAlias->getVersion())) { $found = true; } } if (!$found) { foreach ($packageAndAliases as $index => $packageOrAlias) { unset($this->packages[$index]); } } } } if ($this->eventDispatcher) { $prePoolCreateEvent = new PrePoolCreateEvent( PluginEvents::PRE_POOL_CREATE, $repositories, $request, $this->acceptableStabilities, $this->stabilityFlags, $this->rootAliases, $this->rootReferences, $this->packages, $this->unacceptableFixedOrLockedPackages ); $this->eventDispatcher->dispatch($prePoolCreateEvent->getName(), $prePoolCreateEvent); $this->packages = $prePoolCreateEvent->getPackages(); $this->unacceptableFixedOrLockedPackages = $prePoolCreateEvent->getUnacceptableFixedPackages(); } $pool = new Pool($this->packages, $this->unacceptableFixedOrLockedPackages); $this->aliasMap = []; $this->packagesToLoad = []; $this->loadedPackages = []; $this->loadedPerRepo = []; $this->packages = []; $this->unacceptableFixedOrLockedPackages = []; $this->maxExtendedReqs = []; $this->skippedLoad = []; $this->indexCounter = 0; $this->io->debug('Built pool.'); $pool = $this->runOptimizer($request, $pool); Intervals::clear(); return $pool; } private function markPackageNameForLoading(Request $request, string $name, ConstraintInterface $constraint): void { if (PlatformRepository::isPlatformPackage($name)) { return; } if (isset($this->maxExtendedReqs[$name])) { return; } $rootRequires = $request->getRequires(); if (isset($rootRequires[$name]) && !Intervals::isSubsetOf($constraint, $rootRequires[$name])) { $constraint = $rootRequires[$name]; } if (!isset($this->loadedPackages[$name])) { if (isset($this->packagesToLoad[$name])) { if (Intervals::isSubsetOf($constraint, $this->packagesToLoad[$name])) { return; } $constraint = Intervals::compactConstraint(MultiConstraint::create([$this->packagesToLoad[$name], $constraint], false)); } $this->packagesToLoad[$name] = $constraint; return; } if (Intervals::isSubsetOf($constraint, $this->loadedPackages[$name])) { return; } $this->packagesToLoad[$name] = Intervals::compactConstraint(MultiConstraint::create([$this->loadedPackages[$name], $constraint], false)); unset($this->loadedPackages[$name]); } private function loadPackagesMarkedForLoading(Request $request, array $repositories): void { foreach ($this->packagesToLoad as $name => $constraint) { $this->loadedPackages[$name] = $constraint; } $packageBatch = $this->packagesToLoad; $this->packagesToLoad = []; foreach ($repositories as $repoIndex => $repository) { if (empty($packageBatch)) { break; } if ($repository instanceof PlatformRepository || $repository === $request->getLockedRepository()) { continue; } $result = $repository->loadPackages($packageBatch, $this->acceptableStabilities, $this->stabilityFlags, $this->loadedPerRepo[$repoIndex] ?? []); foreach ($result['namesFound'] as $name) { unset($packageBatch[$name]); } foreach ($result['packages'] as $package) { $this->loadedPerRepo[$repoIndex][$package->getName()][$package->getVersion()] = $package; $this->loadPackage($request, $repositories, $package, !isset($this->pathRepoUnlocked[$package->getName()])); } } } private function loadPackage(Request $request, array $repositories, BasePackage $package, bool $propagateUpdate): void { $index = $this->indexCounter++; $this->packages[$index] = $package; if ($package instanceof AliasPackage) { $this->aliasMap[spl_object_hash($package->getAliasOf())][$index] = $package; } $name = $package->getName(); if (isset($this->rootReferences[$name])) { if (!$request->isLockedPackage($package) && !$request->isFixedPackage($package)) { $package->setSourceDistReferences($this->rootReferences[$name]); } } if ($propagateUpdate && isset($this->rootAliases[$name][$package->getVersion()])) { $alias = $this->rootAliases[$name][$package->getVersion()]; if ($package instanceof AliasPackage) { $basePackage = $package->getAliasOf(); } else { $basePackage = $package; } if ($basePackage instanceof CompletePackage) { $aliasPackage = new CompleteAliasPackage($basePackage, $alias['alias_normalized'], $alias['alias']); } else { $aliasPackage = new AliasPackage($basePackage, $alias['alias_normalized'], $alias['alias']); } $aliasPackage->setRootPackageAlias(true); $newIndex = $this->indexCounter++; $this->packages[$newIndex] = $aliasPackage; $this->aliasMap[spl_object_hash($aliasPackage->getAliasOf())][$newIndex] = $aliasPackage; } foreach ($package->getRequires() as $link) { $require = $link->getTarget(); $linkConstraint = $link->getConstraint(); if (isset($this->skippedLoad[$require])) { if ($propagateUpdate && $request->getUpdateAllowTransitiveDependencies()) { $skippedRootRequires = $this->getSkippedRootRequires($request, $require); if ($request->getUpdateAllowTransitiveRootDependencies() || !$skippedRootRequires) { $this->unlockPackage($request, $repositories, $require); $this->markPackageNameForLoading($request, $require, $linkConstraint); } else { foreach ($skippedRootRequires as $rootRequire) { if (!isset($this->updateAllowWarned[$rootRequire])) { $this->updateAllowWarned[$rootRequire] = true; $this->io->writeError('Dependency '.$rootRequire.' is also a root requirement. Package has not been listed as an update argument, so keeping locked at old version. Use --with-all-dependencies (-W) to include root dependencies.'); } } } } elseif (isset($this->pathRepoUnlocked[$require]) && !isset($this->loadedPackages[$require])) { $this->markPackageNameForLoading($request, $require, $linkConstraint); } } else { $this->markPackageNameForLoading($request, $require, $linkConstraint); } } if ($propagateUpdate && $request->getUpdateAllowTransitiveDependencies()) { foreach ($package->getReplaces() as $link) { $replace = $link->getTarget(); if (isset($this->loadedPackages[$replace], $this->skippedLoad[$replace])) { $skippedRootRequires = $this->getSkippedRootRequires($request, $replace); if ($request->getUpdateAllowTransitiveRootDependencies() || !$skippedRootRequires) { $this->unlockPackage($request, $repositories, $replace); $this->markPackageNameForLoading($request, $replace, $link->getConstraint()); } else { foreach ($skippedRootRequires as $rootRequire) { if (!isset($this->updateAllowWarned[$rootRequire])) { $this->updateAllowWarned[$rootRequire] = true; $this->io->writeError('Dependency '.$rootRequire.' is also a root requirement. Package has not been listed as an update argument, so keeping locked at old version. Use --with-all-dependencies (-W) to include root dependencies.'); } } } } } } } private function isRootRequire(Request $request, string $name): bool { $rootRequires = $request->getRequires(); return isset($rootRequires[$name]); } private function getSkippedRootRequires(Request $request, string $name): array { if (!isset($this->skippedLoad[$name])) { return []; } $rootRequires = $request->getRequires(); $matches = []; if (isset($rootRequires[$name])) { return array_map(static function (PackageInterface $package) use ($name): string { if ($name !== $package->getName()) { return $package->getName() .' (via replace of '.$name.')'; } return $package->getName(); }, $this->skippedLoad[$name]); } foreach ($this->skippedLoad[$name] as $packageOrReplacer) { if (isset($rootRequires[$packageOrReplacer->getName()])) { $matches[] = $packageOrReplacer->getName(); } foreach ($packageOrReplacer->getReplaces() as $link) { if (isset($rootRequires[$link->getTarget()])) { if ($name !== $packageOrReplacer->getName()) { $matches[] = $packageOrReplacer->getName() .' (via replace of '.$name.')'; } else { $matches[] = $packageOrReplacer->getName(); } break; } } } return $matches; } private function isUpdateAllowed(BasePackage $package): bool { foreach ($this->updateAllowList as $pattern => $void) { $patternRegexp = BasePackage::packageNameToRegexp($pattern); if (Preg::isMatch($patternRegexp, $package->getName())) { return true; } } return false; } private function warnAboutNonMatchingUpdateAllowList(Request $request): void { foreach ($this->updateAllowList as $pattern => $void) { $patternRegexp = BasePackage::packageNameToRegexp($pattern); foreach ($request->getLockedRepository()->getPackages() as $package) { if (Preg::isMatch($patternRegexp, $package->getName())) { continue 2; } } foreach ($request->getRequires() as $packageName => $constraint) { if (Preg::isMatch($patternRegexp, $packageName)) { continue 2; } } if (strpos($pattern, '*') !== false) { $this->io->writeError('Pattern "' . $pattern . '" listed for update does not match any locked packages.'); } else { $this->io->writeError('Package "' . $pattern . '" listed for update is not locked.'); } } } private function unlockPackage(Request $request, array $repositories, string $name): void { foreach ($this->skippedLoad[$name] as $packageOrReplacer) { if ($packageOrReplacer->getName() !== $name && isset($this->skippedLoad[$packageOrReplacer->getName()])) { $replacerName = $packageOrReplacer->getName(); if ($request->getUpdateAllowTransitiveRootDependencies() || (!$this->isRootRequire($request, $name) && !$this->isRootRequire($request, $replacerName))) { $this->unlockPackage($request, $repositories, $replacerName); if ($this->isRootRequire($request, $replacerName)) { $this->markPackageNameForLoading($request, $replacerName, new MatchAllConstraint); } else { foreach ($this->packages as $loadedPackage) { $requires = $loadedPackage->getRequires(); if (isset($requires[$replacerName])) { $this->markPackageNameForLoading($request, $replacerName, $requires[$replacerName]->getConstraint()); } } } } } } if (isset($this->pathRepoUnlocked[$name])) { foreach ($this->packages as $index => $package) { if ($package->getName() === $name) { $this->removeLoadedPackage($request, $repositories, $package, $index); } } } unset($this->skippedLoad[$name], $this->loadedPackages[$name], $this->maxExtendedReqs[$name], $this->pathRepoUnlocked[$name]); foreach ($request->getLockedPackages() as $lockedPackage) { if (!($lockedPackage instanceof AliasPackage) && $lockedPackage->getName() === $name) { if (false !== $index = array_search($lockedPackage, $this->packages, true)) { $request->unlockPackage($lockedPackage); $this->removeLoadedPackage($request, $repositories, $lockedPackage, $index); foreach ($request->getFixedOrLockedPackages() as $fixedOrLockedPackage) { if ($fixedOrLockedPackage === $lockedPackage) { continue; } if (isset($this->skippedLoad[$fixedOrLockedPackage->getName()])) { $requires = $fixedOrLockedPackage->getRequires(); if (isset($requires[$lockedPackage->getName()])) { $this->markPackageNameForLoading($request, $lockedPackage->getName(), $requires[$lockedPackage->getName()]->getConstraint()); } } } } } } } private function removeLoadedPackage(Request $request, array $repositories, BasePackage $package, int $index): void { $repoIndex = array_search($package->getRepository(), $repositories, true); unset($this->loadedPerRepo[$repoIndex][$package->getName()][$package->getVersion()]); unset($this->packages[$index]); if (isset($this->aliasMap[spl_object_hash($package)])) { foreach ($this->aliasMap[spl_object_hash($package)] as $aliasIndex => $aliasPackage) { unset($this->loadedPerRepo[$repoIndex][$aliasPackage->getName()][$aliasPackage->getVersion()]); unset($this->packages[$aliasIndex]); } unset($this->aliasMap[spl_object_hash($package)]); } } private function runOptimizer(Request $request, Pool $pool): Pool { if (null === $this->poolOptimizer) { return $pool; } $this->io->debug('Running pool optimizer.'); $before = microtime(true); $total = \count($pool->getPackages()); $pool = $this->poolOptimizer->optimize($request, $pool); $filtered = $total - \count($pool->getPackages()); if (0 === $filtered) { return $pool; } $this->io->write(sprintf('Pool optimizer completed in %.3f seconds', microtime(true) - $before), true, IOInterface::VERY_VERBOSE); $this->io->write(sprintf( 'Found %s package versions referenced in your dependency graph. %s (%d%%) were optimized away.', number_format($total), number_format($filtered), round(100 / $total * $filtered) ), true, IOInterface::VERY_VERBOSE); return $pool; } } policy = $policy; } public function optimize(Request $request, Pool $pool): Pool { $this->prepare($request, $pool); $this->optimizeByIdenticalDependencies($request, $pool); $this->optimizeImpossiblePackagesAway($request, $pool); $optimizedPool = $this->applyRemovalsToPool($pool); $this->irremovablePackages = []; $this->requireConstraintsPerPackage = []; $this->conflictConstraintsPerPackage = []; $this->packagesToRemove = []; $this->aliasesPerPackage = []; $this->removedVersionsByPackage = []; return $optimizedPool; } private function prepare(Request $request, Pool $pool): void { $irremovablePackageConstraintGroups = []; foreach ($request->getFixedOrLockedPackages() as $package) { $irremovablePackageConstraintGroups[$package->getName()][] = new Constraint('==', $package->getVersion()); } foreach ($request->getRequires() as $require => $constraint) { $this->extractRequireConstraintsPerPackage($require, $constraint); } foreach ($pool->getPackages() as $package) { foreach ($package->getRequires() as $link) { $this->extractRequireConstraintsPerPackage($link->getTarget(), $link->getConstraint()); } foreach ($package->getConflicts() as $link) { $this->extractConflictConstraintsPerPackage($link->getTarget(), $link->getConstraint()); } if ($package instanceof AliasPackage) { $this->aliasesPerPackage[$package->getAliasOf()->id][] = $package; } } $irremovablePackageConstraints = []; foreach ($irremovablePackageConstraintGroups as $packageName => $constraints) { $irremovablePackageConstraints[$packageName] = 1 === \count($constraints) ? $constraints[0] : new MultiConstraint($constraints, false); } unset($irremovablePackageConstraintGroups); foreach ($pool->getPackages() as $package) { if (!isset($irremovablePackageConstraints[$package->getName()])) { continue; } if (CompilingMatcher::match($irremovablePackageConstraints[$package->getName()], Constraint::OP_EQ, $package->getVersion())) { $this->markPackageIrremovable($package); } } } private function markPackageIrremovable(BasePackage $package): void { $this->irremovablePackages[$package->id] = true; if ($package instanceof AliasPackage) { $this->markPackageIrremovable($package->getAliasOf()); } if (isset($this->aliasesPerPackage[$package->id])) { foreach ($this->aliasesPerPackage[$package->id] as $aliasPackage) { $this->irremovablePackages[$aliasPackage->id] = true; } } } private function applyRemovalsToPool(Pool $pool): Pool { $packages = []; $removedVersions = []; foreach ($pool->getPackages() as $package) { if (!isset($this->packagesToRemove[$package->id])) { $packages[] = $package; } else { $removedVersions[$package->getName()][$package->getVersion()] = $package->getPrettyVersion(); } } $optimizedPool = new Pool($packages, $pool->getUnacceptableFixedOrLockedPackages(), $removedVersions, $this->removedVersionsByPackage); return $optimizedPool; } private function optimizeByIdenticalDependencies(Request $request, Pool $pool): void { $identicalDefinitionsPerPackage = []; $packageIdenticalDefinitionLookup = []; foreach ($pool->getPackages() as $package) { if (isset($this->irremovablePackages[$package->id])) { continue; } $this->markPackageForRemoval($package->id); $dependencyHash = $this->calculateDependencyHash($package); foreach ($package->getNames(false) as $packageName) { if (!isset($this->requireConstraintsPerPackage[$packageName])) { continue; } foreach ($this->requireConstraintsPerPackage[$packageName] as $requireConstraint) { $groupHashParts = []; if (CompilingMatcher::match($requireConstraint, Constraint::OP_EQ, $package->getVersion())) { $groupHashParts[] = 'require:' . (string) $requireConstraint; } if ($package->getReplaces()) { foreach ($package->getReplaces() as $link) { if (CompilingMatcher::match($link->getConstraint(), Constraint::OP_EQ, $package->getVersion())) { $groupHashParts[] = 'require:' . (string) $link->getConstraint(); } } } if (isset($this->conflictConstraintsPerPackage[$packageName])) { foreach ($this->conflictConstraintsPerPackage[$packageName] as $conflictConstraint) { if (CompilingMatcher::match($conflictConstraint, Constraint::OP_EQ, $package->getVersion())) { $groupHashParts[] = 'conflict:' . (string) $conflictConstraint; } } } if (!$groupHashParts) { continue; } $groupHash = implode('', $groupHashParts); $identicalDefinitionsPerPackage[$packageName][$groupHash][$dependencyHash][] = $package; $packageIdenticalDefinitionLookup[$package->id][$packageName] = ['groupHash' => $groupHash, 'dependencyHash' => $dependencyHash]; } } } foreach ($identicalDefinitionsPerPackage as $constraintGroups) { foreach ($constraintGroups as $constraintGroup) { foreach ($constraintGroup as $packages) { if (1 === \count($packages)) { $this->keepPackage($packages[0], $identicalDefinitionsPerPackage, $packageIdenticalDefinitionLookup); continue; } $literals = []; foreach ($packages as $package) { $literals[] = $package->id; } foreach ($this->policy->selectPreferredPackages($pool, $literals) as $preferredLiteral) { $this->keepPackage($pool->literalToPackage($preferredLiteral), $identicalDefinitionsPerPackage, $packageIdenticalDefinitionLookup); } } } } } private function calculateDependencyHash(BasePackage $package): string { $hash = ''; $hashRelevantLinks = [ 'requires' => $package->getRequires(), 'conflicts' => $package->getConflicts(), 'replaces' => $package->getReplaces(), 'provides' => $package->getProvides(), ]; foreach ($hashRelevantLinks as $key => $links) { if (0 === \count($links)) { continue; } $hash .= $key . ':'; $subhash = []; foreach ($links as $link) { $subhash[$link->getTarget()] = (string) $link->getConstraint(); } ksort($subhash); foreach ($subhash as $target => $constraint) { $hash .= $target . '@' . $constraint; } } return $hash; } private function markPackageForRemoval(int $id): void { if (isset($this->irremovablePackages[$id])) { throw new \LogicException('Attempted removing a package which was previously marked irremovable'); } $this->packagesToRemove[$id] = true; } private function keepPackage(BasePackage $package, array $identicalDefinitionsPerPackage, array $packageIdenticalDefinitionLookup): void { if (!isset($this->packagesToRemove[$package->id])) { return; } unset($this->packagesToRemove[$package->id]); if ($package instanceof AliasPackage) { $this->keepPackage($package->getAliasOf(), $identicalDefinitionsPerPackage, $packageIdenticalDefinitionLookup); } foreach ($package->getNames(false) as $name) { if (isset($packageIdenticalDefinitionLookup[$package->id][$name])) { $packageGroupPointers = $packageIdenticalDefinitionLookup[$package->id][$name]; $packageGroup = $identicalDefinitionsPerPackage[$name][$packageGroupPointers['groupHash']][$packageGroupPointers['dependencyHash']]; foreach ($packageGroup as $pkg) { if ($pkg instanceof AliasPackage && $pkg->getPrettyVersion() === VersionParser::DEFAULT_BRANCH_ALIAS) { $pkg = $pkg->getAliasOf(); } $this->removedVersionsByPackage[spl_object_hash($package)][$pkg->getVersion()] = $pkg->getPrettyVersion(); } } } if (isset($this->aliasesPerPackage[$package->id])) { foreach ($this->aliasesPerPackage[$package->id] as $aliasPackage) { unset($this->packagesToRemove[$aliasPackage->id]); foreach ($aliasPackage->getNames(false) as $name) { if (isset($packageIdenticalDefinitionLookup[$aliasPackage->id][$name])) { $packageGroupPointers = $packageIdenticalDefinitionLookup[$aliasPackage->id][$name]; $packageGroup = $identicalDefinitionsPerPackage[$name][$packageGroupPointers['groupHash']][$packageGroupPointers['dependencyHash']]; foreach ($packageGroup as $pkg) { if ($pkg instanceof AliasPackage && $pkg->getPrettyVersion() === VersionParser::DEFAULT_BRANCH_ALIAS) { $pkg = $pkg->getAliasOf(); } $this->removedVersionsByPackage[spl_object_hash($aliasPackage)][$pkg->getVersion()] = $pkg->getPrettyVersion(); } } } } } } private function optimizeImpossiblePackagesAway(Request $request, Pool $pool): void { if (count($request->getLockedPackages()) === 0) { return; } $packageIndex = []; foreach ($pool->getPackages() as $package) { $id = $package->id; if (isset($this->irremovablePackages[$id])) { continue; } if (isset($this->aliasesPerPackage[$id]) || $package instanceof AliasPackage) { continue; } if ($request->isFixedPackage($package) || $request->isLockedPackage($package)) { continue; } $packageIndex[$package->getName()][$package->id] = $package; } foreach ($request->getLockedPackages() as $package) { $isUnusedPackage = true; foreach ($package->getNames(false) as $packageName) { if (isset($this->requireConstraintsPerPackage[$packageName])) { $isUnusedPackage = false; break; } } if ($isUnusedPackage) { continue; } foreach ($package->getRequires() as $link) { $require = $link->getTarget(); if (!isset($packageIndex[$require])) { continue; } $linkConstraint = $link->getConstraint(); foreach ($packageIndex[$require] as $id => $requiredPkg) { if (false === CompilingMatcher::match($linkConstraint, Constraint::OP_EQ, $requiredPkg->getVersion())) { $this->markPackageForRemoval($id); unset($packageIndex[$require][$id]); } } } } } private function extractRequireConstraintsPerPackage(string $package, ConstraintInterface $constraint) { foreach ($this->expandDisjunctiveMultiConstraints($constraint) as $expanded) { $this->requireConstraintsPerPackage[$package][(string) $expanded] = $expanded; } } private function extractConflictConstraintsPerPackage(string $package, ConstraintInterface $constraint) { foreach ($this->expandDisjunctiveMultiConstraints($constraint) as $expanded) { $this->conflictConstraintsPerPackage[$package][(string) $expanded] = $expanded; } } private function expandDisjunctiveMultiConstraints(ConstraintInterface $constraint) { $constraint = Intervals::compactConstraint($constraint); if ($constraint instanceof MultiConstraint && $constraint->isDisjunctive()) { return $constraint->getConstraints(); } return [$constraint]; } } addReason(spl_object_hash($rule), $rule); } public function getReasons(): array { return $this->reasons; } public function getPrettyString(RepositorySet $repositorySet, Request $request, Pool $pool, bool $isVerbose, array $installedMap = [], array $learnedPool = []): string { $reasons = array_merge(...array_reverse($this->reasons)); if (count($reasons) === 1) { reset($reasons); $rule = current($reasons); if ($rule->getReason() !== Rule::RULE_ROOT_REQUIRE) { throw new \LogicException("Single reason problems must contain a root require rule."); } $reasonData = $rule->getReasonData(); $packageName = $reasonData['packageName']; $constraint = $reasonData['constraint']; $packages = $pool->whatProvides($packageName, $constraint); if (count($packages) === 0) { return "\n ".implode(self::getMissingPackageReason($repositorySet, $request, $pool, $isVerbose, $packageName, $constraint)); } } return self::formatDeduplicatedRules($reasons, ' ', $repositorySet, $request, $pool, $isVerbose, $installedMap, $learnedPool); } public static function formatDeduplicatedRules(array $rules, string $indent, RepositorySet $repositorySet, Request $request, Pool $pool, bool $isVerbose, array $installedMap = [], array $learnedPool = []): string { $messages = []; $templates = []; $parser = new VersionParser; $deduplicatableRuleTypes = [Rule::RULE_PACKAGE_REQUIRES, Rule::RULE_PACKAGE_CONFLICT]; foreach ($rules as $rule) { $message = $rule->getPrettyString($repositorySet, $request, $pool, $isVerbose, $installedMap, $learnedPool); if (in_array($rule->getReason(), $deduplicatableRuleTypes, true) && Preg::isMatch('{^(?P\S+) (?P\S+) (?Prequires|conflicts)}', $message, $m)) { $template = Preg::replace('{^\S+ \S+ }', '%s%s ', $message); $messages[] = $template; $templates[$template][$m[1]][$parser->normalize($m[2])] = $m[2]; $sourcePackage = $rule->getSourcePackage($pool); foreach ($pool->getRemovedVersionsByPackage(spl_object_hash($sourcePackage)) as $version => $prettyVersion) { $templates[$template][$m[1]][$version] = $prettyVersion; } } elseif ($message !== '') { $messages[] = $message; } } $result = []; foreach (array_unique($messages) as $message) { if (isset($templates[$message])) { foreach ($templates[$message] as $package => $versions) { uksort($versions, 'version_compare'); if (!$isVerbose) { $versions = self::condenseVersionList($versions, 1); } if (count($versions) > 1) { $message = Preg::replace('{^(%s%s (?:require|conflict))s}', '$1', $message); $result[] = sprintf($message, $package, '['.implode(', ', $versions).']'); } else { $result[] = sprintf($message, $package, ' '.reset($versions)); } } } else { $result[] = $message; } } return "\n$indent- ".implode("\n$indent- ", $result); } public function isCausedByLock(RepositorySet $repositorySet, Request $request, Pool $pool): bool { foreach ($this->reasons as $sectionRules) { foreach ($sectionRules as $rule) { if ($rule->isCausedByLock($repositorySet, $request, $pool)) { return true; } } } return false; } protected function addReason(string $id, Rule $reason): void { if (!isset($this->reasonSeen[$id])) { $this->reasonSeen[$id] = true; $this->reasons[$this->section][] = $reason; } } public function nextSection(): void { $this->section++; } public static function getMissingPackageReason(RepositorySet $repositorySet, Request $request, Pool $pool, bool $isVerbose, string $packageName, ?ConstraintInterface $constraint = null): array { if (PlatformRepository::isPlatformPackage($packageName)) { if (0 === stripos($packageName, 'php') || $packageName === 'hhvm') { $version = self::getPlatformPackageVersion($pool, $packageName, phpversion()); $msg = "- Root composer.json requires ".$packageName.self::constraintToText($constraint).' but '; if (defined('HHVM_VERSION') || ($packageName === 'hhvm' && count($pool->whatProvides($packageName)) > 0)) { return [$msg, 'your HHVM version does not satisfy that requirement.']; } if ($packageName === 'hhvm') { return [$msg, 'HHVM was not detected on this machine, make sure it is in your PATH.']; } if (null === $version) { return [$msg, 'the '.$packageName.' package is disabled by your platform config. Enable it again with "composer config platform.'.$packageName.' --unset".']; } return [$msg, 'your '.$packageName.' version ('. $version .') does not satisfy that requirement.']; } if (0 === stripos($packageName, 'ext-')) { if (false !== strpos($packageName, ' ')) { return ['- ', "PHP extension ".$packageName.' should be required as '.str_replace(' ', '-', $packageName).'.']; } $ext = substr($packageName, 4); $msg = "- Root composer.json requires PHP extension ".$packageName.self::constraintToText($constraint).' but '; $version = self::getPlatformPackageVersion($pool, $packageName, phpversion($ext) ?: '0'); if (null === $version) { if (extension_loaded($ext)) { return [ $msg, 'the '.$packageName.' package is disabled by your platform config. Enable it again with "composer config platform.'.$packageName.' --unset".', ]; } return [$msg, 'it is missing from your system. Install or enable PHP\'s '.$ext.' extension.']; } return [$msg, 'it has the wrong version installed ('.$version.').']; } if (0 === stripos($packageName, 'lib-')) { if (strtolower($packageName) === 'lib-icu') { $error = extension_loaded('intl') ? 'it has the wrong version installed, try upgrading the intl extension.' : 'it is missing from your system, make sure the intl extension is loaded.'; return ["- Root composer.json requires linked library ".$packageName.self::constraintToText($constraint).' but ', $error]; } return ["- Root composer.json requires linked library ".$packageName.self::constraintToText($constraint).' but ', 'it has the wrong version installed or is missing from your system, make sure to load the extension providing it.']; } } $lockedPackage = null; foreach ($request->getLockedPackages() as $package) { if ($package->getName() === $packageName) { $lockedPackage = $package; if ($pool->isUnacceptableFixedOrLockedPackage($package)) { return ["- ", $package->getPrettyName().' is fixed to '.$package->getPrettyVersion().' (lock file version) by a partial update but that version is rejected by your minimum-stability. Make sure you list it as an argument for the update command.']; } break; } } if ($packages = $repositorySet->findPackages($packageName, $constraint)) { $rootReqs = $repositorySet->getRootRequires(); if (isset($rootReqs[$packageName])) { $filtered = array_filter($packages, static function ($p) use ($rootReqs, $packageName): bool { return $rootReqs[$packageName]->matches(new Constraint('==', $p->getVersion())); }); if (0 === count($filtered)) { return ["- Root composer.json requires $packageName".self::constraintToText($constraint) . ', ', 'found '.self::getPackageList($packages, $isVerbose, $pool, $constraint).' but '.(self::hasMultipleNames($packages) ? 'these conflict' : 'it conflicts').' with your root composer.json require ('.$rootReqs[$packageName]->getPrettyString().').']; } } $tempReqs = $repositorySet->getTemporaryConstraints(); if (isset($tempReqs[$packageName])) { $filtered = array_filter($packages, static function ($p) use ($tempReqs, $packageName): bool { return $tempReqs[$packageName]->matches(new Constraint('==', $p->getVersion())); }); if (0 === count($filtered)) { return ["- Root composer.json requires $packageName".self::constraintToText($constraint) . ', ', 'found '.self::getPackageList($packages, $isVerbose, $pool, $constraint).' but '.(self::hasMultipleNames($packages) ? 'these conflict' : 'it conflicts').' with your temporary update constraint ('.$packageName.':'.$tempReqs[$packageName]->getPrettyString().').']; } } if ($lockedPackage) { $fixedConstraint = new Constraint('==', $lockedPackage->getVersion()); $filtered = array_filter($packages, static function ($p) use ($fixedConstraint): bool { return $fixedConstraint->matches(new Constraint('==', $p->getVersion())); }); if (0 === count($filtered)) { return ["- Root composer.json requires $packageName".self::constraintToText($constraint) . ', ', 'found '.self::getPackageList($packages, $isVerbose, $pool, $constraint).' but the package is fixed to '.$lockedPackage->getPrettyVersion().' (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.']; } } $nonLockedPackages = array_filter($packages, static function ($p): bool { return !$p->getRepository() instanceof LockArrayRepository; }); if (!$nonLockedPackages) { return ["- Root composer.json requires $packageName".self::constraintToText($constraint) . ', ', 'found '.self::getPackageList($packages, $isVerbose, $pool, $constraint).' in the lock file but not in remote repositories, make sure you avoid updating this package to keep the one from the lock file.']; } return ["- Root composer.json requires $packageName".self::constraintToText($constraint) . ', ', 'found '.self::getPackageList($packages, $isVerbose, $pool, $constraint).' but these were not loaded, likely because '.(self::hasMultipleNames($packages) ? 'they conflict' : 'it conflicts').' with another require.']; } if ($packages = $repositorySet->findPackages($packageName, $constraint, RepositorySet::ALLOW_UNACCEPTABLE_STABILITIES)) { if ($allReposPackages = $repositorySet->findPackages($packageName, $constraint, RepositorySet::ALLOW_SHADOWED_REPOSITORIES)) { return self::computeCheckForLowerPrioRepo($pool, $isVerbose, $packageName, $packages, $allReposPackages, 'minimum-stability', $constraint); } return ["- Root composer.json requires $packageName".self::constraintToText($constraint) . ', ', 'found '.self::getPackageList($packages, $isVerbose, $pool, $constraint).' but '.(self::hasMultipleNames($packages) ? 'these do' : 'it does').' not match your minimum-stability.']; } if ($packages = $repositorySet->findPackages($packageName, null, RepositorySet::ALLOW_UNACCEPTABLE_STABILITIES)) { if ($allReposPackages = $repositorySet->findPackages($packageName, $constraint, RepositorySet::ALLOW_SHADOWED_REPOSITORIES)) { return self::computeCheckForLowerPrioRepo($pool, $isVerbose, $packageName, $packages, $allReposPackages, 'constraint', $constraint); } $suffix = ''; if ($constraint instanceof Constraint && $constraint->getVersion() === 'dev-master') { foreach ($packages as $candidate) { if (in_array($candidate->getVersion(), ['dev-default', 'dev-main'], true)) { $suffix = ' Perhaps dev-master was renamed to '.$candidate->getPrettyVersion().'?'; break; } } } $allReposPackages = $packages; $topPackage = reset($allReposPackages); if ($topPackage instanceof RootPackageInterface) { $suffix = ' See https://getcomposer.org/dep-on-root for details and assistance.'; } return ["- Root composer.json requires $packageName".self::constraintToText($constraint) . ', ', 'found '.self::getPackageList($packages, $isVerbose, $pool, $constraint).' but '.(self::hasMultipleNames($packages) ? 'these do' : 'it does').' not match the constraint.' . $suffix]; } if (!Preg::isMatch('{^[A-Za-z0-9_./-]+$}', $packageName)) { $illegalChars = Preg::replace('{[A-Za-z0-9_./-]+}', '', $packageName); return ["- Root composer.json requires $packageName, it ", 'could not be found, it looks like its name is invalid, "'.$illegalChars.'" is not allowed in package names.']; } if ($providers = $repositorySet->getProviders($packageName)) { $maxProviders = 20; $providersStr = implode(array_map(static function ($p): string { $description = $p['description'] ? ' '.substr($p['description'], 0, 100) : ''; return ' - '.$p['name'].$description."\n"; }, count($providers) > $maxProviders + 1 ? array_slice($providers, 0, $maxProviders) : $providers)); if (count($providers) > $maxProviders + 1) { $providersStr .= ' ... and '.(count($providers) - $maxProviders).' more.'."\n"; } return ["- Root composer.json requires $packageName".self::constraintToText($constraint).", it ", "could not be found in any version, but the following packages provide it:\n".$providersStr." Consider requiring one of these to satisfy the $packageName requirement."]; } return ["- Root composer.json requires $packageName, it ", "could not be found in any version, there may be a typo in the package name."]; } public static function getPackageList(array $packages, bool $isVerbose, ?Pool $pool = null, ?ConstraintInterface $constraint = null, bool $useRemovedVersionGroup = false): string { $prepared = []; $hasDefaultBranch = []; foreach ($packages as $package) { $prepared[$package->getName()]['name'] = $package->getPrettyName(); $prepared[$package->getName()]['versions'][$package->getVersion()] = $package->getPrettyVersion().($package instanceof AliasPackage ? ' (alias of '.$package->getAliasOf()->getPrettyVersion().')' : ''); if ($pool && $constraint) { foreach ($pool->getRemovedVersions($package->getName(), $constraint) as $version => $prettyVersion) { $prepared[$package->getName()]['versions'][$version] = $prettyVersion; } } if ($pool && $useRemovedVersionGroup) { foreach ($pool->getRemovedVersionsByPackage(spl_object_hash($package)) as $version => $prettyVersion) { $prepared[$package->getName()]['versions'][$version] = $prettyVersion; } } if ($package->isDefaultBranch()) { $hasDefaultBranch[$package->getName()] = true; } } $preparedStrings = []; foreach ($prepared as $name => $package) { if (isset($package['versions'][VersionParser::DEFAULT_BRANCH_ALIAS], $hasDefaultBranch[$name])) { unset($package['versions'][VersionParser::DEFAULT_BRANCH_ALIAS]); } uksort($package['versions'], 'version_compare'); if (!$isVerbose) { $package['versions'] = self::condenseVersionList($package['versions'], 4); } $preparedStrings[] = $package['name'].'['.implode(', ', $package['versions']).']'; } return implode(', ', $preparedStrings); } private static function getPlatformPackageVersion(Pool $pool, string $packageName, string $version): ?string { $available = $pool->whatProvides($packageName); if (count($available)) { $selected = null; foreach ($available as $pkg) { if ($pkg->getRepository() instanceof PlatformRepository) { $selected = $pkg; break; } } if ($selected === null) { $selected = reset($available); } if ($selected->getName() !== $packageName) { foreach (array_merge(array_values($selected->getProvides()), array_values($selected->getReplaces())) as $link) { if ($link->getTarget() === $packageName) { return $link->getPrettyConstraint().' '.substr($link->getDescription(), 0, -1).'d by '.$selected->getPrettyString(); } } } $version = $selected->getPrettyVersion(); $extra = $selected->getExtra(); if ($selected instanceof CompletePackageInterface && isset($extra['config.platform']) && $extra['config.platform'] === true) { $version .= '; ' . str_replace('Package ', '', $selected->getDescription()); } } else { return null; } return $version; } private static function condenseVersionList(array $versions, int $max, int $maxDev = 16): array { if (count($versions) <= $max) { return $versions; } $filtered = []; $byMajor = []; foreach ($versions as $version => $pretty) { if (0 === stripos($version, 'dev-')) { $byMajor['dev'][] = $pretty; } else { $byMajor[Preg::replace('{^(\d+)\..*}', '$1', $version)][] = $pretty; } } foreach ($byMajor as $majorVersion => $versionsForMajor) { $maxVersions = $majorVersion === 'dev' ? $maxDev : $max; if (count($versionsForMajor) > $maxVersions) { $filtered[] = $versionsForMajor[0]; $filtered[] = '...'; $filtered[] = $versionsForMajor[count($versionsForMajor) - 1]; } else { $filtered = array_merge($filtered, $versionsForMajor); } } return $filtered; } private static function hasMultipleNames(array $packages): bool { $name = null; foreach ($packages as $package) { if ($name === null || $name === $package->getName()) { $name = $package->getName(); } else { return true; } } return false; } private static function computeCheckForLowerPrioRepo(Pool $pool, bool $isVerbose, string $packageName, array $higherRepoPackages, array $allReposPackages, string $reason, ?ConstraintInterface $constraint = null): array { $nextRepoPackages = []; $nextRepo = null; foreach ($allReposPackages as $package) { if ($nextRepo === null || $nextRepo === $package->getRepository()) { $nextRepoPackages[] = $package; $nextRepo = $package->getRepository(); } else { break; } } if ($higherRepoPackages) { $topPackage = reset($higherRepoPackages); if ($topPackage instanceof RootPackageInterface) { return [ "- Root composer.json requires $packageName".self::constraintToText($constraint).', it is ', 'satisfiable by '.self::getPackageList($nextRepoPackages, $isVerbose, $pool, $constraint).' from '.$nextRepo->getRepoName().' but '.$topPackage->getPrettyName().' is the root package and cannot be modified. See https://getcomposer.org/dep-on-root for details and assistance.', ]; } } if ($nextRepo instanceof LockArrayRepository) { $singular = count($higherRepoPackages) === 1; $suggestion = 'Make sure you either fix the '.$reason.' or avoid updating this package to keep the one present in the lock file ('.self::getPackageList($nextRepoPackages, $isVerbose, $pool, $constraint).').'; if ($nextRepoPackages[0]->getDistType() === 'path') { $transportOptions = $nextRepoPackages[0]->getTransportOptions(); if (!isset($transportOptions['symlink']) || $transportOptions['symlink'] !== false) { $suggestion = 'Make sure you fix the '.$reason.' as packages installed from symlinked path repos are updated even in partial updates and the one from the lock file can thus not be used.'; } } return ["- Root composer.json requires $packageName".self::constraintToText($constraint) . ', ', 'found ' . self::getPackageList($higherRepoPackages, $isVerbose, $pool, $constraint).' but ' . ($singular ? 'it does' : 'these do') . ' not match your '.$reason.' and ' . ($singular ? 'is' : 'are') . ' therefore not installable. '.$suggestion, ]; } return ["- Root composer.json requires $packageName".self::constraintToText($constraint) . ', it is ', 'satisfiable by '.self::getPackageList($nextRepoPackages, $isVerbose, $pool, $constraint).' from '.$nextRepo->getRepoName().' but '.self::getPackageList($higherRepoPackages, $isVerbose, $pool, $constraint).' from '.reset($higherRepoPackages)->getRepository()->getRepoName().' has higher repository priority. The packages from the higher priority repository do not match your '.$reason.' and are therefore not installable. That repository is canonical so the lower priority repo\'s packages are not installable. See https://getcomposer.org/repoprio for details and assistance.']; } protected static function constraintToText(?ConstraintInterface $constraint = null): string { return $constraint ? ' '.$constraint->getPrettyString() : ''; } } lockedRepository = $lockedRepository; } public function requireName(string $packageName, ?ConstraintInterface $constraint = null): void { $packageName = strtolower($packageName); if ($constraint === null) { $constraint = new MatchAllConstraint(); } if (isset($this->requires[$packageName])) { throw new \LogicException('Overwriting requires seems like a bug ('.$packageName.' '.$this->requires[$packageName]->getPrettyString().' => '.$constraint->getPrettyString().', check why it is happening, might be a root alias'); } $this->requires[$packageName] = $constraint; } public function fixPackage(BasePackage $package): void { $this->fixedPackages[spl_object_hash($package)] = $package; } public function lockPackage(BasePackage $package): void { $this->lockedPackages[spl_object_hash($package)] = $package; } public function fixLockedPackage(BasePackage $package): void { $this->fixedPackages[spl_object_hash($package)] = $package; $this->fixedLockedPackages[spl_object_hash($package)] = $package; } public function unlockPackage(BasePackage $package): void { unset($this->lockedPackages[spl_object_hash($package)]); } public function setUpdateAllowList(array $updateAllowList, $updateAllowTransitiveDependencies): void { $this->updateAllowList = $updateAllowList; $this->updateAllowTransitiveDependencies = $updateAllowTransitiveDependencies; } public function getUpdateAllowList(): array { return $this->updateAllowList; } public function getUpdateAllowTransitiveDependencies(): bool { return $this->updateAllowTransitiveDependencies !== self::UPDATE_ONLY_LISTED; } public function getUpdateAllowTransitiveRootDependencies(): bool { return $this->updateAllowTransitiveDependencies === self::UPDATE_LISTED_WITH_TRANSITIVE_DEPS; } public function getRequires(): array { return $this->requires; } public function getFixedPackages(): array { return $this->fixedPackages; } public function isFixedPackage(BasePackage $package): bool { return isset($this->fixedPackages[spl_object_hash($package)]); } public function getLockedPackages(): array { return $this->lockedPackages; } public function isLockedPackage(PackageInterface $package): bool { return isset($this->lockedPackages[spl_object_hash($package)]) || isset($this->fixedLockedPackages[spl_object_hash($package)]); } public function getFixedOrLockedPackages(): array { return array_merge($this->fixedPackages, $this->lockedPackages); } public function getPresentMap(bool $packageIds = false): array { $presentMap = []; if ($this->lockedRepository) { foreach ($this->lockedRepository->getPackages() as $package) { $presentMap[$packageIds ? $package->getId() : spl_object_hash($package)] = $package; } } foreach ($this->fixedPackages as $package) { $presentMap[$packageIds ? $package->getId() : spl_object_hash($package)] = $package; } return $presentMap; } public function getFixedPackagesMap(): array { $fixedPackagesMap = []; foreach ($this->fixedPackages as $package) { $fixedPackagesMap[$package->getId()] = $package; } return $fixedPackagesMap; } public function getLockedRepository(): ?LockArrayRepository { return $this->lockedRepository; } } reasonData = $reasonData; $this->bitfield = (0 << self::BITFIELD_DISABLED) | ($reason << self::BITFIELD_REASON) | (255 << self::BITFIELD_TYPE); } abstract public function getLiterals(): array; abstract public function getHash(); abstract public function __toString(): string; abstract public function equals(Rule $rule): bool; public function getReason(): int { return ($this->bitfield & (255 << self::BITFIELD_REASON)) >> self::BITFIELD_REASON; } public function getReasonData() { return $this->reasonData; } public function getRequiredPackage(): ?string { switch ($this->getReason()) { case self::RULE_ROOT_REQUIRE: return $this->getReasonData()['packageName']; case self::RULE_FIXED: return $this->getReasonData()['package']->getName(); case self::RULE_PACKAGE_REQUIRES: return $this->getReasonData()->getTarget(); } return null; } public function setType($type): void { $this->bitfield = ($this->bitfield & ~(255 << self::BITFIELD_TYPE)) | ((255 & $type) << self::BITFIELD_TYPE); } public function getType(): int { return ($this->bitfield & (255 << self::BITFIELD_TYPE)) >> self::BITFIELD_TYPE; } public function disable(): void { $this->bitfield = ($this->bitfield & ~(255 << self::BITFIELD_DISABLED)) | (1 << self::BITFIELD_DISABLED); } public function enable(): void { $this->bitfield &= ~(255 << self::BITFIELD_DISABLED); } public function isDisabled(): bool { return (bool) (($this->bitfield & (255 << self::BITFIELD_DISABLED)) >> self::BITFIELD_DISABLED); } public function isEnabled(): bool { return !(($this->bitfield & (255 << self::BITFIELD_DISABLED)) >> self::BITFIELD_DISABLED); } abstract public function isAssertion(): bool; public function isCausedByLock(RepositorySet $repositorySet, Request $request, Pool $pool): bool { if ($this->getReason() === self::RULE_PACKAGE_REQUIRES) { if (PlatformRepository::isPlatformPackage($this->getReasonData()->getTarget())) { return false; } if ($request->getLockedRepository()) { foreach ($request->getLockedRepository()->getPackages() as $package) { if ($package->getName() === $this->getReasonData()->getTarget()) { if ($pool->isUnacceptableFixedOrLockedPackage($package)) { return true; } if (!$this->getReasonData()->getConstraint()->matches(new Constraint('=', $package->getVersion()))) { return true; } if (!$request->isLockedPackage($package)) { return true; } break; } } } } if ($this->getReason() === self::RULE_ROOT_REQUIRE) { if (PlatformRepository::isPlatformPackage($this->getReasonData()['packageName'])) { return false; } if ($request->getLockedRepository()) { foreach ($request->getLockedRepository()->getPackages() as $package) { if ($package->getName() === $this->getReasonData()['packageName']) { if ($pool->isUnacceptableFixedOrLockedPackage($package)) { return true; } if (!$this->getReasonData()['constraint']->matches(new Constraint('=', $package->getVersion()))) { return true; } break; } } } } return false; } public function getSourcePackage(Pool $pool): BasePackage { $literals = $this->getLiterals(); switch ($this->getReason()) { case self::RULE_PACKAGE_CONFLICT: $package1 = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($literals[0])); $package2 = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($literals[1])); $reasonData = $this->getReasonData(); if ($reasonData->getSource() === $package1->getName()) { [$package2, $package1] = [$package1, $package2]; } return $package2; case self::RULE_PACKAGE_REQUIRES: $sourceLiteral = array_shift($literals); $sourcePackage = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($sourceLiteral)); return $sourcePackage; default: throw new \LogicException('Not implemented'); } } public function getPrettyString(RepositorySet $repositorySet, Request $request, Pool $pool, bool $isVerbose, array $installedMap = [], array $learnedPool = []): string { $literals = $this->getLiterals(); switch ($this->getReason()) { case self::RULE_ROOT_REQUIRE: $reasonData = $this->getReasonData(); $packageName = $reasonData['packageName']; $constraint = $reasonData['constraint']; $packages = $pool->whatProvides($packageName, $constraint); if (!$packages) { return 'No package found to satisfy root composer.json require '.$packageName.' '.$constraint->getPrettyString(); } $packagesNonAlias = array_values(array_filter($packages, static function ($p): bool { return !($p instanceof AliasPackage); })); if (count($packagesNonAlias) === 1) { $package = $packagesNonAlias[0]; if ($request->isLockedPackage($package)) { return $package->getPrettyName().' is locked to version '.$package->getPrettyVersion()." and an update of this package was not requested."; } } return 'Root composer.json requires '.$packageName.' '.$constraint->getPrettyString().' -> satisfiable by '.$this->formatPackagesUnique($pool, $packages, $isVerbose, $constraint).'.'; case self::RULE_FIXED: $package = $this->deduplicateDefaultBranchAlias($this->getReasonData()['package']); if ($request->isLockedPackage($package)) { return $package->getPrettyName().' is locked to version '.$package->getPrettyVersion().' and an update of this package was not requested.'; } return $package->getPrettyName().' is present at version '.$package->getPrettyVersion() . ' and cannot be modified by Composer'; case self::RULE_PACKAGE_CONFLICT: $package1 = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($literals[0])); $package2 = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($literals[1])); $conflictTarget = $package1->getPrettyString(); $reasonData = $this->getReasonData(); if ($reasonData->getSource() === $package1->getName()) { [$package2, $package1] = [$package1, $package2]; $conflictTarget = $package1->getPrettyName().' '.$reasonData->getPrettyConstraint(); } if ($reasonData->getTarget() !== $package1->getName()) { $provideType = null; $provided = null; foreach ($package1->getProvides() as $provide) { if ($provide->getTarget() === $reasonData->getTarget()) { $provideType = 'provides'; $provided = $provide->getPrettyConstraint(); break; } } foreach ($package1->getReplaces() as $replace) { if ($replace->getTarget() === $reasonData->getTarget()) { $provideType = 'replaces'; $provided = $replace->getPrettyConstraint(); break; } } if (null !== $provideType) { $conflictTarget = $reasonData->getTarget().' '.$reasonData->getPrettyConstraint().' ('.$package1->getPrettyString().' '.$provideType.' '.$reasonData->getTarget().' '.$provided.')'; } } return $package2->getPrettyString().' conflicts with '.$conflictTarget.'.'; case self::RULE_PACKAGE_REQUIRES: $sourceLiteral = array_shift($literals); $sourcePackage = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($sourceLiteral)); $reasonData = $this->getReasonData(); $requires = []; foreach ($literals as $literal) { $requires[] = $pool->literalToPackage($literal); } $text = $reasonData->getPrettyString($sourcePackage); if ($requires) { $text .= ' -> satisfiable by ' . $this->formatPackagesUnique($pool, $requires, $isVerbose, $reasonData->getConstraint()) . '.'; } else { $targetName = $reasonData->getTarget(); $reason = Problem::getMissingPackageReason($repositorySet, $request, $pool, $isVerbose, $targetName, $reasonData->getConstraint()); return $text . ' -> ' . $reason[1]; } return $text; case self::RULE_PACKAGE_SAME_NAME: $packageNames = []; foreach ($literals as $literal) { $package = $pool->literalToPackage($literal); $packageNames[$package->getName()] = true; } $replacedName = $this->getReasonData(); if (count($packageNames) > 1) { $reason = null; if (!isset($packageNames[$replacedName])) { $reason = 'They '.(count($literals) === 2 ? 'both' : 'all').' replace '.$replacedName.' and thus cannot coexist.'; } else { $replacerNames = $packageNames; unset($replacerNames[$replacedName]); $replacerNames = array_keys($replacerNames); if (count($replacerNames) === 1) { $reason = $replacerNames[0] . ' replaces '; } else { $reason = '['.implode(', ', $replacerNames).'] replace '; } $reason .= $replacedName.' and thus cannot coexist with it.'; } $installedPackages = []; $removablePackages = []; foreach ($literals as $literal) { if (isset($installedMap[abs($literal)])) { $installedPackages[] = $pool->literalToPackage($literal); } else { $removablePackages[] = $pool->literalToPackage($literal); } } if ($installedPackages && $removablePackages) { return $this->formatPackagesUnique($pool, $removablePackages, $isVerbose, null, true).' cannot be installed as that would require removing '.$this->formatPackagesUnique($pool, $installedPackages, $isVerbose, null, true).'. '.$reason; } return 'Only one of these can be installed: '.$this->formatPackagesUnique($pool, $literals, $isVerbose, null, true).'. '.$reason; } return 'You can only install one version of a package, so only one of these can be installed: ' . $this->formatPackagesUnique($pool, $literals, $isVerbose, null, true) . '.'; case self::RULE_LEARNED: $learnedString = ' (conflict analysis result)'; if (count($literals) === 1) { $ruleText = $pool->literalToPrettyString($literals[0], $installedMap); } else { $groups = []; foreach ($literals as $literal) { $package = $pool->literalToPackage($literal); if (isset($installedMap[$package->id])) { $group = $literal > 0 ? 'keep' : 'remove'; } else { $group = $literal > 0 ? 'install' : 'don\'t install'; } $groups[$group][] = $this->deduplicateDefaultBranchAlias($package); } $ruleTexts = []; foreach ($groups as $group => $packages) { $ruleTexts[] = $group . (count($packages) > 1 ? ' one of' : '').' ' . $this->formatPackagesUnique($pool, $packages, $isVerbose); } $ruleText = implode(' | ', $ruleTexts); } return 'Conclusion: '.$ruleText.$learnedString; case self::RULE_PACKAGE_ALIAS: $aliasPackage = $pool->literalToPackage($literals[0]); if ($aliasPackage->getVersion() === VersionParser::DEFAULT_BRANCH_ALIAS) { return ''; } $package = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($literals[1])); return $aliasPackage->getPrettyString() .' is an alias of '.$package->getPrettyString().' and thus requires it to be installed too.'; case self::RULE_PACKAGE_INVERSE_ALIAS: $aliasPackage = $pool->literalToPackage($literals[1]); if ($aliasPackage->getVersion() === VersionParser::DEFAULT_BRANCH_ALIAS) { return ''; } $package = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($literals[0])); return $aliasPackage->getPrettyString() .' is an alias of '.$package->getPrettyString().' and must be installed with it.'; default: $ruleText = ''; foreach ($literals as $i => $literal) { if ($i !== 0) { $ruleText .= '|'; } $ruleText .= $pool->literalToPrettyString($literal, $installedMap); } return '('.$ruleText.')'; } } protected function formatPackagesUnique(Pool $pool, array $packages, bool $isVerbose, ?ConstraintInterface $constraint = null, bool $useRemovedVersionGroup = false): string { foreach ($packages as $index => $package) { if (!\is_object($package)) { $packages[$index] = $pool->literalToPackage($package); } } return Problem::getPackageList($packages, $isVerbose, $pool, $constraint, $useRemovedVersionGroup); } private function deduplicateDefaultBranchAlias(BasePackage $package): BasePackage { if ($package instanceof AliasPackage && $package->getPrettyVersion() === VersionParser::DEFAULT_BRANCH_ALIAS) { $package = $package->getAliasOf(); } return $package; } } literal1 = $literal1; $this->literal2 = $literal2; } else { $this->literal1 = $literal2; $this->literal2 = $literal1; } } public function getLiterals(): array { return [$this->literal1, $this->literal2]; } public function getHash() { return $this->literal1.','.$this->literal2; } public function equals(Rule $rule): bool { if ($rule instanceof self) { if ($this->literal1 !== $rule->literal1) { return false; } if ($this->literal2 !== $rule->literal2) { return false; } return true; } $literals = $rule->getLiterals(); if (2 !== \count($literals)) { return false; } if ($this->literal1 !== $literals[0]) { return false; } if ($this->literal2 !== $literals[1]) { return false; } return true; } public function isAssertion(): bool { return false; } public function __toString(): string { $result = $this->isDisabled() ? 'disabled(' : '('; $result .= $this->literal1 . '|' . $this->literal2 . ')'; return $result; } } 'PACKAGE', self::TYPE_REQUEST => 'REQUEST', self::TYPE_LEARNED => 'LEARNED', ]; protected $rules; protected $nextRuleId = 0; protected $rulesByHash = []; public function __construct() { foreach ($this->getTypes() as $type) { $this->rules[$type] = []; } } public function add(Rule $rule, $type): void { if (!isset(self::$types[$type])) { throw new \OutOfBoundsException('Unknown rule type: ' . $type); } $hash = $rule->getHash(); if (isset($this->rulesByHash[$hash])) { $potentialDuplicates = $this->rulesByHash[$hash]; if (\is_array($potentialDuplicates)) { foreach ($potentialDuplicates as $potentialDuplicate) { if ($rule->equals($potentialDuplicate)) { return; } } } else { if ($rule->equals($potentialDuplicates)) { return; } } } if (!isset($this->rules[$type])) { $this->rules[$type] = []; } $this->rules[$type][] = $rule; $this->ruleById[$this->nextRuleId] = $rule; $rule->setType($type); $this->nextRuleId++; if (!isset($this->rulesByHash[$hash])) { $this->rulesByHash[$hash] = $rule; } elseif (\is_array($this->rulesByHash[$hash])) { $this->rulesByHash[$hash][] = $rule; } else { $originalRule = $this->rulesByHash[$hash]; $this->rulesByHash[$hash] = [$originalRule, $rule]; } } public function count(): int { return $this->nextRuleId; } public function ruleById(int $id): Rule { return $this->ruleById[$id]; } public function getRules(): array { return $this->rules; } public function getIterator(): RuleSetIterator { return new RuleSetIterator($this->getRules()); } public function getIteratorFor($types): RuleSetIterator { if (!\is_array($types)) { $types = [$types]; } $allRules = $this->getRules(); $rules = []; foreach ($types as $type) { $rules[$type] = $allRules[$type]; } return new RuleSetIterator($rules); } public function getIteratorWithout($types): RuleSetIterator { if (!\is_array($types)) { $types = [$types]; } $rules = $this->getRules(); foreach ($types as $type) { unset($rules[$type]); } return new RuleSetIterator($rules); } public function getTypes(): array { $types = self::$types; return array_keys($types); } public function getPrettyString(?RepositorySet $repositorySet = null, ?Request $request = null, ?Pool $pool = null, bool $isVerbose = false): string { $string = "\n"; foreach ($this->rules as $type => $rules) { $string .= str_pad(self::$types[$type], 8, ' ') . ": "; foreach ($rules as $rule) { $string .= ($repositorySet && $request && $pool ? $rule->getPrettyString($repositorySet, $request, $pool, $isVerbose) : $rule)."\n"; } $string .= "\n\n"; } return $string; } public function __toString(): string { return $this->getPrettyString(); } } policy = $policy; $this->pool = $pool; $this->rules = new RuleSet; } protected function createRequireRule(BasePackage $package, array $providers, $reason, $reasonData = null): ?Rule { $literals = [-$package->id]; foreach ($providers as $provider) { if ($provider === $package) { return null; } $literals[] = $provider->id; } return new GenericRule($literals, $reason, $reasonData); } protected function createInstallOneOfRule(array $packages, $reason, $reasonData): Rule { $literals = []; foreach ($packages as $package) { $literals[] = $package->id; } return new GenericRule($literals, $reason, $reasonData); } protected function createRule2Literals(BasePackage $issuer, BasePackage $provider, $reason, $reasonData = null): ?Rule { if ($issuer === $provider) { return null; } return new Rule2Literals(-$issuer->id, -$provider->id, $reason, $reasonData); } protected function createMultiConflictRule(array $packages, $reason, $reasonData): Rule { $literals = []; foreach ($packages as $package) { $literals[] = -$package->id; } if (\count($literals) === 2) { return new Rule2Literals($literals[0], $literals[1], $reason, $reasonData); } return new MultiConflictRule($literals, $reason, $reasonData); } private function addRule($type, ?Rule $newRule = null): void { if (!$newRule) { return; } $this->rules->add($newRule, $type); } protected function addRulesForPackage(BasePackage $package, PlatformRequirementFilterInterface $platformRequirementFilter): void { $workQueue = new \SplQueue; $workQueue->enqueue($package); while (!$workQueue->isEmpty()) { $package = $workQueue->dequeue(); if (isset($this->addedMap[$package->id])) { continue; } $this->addedMap[$package->id] = $package; if (!$package instanceof AliasPackage) { foreach ($package->getNames(false) as $name) { $this->addedPackagesByNames[$name][] = $package; } } else { $workQueue->enqueue($package->getAliasOf()); $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRequireRule($package, [$package->getAliasOf()], Rule::RULE_PACKAGE_ALIAS, $package)); $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRequireRule($package->getAliasOf(), [$package], Rule::RULE_PACKAGE_INVERSE_ALIAS, $package->getAliasOf())); if (!$package->hasSelfVersionRequires()) { continue; } } foreach ($package->getRequires() as $link) { $constraint = $link->getConstraint(); if ($platformRequirementFilter->isIgnored($link->getTarget())) { continue; } elseif ($platformRequirementFilter instanceof IgnoreListPlatformRequirementFilter) { $constraint = $platformRequirementFilter->filterConstraint($link->getTarget(), $constraint); } $possibleRequires = $this->pool->whatProvides($link->getTarget(), $constraint); $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRequireRule($package, $possibleRequires, Rule::RULE_PACKAGE_REQUIRES, $link)); foreach ($possibleRequires as $require) { $workQueue->enqueue($require); } } } } protected function addConflictRules(PlatformRequirementFilterInterface $platformRequirementFilter): void { foreach ($this->addedMap as $package) { foreach ($package->getConflicts() as $link) { if (!isset($this->addedPackagesByNames[$link->getTarget()])) { continue; } $constraint = $link->getConstraint(); if ($platformRequirementFilter->isIgnored($link->getTarget())) { continue; } elseif ($platformRequirementFilter instanceof IgnoreListPlatformRequirementFilter) { $constraint = $platformRequirementFilter->filterConstraint($link->getTarget(), $constraint, false); } $conflicts = $this->pool->whatProvides($link->getTarget(), $constraint); foreach ($conflicts as $conflict) { if (!$conflict instanceof AliasPackage || $conflict->getName() === $link->getTarget()) { $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRule2Literals($package, $conflict, Rule::RULE_PACKAGE_CONFLICT, $link)); } } } } foreach ($this->addedPackagesByNames as $name => $packages) { if (\count($packages) > 1) { $reason = Rule::RULE_PACKAGE_SAME_NAME; $this->addRule(RuleSet::TYPE_PACKAGE, $this->createMultiConflictRule($packages, $reason, $name)); } } } protected function addRulesForRequest(Request $request, PlatformRequirementFilterInterface $platformRequirementFilter): void { foreach ($request->getFixedPackages() as $package) { if ($package->id === -1) { if ($this->pool->isUnacceptableFixedOrLockedPackage($package)) { continue; } throw new \LogicException("Fixed package ".$package->getPrettyString()." was not added to solver pool."); } $this->addRulesForPackage($package, $platformRequirementFilter); $rule = $this->createInstallOneOfRule([$package], Rule::RULE_FIXED, [ 'package' => $package, ]); $this->addRule(RuleSet::TYPE_REQUEST, $rule); } foreach ($request->getRequires() as $packageName => $constraint) { if ($platformRequirementFilter->isIgnored($packageName)) { continue; } elseif ($platformRequirementFilter instanceof IgnoreListPlatformRequirementFilter) { $constraint = $platformRequirementFilter->filterConstraint($packageName, $constraint); } $packages = $this->pool->whatProvides($packageName, $constraint); if ($packages) { foreach ($packages as $package) { $this->addRulesForPackage($package, $platformRequirementFilter); } $rule = $this->createInstallOneOfRule($packages, Rule::RULE_ROOT_REQUIRE, [ 'packageName' => $packageName, 'constraint' => $constraint, ]); $this->addRule(RuleSet::TYPE_REQUEST, $rule); } } } protected function addRulesForRootAliases(PlatformRequirementFilterInterface $platformRequirementFilter): void { foreach ($this->pool->getPackages() as $package) { if (!isset($this->addedMap[$package->id]) && $package instanceof AliasPackage && ($package->isRootPackageAlias() || isset($this->addedMap[$package->getAliasOf()->id])) ) { $this->addRulesForPackage($package, $platformRequirementFilter); } } } public function getRulesFor(Request $request, ?PlatformRequirementFilterInterface $platformRequirementFilter = null): RuleSet { $platformRequirementFilter = $platformRequirementFilter ?: PlatformRequirementFilterFactory::ignoreNothing(); $this->addRulesForRequest($request, $platformRequirementFilter); $this->addRulesForRootAliases($platformRequirementFilter); $this->addConflictRules($platformRequirementFilter); $this->addedMap = $this->addedPackagesByNames = []; $rules = $this->rules; $this->rules = new RuleSet; return $rules; } } rules = $rules; $this->types = array_keys($rules); sort($this->types); $this->rewind(); } public function current(): Rule { return $this->rules[$this->currentType][$this->currentOffset]; } public function key(): int { return $this->currentType; } public function next(): void { $this->currentOffset++; if (!isset($this->rules[$this->currentType])) { return; } if ($this->currentOffset >= \count($this->rules[$this->currentType])) { $this->currentOffset = 0; do { $this->currentTypeOffset++; if (!isset($this->types[$this->currentTypeOffset])) { $this->currentType = -1; break; } $this->currentType = $this->types[$this->currentTypeOffset]; } while (0 === \count($this->rules[$this->currentType])); } } public function rewind(): void { $this->currentOffset = 0; $this->currentTypeOffset = -1; $this->currentType = -1; do { $this->currentTypeOffset++; if (!isset($this->types[$this->currentTypeOffset])) { $this->currentType = -1; break; } $this->currentType = $this->types[$this->currentTypeOffset]; } while (0 === \count($this->rules[$this->currentType])); } public function valid(): bool { return isset($this->rules[$this->currentType], $this->rules[$this->currentType][$this->currentOffset]); } } rewind(); for ($i = 0; $i < $offset; $i++, $this->next()); } public function remove(): void { $offset = $this->key(); $this->offsetUnset($offset); $this->seek($offset); } } getRule()->isAssertion()) { return; } if (!$node->getRule() instanceof MultiConflictRule) { foreach ([$node->watch1, $node->watch2] as $literal) { if (!isset($this->watchChains[$literal])) { $this->watchChains[$literal] = new RuleWatchChain; } $this->watchChains[$literal]->unshift($node); } } else { foreach ($node->getRule()->getLiterals() as $literal) { if (!isset($this->watchChains[$literal])) { $this->watchChains[$literal] = new RuleWatchChain; } $this->watchChains[$literal]->unshift($node); } } } public function propagateLiteral(int $decidedLiteral, int $level, Decisions $decisions): ?Rule { $literal = -$decidedLiteral; if (!isset($this->watchChains[$literal])) { return null; } $chain = $this->watchChains[$literal]; $chain->rewind(); while ($chain->valid()) { $node = $chain->current(); if (!$node->getRule() instanceof MultiConflictRule) { $otherWatch = $node->getOtherWatch($literal); if (!$node->getRule()->isDisabled() && !$decisions->satisfy($otherWatch)) { $ruleLiterals = $node->getRule()->getLiterals(); $alternativeLiterals = array_filter($ruleLiterals, static function ($ruleLiteral) use ($literal, $otherWatch, $decisions): bool { return $literal !== $ruleLiteral && $otherWatch !== $ruleLiteral && !$decisions->conflict($ruleLiteral); }); if (\count($alternativeLiterals) > 0) { reset($alternativeLiterals); $this->moveWatch($literal, current($alternativeLiterals), $node); continue; } if ($decisions->conflict($otherWatch)) { return $node->getRule(); } $decisions->decide($otherWatch, $level, $node->getRule()); } } else { foreach ($node->getRule()->getLiterals() as $otherLiteral) { if ($literal !== $otherLiteral && !$decisions->satisfy($otherLiteral)) { if ($decisions->conflict($otherLiteral)) { return $node->getRule(); } $decisions->decide($otherLiteral, $level, $node->getRule()); } } } $chain->next(); } return null; } protected function moveWatch(int $fromLiteral, int $toLiteral, RuleWatchNode $node): void { if (!isset($this->watchChains[$toLiteral])) { $this->watchChains[$toLiteral] = new RuleWatchChain; } $node->moveWatch($fromLiteral, $toLiteral); $this->watchChains[$fromLiteral]->remove(); $this->watchChains[$toLiteral]->unshift($node); } } rule = $rule; $literals = $rule->getLiterals(); $literalCount = \count($literals); $this->watch1 = $literalCount > 0 ? $literals[0] : 0; $this->watch2 = $literalCount > 1 ? $literals[1] : 0; } public function watch2OnHighest(Decisions $decisions): void { $literals = $this->rule->getLiterals(); if (\count($literals) < 3 || $this->rule instanceof MultiConflictRule) { return; } $watchLevel = 0; foreach ($literals as $literal) { $level = $decisions->decisionLevel($literal); if ($level > $watchLevel) { $this->watch2 = $literal; $watchLevel = $level; } } } public function getRule(): Rule { return $this->rule; } public function getOtherWatch(int $literal): int { if ($this->watch1 === $literal) { return $this->watch2; } return $this->watch1; } public function moveWatch(int $from, int $to): void { if ($this->watch1 === $from) { $this->watch1 = $to; } else { $this->watch2 = $to; } } } io = $io; $this->policy = $policy; $this->pool = $pool; } public function getRuleSetSize(): int { return \count($this->rules); } public function getPool(): Pool { return $this->pool; } private function makeAssertionRuleDecisions(): void { $decisionStart = \count($this->decisions) - 1; $rulesCount = \count($this->rules); for ($ruleIndex = 0; $ruleIndex < $rulesCount; $ruleIndex++) { $rule = $this->rules->ruleById[$ruleIndex]; if (!$rule->isAssertion() || $rule->isDisabled()) { continue; } $literals = $rule->getLiterals(); $literal = $literals[0]; if (!$this->decisions->decided($literal)) { $this->decisions->decide($literal, 1, $rule); continue; } if ($this->decisions->satisfy($literal)) { continue; } if (RuleSet::TYPE_LEARNED === $rule->getType()) { $rule->disable(); continue; } $conflict = $this->decisions->decisionRule($literal); if ($conflict && RuleSet::TYPE_PACKAGE === $conflict->getType()) { $problem = new Problem(); $problem->addRule($rule); $problem->addRule($conflict); $rule->disable(); $this->problems[] = $problem; continue; } $problem = new Problem(); $problem->addRule($rule); $problem->addRule($conflict); foreach ($this->rules->getIteratorFor(RuleSet::TYPE_REQUEST) as $assertRule) { if ($assertRule->isDisabled() || !$assertRule->isAssertion()) { continue; } $assertRuleLiterals = $assertRule->getLiterals(); $assertRuleLiteral = $assertRuleLiterals[0]; if (abs($literal) !== abs($assertRuleLiteral)) { continue; } $problem->addRule($assertRule); $assertRule->disable(); } $this->problems[] = $problem; $this->decisions->resetToOffset($decisionStart); $ruleIndex = -1; } } protected function setupFixedMap(Request $request): void { $this->fixedMap = []; foreach ($request->getFixedPackages() as $package) { $this->fixedMap[$package->id] = $package; } } protected function checkForRootRequireProblems(Request $request, PlatformRequirementFilterInterface $platformRequirementFilter): void { foreach ($request->getRequires() as $packageName => $constraint) { if ($platformRequirementFilter->isIgnored($packageName)) { continue; } elseif ($platformRequirementFilter instanceof IgnoreListPlatformRequirementFilter) { $constraint = $platformRequirementFilter->filterConstraint($packageName, $constraint); } if (!$this->pool->whatProvides($packageName, $constraint)) { $problem = new Problem(); $problem->addRule(new GenericRule([], Rule::RULE_ROOT_REQUIRE, ['packageName' => $packageName, 'constraint' => $constraint])); $this->problems[] = $problem; } } } public function solve(Request $request, ?PlatformRequirementFilterInterface $platformRequirementFilter = null): LockTransaction { $platformRequirementFilter = $platformRequirementFilter ?: PlatformRequirementFilterFactory::ignoreNothing(); $this->setupFixedMap($request); $this->io->writeError('Generating rules', true, IOInterface::DEBUG); $ruleSetGenerator = new RuleSetGenerator($this->policy, $this->pool); $this->rules = $ruleSetGenerator->getRulesFor($request, $platformRequirementFilter); unset($ruleSetGenerator); $this->checkForRootRequireProblems($request, $platformRequirementFilter); $this->decisions = new Decisions($this->pool); $this->watchGraph = new RuleWatchGraph; foreach ($this->rules as $rule) { $this->watchGraph->insert(new RuleWatchNode($rule)); } $this->makeAssertionRuleDecisions(); $this->io->writeError('Resolving dependencies through SAT', true, IOInterface::DEBUG); $before = microtime(true); $this->runSat(); $this->io->writeError('', true, IOInterface::DEBUG); $this->io->writeError(sprintf('Dependency resolution completed in %.3f seconds', microtime(true) - $before), true, IOInterface::VERBOSE); if ($this->problems) { throw new SolverProblemsException($this->problems, $this->learnedPool); } return new LockTransaction($this->pool, $request->getPresentMap(), $request->getFixedPackagesMap(), $this->decisions); } protected function propagate(int $level): ?Rule { while ($this->decisions->validOffset($this->propagateIndex)) { $decision = $this->decisions->atOffset($this->propagateIndex); $conflict = $this->watchGraph->propagateLiteral( $decision[Decisions::DECISION_LITERAL], $level, $this->decisions ); $this->propagateIndex++; if ($conflict) { return $conflict; } } return null; } private function revert(int $level): void { while (!$this->decisions->isEmpty()) { $literal = $this->decisions->lastLiteral(); if ($this->decisions->undecided($literal)) { break; } $decisionLevel = $this->decisions->decisionLevel($literal); if ($decisionLevel <= $level) { break; } $this->decisions->revertLast(); $this->propagateIndex = \count($this->decisions); } while (!empty($this->branches) && $this->branches[\count($this->branches) - 1][self::BRANCH_LEVEL] >= $level) { array_pop($this->branches); } } private function setPropagateLearn(int $level, $literal, Rule $rule): int { $level++; $this->decisions->decide($literal, $level, $rule); while (true) { $rule = $this->propagate($level); if (null === $rule) { break; } if ($level === 1) { return $this->analyzeUnsolvable($rule); } [$learnLiteral, $newLevel, $newRule, $why] = $this->analyze($level, $rule); if ($newLevel <= 0 || $newLevel >= $level) { throw new SolverBugException( "Trying to revert to invalid level ".$newLevel." from level ".$level."." ); } $level = $newLevel; $this->revert($level); $this->rules->add($newRule, RuleSet::TYPE_LEARNED); $this->learnedWhy[spl_object_hash($newRule)] = $why; $ruleNode = new RuleWatchNode($newRule); $ruleNode->watch2OnHighest($this->decisions); $this->watchGraph->insert($ruleNode); $this->decisions->decide($learnLiteral, $level, $newRule); } return $level; } private function selectAndInstall(int $level, array $decisionQueue, Rule $rule): int { $literals = $this->policy->selectPreferredPackages($this->pool, $decisionQueue, $rule->getRequiredPackage()); $selectedLiteral = array_shift($literals); if (\count($literals)) { $this->branches[] = [$literals, $level]; } return $this->setPropagateLearn($level, $selectedLiteral, $rule); } protected function analyze(int $level, Rule $rule): array { $analyzedRule = $rule; $ruleLevel = 1; $num = 0; $l1num = 0; $seen = []; $learnedLiterals = [null]; $decisionId = \count($this->decisions); $this->learnedPool[] = []; while (true) { $this->learnedPool[\count($this->learnedPool) - 1][] = $rule; foreach ($rule->getLiterals() as $literal) { if ($rule instanceof MultiConflictRule && !$this->decisions->decided($literal)) { continue; } if ($this->decisions->satisfy($literal)) { continue; } if (isset($seen[abs($literal)])) { continue; } $seen[abs($literal)] = true; $l = $this->decisions->decisionLevel($literal); if (1 === $l) { $l1num++; } elseif ($level === $l) { $num++; } else { $learnedLiterals[] = $literal; if ($l > $ruleLevel) { $ruleLevel = $l; } } } unset($literal); $l1retry = true; while ($l1retry) { $l1retry = false; if (0 === $num && 0 === --$l1num) { break 2; } while (true) { if ($decisionId <= 0) { throw new SolverBugException( "Reached invalid decision id $decisionId while looking through $rule for a literal present in the analyzed rule $analyzedRule." ); } $decisionId--; $decision = $this->decisions->atOffset($decisionId); $literal = $decision[Decisions::DECISION_LITERAL]; if (isset($seen[abs($literal)])) { break; } } unset($seen[abs($literal)]); if (0 !== $num && 0 === --$num) { if ($literal < 0) { $this->testFlagLearnedPositiveLiteral = true; } $learnedLiterals[0] = -$literal; if (!$l1num) { break 2; } foreach ($learnedLiterals as $i => $learnedLiteral) { if ($i !== 0) { unset($seen[abs($learnedLiteral)]); } } $l1num++; $l1retry = true; } else { $decision = $this->decisions->atOffset($decisionId); $rule = $decision[Decisions::DECISION_REASON]; if ($rule instanceof MultiConflictRule) { foreach ($rule->getLiterals() as $literal) { if (!isset($seen[abs($literal)]) && $this->decisions->satisfy(-$literal)) { $this->learnedPool[\count($this->learnedPool) - 1][] = $rule; $l = $this->decisions->decisionLevel($literal); if (1 === $l) { $l1num++; } elseif ($level === $l) { $num++; } else { $learnedLiterals[] = $literal; if ($l > $ruleLevel) { $ruleLevel = $l; } } $seen[abs($literal)] = true; break; } } $l1retry = true; } } } $decision = $this->decisions->atOffset($decisionId); $rule = $decision[Decisions::DECISION_REASON]; } $why = \count($this->learnedPool) - 1; if (!$learnedLiterals[0]) { throw new SolverBugException( "Did not find a learnable literal in analyzed rule $analyzedRule." ); } $newRule = new GenericRule($learnedLiterals, Rule::RULE_LEARNED, $why); return [$learnedLiterals[0], $ruleLevel, $newRule, $why]; } private function analyzeUnsolvableRule(Problem $problem, Rule $conflictRule, array &$ruleSeen): void { $why = spl_object_hash($conflictRule); $ruleSeen[$why] = true; if ($conflictRule->getType() === RuleSet::TYPE_LEARNED) { $learnedWhy = $this->learnedWhy[$why]; $problemRules = $this->learnedPool[$learnedWhy]; foreach ($problemRules as $problemRule) { if (!isset($ruleSeen[spl_object_hash($problemRule)])) { $this->analyzeUnsolvableRule($problem, $problemRule, $ruleSeen); } } return; } if ($conflictRule->getType() === RuleSet::TYPE_PACKAGE) { return; } $problem->nextSection(); $problem->addRule($conflictRule); } private function analyzeUnsolvable(Rule $conflictRule): int { $problem = new Problem(); $problem->addRule($conflictRule); $ruleSeen = []; $this->analyzeUnsolvableRule($problem, $conflictRule, $ruleSeen); $this->problems[] = $problem; $seen = []; $literals = $conflictRule->getLiterals(); foreach ($literals as $literal) { if ($this->decisions->satisfy($literal)) { continue; } $seen[abs($literal)] = true; } foreach ($this->decisions as $decision) { $literal = $decision[Decisions::DECISION_LITERAL]; if (!isset($seen[abs($literal)])) { continue; } $why = $decision[Decisions::DECISION_REASON]; $problem->addRule($why); $this->analyzeUnsolvableRule($problem, $why, $ruleSeen); $literals = $why->getLiterals(); foreach ($literals as $literal) { if ($this->decisions->satisfy($literal)) { continue; } $seen[abs($literal)] = true; } } return 0; } private function enableDisableLearnedRules(): void { foreach ($this->rules->getIteratorFor(RuleSet::TYPE_LEARNED) as $rule) { $why = $this->learnedWhy[spl_object_hash($rule)]; $problemRules = $this->learnedPool[$why]; $foundDisabled = false; foreach ($problemRules as $problemRule) { if ($problemRule->isDisabled()) { $foundDisabled = true; break; } } if ($foundDisabled && $rule->isEnabled()) { $rule->disable(); } elseif (!$foundDisabled && $rule->isDisabled()) { $rule->enable(); } } } private function runSat(): void { $this->propagateIndex = 0; $level = 1; $systemLevel = $level + 1; while (true) { if (1 === $level) { $conflictRule = $this->propagate($level); if (null !== $conflictRule) { if ($this->analyzeUnsolvable($conflictRule)) { continue; } return; } } if ($level < $systemLevel) { $iterator = $this->rules->getIteratorFor(RuleSet::TYPE_REQUEST); foreach ($iterator as $rule) { if ($rule->isEnabled()) { $decisionQueue = []; $noneSatisfied = true; foreach ($rule->getLiterals() as $literal) { if ($this->decisions->satisfy($literal)) { $noneSatisfied = false; break; } if ($literal > 0 && $this->decisions->undecided($literal)) { $decisionQueue[] = $literal; } } if ($noneSatisfied && \count($decisionQueue)) { $prunedQueue = []; foreach ($decisionQueue as $literal) { if (isset($this->fixedMap[abs($literal)])) { $prunedQueue[] = $literal; } } if (!empty($prunedQueue)) { $decisionQueue = $prunedQueue; } } if ($noneSatisfied && \count($decisionQueue)) { $oLevel = $level; $level = $this->selectAndInstall($level, $decisionQueue, $rule); if (0 === $level) { return; } if ($level <= $oLevel) { break; } } } } $systemLevel = $level + 1; $iterator->next(); if ($iterator->valid()) { continue; } } if ($level < $systemLevel) { $systemLevel = $level; } $rulesCount = \count($this->rules); $pass = 1; $this->io->writeError('Looking at all rules.', true, IOInterface::DEBUG); for ($i = 0, $n = 0; $n < $rulesCount; $i++, $n++) { if ($i === $rulesCount) { if (1 === $pass) { $this->io->writeError("Something's changed, looking at all rules again (pass #$pass)", false, IOInterface::DEBUG); } else { $this->io->overwriteError("Something's changed, looking at all rules again (pass #$pass)", false, null, IOInterface::DEBUG); } $i = 0; $pass++; } $rule = $this->rules->ruleById[$i]; $literals = $rule->getLiterals(); if ($rule->isDisabled()) { continue; } $decisionQueue = []; foreach ($literals as $literal) { if ($literal <= 0) { if (!$this->decisions->decidedInstall($literal)) { continue 2; } } else { if ($this->decisions->decidedInstall($literal)) { continue 2; } if ($this->decisions->undecided($literal)) { $decisionQueue[] = $literal; } } } if (\count($decisionQueue) < 2) { continue; } $level = $this->selectAndInstall($level, $decisionQueue, $rule); if (0 === $level) { return; } $rulesCount = \count($this->rules); $n = -1; } if ($level < $systemLevel) { continue; } if (\count($this->branches)) { $lastLiteral = null; $lastLevel = null; $lastBranchIndex = 0; $lastBranchOffset = 0; for ($i = \count($this->branches) - 1; $i >= 0; $i--) { [$literals, $l] = $this->branches[$i]; foreach ($literals as $offset => $literal) { if ($literal && $literal > 0 && $this->decisions->decisionLevel($literal) > $l + 1) { $lastLiteral = $literal; $lastBranchIndex = $i; $lastBranchOffset = $offset; $lastLevel = $l; } } } if ($lastLiteral) { unset($this->branches[$lastBranchIndex][self::BRANCH_LITERALS][$lastBranchOffset]); $level = $lastLevel; $this->revert($level); $why = $this->decisions->lastReason(); $level = $this->setPropagateLearn($level, $lastLiteral, $why); if ($level === 0) { return; } continue; } } break; } } } problems = $problems; $this->learnedPool = $learnedPool; parent::__construct('Failed resolving dependencies with '.count($problems).' problems, call getPrettyString to get formatted details', self::ERROR_DEPENDENCY_RESOLUTION_FAILED); } public function getPrettyString(RepositorySet $repositorySet, Request $request, Pool $pool, bool $isVerbose, bool $isDevExtraction = false): string { $installedMap = $request->getPresentMap(true); $missingExtensions = []; $isCausedByLock = false; $problems = []; foreach ($this->problems as $problem) { $problems[] = $problem->getPrettyString($repositorySet, $request, $pool, $isVerbose, $installedMap, $this->learnedPool)."\n"; $missingExtensions = array_merge($missingExtensions, $this->getExtensionProblems($problem->getReasons())); $isCausedByLock = $isCausedByLock || $problem->isCausedByLock($repositorySet, $request, $pool); } $i = 1; $text = "\n"; foreach (array_unique($problems) as $problem) { $text .= " Problem ".($i++).$problem; } $hints = []; if (!$isDevExtraction && (strpos($text, 'could not be found') || strpos($text, 'no matching package found'))) { $hints[] = "Potential causes:\n - A typo in the package name\n - The package is not available in a stable-enough version according to your minimum-stability setting\n see for more details.\n - It's a private package and you forgot to add a custom repository to find it\n\nRead for further common problems."; } if (!empty($missingExtensions)) { $hints[] = $this->createExtensionHint($missingExtensions); } if ($isCausedByLock && !$isDevExtraction && !$request->getUpdateAllowTransitiveRootDependencies()) { $hints[] = "Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions."; } if (strpos($text, 'found composer-plugin-api[2.0.0] but it does not match') && strpos($text, '- ocramius/package-versions')) { $hints[] = "ocramius/package-versions only provides support for Composer 2 in 1.8+, which requires PHP 7.4.\nIf you can not upgrade PHP you can require composer/package-versions-deprecated to resolve this with PHP 7.0+."; } if (!class_exists('PHPUnit\Framework\TestCase', false)) { if (strpos($text, 'found composer-plugin-api[2.0.0] but it does not match')) { $hints[] = "You are using Composer 2, which some of your plugins seem to be incompatible with. Make sure you update your plugins or report a plugin-issue to ask them to support Composer 2."; } } if ($hints) { $text .= "\n" . implode("\n\n", $hints); } return $text; } public function getProblems(): array { return $this->problems; } private function createExtensionHint(array $missingExtensions): string { $paths = IniHelper::getAll(); if ('' === $paths[0]) { if (count($paths) === 1) { return ''; } array_shift($paths); } $ignoreExtensionsArguments = implode(" ", array_map(static function ($extension) { return "--ignore-platform-req=$extension"; }, array_unique($missingExtensions))); $text = "To enable extensions, verify that they are enabled in your .ini files:\n - "; $text .= implode("\n - ", $paths); $text .= "\nYou can also run `php --ini` in a terminal to see which files are used by PHP in CLI mode."; $text .= "\nAlternatively, you can run Composer with `$ignoreExtensionsArguments` to temporarily ignore these required extensions."; return $text; } private function getExtensionProblems(array $reasonSets): array { $missingExtensions = []; foreach ($reasonSets as $reasonSet) { foreach ($reasonSet as $rule) { $required = $rule->getRequiredPackage(); if (null !== $required && 0 === strpos($required, 'ext-')) { $missingExtensions[$required] = 1; } } } return array_keys($missingExtensions); } } presentPackages = $presentPackages; $this->setResultPackageMaps($resultPackages); $this->operations = $this->calculateOperations(); } public function getOperations(): array { return $this->operations; } private function setResultPackageMaps(array $resultPackages): void { $packageSort = static function (PackageInterface $a, PackageInterface $b): int { if ($a->getName() === $b->getName()) { if ($a instanceof AliasPackage !== $b instanceof AliasPackage) { return $a instanceof AliasPackage ? -1 : 1; } return strcmp($b->getVersion(), $a->getVersion()); } return strcmp($b->getName(), $a->getName()); }; $this->resultPackageMap = []; foreach ($resultPackages as $package) { $this->resultPackageMap[spl_object_hash($package)] = $package; foreach ($package->getNames() as $name) { $this->resultPackagesByName[$name][] = $package; } } uasort($this->resultPackageMap, $packageSort); foreach ($this->resultPackagesByName as $name => $packages) { uasort($this->resultPackagesByName[$name], $packageSort); } } protected function calculateOperations(): array { $operations = []; $presentPackageMap = []; $removeMap = []; $presentAliasMap = []; $removeAliasMap = []; foreach ($this->presentPackages as $package) { if ($package instanceof AliasPackage) { $presentAliasMap[$package->getName().'::'.$package->getVersion()] = $package; $removeAliasMap[$package->getName().'::'.$package->getVersion()] = $package; } else { $presentPackageMap[$package->getName()] = $package; $removeMap[$package->getName()] = $package; } } $stack = $this->getRootPackages(); $visited = []; $processed = []; while (!empty($stack)) { $package = array_pop($stack); if (isset($processed[spl_object_hash($package)])) { continue; } if (!isset($visited[spl_object_hash($package)])) { $visited[spl_object_hash($package)] = true; $stack[] = $package; if ($package instanceof AliasPackage) { $stack[] = $package->getAliasOf(); } else { foreach ($package->getRequires() as $link) { $possibleRequires = $this->getProvidersInResult($link); foreach ($possibleRequires as $require) { $stack[] = $require; } } } } elseif (!isset($processed[spl_object_hash($package)])) { $processed[spl_object_hash($package)] = true; if ($package instanceof AliasPackage) { $aliasKey = $package->getName().'::'.$package->getVersion(); if (isset($presentAliasMap[$aliasKey])) { unset($removeAliasMap[$aliasKey]); } else { $operations[] = new Operation\MarkAliasInstalledOperation($package); } } else { if (isset($presentPackageMap[$package->getName()])) { $source = $presentPackageMap[$package->getName()]; if ($package->getVersion() !== $presentPackageMap[$package->getName()]->getVersion() || $package->getDistReference() !== $presentPackageMap[$package->getName()]->getDistReference() || $package->getSourceReference() !== $presentPackageMap[$package->getName()]->getSourceReference() ) { $operations[] = new Operation\UpdateOperation($source, $package); } unset($removeMap[$package->getName()]); } else { $operations[] = new Operation\InstallOperation($package); unset($removeMap[$package->getName()]); } } } } foreach ($removeMap as $name => $package) { array_unshift($operations, new Operation\UninstallOperation($package)); } foreach ($removeAliasMap as $nameVersion => $package) { $operations[] = new Operation\MarkAliasUninstalledOperation($package); } $operations = $this->movePluginsToFront($operations); $operations = $this->moveUninstallsToFront($operations); return $this->operations = $operations; } protected function getRootPackages(): array { $roots = $this->resultPackageMap; foreach ($this->resultPackageMap as $packageHash => $package) { if (!isset($roots[$packageHash])) { continue; } foreach ($package->getRequires() as $link) { $possibleRequires = $this->getProvidersInResult($link); foreach ($possibleRequires as $require) { if ($require !== $package) { unset($roots[spl_object_hash($require)]); } } } } return $roots; } protected function getProvidersInResult(Link $link): array { if (!isset($this->resultPackagesByName[$link->getTarget()])) { return []; } return $this->resultPackagesByName[$link->getTarget()]; } private function movePluginsToFront(array $operations): array { $dlModifyingPluginsNoDeps = []; $dlModifyingPluginsWithDeps = []; $dlModifyingPluginRequires = []; $pluginsNoDeps = []; $pluginsWithDeps = []; $pluginRequires = []; foreach (array_reverse($operations, true) as $idx => $op) { if ($op instanceof Operation\InstallOperation) { $package = $op->getPackage(); } elseif ($op instanceof Operation\UpdateOperation) { $package = $op->getTargetPackage(); } else { continue; } $isDownloadsModifyingPlugin = $package->getType() === 'composer-plugin' && ($extra = $package->getExtra()) && isset($extra['plugin-modifies-downloads']) && $extra['plugin-modifies-downloads'] === true; if ($isDownloadsModifyingPlugin || count(array_intersect($package->getNames(), $dlModifyingPluginRequires))) { $requires = array_filter(array_keys($package->getRequires()), static function ($req): bool { return !PlatformRepository::isPlatformPackage($req); }); if ($isDownloadsModifyingPlugin && !count($requires)) { array_unshift($dlModifyingPluginsNoDeps, $op); } else { $dlModifyingPluginRequires = array_merge($dlModifyingPluginRequires, $requires); array_unshift($dlModifyingPluginsWithDeps, $op); } unset($operations[$idx]); continue; } $isPlugin = $package->getType() === 'composer-plugin' || $package->getType() === 'composer-installer'; if ($isPlugin || count(array_intersect($package->getNames(), $pluginRequires))) { $requires = array_filter(array_keys($package->getRequires()), static function ($req): bool { return !PlatformRepository::isPlatformPackage($req); }); if ($isPlugin && !count($requires)) { array_unshift($pluginsNoDeps, $op); } else { $pluginRequires = array_merge($pluginRequires, $requires); array_unshift($pluginsWithDeps, $op); } unset($operations[$idx]); } } return array_merge($dlModifyingPluginsNoDeps, $dlModifyingPluginsWithDeps, $pluginsNoDeps, $pluginsWithDeps, $operations); } private function moveUninstallsToFront(array $operations): array { $uninstOps = []; foreach ($operations as $idx => $op) { if ($op instanceof Operation\UninstallOperation || $op instanceof Operation\MarkAliasUninstalledOperation) { $uninstOps[] = $op; unset($operations[$idx]); } } return array_merge($uninstOps, $operations); } } cleanupExecuted[$package->getName()]); return parent::prepare($type, $package, $path, $prevPackage); } public function cleanup(string $type, PackageInterface $package, string $path, ?PackageInterface $prevPackage = null): PromiseInterface { $this->cleanupExecuted[$package->getName()] = true; return parent::cleanup($type, $package, $path, $prevPackage); } public function install(PackageInterface $package, string $path, bool $output = true): PromiseInterface { if ($output) { $this->io->writeError(" - " . InstallOperation::format($package) . $this->getInstallOperationAppendix($package, $path)); } $vendorDir = $this->config->get('vendor-dir'); if (false === strpos($this->filesystem->normalizePath($vendorDir), $this->filesystem->normalizePath($path.DIRECTORY_SEPARATOR))) { $this->filesystem->emptyDirectory($path); } do { $temporaryDir = $vendorDir.'/composer/'.substr(md5(uniqid('', true)), 0, 8); } while (is_dir($temporaryDir)); $this->addCleanupPath($package, $temporaryDir); if (!is_dir($path) || realpath($path) !== Platform::getCwd()) { $this->addCleanupPath($package, $path); } $this->filesystem->ensureDirectoryExists($temporaryDir); $fileName = $this->getFileName($package, $path); $filesystem = $this->filesystem; $cleanup = function () use ($path, $filesystem, $temporaryDir, $package) { $this->clearLastCacheWrite($package); $filesystem->removeDirectory($temporaryDir); if (is_dir($path) && realpath($path) !== Platform::getCwd()) { $filesystem->removeDirectory($path); } $this->removeCleanupPath($package, $temporaryDir); $realpath = realpath($path); if ($realpath !== false) { $this->removeCleanupPath($package, $realpath); } }; try { $promise = $this->extract($package, $fileName, $temporaryDir); } catch (\Exception $e) { $cleanup(); throw $e; } return $promise->then(function () use ($package, $filesystem, $fileName, $temporaryDir, $path): \React\Promise\PromiseInterface { if (file_exists($fileName)) { $filesystem->unlink($fileName); } $getFolderContent = static function ($dir): array { $finder = Finder::create() ->ignoreVCS(false) ->ignoreDotFiles(false) ->notName('.DS_Store') ->depth(0) ->in($dir); return iterator_to_array($finder); }; $renameRecursively = null; $renameRecursively = static function ($from, $to) use ($filesystem, $getFolderContent, $package, &$renameRecursively) { $contentDir = $getFolderContent($from); foreach ($contentDir as $file) { $file = (string) $file; if (is_dir($to . '/' . basename($file))) { if (!is_dir($file)) { throw new \RuntimeException('Installing '.$package.' would lead to overwriting the '.$to.'/'.basename($file).' directory with a file from the package, invalid operation.'); } $renameRecursively($file, $to . '/' . basename($file)); } else { $filesystem->rename($file, $to . '/' . basename($file)); } } }; $renameAsOne = false; if (!file_exists($path)) { $renameAsOne = true; } elseif ($filesystem->isDirEmpty($path)) { try { if ($filesystem->removeDirectoryPhp($path)) { $renameAsOne = true; } } catch (\RuntimeException $e) { } } $contentDir = $getFolderContent($temporaryDir); $singleDirAtTopLevel = 1 === count($contentDir) && is_dir((string) reset($contentDir)); if ($renameAsOne) { if ($singleDirAtTopLevel) { $extractedDir = (string) reset($contentDir); } else { $extractedDir = $temporaryDir; } $filesystem->rename($extractedDir, $path); } else { $from = $temporaryDir; if ($singleDirAtTopLevel) { $from = (string) reset($contentDir); } $renameRecursively($from, $path); } $promise = $filesystem->removeDirectoryAsync($temporaryDir); return $promise->then(function () use ($package, $path, $temporaryDir) { $this->removeCleanupPath($package, $temporaryDir); $this->removeCleanupPath($package, $path); }); }, static function ($e) use ($cleanup) { $cleanup(); throw $e; }); } protected function getInstallOperationAppendix(PackageInterface $package, string $path): string { return ': Extracting archive'; } abstract protected function extract(PackageInterface $package, string $file, string $path): PromiseInterface; } io = $io; $this->preferSource = $preferSource; $this->filesystem = $filesystem ?: new Filesystem(); } public function setPreferSource(bool $preferSource): self { $this->preferSource = $preferSource; return $this; } public function setPreferDist(bool $preferDist): self { $this->preferDist = $preferDist; return $this; } public function setPreferences(array $preferences): self { $this->packagePreferences = $preferences; return $this; } public function setDownloader(string $type, DownloaderInterface $downloader): self { $type = strtolower($type); $this->downloaders[$type] = $downloader; return $this; } public function getDownloader(string $type): DownloaderInterface { $type = strtolower($type); if (!isset($this->downloaders[$type])) { throw new \InvalidArgumentException(sprintf('Unknown downloader type: %s. Available types: %s.', $type, implode(', ', array_keys($this->downloaders)))); } return $this->downloaders[$type]; } public function getDownloaderForPackage(PackageInterface $package): ?DownloaderInterface { $installationSource = $package->getInstallationSource(); if ('metapackage' === $package->getType()) { return null; } if ('dist' === $installationSource) { $downloader = $this->getDownloader($package->getDistType()); } elseif ('source' === $installationSource) { $downloader = $this->getDownloader($package->getSourceType()); } else { throw new \InvalidArgumentException( 'Package '.$package.' does not have an installation source set' ); } if ($installationSource !== $downloader->getInstallationSource()) { throw new \LogicException(sprintf( 'Downloader "%s" is a %s type downloader and can not be used to download %s for package %s', get_class($downloader), $downloader->getInstallationSource(), $installationSource, $package )); } return $downloader; } public function getDownloaderType(DownloaderInterface $downloader): string { return array_search($downloader, $this->downloaders); } public function download(PackageInterface $package, string $targetDir, ?PackageInterface $prevPackage = null): PromiseInterface { $targetDir = $this->normalizeTargetDir($targetDir); $this->filesystem->ensureDirectoryExists(dirname($targetDir)); $sources = $this->getAvailableSources($package, $prevPackage); $io = $this->io; $download = function ($retry = false) use (&$sources, $io, $package, $targetDir, &$download, $prevPackage) { $source = array_shift($sources); if ($retry) { $io->writeError(' Now trying to download from ' . $source . ''); } $package->setInstallationSource($source); $downloader = $this->getDownloaderForPackage($package); if (!$downloader) { return \React\Promise\resolve(null); } $handleError = static function ($e) use ($sources, $source, $package, $io, $download) { if ($e instanceof \RuntimeException && !$e instanceof IrrecoverableDownloadException) { if (!$sources) { throw $e; } $io->writeError( ' Failed to download '. $package->getPrettyName(). ' from ' . $source . ': '. $e->getMessage().'' ); return $download(true); } throw $e; }; try { $result = $downloader->download($package, $targetDir, $prevPackage); } catch (\Exception $e) { return $handleError($e); } $res = $result->then(static function ($res) { return $res; }, $handleError); return $res; }; return $download(); } public function prepare(string $type, PackageInterface $package, string $targetDir, ?PackageInterface $prevPackage = null): PromiseInterface { $targetDir = $this->normalizeTargetDir($targetDir); $downloader = $this->getDownloaderForPackage($package); if ($downloader) { return $downloader->prepare($type, $package, $targetDir, $prevPackage); } return \React\Promise\resolve(null); } public function install(PackageInterface $package, string $targetDir): PromiseInterface { $targetDir = $this->normalizeTargetDir($targetDir); $downloader = $this->getDownloaderForPackage($package); if ($downloader) { return $downloader->install($package, $targetDir); } return \React\Promise\resolve(null); } public function update(PackageInterface $initial, PackageInterface $target, string $targetDir): PromiseInterface { $targetDir = $this->normalizeTargetDir($targetDir); $downloader = $this->getDownloaderForPackage($target); $initialDownloader = $this->getDownloaderForPackage($initial); if (!$initialDownloader && !$downloader) { return \React\Promise\resolve(null); } if (!$downloader) { return $initialDownloader->remove($initial, $targetDir); } $initialType = $this->getDownloaderType($initialDownloader); $targetType = $this->getDownloaderType($downloader); if ($initialType === $targetType) { try { return $downloader->update($initial, $target, $targetDir); } catch (\RuntimeException $e) { if (!$this->io->isInteractive()) { throw $e; } $this->io->writeError(' Update failed ('.$e->getMessage().')'); if (!$this->io->askConfirmation(' Would you like to try reinstalling the package instead [yes]? ')) { throw $e; } } } $promise = $initialDownloader->remove($initial, $targetDir); return $promise->then(function ($res) use ($target, $targetDir): PromiseInterface { return $this->install($target, $targetDir); }); } public function remove(PackageInterface $package, string $targetDir): PromiseInterface { $targetDir = $this->normalizeTargetDir($targetDir); $downloader = $this->getDownloaderForPackage($package); if ($downloader) { return $downloader->remove($package, $targetDir); } return \React\Promise\resolve(null); } public function cleanup(string $type, PackageInterface $package, string $targetDir, ?PackageInterface $prevPackage = null): PromiseInterface { $targetDir = $this->normalizeTargetDir($targetDir); $downloader = $this->getDownloaderForPackage($package); if ($downloader) { return $downloader->cleanup($type, $package, $targetDir, $prevPackage); } return \React\Promise\resolve(null); } protected function resolvePackageInstallPreference(PackageInterface $package): string { foreach ($this->packagePreferences as $pattern => $preference) { $pattern = '{^'.str_replace('\\*', '.*', preg_quote($pattern)).'$}i'; if (Preg::isMatch($pattern, $package->getName())) { if ('dist' === $preference || (!$package->isDev() && 'auto' === $preference)) { return 'dist'; } return 'source'; } } return $package->isDev() ? 'source' : 'dist'; } private function getAvailableSources(PackageInterface $package, ?PackageInterface $prevPackage = null): array { $sourceType = $package->getSourceType(); $distType = $package->getDistType(); $sources = []; if ($sourceType) { $sources[] = 'source'; } if ($distType) { $sources[] = 'dist'; } if (empty($sources)) { throw new \InvalidArgumentException('Package '.$package.' must have a source or dist specified'); } if ( $prevPackage && in_array($prevPackage->getInstallationSource(), $sources, true) && !(!$prevPackage->isDev() && $prevPackage->getInstallationSource() === 'dist' && $package->isDev()) ) { $prevSource = $prevPackage->getInstallationSource(); usort($sources, static function ($a, $b) use ($prevSource): int { return $a === $prevSource ? -1 : 1; }); return $sources; } if (!$this->preferSource && ($this->preferDist || 'dist' === $this->resolvePackageInstallPreference($package))) { $sources = array_reverse($sources); } return $sources; } private function normalizeTargetDir(string $dir): string { if ($dir === '\\' || $dir === '/') { return $dir; } return rtrim($dir, '\\/'); } } io = $io; $this->config = $config; $this->eventDispatcher = $eventDispatcher; $this->httpDownloader = $httpDownloader; $this->cache = $cache; $this->process = $process ?? new ProcessExecutor($io); $this->filesystem = $filesystem ?: new Filesystem($this->process); if ($this->cache && $this->cache->gcIsNecessary()) { $this->io->writeError('Running cache garbage collection', true, IOInterface::VERY_VERBOSE); $this->cache->gc($config->get('cache-files-ttl'), $config->get('cache-files-maxsize')); } } public function getInstallationSource(): string { return 'dist'; } public function download(PackageInterface $package, string $path, ?PackageInterface $prevPackage = null, bool $output = true): PromiseInterface { if (!$package->getDistUrl()) { throw new \InvalidArgumentException('The given package is missing url information'); } $cacheKeyGenerator = static function (PackageInterface $package, $key): string { $cacheKey = sha1($key); return $package->getName().'/'.$cacheKey.'.'.$package->getDistType(); }; $retries = 3; $distUrls = $package->getDistUrls(); $urls = []; foreach ($distUrls as $index => $url) { $processedUrl = $this->processUrl($package, $url); $urls[$index] = [ 'base' => $url, 'processed' => $processedUrl, 'cacheKey' => $cacheKeyGenerator($package, $processedUrl), ]; } $fileName = $this->getFileName($package, $path); $this->filesystem->ensureDirectoryExists($path); $this->filesystem->ensureDirectoryExists(dirname($fileName)); $io = $this->io; $cache = $this->cache; $httpDownloader = $this->httpDownloader; $eventDispatcher = $this->eventDispatcher; $filesystem = $this->filesystem; $accept = null; $reject = null; $download = function () use ($io, $output, $httpDownloader, $cache, $cacheKeyGenerator, $eventDispatcher, $package, $fileName, &$urls, &$accept, &$reject) { $url = reset($urls); $index = key($urls); if ($eventDispatcher) { $preFileDownloadEvent = new PreFileDownloadEvent(PluginEvents::PRE_FILE_DOWNLOAD, $httpDownloader, $url['processed'], 'package', $package); $eventDispatcher->dispatch($preFileDownloadEvent->getName(), $preFileDownloadEvent); if ($preFileDownloadEvent->getCustomCacheKey() !== null) { $url['cacheKey'] = $cacheKeyGenerator($package, $preFileDownloadEvent->getCustomCacheKey()); } elseif ($preFileDownloadEvent->getProcessedUrl() !== $url['processed']) { $url['cacheKey'] = $cacheKeyGenerator($package, $preFileDownloadEvent->getProcessedUrl()); } $url['processed'] = $preFileDownloadEvent->getProcessedUrl(); } $urls[$index] = $url; $checksum = $package->getDistSha1Checksum(); $cacheKey = $url['cacheKey']; if ($cache && (!$checksum || $checksum === $cache->sha1($cacheKey)) && $cache->copyTo($cacheKey, $fileName)) { if ($output) { $io->writeError(" - Loading " . $package->getName() . " (" . $package->getFullPrettyVersion() . ") from cache", true, IOInterface::VERY_VERBOSE); } if (!$cache->isReadOnly()) { $this->lastCacheWrites[$package->getName()] = $cacheKey; } $result = \React\Promise\resolve($fileName); } else { if ($output) { $io->writeError(" - Downloading " . $package->getName() . " (" . $package->getFullPrettyVersion() . ")"); } $result = $httpDownloader->addCopy($url['processed'], $fileName, $package->getTransportOptions()) ->then($accept, $reject); } return $result->then(static function ($result) use ($fileName, $checksum, $url, $package, $eventDispatcher): string { if (null === $result) { return $fileName; } if (!file_exists($fileName)) { throw new \UnexpectedValueException($url['base'].' could not be saved to '.$fileName.', make sure the' .' directory is writable and you have internet connectivity'); } if ($checksum && hash_file('sha1', $fileName) !== $checksum) { throw new \UnexpectedValueException('The checksum verification of the file failed (downloaded from '.$url['base'].')'); } if ($eventDispatcher) { $postFileDownloadEvent = new PostFileDownloadEvent(PluginEvents::POST_FILE_DOWNLOAD, $fileName, $checksum, $url['processed'], 'package', $package); $eventDispatcher->dispatch($postFileDownloadEvent->getName(), $postFileDownloadEvent); } return $fileName; }); }; $accept = function ($response) use ($cache, $package, $fileName, &$urls): string { $url = reset($urls); $cacheKey = $url['cacheKey']; FileDownloader::$downloadMetadata[$package->getName()] = @filesize($fileName) ?: $response->getHeader('Content-Length') ?: '?'; if ($cache && !$cache->isReadOnly()) { $this->lastCacheWrites[$package->getName()] = $cacheKey; $cache->copyFrom($cacheKey, $fileName); } $response->collect(); return $fileName; }; $reject = function ($e) use ($io, &$urls, $download, $fileName, $package, &$retries, $filesystem) { if (file_exists($fileName)) { $filesystem->unlink($fileName); } $this->clearLastCacheWrite($package); if ($e instanceof IrrecoverableDownloadException) { throw $e; } if ($e instanceof MaxFileSizeExceededException) { throw $e; } if ($e instanceof TransportException) { if ((0 !== $e->getCode() && !in_array($e->getCode(), [500, 502, 503, 504])) || !$retries) { $retries = 0; } } if ($e instanceof TransportException && $e->getStatusCode() === 499) { $retries = 0; $urls = []; } if ($retries) { usleep(500000); $retries--; return $download(); } array_shift($urls); if ($urls) { if ($io->isDebug()) { $io->writeError(' Failed downloading '.$package->getName().': ['.get_class($e).'] '.$e->getCode().': '.$e->getMessage()); $io->writeError(' Trying the next URL for '.$package->getName()); } else { $io->writeError(' Failed downloading '.$package->getName().', trying the next URL ('.$e->getCode().': '.$e->getMessage().')'); } $retries = 3; usleep(100000); return $download(); } throw $e; }; return $download(); } public function prepare(string $type, PackageInterface $package, string $path, ?PackageInterface $prevPackage = null): PromiseInterface { return \React\Promise\resolve(null); } public function cleanup(string $type, PackageInterface $package, string $path, ?PackageInterface $prevPackage = null): PromiseInterface { $fileName = $this->getFileName($package, $path); if (file_exists($fileName)) { $this->filesystem->unlink($fileName); } $dirsToCleanUp = [ $this->config->get('vendor-dir').'/composer/', $this->config->get('vendor-dir'), $path, ]; if (isset($this->additionalCleanupPaths[$package->getName()])) { foreach ($this->additionalCleanupPaths[$package->getName()] as $path) { $this->filesystem->remove($path); } } foreach ($dirsToCleanUp as $dir) { if (is_dir($dir) && $this->filesystem->isDirEmpty($dir) && realpath($dir) !== Platform::getCwd()) { $this->filesystem->removeDirectoryPhp($dir); } } return \React\Promise\resolve(null); } public function install(PackageInterface $package, string $path, bool $output = true): PromiseInterface { if ($output) { $this->io->writeError(" - " . InstallOperation::format($package)); } $this->filesystem->emptyDirectory($path); $this->filesystem->ensureDirectoryExists($path); $this->filesystem->rename($this->getFileName($package, $path), $path . '/' . pathinfo(parse_url(strtr((string) $package->getDistUrl(), '\\', '/'), PHP_URL_PATH), PATHINFO_BASENAME)); if ($package->getBinaries()) { foreach ($package->getBinaries() as $bin) { if (file_exists($path . '/' . $bin) && !is_executable($path . '/' . $bin)) { Silencer::call('chmod', $path . '/' . $bin, 0777 & ~umask()); } } } return \React\Promise\resolve(null); } protected function clearLastCacheWrite(PackageInterface $package): void { if ($this->cache && isset($this->lastCacheWrites[$package->getName()])) { $this->cache->remove($this->lastCacheWrites[$package->getName()]); unset($this->lastCacheWrites[$package->getName()]); } } protected function addCleanupPath(PackageInterface $package, string $path): void { $this->additionalCleanupPaths[$package->getName()][] = $path; } protected function removeCleanupPath(PackageInterface $package, string $path): void { if (isset($this->additionalCleanupPaths[$package->getName()])) { $idx = array_search($path, $this->additionalCleanupPaths[$package->getName()]); if (false !== $idx) { unset($this->additionalCleanupPaths[$package->getName()][$idx]); } } } public function update(PackageInterface $initial, PackageInterface $target, string $path): PromiseInterface { $this->io->writeError(" - " . UpdateOperation::format($initial, $target) . $this->getInstallOperationAppendix($target, $path)); $promise = $this->remove($initial, $path, false); return $promise->then(function () use ($target, $path): PromiseInterface { return $this->install($target, $path, false); }); } public function remove(PackageInterface $package, string $path, bool $output = true): PromiseInterface { if ($output) { $this->io->writeError(" - " . UninstallOperation::format($package)); } $promise = $this->filesystem->removeDirectoryAsync($path); return $promise->then(static function ($result) use ($path): void { if (!$result) { throw new \RuntimeException('Could not completely delete '.$path.', aborting.'); } }); } protected function getFileName(PackageInterface $package, string $path): string { return rtrim($this->config->get('vendor-dir').'/composer/tmp-'.md5($package.spl_object_hash($package)).'.'.pathinfo(parse_url(strtr((string) $package->getDistUrl(), '\\', '/'), PHP_URL_PATH), PATHINFO_EXTENSION), '.'); } protected function getInstallOperationAppendix(PackageInterface $package, string $path): string { return ''; } protected function processUrl(PackageInterface $package, string $url): string { if (!extension_loaded('openssl') && 0 === strpos($url, 'https:')) { throw new \RuntimeException('You must enable the openssl extension to download files via https'); } if ($package->getDistReference()) { $url = UrlUtil::updateDistReference($this->config, $url, $package->getDistReference()); } return $url; } public function getLocalChanges(PackageInterface $package, string $path): ?string { $prevIO = $this->io; $this->io = new NullIO; $this->io->loadConfiguration($this->config); $e = null; $output = ''; $targetDir = Filesystem::trimTrailingSlash($path); try { if (is_dir($targetDir.'_compare')) { $this->filesystem->removeDirectory($targetDir.'_compare'); } $this->download($package, $targetDir.'_compare', null, false); $this->httpDownloader->wait(); $this->install($package, $targetDir.'_compare', false); $this->process->wait(); $comparer = new Comparer(); $comparer->setSource($targetDir.'_compare'); $comparer->setUpdate($targetDir); $comparer->doCompare(); $output = $comparer->getChangedAsString(true); $this->filesystem->removeDirectory($targetDir.'_compare'); } catch (\Exception $e) { } $this->io = $prevIO; if ($e) { throw $e; } $output = trim($output); return strlen($output) > 0 ? $output : null; } } config->prohibitUrlByConfig($url, $this->io); $url = ProcessExecutor::escape($url); $ref = ProcessExecutor::escape($package->getSourceReference()); $repoFile = $path . '.fossil'; $this->io->writeError("Cloning ".$package->getSourceReference()); $command = sprintf('fossil clone -- %s %s', $url, ProcessExecutor::escape($repoFile)); if (0 !== $this->process->execute($command, $ignoredOutput)) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } $command = sprintf('fossil open --nested -- %s', ProcessExecutor::escape($repoFile)); if (0 !== $this->process->execute($command, $ignoredOutput, realpath($path))) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } $command = sprintf('fossil update -- %s', $ref); if (0 !== $this->process->execute($command, $ignoredOutput, realpath($path))) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } return \React\Promise\resolve(null); } protected function doUpdate(PackageInterface $initial, PackageInterface $target, string $path, string $url): PromiseInterface { $this->config->prohibitUrlByConfig($url, $this->io); $ref = ProcessExecutor::escape($target->getSourceReference()); $this->io->writeError(" Updating to ".$target->getSourceReference()); if (!$this->hasMetadataRepository($path)) { throw new \RuntimeException('The .fslckout file is missing from '.$path.', see https://getcomposer.org/commit-deps for more information'); } $command = sprintf('fossil pull && fossil up %s', $ref); if (0 !== $this->process->execute($command, $ignoredOutput, realpath($path))) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } return \React\Promise\resolve(null); } public function getLocalChanges(PackageInterface $package, string $path): ?string { if (!$this->hasMetadataRepository($path)) { return null; } $this->process->execute('fossil changes', $output, realpath($path)); $output = trim($output); return strlen($output) > 0 ? $output : null; } protected function getCommitLogs(string $fromReference, string $toReference, string $path): string { $command = sprintf('fossil timeline -t ci -W 0 -n 0 before %s', ProcessExecutor::escape($toReference)); if (0 !== $this->process->execute($command, $output, realpath($path))) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } $log = ''; $match = '/\d\d:\d\d:\d\d\s+\[' . $toReference . '\]/'; foreach ($this->process->splitLines($output) as $line) { if (Preg::isMatch($match, $line)) { break; } $log .= $line; } return $log; } protected function hasMetadataRepository(string $path): bool { return is_file($path . '/.fslckout') || is_file($path . '/_FOSSIL_'); } } gitUtil = new GitUtil($this->io, $this->config, $this->process, $this->filesystem); } protected function doDownload(PackageInterface $package, string $path, string $url, ?PackageInterface $prevPackage = null): PromiseInterface { if (Filesystem::isLocalPath($url)) { return \React\Promise\resolve(null); } GitUtil::cleanEnv(); $cachePath = $this->config->get('cache-vcs-dir').'/'.Preg::replace('{[^a-z0-9.]}i', '-', $url).'/'; $gitVersion = GitUtil::getVersion($this->process); if ($gitVersion && version_compare($gitVersion, '2.3.0-rc0', '>=') && Cache::isUsable($cachePath)) { $this->io->writeError(" - Syncing " . $package->getName() . " (" . $package->getFullPrettyVersion() . ") into cache"); $this->io->writeError(sprintf(' Cloning to cache at %s', ProcessExecutor::escape($cachePath)), true, IOInterface::DEBUG); $ref = $package->getSourceReference(); if ($this->gitUtil->fetchRefOrSyncMirror($url, $cachePath, $ref, $package->getPrettyVersion()) && is_dir($cachePath)) { $this->cachedPackages[$package->getId()][$ref] = true; } } elseif (null === $gitVersion) { throw new \RuntimeException('git was not found in your PATH, skipping source download'); } return \React\Promise\resolve(null); } protected function doInstall(PackageInterface $package, string $path, string $url): PromiseInterface { GitUtil::cleanEnv(); $path = $this->normalizePath($path); $cachePath = $this->config->get('cache-vcs-dir').'/'.Preg::replace('{[^a-z0-9.]}i', '-', $url).'/'; $ref = $package->getSourceReference(); $flag = Platform::isWindows() ? '/D ' : ''; if (!empty($this->cachedPackages[$package->getId()][$ref])) { $msg = "Cloning ".$this->getShortHash($ref).' from cache'; $cloneFlags = '--dissociate --reference %cachePath% '; $transportOptions = $package->getTransportOptions(); if (isset($transportOptions['git']['single_use_clone']) && $transportOptions['git']['single_use_clone']) { $cloneFlags = ''; } $command = 'git clone --no-checkout %cachePath% %path% ' . $cloneFlags . '&& cd '.$flag.'%path% ' . '&& git remote set-url origin -- %sanitizedUrl% && git remote add composer -- %sanitizedUrl%'; } else { $msg = "Cloning ".$this->getShortHash($ref); $command = 'git clone --no-checkout -- %url% %path% && cd '.$flag.'%path% && git remote add composer -- %url% && git fetch composer && git remote set-url origin -- %sanitizedUrl% && git remote set-url composer -- %sanitizedUrl%'; if (Platform::getEnv('COMPOSER_DISABLE_NETWORK')) { throw new \RuntimeException('The required git reference for '.$package->getName().' is not in cache and network is disabled, aborting'); } } $this->io->writeError($msg); $commandCallable = static function (string $url) use ($path, $command, $cachePath): string { return str_replace( ['%url%', '%path%', '%cachePath%', '%sanitizedUrl%'], [ ProcessExecutor::escape($url), ProcessExecutor::escape($path), ProcessExecutor::escape($cachePath), ProcessExecutor::escape(Preg::replace('{://([^@]+?):(.+?)@}', '://', $url)), ], $command ); }; $this->gitUtil->runCommand($commandCallable, $url, $path, true); $sourceUrl = $package->getSourceUrl(); if ($url !== $sourceUrl && $sourceUrl !== null) { $this->updateOriginUrl($path, $sourceUrl); } else { $this->setPushUrl($path, $url); } if ($newRef = $this->updateToCommit($package, $path, (string) $ref, $package->getPrettyVersion())) { if ($package->getDistReference() === $package->getSourceReference()) { $package->setDistReference($newRef); } $package->setSourceReference($newRef); } return \React\Promise\resolve(null); } protected function doUpdate(PackageInterface $initial, PackageInterface $target, string $path, string $url): PromiseInterface { GitUtil::cleanEnv(); $path = $this->normalizePath($path); if (!$this->hasMetadataRepository($path)) { throw new \RuntimeException('The .git directory is missing from '.$path.', see https://getcomposer.org/commit-deps for more information'); } $cachePath = $this->config->get('cache-vcs-dir').'/'.Preg::replace('{[^a-z0-9.]}i', '-', $url).'/'; $ref = $target->getSourceReference(); if (!empty($this->cachedPackages[$target->getId()][$ref])) { $msg = "Checking out ".$this->getShortHash($ref).' from cache'; $command = '(git rev-parse --quiet --verify %ref% || (git remote set-url composer -- %cachePath% && git fetch composer && git fetch --tags composer)) && git remote set-url composer -- %sanitizedUrl%'; } else { $msg = "Checking out ".$this->getShortHash($ref); $command = '(git remote set-url composer -- %url% && git rev-parse --quiet --verify %ref% || (git fetch composer && git fetch --tags composer)) && git remote set-url composer -- %sanitizedUrl%'; if (Platform::getEnv('COMPOSER_DISABLE_NETWORK')) { throw new \RuntimeException('The required git reference for '.$target->getName().' is not in cache and network is disabled, aborting'); } } $this->io->writeError($msg); $commandCallable = static function ($url) use ($ref, $command, $cachePath): string { return str_replace( ['%url%', '%ref%', '%cachePath%', '%sanitizedUrl%'], [ ProcessExecutor::escape($url), ProcessExecutor::escape($ref.'^{commit}'), ProcessExecutor::escape($cachePath), ProcessExecutor::escape(Preg::replace('{://([^@]+?):(.+?)@}', '://', $url)), ], $command ); }; $this->gitUtil->runCommand($commandCallable, $url, $path); if ($newRef = $this->updateToCommit($target, $path, (string) $ref, $target->getPrettyVersion())) { if ($target->getDistReference() === $target->getSourceReference()) { $target->setDistReference($newRef); } $target->setSourceReference($newRef); } $updateOriginUrl = false; if ( 0 === $this->process->execute('git remote -v', $output, $path) && Preg::isMatch('{^origin\s+(?P\S+)}m', $output, $originMatch) && Preg::isMatch('{^composer\s+(?P\S+)}m', $output, $composerMatch) ) { if ($originMatch['url'] === $composerMatch['url'] && $composerMatch['url'] !== $target->getSourceUrl()) { $updateOriginUrl = true; } } if ($updateOriginUrl && $target->getSourceUrl() !== null) { $this->updateOriginUrl($path, $target->getSourceUrl()); } return \React\Promise\resolve(null); } public function getLocalChanges(PackageInterface $package, string $path): ?string { GitUtil::cleanEnv(); if (!$this->hasMetadataRepository($path)) { return null; } $command = 'git status --porcelain --untracked-files=no'; if (0 !== $this->process->execute($command, $output, $path)) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } $output = trim($output); return strlen($output) > 0 ? $output : null; } public function getUnpushedChanges(PackageInterface $package, string $path): ?string { GitUtil::cleanEnv(); $path = $this->normalizePath($path); if (!$this->hasMetadataRepository($path)) { return null; } $command = 'git show-ref --head -d'; if (0 !== $this->process->execute($command, $output, $path)) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } $refs = trim($output); if (!Preg::isMatch('{^([a-f0-9]+) HEAD$}mi', $refs, $match)) { return null; } $headRef = $match[1]; if (!Preg::isMatchAll('{^'.$headRef.' refs/heads/(.+)$}mi', $refs, $matches)) { return null; } $candidateBranches = $matches[1]; $branch = $candidateBranches[0]; $unpushedChanges = null; $branchNotFoundError = false; for ($i = 0; $i <= 1; $i++) { $remoteBranches = []; foreach ($candidateBranches as $candidate) { if (Preg::isMatchAll('{^[a-f0-9]+ refs/remotes/((?:[^/]+)/'.preg_quote($candidate).')$}mi', $refs, $matches)) { foreach ($matches[1] as $match) { $branch = $candidate; $remoteBranches[] = $match; } break; } } if (!$remoteBranches) { $unpushedChanges = 'Branch ' . $branch . ' could not be found on any remote and appears to be unpushed'; $branchNotFoundError = true; } else { if ($branchNotFoundError) { $unpushedChanges = null; } foreach ($remoteBranches as $remoteBranch) { $command = sprintf('git diff --name-status %s...%s --', $remoteBranch, $branch); if (0 !== $this->process->execute($command, $output, $path)) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } $output = trim($output); if ($unpushedChanges === null || strlen($output) < strlen($unpushedChanges)) { $unpushedChanges = $output; } } } if ($unpushedChanges && $i === 0) { $this->process->execute('git fetch --all', $output, $path); $command = 'git show-ref --head -d'; if (0 !== $this->process->execute($command, $output, $path)) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } $refs = trim($output); } if (!$unpushedChanges) { break; } } return $unpushedChanges; } protected function cleanChanges(PackageInterface $package, string $path, bool $update): PromiseInterface { GitUtil::cleanEnv(); $path = $this->normalizePath($path); $unpushed = $this->getUnpushedChanges($package, $path); if ($unpushed && ($this->io->isInteractive() || $this->config->get('discard-changes') !== true)) { throw new \RuntimeException('Source directory ' . $path . ' has unpushed changes on the current branch: '."\n".$unpushed); } if (null === ($changes = $this->getLocalChanges($package, $path))) { return \React\Promise\resolve(null); } if (!$this->io->isInteractive()) { $discardChanges = $this->config->get('discard-changes'); if (true === $discardChanges) { return $this->discardChanges($path); } if ('stash' === $discardChanges) { if (!$update) { return parent::cleanChanges($package, $path, $update); } return $this->stashChanges($path); } return parent::cleanChanges($package, $path, $update); } $changes = array_map(static function ($elem): string { return ' '.$elem; }, Preg::split('{\s*\r?\n\s*}', $changes)); $this->io->writeError(' '.$package->getPrettyName().' has modified files:'); $this->io->writeError(array_slice($changes, 0, 10)); if (count($changes) > 10) { $this->io->writeError(' ' . (count($changes) - 10) . ' more files modified, choose "v" to view the full list'); } while (true) { switch ($this->io->ask(' Discard changes [y,n,v,d,'.($update ? 's,' : '').'?]? ', '?')) { case 'y': $this->discardChanges($path); break 2; case 's': if (!$update) { goto help; } $this->stashChanges($path); break 2; case 'n': throw new \RuntimeException('Update aborted'); case 'v': $this->io->writeError($changes); break; case 'd': $this->viewDiff($path); break; case '?': default: help : $this->io->writeError([ ' y - discard changes and apply the '.($update ? 'update' : 'uninstall'), ' n - abort the '.($update ? 'update' : 'uninstall').' and let you manually clean things up', ' v - view modified files', ' d - view local modifications (diff)', ]); if ($update) { $this->io->writeError(' s - stash changes and try to reapply them after the update'); } $this->io->writeError(' ? - print help'); break; } } return \React\Promise\resolve(null); } protected function reapplyChanges(string $path): void { $path = $this->normalizePath($path); if (!empty($this->hasStashedChanges[$path])) { unset($this->hasStashedChanges[$path]); $this->io->writeError(' Re-applying stashed changes'); if (0 !== $this->process->execute('git stash pop', $output, $path)) { throw new \RuntimeException("Failed to apply stashed changes:\n\n".$this->process->getErrorOutput()); } } unset($this->hasDiscardedChanges[$path]); } protected function updateToCommit(PackageInterface $package, string $path, string $reference, string $prettyVersion): ?string { $force = !empty($this->hasDiscardedChanges[$path]) || !empty($this->hasStashedChanges[$path]) ? '-f ' : ''; $template = 'git checkout '.$force.'%s -- && git reset --hard %1$s --'; $branch = Preg::replace('{(?:^dev-|(?:\.x)?-dev$)}i', '', $prettyVersion); $branches = null; if (0 === $this->process->execute('git branch -r', $output, $path)) { $branches = $output; } $gitRef = $reference; if (!Preg::isMatch('{^[a-f0-9]{40}$}', $reference) && null !== $branches && Preg::isMatch('{^\s+composer/'.preg_quote($reference).'$}m', $branches) ) { $command = sprintf('git checkout '.$force.'-B %s %s -- && git reset --hard %2$s --', ProcessExecutor::escape($branch), ProcessExecutor::escape('composer/'.$reference)); if (0 === $this->process->execute($command, $output, $path)) { return null; } } if (Preg::isMatch('{^[a-f0-9]{40}$}', $reference)) { if (null !== $branches && !Preg::isMatch('{^\s+composer/'.preg_quote($branch).'$}m', $branches) && Preg::isMatch('{^\s+composer/v'.preg_quote($branch).'$}m', $branches)) { $branch = 'v' . $branch; } $command = sprintf('git checkout %s --', ProcessExecutor::escape($branch)); $fallbackCommand = sprintf('git checkout '.$force.'-B %s %s --', ProcessExecutor::escape($branch), ProcessExecutor::escape('composer/'.$branch)); $resetCommand = sprintf('git reset --hard %s --', ProcessExecutor::escape($reference)); if (0 === $this->process->execute("($command || $fallbackCommand) && $resetCommand", $output, $path)) { return null; } } $command = sprintf($template, ProcessExecutor::escape($gitRef)); if (0 === $this->process->execute($command, $output, $path)) { return null; } $exceptionExtra = ''; if (false !== strpos($this->process->getErrorOutput(), $reference)) { $this->io->writeError(' '.$reference.' is gone (history was rewritten?)'); $exceptionExtra = "\nIt looks like the commit hash is not available in the repository, maybe ".($package->isDev() ? 'the commit was removed from the branch' : 'the tag was recreated').'? Run "composer update '.$package->getPrettyName().'" to resolve this.'; } throw new \RuntimeException(Url::sanitize('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput() . $exceptionExtra)); } protected function updateOriginUrl(string $path, string $url): void { $this->process->execute(sprintf('git remote set-url origin -- %s', ProcessExecutor::escape($url)), $output, $path); $this->setPushUrl($path, $url); } protected function setPushUrl(string $path, string $url): void { if (Preg::isMatch('{^(?:https?|git)://'.GitUtil::getGitHubDomainsRegex($this->config).'/([^/]+)/([^/]+?)(?:\.git)?$}', $url, $match)) { $protocols = $this->config->get('github-protocols'); $pushUrl = 'git@'.$match[1].':'.$match[2].'/'.$match[3].'.git'; if (!in_array('ssh', $protocols, true)) { $pushUrl = 'https://' . $match[1] . '/'.$match[2].'/'.$match[3].'.git'; } $cmd = sprintf('git remote set-url --push origin -- %s', ProcessExecutor::escape($pushUrl)); $this->process->execute($cmd, $ignoredOutput, $path); } } protected function getCommitLogs(string $fromReference, string $toReference, string $path): string { $path = $this->normalizePath($path); $command = sprintf('git log %s..%s --pretty=format:"%%h - %%an: %%s"'.GitUtil::getNoShowSignatureFlag($this->process), ProcessExecutor::escape($fromReference), ProcessExecutor::escape($toReference)); if (0 !== $this->process->execute($command, $output, $path)) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } return $output; } protected function discardChanges(string $path): PromiseInterface { $path = $this->normalizePath($path); if (0 !== $this->process->execute('git clean -df && git reset --hard', $output, $path)) { throw new \RuntimeException("Could not reset changes\n\n:".$output); } $this->hasDiscardedChanges[$path] = true; return \React\Promise\resolve(null); } protected function stashChanges(string $path): PromiseInterface { $path = $this->normalizePath($path); if (0 !== $this->process->execute('git stash --include-untracked', $output, $path)) { throw new \RuntimeException("Could not stash changes\n\n:".$output); } $this->hasStashedChanges[$path] = true; return \React\Promise\resolve(null); } protected function viewDiff(string $path): void { $path = $this->normalizePath($path); if (0 !== $this->process->execute('git diff HEAD', $output, $path)) { throw new \RuntimeException("Could not view diff\n\n:".$output); } $this->io->writeError($output); } protected function normalizePath(string $path): string { if (Platform::isWindows() && strlen($path) > 0) { $basePath = $path; $removed = []; while (!is_dir($basePath) && $basePath !== '\\') { array_unshift($removed, basename($basePath)); $basePath = dirname($basePath); } if ($basePath === '\\') { return $path; } $path = rtrim(realpath($basePath) . '/' . implode('/', $removed), '/'); } return $path; } protected function hasMetadataRepository(string $path): bool { $path = $this->normalizePath($path); return is_dir($path.'/.git'); } protected function getShortHash(string $reference): string { if (!$this->io->isVerbose() && Preg::isMatch('{^[0-9a-f]{40}$}', $reference)) { return substr($reference, 0, 10); } return $reference; } } getDistUrl(), '\\', '/'), PHP_URL_PATH), PATHINFO_FILENAME); $targetFilepath = $path . DIRECTORY_SEPARATOR . $filename; if (!Platform::isWindows()) { $command = 'gzip -cd -- ' . ProcessExecutor::escape($file) . ' > ' . ProcessExecutor::escape($targetFilepath); if (0 === $this->process->execute($command, $ignoredOutput)) { return \React\Promise\resolve(null); } if (extension_loaded('zlib')) { $this->extractUsingExt($file, $targetFilepath); return \React\Promise\resolve(null); } $processError = 'Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput(); throw new \RuntimeException($processError); } $this->extractUsingExt($file, $targetFilepath); return \React\Promise\resolve(null); } private function extractUsingExt(string $file, string $targetFilepath): void { $archiveFile = gzopen($file, 'rb'); $targetFile = fopen($targetFilepath, 'wb'); while ($string = gzread($archiveFile, 4096)) { fwrite($targetFile, $string, Platform::strlen($string)); } gzclose($archiveFile); fclose($targetFile); } } process)) { throw new \RuntimeException('hg was not found in your PATH, skipping source download'); } return \React\Promise\resolve(null); } protected function doInstall(PackageInterface $package, string $path, string $url): PromiseInterface { $hgUtils = new HgUtils($this->io, $this->config, $this->process); $cloneCommand = static function (string $url) use ($path): string { return sprintf('hg clone -- %s %s', ProcessExecutor::escape($url), ProcessExecutor::escape($path)); }; $hgUtils->runCommand($cloneCommand, $url, $path); $ref = ProcessExecutor::escape($package->getSourceReference()); $command = sprintf('hg up -- %s', $ref); if (0 !== $this->process->execute($command, $ignoredOutput, realpath($path))) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } return \React\Promise\resolve(null); } protected function doUpdate(PackageInterface $initial, PackageInterface $target, string $path, string $url): PromiseInterface { $hgUtils = new HgUtils($this->io, $this->config, $this->process); $ref = $target->getSourceReference(); $this->io->writeError(" Updating to ".$target->getSourceReference()); if (!$this->hasMetadataRepository($path)) { throw new \RuntimeException('The .hg directory is missing from '.$path.', see https://getcomposer.org/commit-deps for more information'); } $command = static function ($url) use ($ref): string { return sprintf('hg pull -- %s && hg up -- %s', ProcessExecutor::escape($url), ProcessExecutor::escape($ref)); }; $hgUtils->runCommand($command, $url, $path); return \React\Promise\resolve(null); } public function getLocalChanges(PackageInterface $package, string $path): ?string { if (!is_dir($path.'/.hg')) { return null; } $this->process->execute('hg st', $output, realpath($path)); $output = trim($output); return strlen($output) > 0 ? $output : null; } protected function getCommitLogs(string $fromReference, string $toReference, string $path): string { $command = sprintf('hg log -r %s:%s --style compact', ProcessExecutor::escape($fromReference), ProcessExecutor::escape($toReference)); if (0 !== $this->process->execute($command, $output, realpath($path))) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } return $output; } protected function hasMetadataRepository(string $path): bool { return is_dir($path . '/.hg'); } } getDistUrl(); $realUrl = realpath($url); if (false === $realUrl || !file_exists($realUrl) || !is_dir($realUrl)) { throw new \RuntimeException(sprintf( 'Source path "%s" is not found for package %s', $url, $package->getName() )); } if (realpath($path) === $realUrl) { return \React\Promise\resolve(null); } if (strpos(realpath($path) . DIRECTORY_SEPARATOR, $realUrl . DIRECTORY_SEPARATOR) === 0) { throw new \RuntimeException(sprintf( 'Package %s cannot install to "%s" inside its source at "%s"', $package->getName(), realpath($path), $realUrl )); } return \React\Promise\resolve(null); } public function install(PackageInterface $package, string $path, bool $output = true): PromiseInterface { $path = Filesystem::trimTrailingSlash($path); $url = $package->getDistUrl(); $realUrl = realpath($url); if (realpath($path) === $realUrl) { if ($output) { $this->io->writeError(" - " . InstallOperation::format($package) . $this->getInstallOperationAppendix($package, $path)); } return \React\Promise\resolve(null); } $transportOptions = $package->getTransportOptions() + ['relative' => true]; [$currentStrategy, $allowedStrategies] = $this->computeAllowedStrategies($transportOptions); $symfonyFilesystem = new SymfonyFilesystem(); $this->filesystem->removeDirectory($path); if ($output) { $this->io->writeError(" - " . InstallOperation::format($package).': ', false); } $isFallback = false; if (self::STRATEGY_SYMLINK === $currentStrategy) { try { if (Platform::isWindows()) { if ($output) { $this->io->writeError(sprintf('Junctioning from %s', $url), false); } $this->filesystem->junction($realUrl, $path); } else { $absolutePath = $path; if (!$this->filesystem->isAbsolutePath($absolutePath)) { $absolutePath = Platform::getCwd() . DIRECTORY_SEPARATOR . $path; } $shortestPath = $this->filesystem->findShortestPath($absolutePath, $realUrl); $path = rtrim($path, "/"); if ($output) { $this->io->writeError(sprintf('Symlinking from %s', $url), false); } if ($transportOptions['relative']) { $symfonyFilesystem->symlink($shortestPath.'/', $path); } else { $symfonyFilesystem->symlink($realUrl.'/', $path); } } } catch (IOException $e) { if (in_array(self::STRATEGY_MIRROR, $allowedStrategies, true)) { if ($output) { $this->io->writeError(''); $this->io->writeError(' Symlink failed, fallback to use mirroring!'); } $currentStrategy = self::STRATEGY_MIRROR; $isFallback = true; } else { throw new \RuntimeException(sprintf('Symlink from "%s" to "%s" failed!', $realUrl, $path)); } } } if (self::STRATEGY_MIRROR === $currentStrategy) { $realUrl = $this->filesystem->normalizePath($realUrl); if ($output) { $this->io->writeError(sprintf('%sMirroring from %s', $isFallback ? ' ' : '', $url), false); } $iterator = new ArchivableFilesFinder($realUrl, []); $symfonyFilesystem->mirror($realUrl, $path, $iterator); } if ($output) { $this->io->writeError(''); } return \React\Promise\resolve(null); } public function remove(PackageInterface $package, string $path, bool $output = true): PromiseInterface { $path = Filesystem::trimTrailingSlash($path); if (Platform::isWindows() && $this->filesystem->isJunction($path)) { if ($output) { $this->io->writeError(" - " . UninstallOperation::format($package).", source is still present in $path"); } if (!$this->filesystem->removeJunction($path)) { $this->io->writeError(" Could not remove junction at " . $path . " - is another process locking it?"); throw new \RuntimeException('Could not reliably remove junction for package ' . $package->getName()); } return \React\Promise\resolve(null); } $fs = new Filesystem; $absPath = $fs->isAbsolutePath($path) ? $path : Platform::getCwd() . '/' . $path; $absDistUrl = $fs->isAbsolutePath($package->getDistUrl()) ? $package->getDistUrl() : Platform::getCwd() . '/' . $package->getDistUrl(); if ($fs->normalizePath($absPath) === $fs->normalizePath($absDistUrl)) { if ($output) { $this->io->writeError(" - " . UninstallOperation::format($package).", source is still present in $path"); } return \React\Promise\resolve(null); } return parent::remove($package, $path, $output); } public function getVcsReference(PackageInterface $package, string $path): ?string { $path = Filesystem::trimTrailingSlash($path); $parser = new VersionParser; $guesser = new VersionGuesser($this->config, $this->process, $parser); $dumper = new ArrayDumper; $packageConfig = $dumper->dump($package); if ($packageVersion = $guesser->guessVersion($packageConfig, $path)) { return $packageVersion['commit']; } return null; } protected function getInstallOperationAppendix(PackageInterface $package, string $path): string { $realUrl = realpath($package->getDistUrl()); if (realpath($path) === $realUrl) { return ': Source already present'; } [$currentStrategy] = $this->computeAllowedStrategies($package->getTransportOptions()); if ($currentStrategy === self::STRATEGY_SYMLINK) { if (Platform::isWindows()) { return ': Junctioning from '.$package->getDistUrl(); } return ': Symlinking from '.$package->getDistUrl(); } return ': Mirroring from '.$package->getDistUrl(); } private function computeAllowedStrategies(array $transportOptions): array { $currentStrategy = self::STRATEGY_SYMLINK; $allowedStrategies = [self::STRATEGY_SYMLINK, self::STRATEGY_MIRROR]; $mirrorPathRepos = Platform::getEnv('COMPOSER_MIRROR_PATH_REPOS'); if ($mirrorPathRepos) { $currentStrategy = self::STRATEGY_MIRROR; } $symlinkOption = $transportOptions['symlink'] ?? null; if (true === $symlinkOption) { $currentStrategy = self::STRATEGY_SYMLINK; $allowedStrategies = [self::STRATEGY_SYMLINK]; } elseif (false === $symlinkOption) { $currentStrategy = self::STRATEGY_MIRROR; $allowedStrategies = [self::STRATEGY_MIRROR]; } if (Platform::isWindows() && self::STRATEGY_SYMLINK === $currentStrategy && !$this->safeJunctions()) { if (!in_array(self::STRATEGY_MIRROR, $allowedStrategies, true)) { throw new \RuntimeException('You are on an old Windows / old PHP combo which does not allow Composer to use junctions/symlinks and this path repository has symlink:true in its options so copying is not allowed'); } $currentStrategy = self::STRATEGY_MIRROR; $allowedStrategies = [self::STRATEGY_MIRROR]; } if (!Platform::isWindows() && self::STRATEGY_SYMLINK === $currentStrategy && !function_exists('symlink')) { if (!in_array(self::STRATEGY_MIRROR, $allowedStrategies, true)) { throw new \RuntimeException('Your PHP has the symlink() function disabled which does not allow Composer to use symlinks and this path repository has symlink:true in its options so copying is not allowed'); } $currentStrategy = self::STRATEGY_MIRROR; $allowedStrategies = [self::STRATEGY_MIRROR]; } return [$currentStrategy, $allowedStrategies]; } private function safeJunctions(): bool { return function_exists('proc_open') && (PHP_WINDOWS_VERSION_MAJOR > 6 || (PHP_WINDOWS_VERSION_MAJOR === 6 && PHP_WINDOWS_VERSION_MINOR >= 1)); } } getSourceReference(); $label = $this->getLabelFromSourceReference((string) $ref); $this->io->writeError('Cloning ' . $ref); $this->initPerforce($package, $path, $url); $this->perforce->setStream($ref); $this->perforce->p4Login(); $this->perforce->writeP4ClientSpec(); $this->perforce->connectClient(); $this->perforce->syncCodeBase($label); $this->perforce->cleanupClientSpec(); return \React\Promise\resolve(null); } private function getLabelFromSourceReference(string $ref): ?string { $pos = strpos($ref, '@'); if (false !== $pos) { return substr($ref, $pos + 1); } return null; } public function initPerforce(PackageInterface $package, string $path, string $url): void { if (!empty($this->perforce)) { $this->perforce->initializePath($path); return; } $repository = $package->getRepository(); $repoConfig = null; if ($repository instanceof VcsRepository) { $repoConfig = $this->getRepoConfig($repository); } $this->perforce = Perforce::create($repoConfig, $url, $path, $this->process, $this->io); } private function getRepoConfig(VcsRepository $repository): array { return $repository->getRepoConfig(); } protected function doUpdate(PackageInterface $initial, PackageInterface $target, string $path, string $url): PromiseInterface { return $this->doInstall($target, $path, $url); } public function getLocalChanges(PackageInterface $package, string $path): ?string { $this->io->writeError('Perforce driver does not check for local changes before overriding'); return null; } protected function getCommitLogs(string $fromReference, string $toReference, string $path): string { return $this->perforce->getCommitLogs($fromReference, $toReference); } public function setPerforce(Perforce $perforce): void { $this->perforce = $perforce; } protected function hasMetadataRepository(string $path): bool { return true; } } extractTo($path, null, true); return \React\Promise\resolve(null); } } /dev/null && chmod -R u+w ' . ProcessExecutor::escape($path); if (0 === $this->process->execute($command, $ignoredOutput)) { return \React\Promise\resolve(null); } $processError = 'Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput(); } if (!class_exists('RarArchive')) { $iniMessage = IniHelper::getMessage(); $error = "Could not decompress the archive, enable the PHP rar extension or install unrar.\n" . $iniMessage . "\n" . $processError; if (!Platform::isWindows()) { $error = "Could not decompress the archive, enable the PHP rar extension.\n" . $iniMessage; } throw new \RuntimeException($error); } $rarArchive = RarArchive::open($file); if (false === $rarArchive) { throw new \UnexpectedValueException('Could not open RAR archive: ' . $file); } $entries = $rarArchive->getEntries(); if (false === $entries) { throw new \RuntimeException('Could not retrieve RAR archive entries'); } foreach ($entries as $entry) { if (false === $entry->extract($path)) { throw new \RuntimeException('Could not extract entry'); } } $rarArchive->close(); return \React\Promise\resolve(null); } } io, $this->config, $this->process); if (null === $util->binaryVersion()) { throw new \RuntimeException('svn was not found in your PATH, skipping source download'); } return \React\Promise\resolve(null); } protected function doInstall(PackageInterface $package, string $path, string $url): PromiseInterface { SvnUtil::cleanEnv(); $ref = $package->getSourceReference(); $repo = $package->getRepository(); if ($repo instanceof VcsRepository) { $repoConfig = $repo->getRepoConfig(); if (array_key_exists('svn-cache-credentials', $repoConfig)) { $this->cacheCredentials = (bool) $repoConfig['svn-cache-credentials']; } } $this->io->writeError(" Checking out ".$package->getSourceReference()); $this->execute($package, $url, "svn co", sprintf("%s/%s", $url, $ref), null, $path); return \React\Promise\resolve(null); } protected function doUpdate(PackageInterface $initial, PackageInterface $target, string $path, string $url): PromiseInterface { SvnUtil::cleanEnv(); $ref = $target->getSourceReference(); if (!$this->hasMetadataRepository($path)) { throw new \RuntimeException('The .svn directory is missing from '.$path.', see https://getcomposer.org/commit-deps for more information'); } $util = new SvnUtil($url, $this->io, $this->config, $this->process); $flags = ""; if (version_compare($util->binaryVersion(), '1.7.0', '>=')) { $flags .= ' --ignore-ancestry'; } $this->io->writeError(" Checking out " . $ref); $this->execute($target, $url, "svn switch" . $flags, sprintf("%s/%s", $url, $ref), $path); return \React\Promise\resolve(null); } public function getLocalChanges(PackageInterface $package, string $path): ?string { if (!$this->hasMetadataRepository($path)) { return null; } $this->process->execute('svn status --ignore-externals', $output, $path); return Preg::isMatch('{^ *[^X ] +}m', $output) ? $output : null; } protected function execute(PackageInterface $package, string $baseUrl, string $command, string $url, ?string $cwd = null, ?string $path = null): string { $util = new SvnUtil($baseUrl, $this->io, $this->config, $this->process); $util->setCacheCredentials($this->cacheCredentials); try { return $util->execute($command, $url, $cwd, $path, $this->io->isVerbose()); } catch (\RuntimeException $e) { throw new \RuntimeException( $package->getPrettyName().' could not be downloaded, '.$e->getMessage() ); } } protected function cleanChanges(PackageInterface $package, string $path, bool $update): PromiseInterface { if (null === ($changes = $this->getLocalChanges($package, $path))) { return \React\Promise\resolve(null); } if (!$this->io->isInteractive()) { if (true === $this->config->get('discard-changes')) { return $this->discardChanges($path); } return parent::cleanChanges($package, $path, $update); } $changes = array_map(static function ($elem): string { return ' '.$elem; }, Preg::split('{\s*\r?\n\s*}', $changes)); $countChanges = count($changes); $this->io->writeError(sprintf(' '.$package->getPrettyName().' has modified file%s:', $countChanges === 1 ? '' : 's')); $this->io->writeError(array_slice($changes, 0, 10)); if ($countChanges > 10) { $remainingChanges = $countChanges - 10; $this->io->writeError( sprintf( ' '.$remainingChanges.' more file%s modified, choose "v" to view the full list', $remainingChanges === 1 ? '' : 's' ) ); } while (true) { switch ($this->io->ask(' Discard changes [y,n,v,?]? ', '?')) { case 'y': $this->discardChanges($path); break 2; case 'n': throw new \RuntimeException('Update aborted'); case 'v': $this->io->writeError($changes); break; case '?': default: $this->io->writeError([ ' y - discard changes and apply the '.($update ? 'update' : 'uninstall'), ' n - abort the '.($update ? 'update' : 'uninstall').' and let you manually clean things up', ' v - view modified files', ' ? - print help', ]); break; } } return \React\Promise\resolve(null); } protected function getCommitLogs(string $fromReference, string $toReference, string $path): string { if (Preg::isMatch('{@(\d+)$}', $fromReference) && Preg::isMatch('{@(\d+)$}', $toReference)) { $command = sprintf('svn info --non-interactive --xml -- %s', ProcessExecutor::escape($path)); if (0 !== $this->process->execute($command, $output, $path)) { throw new \RuntimeException( 'Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput() ); } $urlPattern = '#(.*)#'; if (Preg::isMatch($urlPattern, $output, $matches)) { $baseUrl = $matches[1]; } else { throw new \RuntimeException( 'Unable to determine svn url for path '. $path ); } $fromRevision = Preg::replace('{.*@(\d+)$}', '$1', $fromReference); $toRevision = Preg::replace('{.*@(\d+)$}', '$1', $toReference); $command = sprintf('svn log -r%s:%s --incremental', ProcessExecutor::escape($fromRevision), ProcessExecutor::escape($toRevision)); $util = new SvnUtil($baseUrl, $this->io, $this->config, $this->process); $util->setCacheCredentials($this->cacheCredentials); try { return $util->executeLocal($command, $path, null, $this->io->isVerbose()); } catch (\RuntimeException $e) { throw new \RuntimeException( 'Failed to execute ' . $command . "\n\n".$e->getMessage() ); } } return "Could not retrieve changes between $fromReference and $toReference due to missing revision information"; } protected function discardChanges(string $path): PromiseInterface { if (0 !== $this->process->execute('svn revert -R .', $output, $path)) { throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput()); } return \React\Promise\resolve(null); } protected function hasMetadataRepository(string $path): bool { return is_dir($path.'/.svn'); } } extractTo($path, null, true); return \React\Promise\resolve(null); } } headers = $headers; } public function getHeaders(): ?array { return $this->headers; } public function setResponse(?string $response): void { $this->response = $response; } public function getResponse(): ?string { return $this->response; } public function setStatusCode($statusCode): void { $this->statusCode = $statusCode; } public function getStatusCode(): ?int { return $this->statusCode; } public function getResponseInfo(): array { return $this->responseInfo; } public function setResponseInfo(array $responseInfo): void { $this->responseInfo = $responseInfo; } } io = $io; $this->config = $config; $this->process = $process ?? new ProcessExecutor($io); $this->filesystem = $fs ?? new Filesystem($this->process); } public function getInstallationSource(): string { return 'source'; } public function download(PackageInterface $package, string $path, ?PackageInterface $prevPackage = null): PromiseInterface { if (!$package->getSourceReference()) { throw new \InvalidArgumentException('Package '.$package->getPrettyName().' is missing reference information'); } $urls = $this->prepareUrls($package->getSourceUrls()); while ($url = array_shift($urls)) { try { return $this->doDownload($package, $path, $url, $prevPackage); } catch (\Exception $e) { if ($e instanceof \PHPUnit\Framework\Exception) { throw $e; } if ($this->io->isDebug()) { $this->io->writeError('Failed: ['.get_class($e).'] '.$e->getMessage()); } elseif (count($urls)) { $this->io->writeError(' Failed, trying the next URL'); } if (!count($urls)) { throw $e; } } } return \React\Promise\resolve(null); } public function prepare(string $type, PackageInterface $package, string $path, ?PackageInterface $prevPackage = null): PromiseInterface { if ($type === 'update') { $this->cleanChanges($prevPackage, $path, true); $this->hasCleanedChanges[$prevPackage->getUniqueName()] = true; } elseif ($type === 'install') { $this->filesystem->emptyDirectory($path); } elseif ($type === 'uninstall') { $this->cleanChanges($package, $path, false); } return \React\Promise\resolve(null); } public function cleanup(string $type, PackageInterface $package, string $path, ?PackageInterface $prevPackage = null): PromiseInterface { if ($type === 'update' && isset($this->hasCleanedChanges[$prevPackage->getUniqueName()])) { $this->reapplyChanges($path); unset($this->hasCleanedChanges[$prevPackage->getUniqueName()]); } return \React\Promise\resolve(null); } public function install(PackageInterface $package, string $path): PromiseInterface { if (!$package->getSourceReference()) { throw new \InvalidArgumentException('Package '.$package->getPrettyName().' is missing reference information'); } $this->io->writeError(" - " . InstallOperation::format($package).': ', false); $urls = $this->prepareUrls($package->getSourceUrls()); while ($url = array_shift($urls)) { try { $this->doInstall($package, $path, $url); break; } catch (\Exception $e) { if ($e instanceof \PHPUnit\Framework\Exception) { throw $e; } if ($this->io->isDebug()) { $this->io->writeError('Failed: ['.get_class($e).'] '.$e->getMessage()); } elseif (count($urls)) { $this->io->writeError(' Failed, trying the next URL'); } if (!count($urls)) { throw $e; } } } return \React\Promise\resolve(null); } public function update(PackageInterface $initial, PackageInterface $target, string $path): PromiseInterface { if (!$target->getSourceReference()) { throw new \InvalidArgumentException('Package '.$target->getPrettyName().' is missing reference information'); } $this->io->writeError(" - " . UpdateOperation::format($initial, $target).': ', false); $urls = $this->prepareUrls($target->getSourceUrls()); $exception = null; while ($url = array_shift($urls)) { try { $this->doUpdate($initial, $target, $path, $url); $exception = null; break; } catch (\Exception $exception) { if ($exception instanceof \PHPUnit\Framework\Exception) { throw $exception; } if ($this->io->isDebug()) { $this->io->writeError('Failed: ['.get_class($exception).'] '.$exception->getMessage()); } elseif (count($urls)) { $this->io->writeError(' Failed, trying the next URL'); } } } if (!$exception && $this->io->isVerbose() && $this->hasMetadataRepository($path)) { $message = 'Pulling in changes:'; $logs = $this->getCommitLogs($initial->getSourceReference(), $target->getSourceReference(), $path); if ('' === trim($logs)) { $message = 'Rolling back changes:'; $logs = $this->getCommitLogs($target->getSourceReference(), $initial->getSourceReference(), $path); } if ('' !== trim($logs)) { $logs = implode("\n", array_map(static function ($line): string { return ' ' . $line; }, explode("\n", $logs))); $logs = str_replace('<', '\<', $logs); $this->io->writeError(' '.$message); $this->io->writeError($logs); } } if (!$urls && $exception) { throw $exception; } return \React\Promise\resolve(null); } public function remove(PackageInterface $package, string $path): PromiseInterface { $this->io->writeError(" - " . UninstallOperation::format($package)); $promise = $this->filesystem->removeDirectoryAsync($path); return $promise->then(static function (bool $result) use ($path) { if (!$result) { throw new \RuntimeException('Could not completely delete '.$path.', aborting.'); } }); } public function getVcsReference(PackageInterface $package, string $path): ?string { $parser = new VersionParser; $guesser = new VersionGuesser($this->config, $this->process, $parser); $dumper = new ArrayDumper; $packageConfig = $dumper->dump($package); if ($packageVersion = $guesser->guessVersion($packageConfig, $path)) { return $packageVersion['commit']; } return null; } protected function cleanChanges(PackageInterface $package, string $path, bool $update): PromiseInterface { if (null !== $this->getLocalChanges($package, $path)) { throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes.'); } return \React\Promise\resolve(null); } protected function reapplyChanges(string $path): void { } abstract protected function doDownload(PackageInterface $package, string $path, string $url, ?PackageInterface $prevPackage = null): PromiseInterface; abstract protected function doInstall(PackageInterface $package, string $path, string $url): PromiseInterface; abstract protected function doUpdate(PackageInterface $initial, PackageInterface $target, string $path, string $url): PromiseInterface; abstract protected function getCommitLogs(string $fromReference, string $toReference, string $path): string; abstract protected function hasMetadataRepository(string $path): bool; private function prepareUrls(array $urls): array { foreach ($urls as $index => $url) { if (Filesystem::isLocalPath($url)) { $fileProtocol = 'file://'; $isFileProtocol = false; if (0 === strpos($url, $fileProtocol)) { $url = substr($url, strlen($fileProtocol)); $isFileProtocol = true; } if (false !== strpos($url, '%')) { $url = rawurldecode($url); } $urls[$index] = realpath($url); if ($isFileProtocol) { $urls[$index] = $fileProtocol . $urls[$index]; } } } return $urls; } } process->execute($command, $ignoredOutput)) { return \React\Promise\resolve(null); } $processError = 'Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput(); throw new \RuntimeException($processError); } } find('7z', null, ['C:\Program Files\7-Zip']))) { self::$unzipCommands[] = ['7z', ProcessExecutor::escape($cmd).' x -bb0 -y %s -o%s']; } if ($cmd = $finder->find('unzip')) { self::$unzipCommands[] = ['unzip', ProcessExecutor::escape($cmd).' -qq %s -d %s']; } if (!Platform::isWindows() && ($cmd = $finder->find('7z'))) { self::$unzipCommands[] = ['7z', ProcessExecutor::escape($cmd).' x -bb0 -y %s -o%s']; } if (!Platform::isWindows() && ($cmd = $finder->find('7zz'))) { self::$unzipCommands[] = ['7zz', ProcessExecutor::escape($cmd).' x -bb0 -y %s -o%s']; } } $procOpenMissing = false; if (!function_exists('proc_open')) { self::$unzipCommands = []; $procOpenMissing = true; } if (null === self::$hasZipArchive) { self::$hasZipArchive = class_exists('ZipArchive'); } if (!self::$hasZipArchive && !self::$unzipCommands) { $iniMessage = IniHelper::getMessage(); if ($procOpenMissing) { $error = "The zip extension is missing and unzip/7z commands cannot be called as proc_open is disabled, skipping.\n" . $iniMessage; } else { $error = "The zip extension and unzip/7z commands are both missing, skipping.\n" . $iniMessage; } throw new \RuntimeException($error); } if (null === self::$isWindows) { self::$isWindows = Platform::isWindows(); if (!self::$isWindows && !self::$unzipCommands) { if ($procOpenMissing) { $this->io->writeError("proc_open is disabled so 'unzip' and '7z' commands cannot be used, zip files are being unpacked using the PHP zip extension."); $this->io->writeError("This may cause invalid reports of corrupted archives. Besides, any UNIX permissions (e.g. executable) defined in the archives will be lost."); $this->io->writeError("Enabling proc_open and installing 'unzip' or '7z' (21.01+) may remediate them."); } else { $this->io->writeError("As there is no 'unzip' nor '7z' command installed zip files are being unpacked using the PHP zip extension."); $this->io->writeError("This may cause invalid reports of corrupted archives. Besides, any UNIX permissions (e.g. executable) defined in the archives will be lost."); $this->io->writeError("Installing 'unzip' or '7z' (21.01+) may remediate them."); } } } return parent::download($package, $path, $prevPackage, $output); } private function extractWithSystemUnzip(PackageInterface $package, string $file, string $path): PromiseInterface { static $warned7ZipLinux = false; $isLastChance = !self::$hasZipArchive; if (!self::$unzipCommands) { return $this->extractWithZipArchive($package, $file, $path); } $commandSpec = reset(self::$unzipCommands); $command = sprintf($commandSpec[1], ProcessExecutor::escape($file), ProcessExecutor::escape($path)); if (Platform::isWindows()) { $command = sprintf($commandSpec[1], ProcessExecutor::escape(strtr($file, '/', '\\')), ProcessExecutor::escape(strtr($path, '/', '\\'))); } $executable = $commandSpec[0]; if (!$warned7ZipLinux && !Platform::isWindows() && in_array($executable, ['7z', '7zz'], true)) { $warned7ZipLinux = true; if (0 === $this->process->execute($executable, $output)) { if (Preg::isMatch('{^\s*7-Zip(?: \[64\])? ([0-9.]+)}', $output, $match) && version_compare($match[1], '21.01', '<')) { $this->io->writeError(' Unzipping using '.$executable.' '.$match[1].' may result in incorrect file permissions. Install '.$executable.' 21.01+ or unzip to ensure you get correct permissions.'); } } } $io = $this->io; $tryFallback = function (\Throwable $processError) use ($isLastChance, $io, $file, $path, $package, $executable): \React\Promise\PromiseInterface { if ($isLastChance) { throw $processError; } if (!is_file($file)) { $io->writeError(' '.$processError->getMessage().''); $io->writeError(' This most likely is due to a custom installer plugin not handling the returned Promise from the downloader'); $io->writeError(' See https://github.com/composer/installers/commit/5006d0c28730ade233a8f42ec31ac68fb1c5c9bb for an example fix'); } else { $io->writeError(' '.$processError->getMessage().''); $io->writeError(' The archive may contain identical file names with different capitalization (which fails on case insensitive filesystems)'); $io->writeError(' Unzip with '.$executable.' command failed, falling back to ZipArchive class'); } return $this->extractWithZipArchive($package, $file, $path); }; try { $promise = $this->process->executeAsync($command); return $promise->then(function (Process $process) use ($tryFallback, $command, $package, $file) { if (!$process->isSuccessful()) { if (isset($this->cleanupExecuted[$package->getName()])) { throw new \RuntimeException('Failed to extract '.$package->getName().' as the installation was aborted by another package operation.'); } $output = $process->getErrorOutput(); $output = str_replace(', '.$file.'.zip or '.$file.'.ZIP', '', $output); return $tryFallback(new \RuntimeException('Failed to extract '.$package->getName().': ('.$process->getExitCode().') '.$command."\n\n".$output)); } }); } catch (\Throwable $e) { return $tryFallback($e); } } private function extractWithZipArchive(PackageInterface $package, string $file, string $path): PromiseInterface { $processError = null; $zipArchive = $this->zipArchiveObject ?: new ZipArchive(); try { if (!file_exists($file) || ($filesize = filesize($file)) === false || $filesize === 0) { $retval = -1; } else { $retval = $zipArchive->open($file); } if (true === $retval) { $extractResult = $zipArchive->extractTo($path); if (true === $extractResult) { $zipArchive->close(); return \React\Promise\resolve(null); } $processError = new \RuntimeException(rtrim("There was an error extracting the ZIP file, it is either corrupted or using an invalid format.\n")); } else { $processError = new \UnexpectedValueException(rtrim($this->getErrorMessage($retval, $file)."\n"), $retval); } } catch (\ErrorException $e) { $processError = new \RuntimeException('The archive may contain identical file names with different capitalization (which fails on case insensitive filesystems): '.$e->getMessage(), 0, $e); } catch (\Throwable $e) { $processError = $e; } throw $processError; } protected function extract(PackageInterface $package, string $file, string $path): PromiseInterface { return $this->extractWithSystemUnzip($package, $file, $path); } protected function getErrorMessage(int $retval, string $file): string { switch ($retval) { case ZipArchive::ER_EXISTS: return sprintf("File '%s' already exists.", $file); case ZipArchive::ER_INCONS: return sprintf("Zip archive '%s' is inconsistent.", $file); case ZipArchive::ER_INVAL: return sprintf("Invalid argument (%s)", $file); case ZipArchive::ER_MEMORY: return sprintf("Malloc failure (%s)", $file); case ZipArchive::ER_NOENT: return sprintf("No such zip file: '%s'", $file); case ZipArchive::ER_NOZIP: return sprintf("'%s' is not a zip archive.", $file); case ZipArchive::ER_OPEN: return sprintf("Can't open zip file: %s", $file); case ZipArchive::ER_READ: return sprintf("Zip read error (%s)", $file); case ZipArchive::ER_SEEK: return sprintf("Zip seek error (%s)", $file); case -1: return sprintf("'%s' is a corrupted zip archive (0 bytes), try again.", $file); default: return sprintf("'%s' is not a valid zip archive, got error code: %s", $file, $retval); } } } name = $name; $this->args = $args; $this->flags = $flags; } public function getName(): string { return $this->name; } public function getArguments(): array { return $this->args; } public function getFlags(): array { return $this->flags; } public function isPropagationStopped(): bool { return $this->propagationStopped; } public function stopPropagation(): void { $this->propagationStopped = true; } } composer = $composer; $this->io = $io; $this->process = $process ?? new ProcessExecutor($io); $this->eventStack = []; } public function setRunScripts(bool $runScripts = true): self { $this->runScripts = (bool) $runScripts; return $this; } public function dispatch(?string $eventName, ?Event $event = null): int { if (null === $event) { if (null === $eventName) { throw new \InvalidArgumentException('If no $event is passed in to '.__METHOD__.' you have to pass in an $eventName, got null.'); } $event = new Event($eventName); } return $this->doDispatch($event); } public function dispatchScript(string $eventName, bool $devMode = false, array $additionalArgs = [], array $flags = []): int { assert($this->composer instanceof Composer, new \LogicException('This should only be reached with a fully loaded Composer')); return $this->doDispatch(new Script\Event($eventName, $this->composer, $this->io, $devMode, $additionalArgs, $flags)); } public function dispatchPackageEvent(string $eventName, bool $devMode, RepositoryInterface $localRepo, array $operations, OperationInterface $operation): int { assert($this->composer instanceof Composer, new \LogicException('This should only be reached with a fully loaded Composer')); return $this->doDispatch(new PackageEvent($eventName, $this->composer, $this->io, $devMode, $localRepo, $operations, $operation)); } public function dispatchInstallerEvent(string $eventName, bool $devMode, bool $executeOperations, Transaction $transaction): int { assert($this->composer instanceof Composer, new \LogicException('This should only be reached with a fully loaded Composer')); return $this->doDispatch(new InstallerEvent($eventName, $this->composer, $this->io, $devMode, $executeOperations, $transaction)); } protected function doDispatch(Event $event) { if (Platform::getEnv('COMPOSER_DEBUG_EVENTS')) { $details = null; if ($event instanceof PackageEvent) { $details = (string) $event->getOperation(); } $this->io->writeError('Dispatching '.$event->getName().''.($details ? ' ('.$details.')' : '').' event'); } $listeners = $this->getListeners($event); $this->pushEvent($event); try { $returnMax = 0; foreach ($listeners as $callable) { $return = 0; $this->ensureBinDirIsInPath(); if (!is_string($callable)) { if (!is_callable($callable)) { $className = is_object($callable[0]) ? get_class($callable[0]) : $callable[0]; throw new \RuntimeException('Subscriber '.$className.'::'.$callable[1].' for event '.$event->getName().' is not callable, make sure the function is defined and public'); } if (is_array($callable) && (is_string($callable[0]) || is_object($callable[0])) && is_string($callable[1])) { $this->io->writeError(sprintf('> %s: %s', $event->getName(), (is_object($callable[0]) ? get_class($callable[0]) : $callable[0]).'->'.$callable[1]), true, IOInterface::VERBOSE); } $return = false === $callable($event) ? 1 : 0; } elseif ($this->isComposerScript($callable)) { $this->io->writeError(sprintf('> %s: %s', $event->getName(), $callable), true, IOInterface::VERBOSE); $script = explode(' ', substr($callable, 1)); $scriptName = $script[0]; unset($script[0]); $args = array_merge($script, $event->getArguments()); $flags = $event->getFlags(); if (strpos($callable, '@composer ') === 0) { $exec = $this->getPhpExecCommand() . ' ' . ProcessExecutor::escape(Platform::getEnv('COMPOSER_BINARY')) . ' ' . implode(' ', $args); if (0 !== ($exitCode = $this->executeTty($exec))) { $this->io->writeError(sprintf('Script %s handling the %s event returned with error code '.$exitCode.'', $callable, $event->getName()), true, IOInterface::QUIET); throw new ScriptExecutionException('Error Output: '.$this->process->getErrorOutput(), $exitCode); } } else { if (!$this->getListeners(new Event($scriptName))) { $this->io->writeError(sprintf('You made a reference to a non-existent script %s', $callable), true, IOInterface::QUIET); } try { $scriptEvent = new Script\Event($scriptName, $event->getComposer(), $event->getIO(), $event->isDevMode(), $args, $flags); $scriptEvent->setOriginatingEvent($event); $return = $this->dispatch($scriptName, $scriptEvent); } catch (ScriptExecutionException $e) { $this->io->writeError(sprintf('Script %s was called via %s', $callable, $event->getName()), true, IOInterface::QUIET); throw $e; } } } elseif ($this->isPhpScript($callable)) { $className = substr($callable, 0, strpos($callable, '::')); $methodName = substr($callable, strpos($callable, '::') + 2); if (!class_exists($className)) { $this->io->writeError('Class '.$className.' is not autoloadable, can not call '.$event->getName().' script', true, IOInterface::QUIET); continue; } if (!is_callable($callable)) { $this->io->writeError('Method '.$callable.' is not callable, can not call '.$event->getName().' script', true, IOInterface::QUIET); continue; } try { $return = false === $this->executeEventPhpScript($className, $methodName, $event) ? 1 : 0; } catch (\Exception $e) { $message = "Script %s handling the %s event terminated with an exception"; $this->io->writeError(''.sprintf($message, $callable, $event->getName()).'', true, IOInterface::QUIET); throw $e; } } else { $args = implode(' ', array_map(['Composer\Util\ProcessExecutor', 'escape'], $event->getArguments())); if (strpos($callable, '@putenv ') === 0) { $exec = $callable; } else { $exec = $callable . ($args === '' ? '' : ' '.$args); } if ($this->io->isVerbose()) { $this->io->writeError(sprintf('> %s: %s', $event->getName(), $exec)); } elseif ($event->getName() !== '__exec_command') { $this->io->writeError(sprintf('> %s', $exec)); } $possibleLocalBinaries = $this->composer->getPackage()->getBinaries(); if ($possibleLocalBinaries) { foreach ($possibleLocalBinaries as $localExec) { if (Preg::isMatch('{\b'.preg_quote($callable).'$}', $localExec)) { $caller = BinaryInstaller::determineBinaryCaller($localExec); $exec = Preg::replace('{^'.preg_quote($callable).'}', $caller . ' ' . $localExec, $exec); break; } } } if (strpos($exec, '@putenv ') === 0) { if (false === strpos($exec, '=')) { Platform::clearEnv(substr($exec, 8)); } else { [$var, $value] = explode('=', substr($exec, 8), 2); Platform::putEnv($var, $value); } continue; } if (strpos($exec, '@php ') === 0) { $pathAndArgs = substr($exec, 5); if (Platform::isWindows()) { $pathAndArgs = Preg::replaceCallback('{^\S+}', static function ($path) { return str_replace('/', '\\', $path[0]); }, $pathAndArgs); } $matched = Preg::isMatch('{^[^\'"\s/\\\\]+}', $pathAndArgs, $match); if ($matched && !file_exists($match[0])) { $finder = new ExecutableFinder; if ($pathToExec = $finder->find($match[0])) { $pathAndArgs = $pathToExec . substr($pathAndArgs, strlen($match[0])); } } $exec = $this->getPhpExecCommand() . ' ' . $pathAndArgs; } else { $finder = new PhpExecutableFinder(); $phpPath = $finder->find(false); if ($phpPath) { Platform::putEnv('PHP_BINARY', $phpPath); } if (Platform::isWindows()) { $exec = Preg::replaceCallback('{^\S+}', static function ($path) { return str_replace('/', '\\', $path[0]); }, $exec); } } if (strpos($exec, 'composer ') === 0) { $exec = $this->getPhpExecCommand() . ' ' . ProcessExecutor::escape(Platform::getEnv('COMPOSER_BINARY')) . substr($exec, 8); } if (0 !== ($exitCode = $this->executeTty($exec))) { $this->io->writeError(sprintf('Script %s handling the %s event returned with error code '.$exitCode.'', $callable, $event->getName()), true, IOInterface::QUIET); throw new ScriptExecutionException('Error Output: '.$this->process->getErrorOutput(), $exitCode); } } $returnMax = max($returnMax, $return); if ($event->isPropagationStopped()) { break; } } } finally { $this->popEvent(); } return $returnMax; } protected function executeTty(string $exec): int { if ($this->io->isInteractive()) { return $this->process->executeTty($exec); } return $this->process->execute($exec); } protected function getPhpExecCommand(): string { $finder = new PhpExecutableFinder(); $phpPath = $finder->find(false); if (!$phpPath) { throw new \RuntimeException('Failed to locate PHP binary to execute '.$phpPath); } $phpArgs = $finder->findArguments(); $phpArgs = $phpArgs ? ' ' . implode(' ', $phpArgs) : ''; $allowUrlFOpenFlag = ' -d allow_url_fopen=' . ProcessExecutor::escape(ini_get('allow_url_fopen')); $disableFunctionsFlag = ' -d disable_functions=' . ProcessExecutor::escape(ini_get('disable_functions')); $memoryLimitFlag = ' -d memory_limit=' . ProcessExecutor::escape(ini_get('memory_limit')); return ProcessExecutor::escape($phpPath) . $phpArgs . $allowUrlFOpenFlag . $disableFunctionsFlag . $memoryLimitFlag; } protected function executeEventPhpScript(string $className, string $methodName, Event $event) { if ($this->io->isVerbose()) { $this->io->writeError(sprintf('> %s: %s::%s', $event->getName(), $className, $methodName)); } else { $this->io->writeError(sprintf('> %s::%s', $className, $methodName)); } return $className::$methodName($event); } public function addListener(string $eventName, $listener, int $priority = 0): void { $this->listeners[$eventName][$priority][] = $listener; } public function removeListener($listener): void { foreach ($this->listeners as $eventName => $priorities) { foreach ($priorities as $priority => $listeners) { foreach ($listeners as $index => $candidate) { if ($listener === $candidate || (is_array($candidate) && is_object($listener) && $candidate[0] === $listener)) { unset($this->listeners[$eventName][$priority][$index]); } } } } } public function addSubscriber(EventSubscriberInterface $subscriber): void { foreach ($subscriber->getSubscribedEvents() as $eventName => $params) { if (is_string($params)) { $this->addListener($eventName, [$subscriber, $params]); } elseif (is_string($params[0])) { $this->addListener($eventName, [$subscriber, $params[0]], $params[1] ?? 0); } else { foreach ($params as $listener) { $this->addListener($eventName, [$subscriber, $listener[0]], $listener[1] ?? 0); } } } } protected function getListeners(Event $event): array { $scriptListeners = $this->runScripts ? $this->getScriptListeners($event) : []; if (!isset($this->listeners[$event->getName()][0])) { $this->listeners[$event->getName()][0] = []; } krsort($this->listeners[$event->getName()]); $listeners = $this->listeners; $listeners[$event->getName()][0] = array_merge($listeners[$event->getName()][0], $scriptListeners); return array_merge(...$listeners[$event->getName()]); } public function hasEventListeners(Event $event): bool { $listeners = $this->getListeners($event); return count($listeners) > 0; } protected function getScriptListeners(Event $event): array { $package = $this->composer->getPackage(); $scripts = $package->getScripts(); if (empty($scripts[$event->getName()])) { return []; } assert($this->composer instanceof Composer, new \LogicException('This should only be reached with a fully loaded Composer')); if ($this->loader) { $this->loader->unregister(); } $generator = $this->composer->getAutoloadGenerator(); if ($event instanceof ScriptEvent) { $generator->setDevMode($event->isDevMode()); } $packages = $this->composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages(); $packageMap = $generator->buildPackageMap($this->composer->getInstallationManager(), $package, $packages); $map = $generator->parseAutoloads($packageMap, $package); $this->loader = $generator->createLoader($map, $this->composer->getConfig()->get('vendor-dir')); $this->loader->register(false); return $scripts[$event->getName()]; } protected function isPhpScript(string $callable): bool { return false === strpos($callable, ' ') && false !== strpos($callable, '::'); } protected function isComposerScript(string $callable): bool { return strpos($callable, '@') === 0 && strpos($callable, '@php ') !== 0 && strpos($callable, '@putenv ') !== 0; } protected function pushEvent(Event $event): int { $eventName = $event->getName(); if (in_array($eventName, $this->eventStack)) { throw new \RuntimeException(sprintf("Circular call to script handler '%s' detected", $eventName)); } return array_push($this->eventStack, $eventName); } protected function popEvent(): ?string { return array_pop($this->eventStack); } private function ensureBinDirIsInPath(): void { $pathEnv = 'PATH'; if (!isset($_SERVER[$pathEnv]) && isset($_SERVER['Path'])) { $pathEnv = 'Path'; } $binDir = $this->composer->getConfig()->get('bin-dir'); if (is_dir($binDir)) { $binDir = realpath($binDir); $pathValue = (string) Platform::getEnv($pathEnv); if (!Preg::isMatch('{(^|'.PATH_SEPARATOR.')'.preg_quote($binDir).'($|'.PATH_SEPARATOR.')}', $pathValue)) { Platform::putEnv($pathEnv, $binDir.PATH_SEPARATOR.$pathValue); } } } } merge([ 'config' => [ 'home' => $home, 'cache-dir' => self::getCacheDir($home), 'data-dir' => self::getDataDir($home), ], ], Config::SOURCE_DEFAULT); $file = new JsonFile($config->get('home').'/config.json'); if ($file->exists()) { if ($io instanceof IOInterface) { $io->writeError('Loading config file ' . $file->getPath(), true, IOInterface::DEBUG); } self::validateJsonSchema($io, $file); $config->merge($file->read(), $file->getPath()); } $config->setConfigSource(new JsonConfigSource($file)); $htaccessProtect = $config->get('htaccess-protect'); if ($htaccessProtect) { $dirs = [$config->get('home'), $config->get('cache-dir'), $config->get('data-dir')]; foreach ($dirs as $dir) { if (!file_exists($dir . '/.htaccess')) { if (!is_dir($dir)) { Silencer::call('mkdir', $dir, 0777, true); } Silencer::call('file_put_contents', $dir . '/.htaccess', 'Deny from all'); } } } $file = new JsonFile($config->get('home').'/auth.json'); if ($file->exists()) { if ($io instanceof IOInterface) { $io->writeError('Loading config file ' . $file->getPath(), true, IOInterface::DEBUG); } self::validateJsonSchema($io, $file, JsonFile::AUTH_SCHEMA); $config->merge(['config' => $file->read()], $file->getPath()); } $config->setAuthConfigSource(new JsonConfigSource($file, true)); if ($composerAuthEnv = Platform::getEnv('COMPOSER_AUTH')) { $authData = json_decode($composerAuthEnv); if (null === $authData) { if ($io instanceof IOInterface) { $io->writeError('COMPOSER_AUTH environment variable is malformed, should be a valid JSON object'); } } else { if ($io instanceof IOInterface) { $io->writeError('Loading auth config from COMPOSER_AUTH', true, IOInterface::DEBUG); } self::validateJsonSchema($io, $authData, JsonFile::AUTH_SCHEMA, 'COMPOSER_AUTH'); $authData = json_decode($composerAuthEnv, true); if (null !== $authData) { $config->merge(['config' => $authData], 'COMPOSER_AUTH'); } } } return $config; } public static function getComposerFile(): string { return trim((string) Platform::getEnv('COMPOSER')) ?: './composer.json'; } public static function getLockFile(string $composerFile): string { return "json" === pathinfo($composerFile, PATHINFO_EXTENSION) ? substr($composerFile, 0, -4).'lock' : $composerFile . '.lock'; } public static function createAdditionalStyles(): array { return [ 'highlight' => new OutputFormatterStyle('red'), 'warning' => new OutputFormatterStyle('black', 'yellow'), ]; } public static function createOutput(): ConsoleOutput { $styles = self::createAdditionalStyles(); $formatter = new OutputFormatter(false, $styles); return new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter); } public function createComposer(IOInterface $io, $localConfig = null, $disablePlugins = false, ?string $cwd = null, bool $fullLoad = true, bool $disableScripts = false) { if (is_string($localConfig) && is_file($localConfig) && null === $cwd) { $cwd = dirname($localConfig); } $cwd = $cwd ?? Platform::getCwd(true); if (null === $localConfig) { $localConfig = static::getComposerFile(); } $localConfigSource = Config::SOURCE_UNKNOWN; if (is_string($localConfig)) { $composerFile = $localConfig; $file = new JsonFile($localConfig, null, $io); if (!$file->exists()) { if ($localConfig === './composer.json' || $localConfig === 'composer.json') { $message = 'Composer could not find a composer.json file in '.$cwd; } else { $message = 'Composer could not find the config file: '.$localConfig; } $instructions = $fullLoad ? 'To initialize a project, please create a composer.json file. See https://getcomposer.org/basic-usage' : ''; throw new \InvalidArgumentException($message.PHP_EOL.$instructions); } if (!Platform::isInputCompletionProcess()) { try { $file->validateSchema(JsonFile::LAX_SCHEMA); } catch (JsonValidationException $e) { $errors = ' - ' . implode(PHP_EOL . ' - ', $e->getErrors()); $message = $e->getMessage() . ':' . PHP_EOL . $errors; throw new JsonValidationException($message); } } $localConfig = $file->read(); $localConfigSource = $file->getPath(); } $config = static::createConfig($io, $cwd); $config->merge($localConfig, $localConfigSource); if (isset($composerFile)) { $io->writeError('Loading config file ' . $composerFile .' ('.realpath($composerFile).')', true, IOInterface::DEBUG); $config->setConfigSource(new JsonConfigSource(new JsonFile(realpath($composerFile), null, $io))); $localAuthFile = new JsonFile(dirname(realpath($composerFile)) . '/auth.json', null, $io); if ($localAuthFile->exists()) { $io->writeError('Loading config file ' . $localAuthFile->getPath(), true, IOInterface::DEBUG); self::validateJsonSchema($io, $localAuthFile, JsonFile::AUTH_SCHEMA); $config->merge(['config' => $localAuthFile->read()], $localAuthFile->getPath()); $config->setAuthConfigSource(new JsonConfigSource($localAuthFile, true)); } } $vendorDir = $config->get('vendor-dir'); $composer = $fullLoad ? new Composer() : new PartialComposer(); $composer->setConfig($config); if ($fullLoad) { $io->loadConfiguration($config); if (!class_exists('Composer\InstalledVersions', false) && file_exists($installedVersionsPath = $config->get('vendor-dir').'/composer/InstalledVersions.php')) { include $installedVersionsPath; } } $httpDownloader = self::createHttpDownloader($io, $config); $process = new ProcessExecutor($io); $loop = new Loop($httpDownloader, $process); $composer->setLoop($loop); $dispatcher = new EventDispatcher($composer, $io, $process); $dispatcher->setRunScripts(!$disableScripts); $composer->setEventDispatcher($dispatcher); $rm = RepositoryFactory::manager($io, $config, $httpDownloader, $dispatcher, $process); $composer->setRepositoryManager($rm); if (!$fullLoad && !isset($localConfig['version'])) { $localConfig['version'] = '1.0.0'; } $parser = new VersionParser; $guesser = new VersionGuesser($config, $process, $parser); $loader = $this->loadRootPackage($rm, $config, $parser, $guesser, $io); $package = $loader->load($localConfig, 'Composer\Package\RootPackage', $cwd); $composer->setPackage($package); $this->addLocalRepository($io, $rm, $vendorDir, $package, $process); $im = $this->createInstallationManager($loop, $io, $dispatcher); $composer->setInstallationManager($im); if ($composer instanceof Composer) { $dm = $this->createDownloadManager($io, $config, $httpDownloader, $process, $dispatcher); $composer->setDownloadManager($dm); $generator = new AutoloadGenerator($dispatcher, $io); $composer->setAutoloadGenerator($generator); $am = $this->createArchiveManager($config, $dm, $loop); $composer->setArchiveManager($am); } $this->createDefaultInstallers($im, $composer, $io, $process); if ($composer instanceof Composer && isset($composerFile)) { $lockFile = self::getLockFile($composerFile); if (!$config->get('lock') && file_exists($lockFile)) { $io->writeError(''.$lockFile.' is present but ignored as the "lock" config option is disabled.'); } $locker = new Package\Locker($io, new JsonFile($config->get('lock') ? $lockFile : Platform::getDevNull(), null, $io), $im, file_get_contents($composerFile), $process); $composer->setLocker($locker); } elseif ($composer instanceof Composer) { $locker = new Package\Locker($io, new JsonFile(Platform::getDevNull(), null, $io), $im, JsonFile::encode($localConfig), $process); $composer->setLocker($locker); } if ($composer instanceof Composer) { $globalComposer = null; if (realpath($config->get('home')) !== $cwd) { $globalComposer = $this->createGlobalComposer($io, $config, $disablePlugins, $disableScripts); } $pm = $this->createPluginManager($io, $composer, $globalComposer, $disablePlugins); $composer->setPluginManager($pm); if (realpath($config->get('home')) === $cwd) { $pm->setRunningInGlobalDir(true); } $pm->loadInstalledPlugins(); } if ($fullLoad) { $initEvent = new Event(PluginEvents::INIT); $composer->getEventDispatcher()->dispatch($initEvent->getName(), $initEvent); $this->purgePackages($rm->getLocalRepository(), $im); } return $composer; } public static function createGlobal(IOInterface $io, bool $disablePlugins = false, bool $disableScripts = false): ?Composer { $factory = new static(); return $factory->createGlobalComposer($io, static::createConfig($io), $disablePlugins, $disableScripts, true); } protected function addLocalRepository(IOInterface $io, RepositoryManager $rm, string $vendorDir, RootPackageInterface $rootPackage, ?ProcessExecutor $process = null): void { $fs = null; if ($process) { $fs = new Filesystem($process); } $rm->setLocalRepository(new Repository\InstalledFilesystemRepository(new JsonFile($vendorDir.'/composer/installed.json', null, $io), true, $rootPackage, $fs)); } protected function createGlobalComposer(IOInterface $io, Config $config, $disablePlugins, bool $disableScripts, bool $fullLoad = false): ?PartialComposer { $disablePlugins = $disablePlugins === 'global' || $disablePlugins === true; $composer = null; try { $composer = $this->createComposer($io, $config->get('home') . '/composer.json', $disablePlugins, $config->get('home'), $fullLoad, $disableScripts); } catch (\Exception $e) { $io->writeError('Failed to initialize global composer: '.$e->getMessage(), true, IOInterface::DEBUG); } return $composer; } public function createDownloadManager(IOInterface $io, Config $config, HttpDownloader $httpDownloader, ProcessExecutor $process, ?EventDispatcher $eventDispatcher = null): Downloader\DownloadManager { $cache = null; if ($config->get('cache-files-ttl') > 0) { $cache = new Cache($io, $config->get('cache-files-dir'), 'a-z0-9_./'); $cache->setReadOnly($config->get('cache-read-only')); } $fs = new Filesystem($process); $dm = new Downloader\DownloadManager($io, false, $fs); switch ($preferred = $config->get('preferred-install')) { case 'dist': $dm->setPreferDist(true); break; case 'source': $dm->setPreferSource(true); break; case 'auto': default: break; } if (is_array($preferred)) { $dm->setPreferences($preferred); } $dm->setDownloader('git', new Downloader\GitDownloader($io, $config, $process, $fs)); $dm->setDownloader('svn', new Downloader\SvnDownloader($io, $config, $process, $fs)); $dm->setDownloader('fossil', new Downloader\FossilDownloader($io, $config, $process, $fs)); $dm->setDownloader('hg', new Downloader\HgDownloader($io, $config, $process, $fs)); $dm->setDownloader('perforce', new Downloader\PerforceDownloader($io, $config, $process, $fs)); $dm->setDownloader('zip', new Downloader\ZipDownloader($io, $config, $httpDownloader, $eventDispatcher, $cache, $fs, $process)); $dm->setDownloader('rar', new Downloader\RarDownloader($io, $config, $httpDownloader, $eventDispatcher, $cache, $fs, $process)); $dm->setDownloader('tar', new Downloader\TarDownloader($io, $config, $httpDownloader, $eventDispatcher, $cache, $fs, $process)); $dm->setDownloader('gzip', new Downloader\GzipDownloader($io, $config, $httpDownloader, $eventDispatcher, $cache, $fs, $process)); $dm->setDownloader('xz', new Downloader\XzDownloader($io, $config, $httpDownloader, $eventDispatcher, $cache, $fs, $process)); $dm->setDownloader('phar', new Downloader\PharDownloader($io, $config, $httpDownloader, $eventDispatcher, $cache, $fs, $process)); $dm->setDownloader('file', new Downloader\FileDownloader($io, $config, $httpDownloader, $eventDispatcher, $cache, $fs, $process)); $dm->setDownloader('path', new Downloader\PathDownloader($io, $config, $httpDownloader, $eventDispatcher, $cache, $fs, $process)); return $dm; } public function createArchiveManager(Config $config, Downloader\DownloadManager $dm, Loop $loop) { $am = new Archiver\ArchiveManager($dm, $loop); if (class_exists(ZipArchive::class)) { $am->addArchiver(new Archiver\ZipArchiver); } if (class_exists(Phar::class)) { $am->addArchiver(new Archiver\PharArchiver); } return $am; } protected function createPluginManager(IOInterface $io, Composer $composer, ?PartialComposer $globalComposer = null, $disablePlugins = false): Plugin\PluginManager { return new Plugin\PluginManager($io, $composer, $globalComposer, $disablePlugins); } public function createInstallationManager(Loop $loop, IOInterface $io, ?EventDispatcher $eventDispatcher = null): Installer\InstallationManager { return new Installer\InstallationManager($loop, $io, $eventDispatcher); } protected function createDefaultInstallers(Installer\InstallationManager $im, PartialComposer $composer, IOInterface $io, ?ProcessExecutor $process = null): void { $fs = new Filesystem($process); $binaryInstaller = new Installer\BinaryInstaller($io, rtrim($composer->getConfig()->get('bin-dir'), '/'), $composer->getConfig()->get('bin-compat'), $fs, rtrim($composer->getConfig()->get('vendor-dir'), '/')); $im->addInstaller(new Installer\LibraryInstaller($io, $composer, null, $fs, $binaryInstaller)); $im->addInstaller(new Installer\PluginInstaller($io, $composer, $fs, $binaryInstaller)); $im->addInstaller(new Installer\MetapackageInstaller($io)); } protected function purgePackages(InstalledRepositoryInterface $repo, Installer\InstallationManager $im): void { foreach ($repo->getPackages() as $package) { if (!$im->isPackageInstalled($repo, $package)) { $repo->removePackage($package); } } } protected function loadRootPackage(RepositoryManager $rm, Config $config, VersionParser $parser, VersionGuesser $guesser, IOInterface $io): Package\Loader\RootPackageLoader { return new Package\Loader\RootPackageLoader($rm, $config, $parser, $guesser, $io); } public static function create(IOInterface $io, $config = null, $disablePlugins = false, bool $disableScripts = false): Composer { $factory = new static(); if ($config !== null && $config !== self::getComposerFile() && $disablePlugins === false) { $disablePlugins = 'local'; } return $factory->createComposer($io, $config, $disablePlugins, null, true, $disableScripts); } public static function createHttpDownloader(IOInterface $io, Config $config, array $options = []): HttpDownloader { static $warned = false; $disableTls = false; if (isset($_SERVER['argv']) && in_array('disable-tls', $_SERVER['argv']) && (in_array('conf', $_SERVER['argv']) || in_array('config', $_SERVER['argv']))) { $warned = true; $disableTls = !extension_loaded('openssl'); } elseif ($config->get('disable-tls') === true) { if (!$warned) { $io->writeError('You are running Composer with SSL/TLS protection disabled.'); } $warned = true; $disableTls = true; } elseif (!extension_loaded('openssl')) { throw new Exception\NoSslException('The openssl extension is required for SSL/TLS protection but is not available. ' . 'If you can not enable the openssl extension, you can disable this error, at your own risk, by setting the \'disable-tls\' option to true.'); } $httpDownloaderOptions = []; if ($disableTls === false) { if ('' !== $config->get('cafile')) { $httpDownloaderOptions['ssl']['cafile'] = $config->get('cafile'); } if ('' !== $config->get('capath')) { $httpDownloaderOptions['ssl']['capath'] = $config->get('capath'); } $httpDownloaderOptions = array_replace_recursive($httpDownloaderOptions, $options); } try { $httpDownloader = new HttpDownloader($io, $config, $httpDownloaderOptions, $disableTls); } catch (TransportException $e) { if (false !== strpos($e->getMessage(), 'cafile')) { $io->write('Unable to locate a valid CA certificate file. You must set a valid \'cafile\' option.'); $io->write('A valid CA certificate file is required for SSL/TLS protection.'); $io->write('You can disable this error, at your own risk, by setting the \'disable-tls\' option to true.'); } throw $e; } return $httpDownloader; } private static function useXdg(): bool { foreach (array_keys($_SERVER) as $key) { if (strpos($key, 'XDG_') === 0) { return true; } } if (Silencer::call('is_dir', '/etc/xdg')) { return true; } return false; } private static function getUserDir(): string { $home = Platform::getEnv('HOME'); if (!$home) { throw new \RuntimeException('The HOME or COMPOSER_HOME environment variable must be set for composer to run correctly'); } return rtrim(strtr($home, '\\', '/'), '/'); } private static function validateJsonSchema(?IOInterface $io, $fileOrData, int $schema = JsonFile::LAX_SCHEMA, ?string $source = null): void { if (Platform::isInputCompletionProcess()) { return; } try { if ($fileOrData instanceof JsonFile) { $fileOrData->validateSchema($schema); } else { if (null === $source) { throw new \InvalidArgumentException('$source is required to be provided if $fileOrData is arbitrary data'); } JsonFile::validateJsonSchema($source, $fileOrData, $schema); } } catch (JsonValidationException $e) { $msg = $e->getMessage().', this may result in errors and should be resolved:'.PHP_EOL.' - '.implode(PHP_EOL.' - ', $e->getErrors()); if ($io instanceof IOInterface) { $io->writeError(''.$msg.''); } else { throw new UnexpectedValueException($msg); } } } } ignoreRegex = BasePackage::packageNamesToRegexp($ignoreAll); $this->ignoreUpperBoundRegex = BasePackage::packageNamesToRegexp($ignoreUpperBound); } public function isIgnored(string $req): bool { if (!PlatformRepository::isPlatformPackage($req)) { return false; } return Preg::isMatch($this->ignoreRegex, $req); } public function filterConstraint(string $req, ConstraintInterface $constraint, bool $allowUpperBoundOverride = true): ConstraintInterface { if (!PlatformRepository::isPlatformPackage($req)) { return $constraint; } if (!$allowUpperBoundOverride || !Preg::isMatch($this->ignoreUpperBoundRegex, $req)) { return $constraint; } if (Preg::isMatch($this->ignoreRegex, $req)) { return new MatchAllConstraint; } $intervals = Intervals::get($constraint); $last = end($intervals['numeric']); if ($last !== false && (string) $last->getEnd() !== (string) Interval::untilPositiveInfinity()) { $constraint = new MultiConstraint([$constraint, new Constraint('>=', $last->getEnd()->getVersion())], false); } return $constraint; } } authentications; } public function resetAuthentications() { $this->authentications = []; } public function hasAuthentication($repositoryName) { return isset($this->authentications[$repositoryName]); } public function getAuthentication($repositoryName) { if (isset($this->authentications[$repositoryName])) { return $this->authentications[$repositoryName]; } return ['username' => null, 'password' => null]; } public function setAuthentication($repositoryName, $username, $password = null) { $this->authentications[$repositoryName] = ['username' => $username, 'password' => $password]; } public function writeRaw($messages, bool $newline = true, int $verbosity = self::NORMAL) { $this->write($messages, $newline, $verbosity); } public function writeErrorRaw($messages, bool $newline = true, int $verbosity = self::NORMAL) { $this->writeError($messages, $newline, $verbosity); } protected function checkAndSetAuthentication(string $repositoryName, string $username, ?string $password = null) { if ($this->hasAuthentication($repositoryName)) { $auth = $this->getAuthentication($repositoryName); if ($auth['username'] === $username && $auth['password'] === $password) { return; } $this->writeError( sprintf( "Warning: You should avoid overwriting already defined auth settings for %s.", $repositoryName ) ); } $this->setAuthentication($repositoryName, $username, $password); } public function loadConfiguration(Config $config) { $bitbucketOauth = $config->get('bitbucket-oauth'); $githubOauth = $config->get('github-oauth'); $gitlabOauth = $config->get('gitlab-oauth'); $gitlabToken = $config->get('gitlab-token'); $httpBasic = $config->get('http-basic'); $bearerToken = $config->get('bearer'); foreach ($bitbucketOauth as $domain => $cred) { $this->checkAndSetAuthentication($domain, $cred['consumer-key'], $cred['consumer-secret']); } foreach ($githubOauth as $domain => $token) { if (!Preg::isMatch('{^[.A-Za-z0-9_]+$}', $token)) { throw new \UnexpectedValueException('Your github oauth token for '.$domain.' contains invalid characters: "'.$token.'"'); } $this->checkAndSetAuthentication($domain, $token, 'x-oauth-basic'); } foreach ($gitlabOauth as $domain => $token) { $token = is_array($token) ? $token["token"] : $token; $this->checkAndSetAuthentication($domain, $token, 'oauth2'); } foreach ($gitlabToken as $domain => $token) { $username = is_array($token) ? $token["username"] : $token; $password = is_array($token) ? $token["token"] : 'private-token'; $this->checkAndSetAuthentication($domain, $username, $password); } foreach ($httpBasic as $domain => $cred) { $this->checkAndSetAuthentication($domain, $cred['username'], $cred['password']); } foreach ($bearerToken as $domain => $token) { $this->checkAndSetAuthentication($domain, $token, 'bearer'); } ProcessExecutor::setTimeout($config->get('process-timeout')); } public function emergency($message, array $context = []): void { $this->log(LogLevel::EMERGENCY, $message, $context); } public function alert($message, array $context = []): void { $this->log(LogLevel::ALERT, $message, $context); } public function critical($message, array $context = []): void { $this->log(LogLevel::CRITICAL, $message, $context); } public function error($message, array $context = []): void { $this->log(LogLevel::ERROR, $message, $context); } public function warning($message, array $context = []): void { $this->log(LogLevel::WARNING, $message, $context); } public function notice($message, array $context = []): void { $this->log(LogLevel::NOTICE, $message, $context); } public function info($message, array $context = []): void { $this->log(LogLevel::INFO, $message, $context); } public function debug($message, array $context = []): void { $this->log(LogLevel::DEBUG, $message, $context); } public function log($level, $message, array $context = []): void { $message = (string) $message; if (in_array($level, [LogLevel::EMERGENCY, LogLevel::ALERT, LogLevel::CRITICAL, LogLevel::ERROR])) { $this->writeError(''.$message.''); } elseif ($level === LogLevel::WARNING) { $this->writeError(''.$message.''); } elseif ($level === LogLevel::NOTICE) { $this->writeError(''.$message.'', true, self::VERBOSE); } elseif ($level === LogLevel::INFO) { $this->writeError(''.$message.'', true, self::VERY_VERBOSE); } else { $this->writeError($message, true, self::DEBUG); } } } setInteractive(false); $output = new StreamOutput(fopen('php://memory', 'rw'), $verbosity, $formatter ? $formatter->isDecorated() : false, $formatter); parent::__construct($input, $output, new HelperSet([ new QuestionHelper(), ])); } public function getOutput(): string { fseek($this->output->getStream(), 0); $output = stream_get_contents($this->output->getStream()); $output = Preg::replaceCallback("{(?<=^|\n|\x08)(.+?)(\x08+)}", static function ($matches): string { $pre = strip_tags($matches[1]); if (strlen($pre) === strlen($matches[2])) { return ''; } return rtrim($matches[1])."\n"; }, $output); return $output; } public function setUserInputs(array $inputs): void { if (!$this->input instanceof StreamableInputInterface) { throw new \RuntimeException('Setting the user inputs requires at least the version 3.2 of the symfony/console component.'); } $this->input->setStream($this->createStream($inputs)); $this->input->setInteractive(true); } private function createStream(array $inputs) { $stream = fopen('php://memory', 'r+'); foreach ($inputs as $input) { fwrite($stream, $input.PHP_EOL); } rewind($stream); return $stream; } } input = $input; $this->output = $output; $this->helperSet = $helperSet; $this->verbosityMap = [ self::QUIET => OutputInterface::VERBOSITY_QUIET, self::NORMAL => OutputInterface::VERBOSITY_NORMAL, self::VERBOSE => OutputInterface::VERBOSITY_VERBOSE, self::VERY_VERBOSE => OutputInterface::VERBOSITY_VERY_VERBOSE, self::DEBUG => OutputInterface::VERBOSITY_DEBUG, ]; } public function enableDebugging(float $startTime) { $this->startTime = $startTime; } public function isInteractive() { return $this->input->isInteractive(); } public function isDecorated() { return $this->output->isDecorated(); } public function isVerbose() { return $this->output->isVerbose(); } public function isVeryVerbose() { return $this->output->isVeryVerbose(); } public function isDebug() { return $this->output->isDebug(); } public function write($messages, bool $newline = true, int $verbosity = self::NORMAL) { $this->doWrite($messages, $newline, false, $verbosity); } public function writeError($messages, bool $newline = true, int $verbosity = self::NORMAL) { $this->doWrite($messages, $newline, true, $verbosity); } public function writeRaw($messages, bool $newline = true, int $verbosity = self::NORMAL) { $this->doWrite($messages, $newline, false, $verbosity, true); } public function writeErrorRaw($messages, bool $newline = true, int $verbosity = self::NORMAL) { $this->doWrite($messages, $newline, true, $verbosity, true); } private function doWrite($messages, bool $newline, bool $stderr, int $verbosity, bool $raw = false): void { $sfVerbosity = $this->verbosityMap[$verbosity]; if ($sfVerbosity > $this->output->getVerbosity()) { return; } if ($raw) { $sfVerbosity |= OutputInterface::OUTPUT_RAW; } if (null !== $this->startTime) { $memoryUsage = memory_get_usage() / 1024 / 1024; $timeSpent = microtime(true) - $this->startTime; $messages = array_map(static function ($message) use ($memoryUsage, $timeSpent): string { return sprintf('[%.1fMiB/%.2fs] %s', $memoryUsage, $timeSpent, $message); }, (array) $messages); } if (true === $stderr && $this->output instanceof ConsoleOutputInterface) { $this->output->getErrorOutput()->write($messages, $newline, $sfVerbosity); $this->lastMessageErr = implode($newline ? "\n" : '', (array) $messages); return; } $this->output->write($messages, $newline, $sfVerbosity); $this->lastMessage = implode($newline ? "\n" : '', (array) $messages); } public function overwrite($messages, bool $newline = true, ?int $size = null, int $verbosity = self::NORMAL) { $this->doOverwrite($messages, $newline, $size, false, $verbosity); } public function overwriteError($messages, bool $newline = true, ?int $size = null, int $verbosity = self::NORMAL) { $this->doOverwrite($messages, $newline, $size, true, $verbosity); } private function doOverwrite($messages, bool $newline, ?int $size, bool $stderr, int $verbosity): void { $messages = implode($newline ? "\n" : '', (array) $messages); if (!isset($size)) { $size = strlen(strip_tags($stderr ? $this->lastMessageErr : $this->lastMessage)); } $this->doWrite(str_repeat("\x08", $size), false, $stderr, $verbosity); $this->doWrite($messages, false, $stderr, $verbosity); $fill = $size - strlen(strip_tags($messages)); if ($fill > 0) { $this->doWrite(str_repeat(' ', $fill), false, $stderr, $verbosity); $this->doWrite(str_repeat("\x08", $fill), false, $stderr, $verbosity); } if ($newline) { $this->doWrite('', true, $stderr, $verbosity); } if ($stderr) { $this->lastMessageErr = $messages; } else { $this->lastMessage = $messages; } } public function getProgressBar(int $max = 0) { return new ProgressBar($this->getErrorOutput(), $max); } public function ask($question, $default = null) { $helper = $this->helperSet->get('question'); $question = new Question($question, $default); return $helper->ask($this->input, $this->getErrorOutput(), $question); } public function askConfirmation($question, $default = true) { $helper = $this->helperSet->get('question'); $question = new StrictConfirmationQuestion($question, $default); return $helper->ask($this->input, $this->getErrorOutput(), $question); } public function askAndValidate($question, $validator, $attempts = null, $default = null) { $helper = $this->helperSet->get('question'); $question = new Question($question, $default); $question->setValidator($validator); $question->setMaxAttempts($attempts); return $helper->ask($this->input, $this->getErrorOutput(), $question); } public function askAndHideAnswer($question) { $helper = $this->helperSet->get('question'); $question = new Question($question); $question->setHidden(true); return $helper->ask($this->input, $this->getErrorOutput(), $question); } public function select($question, $choices, $default, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false) { $helper = $this->helperSet->get('question'); $question = new ChoiceQuestion($question, $choices, $default); $question->setMaxAttempts($attempts ?: null); $question->setErrorMessage($errorMessage); $question->setMultiselect($multiselect); $result = $helper->ask($this->input, $this->getErrorOutput(), $question); if (!is_array($result)) { return (string) array_search($result, $choices, true); } $results = []; foreach ($choices as $index => $choice) { if (in_array($choice, $result, true)) { $results[] = (string) $index; } } return $results; } public function getTable(): Table { return new Table($this->output); } private function getErrorOutput(): OutputInterface { if ($this->output instanceof ConsoleOutputInterface) { return $this->output->getErrorOutput(); } return $this->output; } } io = $io; $this->config = $config; $this->package = $package; $this->downloadManager = $downloadManager; $this->repositoryManager = $repositoryManager; $this->locker = $locker; $this->installationManager = $installationManager; $this->eventDispatcher = $eventDispatcher; $this->autoloadGenerator = $autoloadGenerator; $this->suggestedPackagesReporter = new SuggestedPackagesReporter($this->io); $this->platformRequirementFilter = PlatformRequirementFilterFactory::ignoreNothing(); $this->writeLock = $config->get('lock'); } public function run(): int { gc_collect_cycles(); gc_disable(); if ($this->updateAllowList && $this->updateMirrors) { throw new \RuntimeException("The installer options updateMirrors and updateAllowList are mutually exclusive."); } $isFreshInstall = $this->repositoryManager->getLocalRepository()->isFresh(); if (!$this->update && !$this->locker->isLocked()) { $this->io->writeError('No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information.'); $this->update = true; } if ($this->dryRun) { $this->verbose = true; $this->runScripts = false; $this->executeOperations = false; $this->writeLock = false; $this->dumpAutoloader = false; $this->mockLocalRepositories($this->repositoryManager); } if ($this->update && !$this->install) { $this->dumpAutoloader = false; } if ($this->runScripts) { Platform::putEnv('COMPOSER_DEV_MODE', $this->devMode ? '1' : '0'); $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD; $this->eventDispatcher->dispatchScript($eventName, $this->devMode); } $this->downloadManager->setPreferSource($this->preferSource); $this->downloadManager->setPreferDist($this->preferDist); $localRepo = $this->repositoryManager->getLocalRepository(); try { if ($this->update) { $res = $this->doUpdate($localRepo, $this->install); } else { $res = $this->doInstall($localRepo); } if ($res !== 0) { return $res; } } catch (\Exception $e) { if ($this->executeOperations && $this->install && $this->config->get('notify-on-install')) { $this->installationManager->notifyInstalls($this->io); } throw $e; } if ($this->executeOperations && $this->install && $this->config->get('notify-on-install')) { $this->installationManager->notifyInstalls($this->io); } if ($this->update) { $installedRepo = new InstalledRepository([ $this->locker->getLockedRepository($this->devMode), $this->createPlatformRepo(false), new RootPackageRepository(clone $this->package), ]); if ($isFreshInstall) { $this->suggestedPackagesReporter->addSuggestionsFromPackage($this->package); } $this->suggestedPackagesReporter->outputMinimalistic($installedRepo); } $lockedRepository = $this->locker->getLockedRepository(true); foreach ($lockedRepository->getPackages() as $package) { if (!$package instanceof CompletePackage || !$package->isAbandoned()) { continue; } $replacement = is_string($package->getReplacementPackage()) ? 'Use ' . $package->getReplacementPackage() . ' instead' : 'No replacement was suggested'; $this->io->writeError( sprintf( "Package %s is abandoned, you should avoid using it. %s.", $package->getPrettyName(), $replacement ) ); } if ($this->dumpAutoloader) { if ($this->optimizeAutoloader) { $this->io->writeError('Generating optimized autoload files'); } else { $this->io->writeError('Generating autoload files'); } $this->autoloadGenerator->setClassMapAuthoritative($this->classMapAuthoritative); $this->autoloadGenerator->setApcu($this->apcuAutoloader, $this->apcuAutoloaderPrefix); $this->autoloadGenerator->setRunScripts($this->runScripts); $this->autoloadGenerator->setPlatformRequirementFilter($this->platformRequirementFilter); $this->autoloadGenerator->dump($this->config, $localRepo, $this->package, $this->installationManager, 'composer', $this->optimizeAutoloader); } if ($this->install && $this->executeOperations) { foreach ($localRepo->getPackages() as $package) { $this->installationManager->ensureBinariesPresence($package); } } $fundingCount = 0; foreach ($localRepo->getPackages() as $package) { if ($package instanceof CompletePackageInterface && !$package instanceof AliasPackage && $package->getFunding()) { $fundingCount++; } } if ($fundingCount > 0) { $this->io->writeError([ sprintf( "%d package%s you are using %s looking for funding.", $fundingCount, 1 === $fundingCount ? '' : 's', 1 === $fundingCount ? 'is' : 'are' ), 'Use the `composer fund` command to find out more!', ]); } if ($this->runScripts) { $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD; $this->eventDispatcher->dispatchScript($eventName, $this->devMode); } if (!defined('HHVM_VERSION')) { gc_enable(); } if ($this->audit) { if ($this->update && !$this->install) { $packages = $lockedRepository->getCanonicalPackages(); $target = 'locked'; } else { $packages = $localRepo->getCanonicalPackages(); $target = 'installed'; } if (count($packages) > 0) { try { $auditor = new Auditor(); $repoSet = new RepositorySet(); foreach ($this->repositoryManager->getRepositories() as $repo) { $repoSet->addRepository($repo); } $auditor->audit($this->io, $repoSet, $packages, $this->auditFormat); } catch (TransportException $e) { $this->io->error('Failed to audit '.$target.' packages.'); if ($this->io->isVerbose()) { $this->io->error('['.get_class($e).'] '.$e->getMessage()); } } } else { $this->io->writeError('No '.$target.' packages - skipping audit.'); } } return 0; } protected function doUpdate(InstalledRepositoryInterface $localRepo, bool $doInstall): int { $platformRepo = $this->createPlatformRepo(true); $aliases = $this->getRootAliases(true); $lockedRepository = null; try { if ($this->locker->isLocked()) { $lockedRepository = $this->locker->getLockedRepository(true); } } catch (\Seld\JsonLint\ParsingException $e) { if ($this->updateAllowList || $this->updateMirrors) { throw $e; } } if (($this->updateAllowList || $this->updateMirrors) && !$lockedRepository) { $this->io->writeError('Cannot update ' . ($this->updateMirrors ? 'lock file information' : 'only a partial set of packages') . ' without a lock file present. Run `composer update` to generate a lock file.', true, IOInterface::QUIET); return self::ERROR_NO_LOCK_FILE_FOR_PARTIAL_UPDATE; } $this->io->writeError('Loading composer repositories with package information'); $policy = $this->createPolicy(true); $repositorySet = $this->createRepositorySet(true, $platformRepo, $aliases); $repositories = $this->repositoryManager->getRepositories(); foreach ($repositories as $repository) { $repositorySet->addRepository($repository); } if ($lockedRepository) { $repositorySet->addRepository($lockedRepository); } $request = $this->createRequest($this->fixedRootPackage, $platformRepo, $lockedRepository); $this->requirePackagesForUpdate($request, $lockedRepository, true); if ($this->updateAllowList) { $request->setUpdateAllowList($this->updateAllowList, $this->updateAllowTransitiveDependencies); } $pool = $repositorySet->createPool($request, $this->io, $this->eventDispatcher, $this->createPoolOptimizer($policy)); $this->io->writeError('Updating dependencies'); $solver = new Solver($policy, $pool, $this->io); try { $lockTransaction = $solver->solve($request, $this->platformRequirementFilter); $ruleSetSize = $solver->getRuleSetSize(); $solver = null; } catch (SolverProblemsException $e) { $err = 'Your requirements could not be resolved to an installable set of packages.'; $prettyProblem = $e->getPrettyString($repositorySet, $request, $pool, $this->io->isVerbose()); $this->io->writeError(''. $err .'', true, IOInterface::QUIET); $this->io->writeError($prettyProblem); if (!$this->devMode) { $this->io->writeError('Running update with --no-dev does not mean require-dev is ignored, it just means the packages will not be installed. If dev requirements are blocking the update you have to resolve those problems.', true, IOInterface::QUIET); } $ghe = new GithubActionError($this->io); $ghe->emit($err."\n".$prettyProblem); return max(self::ERROR_GENERIC_FAILURE, $e->getCode()); } $this->io->writeError("Analyzed ".count($pool)." packages to resolve dependencies", true, IOInterface::VERBOSE); $this->io->writeError("Analyzed ".$ruleSetSize." rules to resolve dependencies", true, IOInterface::VERBOSE); $pool = null; if (!$lockTransaction->getOperations()) { $this->io->writeError('Nothing to modify in lock file'); } $exitCode = $this->extractDevPackages($lockTransaction, $platformRepo, $aliases, $policy, $lockedRepository); if ($exitCode !== 0) { return $exitCode; } if (method_exists('Composer\Semver\CompilingMatcher', 'clear')) { \Composer\Semver\CompilingMatcher::clear(); } $platformReqs = $this->extractPlatformRequirements($this->package->getRequires()); $platformDevReqs = $this->extractPlatformRequirements($this->package->getDevRequires()); $installsUpdates = $uninstalls = []; if ($lockTransaction->getOperations()) { $installNames = $updateNames = $uninstallNames = []; foreach ($lockTransaction->getOperations() as $operation) { if ($operation instanceof InstallOperation) { $installsUpdates[] = $operation; $installNames[] = $operation->getPackage()->getPrettyName().':'.$operation->getPackage()->getFullPrettyVersion(); } elseif ($operation instanceof UpdateOperation) { if ($this->updateMirrors && $operation->getInitialPackage()->getName() === $operation->getTargetPackage()->getName() && $operation->getInitialPackage()->getVersion() === $operation->getTargetPackage()->getVersion() ) { continue; } $installsUpdates[] = $operation; $updateNames[] = $operation->getTargetPackage()->getPrettyName().':'.$operation->getTargetPackage()->getFullPrettyVersion(); } elseif ($operation instanceof UninstallOperation) { $uninstalls[] = $operation; $uninstallNames[] = $operation->getPackage()->getPrettyName(); } } if ($this->config->get('lock')) { $this->io->writeError(sprintf( "Lock file operations: %d install%s, %d update%s, %d removal%s", count($installNames), 1 === count($installNames) ? '' : 's', count($updateNames), 1 === count($updateNames) ? '' : 's', count($uninstalls), 1 === count($uninstalls) ? '' : 's' )); if ($installNames) { $this->io->writeError("Installs: ".implode(', ', $installNames), true, IOInterface::VERBOSE); } if ($updateNames) { $this->io->writeError("Updates: ".implode(', ', $updateNames), true, IOInterface::VERBOSE); } if ($uninstalls) { $this->io->writeError("Removals: ".implode(', ', $uninstallNames), true, IOInterface::VERBOSE); } } } $sortByName = static function ($a, $b): int { if ($a instanceof UpdateOperation) { $a = $a->getTargetPackage()->getName(); } else { $a = $a->getPackage()->getName(); } if ($b instanceof UpdateOperation) { $b = $b->getTargetPackage()->getName(); } else { $b = $b->getPackage()->getName(); } return strcmp($a, $b); }; usort($uninstalls, $sortByName); usort($installsUpdates, $sortByName); foreach (array_merge($uninstalls, $installsUpdates) as $operation) { if ($operation instanceof InstallOperation) { $this->suggestedPackagesReporter->addSuggestionsFromPackage($operation->getPackage()); } if ($this->config->get('lock') && (false === strpos($operation->getOperationType(), 'Alias') || $this->io->isDebug())) { $this->io->writeError(' - ' . $operation->show(true)); } } $updatedLock = $this->locker->setLockData( $lockTransaction->getNewLockPackages(false, $this->updateMirrors), $lockTransaction->getNewLockPackages(true, $this->updateMirrors), $platformReqs, $platformDevReqs, $lockTransaction->getAliases($aliases), $this->package->getMinimumStability(), $this->package->getStabilityFlags(), $this->preferStable || $this->package->getPreferStable(), $this->preferLowest, $this->config->get('platform') ?: [], $this->writeLock && $this->executeOperations ); if ($updatedLock && $this->writeLock && $this->executeOperations) { $this->io->writeError('Writing lock file'); } if ($this->executeOperations && count($lockTransaction->getOperations()) > 0) { $vendorDir = $this->config->get('vendor-dir'); if (is_dir($vendorDir)) { @touch($vendorDir); } } if ($doInstall) { return $this->doInstall($localRepo, true); } return 0; } protected function extractDevPackages(LockTransaction $lockTransaction, PlatformRepository $platformRepo, array $aliases, PolicyInterface $policy, ?LockArrayRepository $lockedRepository = null): int { if (!$this->package->getDevRequires()) { return 0; } $resultRepo = new ArrayRepository([]); $loader = new ArrayLoader(null, true); $dumper = new ArrayDumper(); foreach ($lockTransaction->getNewLockPackages(false) as $pkg) { $resultRepo->addPackage($loader->load($dumper->dump($pkg))); } $repositorySet = $this->createRepositorySet(true, $platformRepo, $aliases); $repositorySet->addRepository($resultRepo); $request = $this->createRequest($this->fixedRootPackage, $platformRepo); $this->requirePackagesForUpdate($request, $lockedRepository, false); $pool = $repositorySet->createPoolWithAllPackages(); $solver = new Solver($policy, $pool, $this->io); try { $nonDevLockTransaction = $solver->solve($request, $this->platformRequirementFilter); $solver = null; } catch (SolverProblemsException $e) { $err = 'Unable to find a compatible set of packages based on your non-dev requirements alone.'; $prettyProblem = $e->getPrettyString($repositorySet, $request, $pool, $this->io->isVerbose(), true); $this->io->writeError(''. $err .'', true, IOInterface::QUIET); $this->io->writeError('Your requirements can be resolved successfully when require-dev packages are present.'); $this->io->writeError('You may need to move packages from require-dev or some of their dependencies to require.'); $this->io->writeError($prettyProblem); $ghe = new GithubActionError($this->io); $ghe->emit($err."\n".$prettyProblem); return $e->getCode(); } $lockTransaction->setNonDevPackages($nonDevLockTransaction); return 0; } protected function doInstall(InstalledRepositoryInterface $localRepo, bool $alreadySolved = false): int { if ($this->config->get('lock')) { $this->io->writeError('Installing dependencies from lock file'.($this->devMode ? ' (including require-dev)' : '').''); } $lockedRepository = $this->locker->getLockedRepository($this->devMode); if (!$alreadySolved) { $this->io->writeError('Verifying lock file contents can be installed on current platform.'); $platformRepo = $this->createPlatformRepo(false); $policy = $this->createPolicy(false); $repositorySet = $this->createRepositorySet(false, $platformRepo, [], $lockedRepository); $repositorySet->addRepository($lockedRepository); $request = $this->createRequest($this->fixedRootPackage, $platformRepo, $lockedRepository); if (!$this->locker->isFresh()) { $this->io->writeError('Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. It is recommended that you run `composer update` or `composer update `.', true, IOInterface::QUIET); } foreach ($lockedRepository->getPackages() as $package) { $request->fixLockedPackage($package); } foreach ($this->locker->getPlatformRequirements($this->devMode) as $link) { $request->requireName($link->getTarget(), $link->getConstraint()); } $pool = $repositorySet->createPool($request, $this->io, $this->eventDispatcher); $solver = new Solver($policy, $pool, $this->io); try { $lockTransaction = $solver->solve($request, $this->platformRequirementFilter); $solver = null; if (0 !== count($lockTransaction->getOperations())) { $this->io->writeError('Your lock file cannot be installed on this system without changes. Please run composer update.', true, IOInterface::QUIET); return self::ERROR_LOCK_FILE_INVALID; } } catch (SolverProblemsException $e) { $err = 'Your lock file does not contain a compatible set of packages. Please run composer update.'; $prettyProblem = $e->getPrettyString($repositorySet, $request, $pool, $this->io->isVerbose()); $this->io->writeError(''. $err .'', true, IOInterface::QUIET); $this->io->writeError($prettyProblem); $ghe = new GithubActionError($this->io); $ghe->emit($err."\n".$prettyProblem); return max(self::ERROR_GENERIC_FAILURE, $e->getCode()); } } $localRepoTransaction = new LocalRepoTransaction($lockedRepository, $localRepo); $this->eventDispatcher->dispatchInstallerEvent(InstallerEvents::PRE_OPERATIONS_EXEC, $this->devMode, $this->executeOperations, $localRepoTransaction); if (!$localRepoTransaction->getOperations()) { $this->io->writeError('Nothing to install, update or remove'); } if ($localRepoTransaction->getOperations()) { $installs = $updates = $uninstalls = []; foreach ($localRepoTransaction->getOperations() as $operation) { if ($operation instanceof InstallOperation) { $installs[] = $operation->getPackage()->getPrettyName().':'.$operation->getPackage()->getFullPrettyVersion(); } elseif ($operation instanceof UpdateOperation) { $updates[] = $operation->getTargetPackage()->getPrettyName().':'.$operation->getTargetPackage()->getFullPrettyVersion(); } elseif ($operation instanceof UninstallOperation) { $uninstalls[] = $operation->getPackage()->getPrettyName(); } } $this->io->writeError(sprintf( "Package operations: %d install%s, %d update%s, %d removal%s", count($installs), 1 === count($installs) ? '' : 's', count($updates), 1 === count($updates) ? '' : 's', count($uninstalls), 1 === count($uninstalls) ? '' : 's' )); if ($installs) { $this->io->writeError("Installs: ".implode(', ', $installs), true, IOInterface::VERBOSE); } if ($updates) { $this->io->writeError("Updates: ".implode(', ', $updates), true, IOInterface::VERBOSE); } if ($uninstalls) { $this->io->writeError("Removals: ".implode(', ', $uninstalls), true, IOInterface::VERBOSE); } } if ($this->executeOperations) { $localRepo->setDevPackageNames($this->locker->getDevPackageNames()); $this->installationManager->execute($localRepo, $localRepoTransaction->getOperations(), $this->devMode, $this->runScripts); } else { foreach ($localRepoTransaction->getOperations() as $operation) { if (false === strpos($operation->getOperationType(), 'Alias') || $this->io->isDebug()) { $this->io->writeError(' - ' . $operation->show(false)); } } } return 0; } protected function createPlatformRepo(bool $forUpdate): PlatformRepository { if ($forUpdate) { $platformOverrides = $this->config->get('platform') ?: []; } else { $platformOverrides = $this->locker->getPlatformOverrides(); } return new PlatformRepository([], $platformOverrides); } private function createRepositorySet(bool $forUpdate, PlatformRepository $platformRepo, array $rootAliases = [], ?RepositoryInterface $lockedRepository = null): RepositorySet { if ($forUpdate) { $minimumStability = $this->package->getMinimumStability(); $stabilityFlags = $this->package->getStabilityFlags(); $requires = array_merge($this->package->getRequires(), $this->package->getDevRequires()); } else { $minimumStability = $this->locker->getMinimumStability(); $stabilityFlags = $this->locker->getStabilityFlags(); $requires = []; foreach ($lockedRepository->getPackages() as $package) { $constraint = new Constraint('=', $package->getVersion()); $constraint->setPrettyString($package->getPrettyVersion()); $requires[$package->getName()] = $constraint; } } $rootRequires = []; foreach ($requires as $req => $constraint) { if ($constraint instanceof Link) { $constraint = $constraint->getConstraint(); } if ($this->platformRequirementFilter->isIgnored($req)) { continue; } elseif ($this->platformRequirementFilter instanceof IgnoreListPlatformRequirementFilter) { $constraint = $this->platformRequirementFilter->filterConstraint($req, $constraint); } $rootRequires[$req] = $constraint; } $this->fixedRootPackage = clone $this->package; $this->fixedRootPackage->setRequires([]); $this->fixedRootPackage->setDevRequires([]); $stabilityFlags[$this->package->getName()] = BasePackage::$stabilities[VersionParser::parseStability($this->package->getVersion())]; $repositorySet = new RepositorySet($minimumStability, $stabilityFlags, $rootAliases, $this->package->getReferences(), $rootRequires, $this->temporaryConstraints); $repositorySet->addRepository(new RootPackageRepository($this->fixedRootPackage)); $repositorySet->addRepository($platformRepo); if ($this->additionalFixedRepository) { $additionalFixedRepositories = $this->additionalFixedRepository; if ($additionalFixedRepositories instanceof CompositeRepository) { $additionalFixedRepositories = $additionalFixedRepositories->getRepositories(); } else { $additionalFixedRepositories = [$additionalFixedRepositories]; } foreach ($additionalFixedRepositories as $additionalFixedRepository) { if ($additionalFixedRepository instanceof InstalledRepository || $additionalFixedRepository instanceof InstalledRepositoryInterface) { $repositorySet->allowInstalledRepositories(); break; } } $repositorySet->addRepository($this->additionalFixedRepository); } return $repositorySet; } private function createPolicy(bool $forUpdate): DefaultPolicy { $preferStable = null; $preferLowest = null; if (!$forUpdate) { $preferStable = $this->locker->getPreferStable(); $preferLowest = $this->locker->getPreferLowest(); } if (null === $preferStable) { $preferStable = $this->preferStable || $this->package->getPreferStable(); } if (null === $preferLowest) { $preferLowest = $this->preferLowest; } return new DefaultPolicy($preferStable, $preferLowest); } private function createRequest(RootPackageInterface $rootPackage, PlatformRepository $platformRepo, ?LockArrayRepository $lockedRepository = null): Request { $request = new Request($lockedRepository); $request->fixPackage($rootPackage); if ($rootPackage instanceof RootAliasPackage) { $request->fixPackage($rootPackage->getAliasOf()); } $fixedPackages = $platformRepo->getPackages(); if ($this->additionalFixedRepository) { $fixedPackages = array_merge($fixedPackages, $this->additionalFixedRepository->getPackages()); } $provided = $rootPackage->getProvides(); foreach ($fixedPackages as $package) { if ($package->getRepository() !== $platformRepo || !isset($provided[$package->getName()]) || !$provided[$package->getName()]->getConstraint()->matches(new Constraint('=', $package->getVersion())) ) { $request->fixPackage($package); } } return $request; } private function requirePackagesForUpdate(Request $request, ?LockArrayRepository $lockedRepository = null, bool $includeDevRequires = true): void { if ($this->updateMirrors) { $excludedPackages = []; if (!$includeDevRequires) { $excludedPackages = array_flip($this->locker->getDevPackageNames()); } foreach ($lockedRepository->getPackages() as $lockedPackage) { if (!$lockedPackage instanceof AliasPackage && !isset($excludedPackages[$lockedPackage->getName()])) { $request->requireName($lockedPackage->getName(), new Constraint('==', $lockedPackage->getVersion())); } } } else { $links = $this->package->getRequires(); if ($includeDevRequires) { $links = array_merge($links, $this->package->getDevRequires()); } foreach ($links as $link) { $request->requireName($link->getTarget(), $link->getConstraint()); } } } private function getRootAliases(bool $forUpdate): array { if ($forUpdate) { $aliases = $this->package->getAliases(); } else { $aliases = $this->locker->getAliases(); } return $aliases; } private function extractPlatformRequirements(array $links): array { $platformReqs = []; foreach ($links as $link) { if (PlatformRepository::isPlatformPackage($link->getTarget())) { $platformReqs[$link->getTarget()] = $link->getPrettyConstraint(); } } return $platformReqs; } private function mockLocalRepositories(RepositoryManager $rm): void { $packages = []; foreach ($rm->getLocalRepository()->getPackages() as $package) { $packages[(string) $package] = clone $package; } foreach ($packages as $key => $package) { if ($package instanceof AliasPackage) { $alias = (string) $package->getAliasOf(); $className = get_class($package); $packages[$key] = new $className($packages[$alias], $package->getVersion(), $package->getPrettyVersion()); } } $rm->setLocalRepository( new InstalledArrayRepository($packages) ); } private function createPoolOptimizer(PolicyInterface $policy): ?PoolOptimizer { if ('0' === Platform::getEnv('COMPOSER_POOL_OPTIMIZER')) { $this->io->write('Pool Optimizer was disabled for debugging purposes.', true, IOInterface::DEBUG); return null; } return new PoolOptimizer($policy); } public static function create(IOInterface $io, Composer $composer): self { return new static( $io, $composer->getConfig(), $composer->getPackage(), $composer->getDownloadManager(), $composer->getRepositoryManager(), $composer->getLocker(), $composer->getInstallationManager(), $composer->getEventDispatcher(), $composer->getAutoloadGenerator() ); } public function setAdditionalFixedRepository(RepositoryInterface $additionalFixedRepository): self { $this->additionalFixedRepository = $additionalFixedRepository; return $this; } public function setTemporaryConstraints(array $constraints): self { $this->temporaryConstraints = $constraints; return $this; } public function setDryRun(bool $dryRun = true): self { $this->dryRun = (bool) $dryRun; return $this; } public function isDryRun(): bool { return $this->dryRun; } public function setPreferSource(bool $preferSource = true): self { $this->preferSource = (bool) $preferSource; return $this; } public function setPreferDist(bool $preferDist = true): self { $this->preferDist = (bool) $preferDist; return $this; } public function setOptimizeAutoloader(bool $optimizeAutoloader): self { $this->optimizeAutoloader = (bool) $optimizeAutoloader; if (!$this->optimizeAutoloader) { $this->setClassMapAuthoritative(false); } return $this; } public function setClassMapAuthoritative(bool $classMapAuthoritative): self { $this->classMapAuthoritative = (bool) $classMapAuthoritative; if ($this->classMapAuthoritative) { $this->setOptimizeAutoloader(true); } return $this; } public function setApcuAutoloader(bool $apcuAutoloader, ?string $apcuAutoloaderPrefix = null): self { $this->apcuAutoloader = $apcuAutoloader; $this->apcuAutoloaderPrefix = $apcuAutoloaderPrefix; return $this; } public function setUpdate(bool $update): self { $this->update = (bool) $update; return $this; } public function setInstall(bool $install): self { $this->install = (bool) $install; return $this; } public function setDevMode(bool $devMode = true): self { $this->devMode = (bool) $devMode; return $this; } public function setDumpAutoloader(bool $dumpAutoloader = true): self { $this->dumpAutoloader = (bool) $dumpAutoloader; return $this; } public function setRunScripts(bool $runScripts = true): self { $this->runScripts = (bool) $runScripts; return $this; } public function setConfig(Config $config): self { $this->config = $config; return $this; } public function setVerbose(bool $verbose = true): self { $this->verbose = (bool) $verbose; return $this; } public function isVerbose(): bool { return $this->verbose; } public function setIgnorePlatformRequirements($ignorePlatformReqs): self { trigger_error('Installer::setIgnorePlatformRequirements is deprecated since Composer 2.2, use setPlatformRequirementFilter instead.', E_USER_DEPRECATED); return $this->setPlatformRequirementFilter(PlatformRequirementFilterFactory::fromBoolOrList($ignorePlatformReqs)); } public function setPlatformRequirementFilter(PlatformRequirementFilterInterface $platformRequirementFilter): self { $this->platformRequirementFilter = $platformRequirementFilter; return $this; } public function setUpdateMirrors(bool $updateMirrors): self { $this->updateMirrors = $updateMirrors; return $this; } public function setUpdateAllowList(array $packages): self { $this->updateAllowList = array_flip(array_map('strtolower', $packages)); return $this; } public function setUpdateAllowTransitiveDependencies(int $updateAllowTransitiveDependencies): self { if (!in_array($updateAllowTransitiveDependencies, [Request::UPDATE_ONLY_LISTED, Request::UPDATE_LISTED_WITH_TRANSITIVE_DEPS_NO_ROOT_REQUIRE, Request::UPDATE_LISTED_WITH_TRANSITIVE_DEPS], true)) { throw new \RuntimeException("Invalid value for updateAllowTransitiveDependencies supplied"); } $this->updateAllowTransitiveDependencies = $updateAllowTransitiveDependencies; return $this; } public function setPreferStable(bool $preferStable = true): self { $this->preferStable = (bool) $preferStable; return $this; } public function setPreferLowest(bool $preferLowest = true): self { $this->preferLowest = (bool) $preferLowest; return $this; } public function setWriteLock(bool $writeLock = true): self { $this->writeLock = (bool) $writeLock; return $this; } public function setExecuteOperations(bool $executeOperations = true): self { $this->executeOperations = (bool) $executeOperations; return $this; } public function setAudit(bool $audit): self { $this->audit = $audit; return $this; } public function setAuditFormat(string $auditFormat): self { $this->auditFormat = $auditFormat; return $this; } public function disablePlugins(): self { $this->installationManager->disablePlugins(); return $this; } public function setSuggestedPackagesReporter(SuggestedPackagesReporter $suggestedPackagesReporter): self { $this->suggestedPackagesReporter = $suggestedPackagesReporter; return $this; } } binDir = $binDir; $this->binCompat = $binCompat; $this->io = $io; $this->filesystem = $filesystem ?: new Filesystem(); $this->vendorDir = $vendorDir; } public function installBinaries(PackageInterface $package, string $installPath, bool $warnOnOverwrite = true): void { $binaries = $this->getBinaries($package); if (!$binaries) { return; } Platform::workaroundFilesystemIssues(); foreach ($binaries as $bin) { $binPath = $installPath.'/'.$bin; if (!file_exists($binPath)) { $this->io->writeError(' Skipped installation of bin '.$bin.' for package '.$package->getName().': file not found in package'); continue; } if (is_dir($binPath)) { $this->io->writeError(' Skipped installation of bin '.$bin.' for package '.$package->getName().': found a directory at that path'); continue; } if (!$this->filesystem->isAbsolutePath($binPath)) { $binPath = realpath($binPath); } $this->initializeBinDir(); $link = $this->binDir.'/'.basename($bin); if (file_exists($link)) { if (!is_link($link)) { if ($warnOnOverwrite) { $this->io->writeError(' Skipped installation of bin '.$bin.' for package '.$package->getName().': name conflicts with an existing file'); } continue; } if (realpath($link) === realpath($binPath)) { $this->filesystem->unlink($link); } } $binCompat = $this->binCompat; if ($binCompat === "auto" && (Platform::isWindows() || Platform::isWindowsSubsystemForLinux())) { $binCompat = 'full'; } if ($binCompat === "full") { $this->installFullBinaries($binPath, $link, $bin, $package); } else { $this->installUnixyProxyBinaries($binPath, $link); } Silencer::call('chmod', $binPath, 0777 & ~umask()); } } public function removeBinaries(PackageInterface $package): void { $this->initializeBinDir(); $binaries = $this->getBinaries($package); if (!$binaries) { return; } foreach ($binaries as $bin) { $link = $this->binDir.'/'.basename($bin); if (is_link($link) || file_exists($link)) { $this->filesystem->unlink($link); } if (is_file($link.'.bat')) { $this->filesystem->unlink($link.'.bat'); } } if (is_dir($this->binDir) && $this->filesystem->isDirEmpty($this->binDir)) { Silencer::call('rmdir', $this->binDir); } } public static function determineBinaryCaller(string $bin): string { if ('.bat' === substr($bin, -4) || '.exe' === substr($bin, -4)) { return 'call'; } $handle = fopen($bin, 'r'); $line = fgets($handle); fclose($handle); if (Preg::isMatch('{^#!/(?:usr/bin/env )?(?:[^/]+/)*(.+)$}m', $line, $match)) { return trim($match[1]); } return 'php'; } protected function getBinaries(PackageInterface $package): array { return $package->getBinaries(); } protected function installFullBinaries(string $binPath, string $link, string $bin, PackageInterface $package): void { if ('.bat' !== substr($binPath, -4)) { $this->installUnixyProxyBinaries($binPath, $link); $link .= '.bat'; if (file_exists($link)) { $this->io->writeError(' Skipped installation of bin '.$bin.'.bat proxy for package '.$package->getName().': a .bat proxy was already installed'); } } if (!file_exists($link)) { file_put_contents($link, $this->generateWindowsProxyCode($binPath, $link)); Silencer::call('chmod', $link, 0777 & ~umask()); } } protected function installUnixyProxyBinaries(string $binPath, string $link): void { file_put_contents($link, $this->generateUnixyProxyCode($binPath, $link)); Silencer::call('chmod', $link, 0777 & ~umask()); } protected function initializeBinDir(): void { $this->filesystem->ensureDirectoryExists($this->binDir); $this->binDir = realpath($this->binDir); } protected function generateWindowsProxyCode(string $bin, string $link): string { $binPath = $this->filesystem->findShortestPath($link, $bin); $caller = self::determineBinaryCaller($bin); if ($caller === 'php') { return "@ECHO OFF\r\n". "setlocal DISABLEDELAYEDEXPANSION\r\n". "SET BIN_TARGET=%~dp0/".trim(ProcessExecutor::escape(basename($link, '.bat')), '"\'')."\r\n". "SET COMPOSER_RUNTIME_BIN_DIR=%~dp0\r\n". "{$caller} \"%BIN_TARGET%\" %*\r\n"; } return "@ECHO OFF\r\n". "setlocal DISABLEDELAYEDEXPANSION\r\n". "SET BIN_TARGET=%~dp0/".trim(ProcessExecutor::escape($binPath), '"\'')."\r\n". "SET COMPOSER_RUNTIME_BIN_DIR=%~dp0\r\n". "{$caller} \"%BIN_TARGET%\" %*\r\n"; } protected function generateUnixyProxyCode(string $bin, string $link): string { $binPath = $this->filesystem->findShortestPath($link, $bin); $binDir = ProcessExecutor::escape(dirname($binPath)); $binFile = basename($binPath); $binContents = file_get_contents($bin); if (Preg::isMatch('{^(#!.*\r?\n)?[\r\n\t ]*<\?php}', $binContents, $match)) { $proxyCode = empty($match[1]) ? '#!/usr/bin/env php' : trim($match[1]); $binPathExported = $this->filesystem->findShortestPathCode($link, $bin, false, true); $streamProxyCode = $streamHint = ''; $globalsCode = '$GLOBALS[\'_composer_bin_dir\'] = __DIR__;'."\n"; $phpunitHack1 = $phpunitHack2 = ''; if ($this->vendorDir) { $globalsCode .= '$GLOBALS[\'_composer_autoload_path\'] = ' . $this->filesystem->findShortestPathCode($link, $this->vendorDir . '/autoload.php', false, true).";\n"; } if ($this->filesystem->normalizePath($bin) === $this->filesystem->normalizePath($this->vendorDir.'/phpunit/phpunit/phpunit')) { $globalsCode .= '$GLOBALS[\'__PHPUNIT_ISOLATION_EXCLUDE_LIST\'] = $GLOBALS[\'__PHPUNIT_ISOLATION_BLACKLIST\'] = array(realpath('.$binPathExported.'));'."\n"; $phpunitHack1 = "'phpvfscomposer://'."; $phpunitHack2 = ' $data = str_replace(\'__DIR__\', var_export(dirname($this->realpath), true), $data); $data = str_replace(\'__FILE__\', var_export($this->realpath, true), $data);'; } if (trim($match[0]) !== 'realpath = realpath(\$opened_path) ?: \$opened_path; \$opened_path = $phpunitHack1\$this->realpath; \$this->handle = fopen(\$this->realpath, \$mode); \$this->position = 0; return (bool) \$this->handle; } public function stream_read(\$count) { \$data = fread(\$this->handle, \$count); if (\$this->position === 0) { \$data = preg_replace('{^#!.*\\r?\\n}', '', \$data); }$phpunitHack2 \$this->position += strlen(\$data); return \$data; } public function stream_cast(\$castAs) { return \$this->handle; } public function stream_close() { fclose(\$this->handle); } public function stream_lock(\$operation) { return \$operation ? flock(\$this->handle, \$operation) : true; } public function stream_seek(\$offset, \$whence) { if (0 === fseek(\$this->handle, \$offset, \$whence)) { \$this->position = ftell(\$this->handle); return true; } return false; } public function stream_tell() { return \$this->position; } public function stream_eof() { return feof(\$this->handle); } public function stream_stat() { return array(); } public function stream_set_option(\$option, \$arg1, \$arg2) { return true; } public function url_stat(\$path, \$flags) { \$path = substr(\$path, 17); if (file_exists(\$path)) { return stat(\$path); } return false; } } } if ( (function_exists('stream_get_wrappers') && in_array('phpvfscomposer', stream_get_wrappers(), true)) || (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper')) ) { include("phpvfscomposer://" . $binPathExported); exit(0); } } STREAMPROXY; } return $proxyCode . "\n" . << /dev/null) if [ -z "\$self" ]; then self="\$selfArg" fi dir=\$(cd "\${self%[/\\\\]*}" > /dev/null; cd $binDir && pwd) if [ -d /proc/cygdrive ]; then case \$(which php) in \$(readlink -n /proc/cygdrive)/*) # We are in Cygwin using Windows php, so the path must be translated dir=\$(cygpath -m "\$dir"); ;; esac fi export COMPOSER_RUNTIME_BIN_DIR="\$(cd "\${self%[/\\\\]*}" > /dev/null; pwd)" # If bash is sourcing this file, we have to source the target as well bashSource="\$BASH_SOURCE" if [ -n "\$bashSource" ]; then if [ "\$bashSource" != "\$0" ]; then source "\${dir}/$binFile" "\$@" return fi fi "\${dir}/$binFile" "\$@" PROXY; } } loop = $loop; $this->io = $io; $this->eventDispatcher = $eventDispatcher; } public function reset(): void { $this->notifiablePackages = []; FileDownloader::$downloadMetadata = []; } public function addInstaller(InstallerInterface $installer): void { array_unshift($this->installers, $installer); $this->cache = []; } public function removeInstaller(InstallerInterface $installer): void { if (false !== ($key = array_search($installer, $this->installers, true))) { array_splice($this->installers, $key, 1); $this->cache = []; } } public function disablePlugins(): void { foreach ($this->installers as $i => $installer) { if (!$installer instanceof PluginInstaller) { continue; } unset($this->installers[$i]); } } public function getInstaller(string $type): InstallerInterface { $type = strtolower($type); if (isset($this->cache[$type])) { return $this->cache[$type]; } foreach ($this->installers as $installer) { if ($installer->supports($type)) { return $this->cache[$type] = $installer; } } throw new \InvalidArgumentException('Unknown installer type: '.$type); } public function isPackageInstalled(InstalledRepositoryInterface $repo, PackageInterface $package): bool { if ($package instanceof AliasPackage) { return $repo->hasPackage($package) && $this->isPackageInstalled($repo, $package->getAliasOf()); } return $this->getInstaller($package->getType())->isInstalled($repo, $package); } public function ensureBinariesPresence(PackageInterface $package): void { try { $installer = $this->getInstaller($package->getType()); } catch (\InvalidArgumentException $e) { return; } if ($installer instanceof BinaryPresenceInterface) { $installer->ensureBinariesPresence($package); } } public function execute(InstalledRepositoryInterface $repo, array $operations, bool $devMode = true, bool $runScripts = true): void { $cleanupPromises = []; $loop = $this->loop; $io = $this->io; $runCleanup = static function () use (&$cleanupPromises, $loop): void { $promises = []; $loop->abortJobs(); foreach ($cleanupPromises as $cleanup) { $promises[] = new \React\Promise\Promise(static function ($resolve, $reject) use ($cleanup): void { $promise = $cleanup(); if (!$promise instanceof PromiseInterface) { $resolve(); } else { $promise->then(static function () use ($resolve): void { $resolve(); }); } }); } if (!empty($promises)) { $loop->wait($promises); } }; $signalHandler = SignalHandler::create([SignalHandler::SIGINT, SignalHandler::SIGTERM, SignalHandler::SIGHUP], static function (string $signal, SignalHandler $handler) use ($io, $runCleanup) { $io->writeError('Received '.$signal.', aborting', true, IOInterface::DEBUG); $runCleanup(); $handler->exitWithLastSignal(); }); try { $batches = []; $batch = []; foreach ($operations as $index => $operation) { if ($operation instanceof UpdateOperation || $operation instanceof InstallOperation) { $package = $operation instanceof UpdateOperation ? $operation->getTargetPackage() : $operation->getPackage(); if ($package->getType() === 'composer-plugin' && ($extra = $package->getExtra()) && isset($extra['plugin-modifies-downloads']) && $extra['plugin-modifies-downloads'] === true) { if ($batch) { $batches[] = $batch; } $batches[] = [$index => $operation]; $batch = []; continue; } } $batch[$index] = $operation; } if ($batch) { $batches[] = $batch; } foreach ($batches as $batch) { $this->downloadAndExecuteBatch($repo, $batch, $cleanupPromises, $devMode, $runScripts, $operations); } } catch (\Exception $e) { $runCleanup(); throw $e; } finally { $signalHandler->unregister(); } $repo->write($devMode, $this); } private function downloadAndExecuteBatch(InstalledRepositoryInterface $repo, array $operations, array &$cleanupPromises, bool $devMode, bool $runScripts, array $allOperations): void { $promises = []; foreach ($operations as $index => $operation) { $opType = $operation->getOperationType(); if (!in_array($opType, ['update', 'install', 'uninstall'])) { continue; } if ($opType === 'update') { $package = $operation->getTargetPackage(); $initialPackage = $operation->getInitialPackage(); } else { $package = $operation->getPackage(); $initialPackage = null; } $installer = $this->getInstaller($package->getType()); $cleanupPromises[$index] = static function () use ($opType, $installer, $package, $initialPackage) { if (!$package->getInstallationSource()) { return; } return $installer->cleanup($opType, $package, $initialPackage); }; if ($opType !== 'uninstall') { $promise = $installer->download($package, $initialPackage); if ($promise) { $promises[] = $promise; } } } if (count($promises)) { $this->waitOnPromises($promises); } $batches = []; $batch = []; foreach ($operations as $index => $operation) { if ($operation instanceof InstallOperation || $operation instanceof UpdateOperation) { $package = $operation instanceof UpdateOperation ? $operation->getTargetPackage() : $operation->getPackage(); if ($package->getType() === 'composer-plugin' || $package->getType() === 'composer-installer') { if ($batch) { $batches[] = $batch; } $batches[] = [$index => $operation]; $batch = []; continue; } } $batch[$index] = $operation; } if ($batch) { $batches[] = $batch; } foreach ($batches as $batch) { $this->executeBatch($repo, $batch, $cleanupPromises, $devMode, $runScripts, $allOperations); } } private function executeBatch(InstalledRepositoryInterface $repo, array $operations, array $cleanupPromises, bool $devMode, bool $runScripts, array $allOperations): void { $promises = []; $postExecCallbacks = []; foreach ($operations as $index => $operation) { $opType = $operation->getOperationType(); if (!in_array($opType, ['update', 'install', 'uninstall'])) { if ($this->io->isDebug()) { $this->io->writeError(' - ' . $operation->show(false)); } $this->{$opType}($repo, $operation); continue; } if ($opType === 'update') { $package = $operation->getTargetPackage(); $initialPackage = $operation->getInitialPackage(); } else { $package = $operation->getPackage(); $initialPackage = null; } $installer = $this->getInstaller($package->getType()); $eventName = [ 'install' => PackageEvents::PRE_PACKAGE_INSTALL, 'update' => PackageEvents::PRE_PACKAGE_UPDATE, 'uninstall' => PackageEvents::PRE_PACKAGE_UNINSTALL, ][$opType] ?? null; if (null !== $eventName && $runScripts && $this->eventDispatcher) { $this->eventDispatcher->dispatchPackageEvent($eventName, $devMode, $repo, $allOperations, $operation); } $dispatcher = $this->eventDispatcher; $io = $this->io; $promise = $installer->prepare($opType, $package, $initialPackage); if (!$promise instanceof PromiseInterface) { $promise = \React\Promise\resolve(null); } $promise = $promise->then(function () use ($opType, $repo, $operation) { return $this->{$opType}($repo, $operation); })->then($cleanupPromises[$index]) ->then(function () use ($devMode, $repo): void { $repo->write($devMode, $this); }, static function ($e) use ($opType, $package, $io): void { $io->writeError(' ' . ucfirst($opType) .' of '.$package->getPrettyName().' failed'); throw $e; }); $eventName = [ 'install' => PackageEvents::POST_PACKAGE_INSTALL, 'update' => PackageEvents::POST_PACKAGE_UPDATE, 'uninstall' => PackageEvents::POST_PACKAGE_UNINSTALL, ][$opType] ?? null; if (null !== $eventName && $runScripts && $dispatcher) { $postExecCallbacks[] = static function () use ($dispatcher, $eventName, $devMode, $repo, $allOperations, $operation): void { $dispatcher->dispatchPackageEvent($eventName, $devMode, $repo, $allOperations, $operation); }; } $promises[] = $promise; } if (count($promises)) { $this->waitOnPromises($promises); } Platform::workaroundFilesystemIssues(); foreach ($postExecCallbacks as $cb) { $cb(); } } private function waitOnPromises(array $promises): void { $progress = null; if ( $this->outputProgress && $this->io instanceof ConsoleIO && !Platform::getEnv('CI') && !$this->io->isDebug() && count($promises) > 1 ) { $progress = $this->io->getProgressBar(); } $this->loop->wait($promises, $progress); if ($progress) { $progress->clear(); if (!$this->io->isDecorated()) { $this->io->writeError(''); } } } public function install(InstalledRepositoryInterface $repo, InstallOperation $operation): ?PromiseInterface { $package = $operation->getPackage(); $installer = $this->getInstaller($package->getType()); $promise = $installer->install($repo, $package); $this->markForNotification($package); return $promise; } public function update(InstalledRepositoryInterface $repo, UpdateOperation $operation): ?PromiseInterface { $initial = $operation->getInitialPackage(); $target = $operation->getTargetPackage(); $initialType = $initial->getType(); $targetType = $target->getType(); if ($initialType === $targetType) { $installer = $this->getInstaller($initialType); $promise = $installer->update($repo, $initial, $target); $this->markForNotification($target); } else { $promise = $this->getInstaller($initialType)->uninstall($repo, $initial); if (!$promise instanceof PromiseInterface) { $promise = \React\Promise\resolve(null); } $installer = $this->getInstaller($targetType); $promise = $promise->then(static function () use ($installer, $repo, $target): PromiseInterface { $promise = $installer->install($repo, $target); if ($promise instanceof PromiseInterface) { return $promise; } return \React\Promise\resolve(null); }); } return $promise; } public function uninstall(InstalledRepositoryInterface $repo, UninstallOperation $operation): ?PromiseInterface { $package = $operation->getPackage(); $installer = $this->getInstaller($package->getType()); return $installer->uninstall($repo, $package); } public function markAliasInstalled(InstalledRepositoryInterface $repo, MarkAliasInstalledOperation $operation): void { $package = $operation->getPackage(); if (!$repo->hasPackage($package)) { $repo->addPackage(clone $package); } } public function markAliasUninstalled(InstalledRepositoryInterface $repo, MarkAliasUninstalledOperation $operation): void { $package = $operation->getPackage(); $repo->removePackage($package); } public function getInstallPath(PackageInterface $package): string { $installer = $this->getInstaller($package->getType()); return $installer->getInstallPath($package); } public function setOutputProgress(bool $outputProgress): void { $this->outputProgress = $outputProgress; } public function notifyInstalls(IOInterface $io): void { $promises = []; try { foreach ($this->notifiablePackages as $repoUrl => $packages) { if (strpos($repoUrl, '%package%')) { foreach ($packages as $package) { $url = str_replace('%package%', $package->getPrettyName(), $repoUrl); $params = [ 'version' => $package->getPrettyVersion(), 'version_normalized' => $package->getVersion(), ]; $opts = [ 'retry-auth-failure' => false, 'http' => [ 'method' => 'POST', 'header' => ['Content-type: application/x-www-form-urlencoded'], 'content' => http_build_query($params, '', '&'), 'timeout' => 3, ], ]; $promises[] = $this->loop->getHttpDownloader()->add($url, $opts); } continue; } $postData = ['downloads' => []]; foreach ($packages as $package) { $packageNotification = [ 'name' => $package->getPrettyName(), 'version' => $package->getVersion(), ]; if (strpos($repoUrl, 'packagist.org/') !== false) { if (isset(FileDownloader::$downloadMetadata[$package->getName()])) { $packageNotification['downloaded'] = FileDownloader::$downloadMetadata[$package->getName()]; } else { $packageNotification['downloaded'] = false; } } $postData['downloads'][] = $packageNotification; } $opts = [ 'retry-auth-failure' => false, 'http' => [ 'method' => 'POST', 'header' => ['Content-Type: application/json'], 'content' => json_encode($postData), 'timeout' => 6, ], ]; $promises[] = $this->loop->getHttpDownloader()->add($repoUrl, $opts); } $this->loop->wait($promises); } catch (\Exception $e) { } $this->reset(); } private function markForNotification(PackageInterface $package): void { if ($package->getNotificationUrl()) { $this->notifiablePackages[$package->getNotificationUrl()][$package->getName()] = $package; } } } composer = $composer; $this->io = $io; $this->devMode = $devMode; $this->executeOperations = $executeOperations; $this->transaction = $transaction; } public function getComposer(): Composer { return $this->composer; } public function getIO(): IOInterface { return $this->io; } public function isDevMode(): bool { return $this->devMode; } public function isExecutingOperations(): bool { return $this->executeOperations; } public function getTransaction(): ?Transaction { return $this->transaction; } } composer = $composer; $this->downloadManager = $composer instanceof Composer ? $composer->getDownloadManager() : null; $this->io = $io; $this->type = $type; $this->filesystem = $filesystem ?: new Filesystem(); $this->vendorDir = rtrim($composer->getConfig()->get('vendor-dir'), '/'); $this->binaryInstaller = $binaryInstaller ?: new BinaryInstaller($this->io, rtrim($composer->getConfig()->get('bin-dir'), '/'), $composer->getConfig()->get('bin-compat'), $this->filesystem, $this->vendorDir); } public function supports(string $packageType) { return $packageType === $this->type || null === $this->type; } public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package) { if (!$repo->hasPackage($package)) { return false; } $installPath = $this->getInstallPath($package); if (Filesystem::isReadable($installPath)) { return true; } if (Platform::isWindows() && $this->filesystem->isJunction($installPath)) { return true; } if (is_link($installPath)) { if (realpath($installPath) === false) { return false; } return true; } return false; } public function download(PackageInterface $package, ?PackageInterface $prevPackage = null) { $this->initializeVendorDir(); $downloadPath = $this->getInstallPath($package); return $this->getDownloadManager()->download($package, $downloadPath, $prevPackage); } public function prepare($type, PackageInterface $package, ?PackageInterface $prevPackage = null) { $this->initializeVendorDir(); $downloadPath = $this->getInstallPath($package); return $this->getDownloadManager()->prepare($type, $package, $downloadPath, $prevPackage); } public function cleanup($type, PackageInterface $package, ?PackageInterface $prevPackage = null) { $this->initializeVendorDir(); $downloadPath = $this->getInstallPath($package); return $this->getDownloadManager()->cleanup($type, $package, $downloadPath, $prevPackage); } public function install(InstalledRepositoryInterface $repo, PackageInterface $package) { $this->initializeVendorDir(); $downloadPath = $this->getInstallPath($package); if (!Filesystem::isReadable($downloadPath) && $repo->hasPackage($package)) { $this->binaryInstaller->removeBinaries($package); } $promise = $this->installCode($package); if (!$promise instanceof PromiseInterface) { $promise = \React\Promise\resolve(null); } $binaryInstaller = $this->binaryInstaller; $installPath = $this->getInstallPath($package); return $promise->then(static function () use ($binaryInstaller, $installPath, $package, $repo): void { $binaryInstaller->installBinaries($package, $installPath); if (!$repo->hasPackage($package)) { $repo->addPackage(clone $package); } }); } public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) { if (!$repo->hasPackage($initial)) { throw new \InvalidArgumentException('Package is not installed: '.$initial); } $this->initializeVendorDir(); $this->binaryInstaller->removeBinaries($initial); $promise = $this->updateCode($initial, $target); if (!$promise instanceof PromiseInterface) { $promise = \React\Promise\resolve(null); } $binaryInstaller = $this->binaryInstaller; $installPath = $this->getInstallPath($target); return $promise->then(static function () use ($binaryInstaller, $installPath, $target, $initial, $repo): void { $binaryInstaller->installBinaries($target, $installPath); $repo->removePackage($initial); if (!$repo->hasPackage($target)) { $repo->addPackage(clone $target); } }); } public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) { if (!$repo->hasPackage($package)) { throw new \InvalidArgumentException('Package is not installed: '.$package); } $promise = $this->removeCode($package); if (!$promise instanceof PromiseInterface) { $promise = \React\Promise\resolve(null); } $binaryInstaller = $this->binaryInstaller; $downloadPath = $this->getPackageBasePath($package); $filesystem = $this->filesystem; return $promise->then(static function () use ($binaryInstaller, $filesystem, $downloadPath, $package, $repo): void { $binaryInstaller->removeBinaries($package); $repo->removePackage($package); if (strpos($package->getName(), '/')) { $packageVendorDir = dirname($downloadPath); if (is_dir($packageVendorDir) && $filesystem->isDirEmpty($packageVendorDir)) { Silencer::call('rmdir', $packageVendorDir); } } }); } public function getInstallPath(PackageInterface $package) { $this->initializeVendorDir(); $basePath = ($this->vendorDir ? $this->vendorDir.'/' : '') . $package->getPrettyName(); $targetDir = $package->getTargetDir(); return $basePath . ($targetDir ? '/'.$targetDir : ''); } public function ensureBinariesPresence(PackageInterface $package) { $this->binaryInstaller->installBinaries($package, $this->getInstallPath($package), false); } protected function getPackageBasePath(PackageInterface $package) { $installPath = $this->getInstallPath($package); $targetDir = $package->getTargetDir(); if ($targetDir) { return Preg::replace('{/*'.str_replace('/', '/+', preg_quote($targetDir)).'/?$}', '', $installPath); } return $installPath; } protected function installCode(PackageInterface $package) { $downloadPath = $this->getInstallPath($package); return $this->getDownloadManager()->install($package, $downloadPath); } protected function updateCode(PackageInterface $initial, PackageInterface $target) { $initialDownloadPath = $this->getInstallPath($initial); $targetDownloadPath = $this->getInstallPath($target); if ($targetDownloadPath !== $initialDownloadPath) { if (strpos($initialDownloadPath, $targetDownloadPath) === 0 || strpos($targetDownloadPath, $initialDownloadPath) === 0 ) { $promise = $this->removeCode($initial); if (!$promise instanceof PromiseInterface) { $promise = \React\Promise\resolve(null); } return $promise->then(function () use ($target): PromiseInterface { $promise = $this->installCode($target); if ($promise instanceof PromiseInterface) { return $promise; } return \React\Promise\resolve(null); }); } $this->filesystem->rename($initialDownloadPath, $targetDownloadPath); } return $this->getDownloadManager()->update($initial, $target, $targetDownloadPath); } protected function removeCode(PackageInterface $package) { $downloadPath = $this->getPackageBasePath($package); return $this->getDownloadManager()->remove($package, $downloadPath); } protected function initializeVendorDir() { $this->filesystem->ensureDirectoryExists($this->vendorDir); $this->vendorDir = realpath($this->vendorDir); } protected function getDownloadManager(): DownloadManager { assert($this->downloadManager instanceof DownloadManager, new \LogicException(self::class.' should be initialized with a fully loaded Composer instance to be able to install/... packages')); return $this->downloadManager; } } io = $io; } public function supports(string $packageType) { return $packageType === 'metapackage'; } public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package) { return $repo->hasPackage($package); } public function download(PackageInterface $package, ?PackageInterface $prevPackage = null) { return \React\Promise\resolve(null); } public function prepare($type, PackageInterface $package, ?PackageInterface $prevPackage = null) { return \React\Promise\resolve(null); } public function cleanup($type, PackageInterface $package, ?PackageInterface $prevPackage = null) { return \React\Promise\resolve(null); } public function install(InstalledRepositoryInterface $repo, PackageInterface $package) { $this->io->writeError(" - " . InstallOperation::format($package)); $repo->addPackage(clone $package); return \React\Promise\resolve(null); } public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) { if (!$repo->hasPackage($initial)) { throw new \InvalidArgumentException('Package is not installed: '.$initial); } $this->io->writeError(" - " . UpdateOperation::format($initial, $target)); $repo->removePackage($initial); $repo->addPackage(clone $target); return \React\Promise\resolve(null); } public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) { if (!$repo->hasPackage($package)) { throw new \InvalidArgumentException('Package is not installed: '.$package); } $this->io->writeError(" - " . UninstallOperation::format($package)); $repo->removePackage($package); return \React\Promise\resolve(null); } public function getInstallPath(PackageInterface $package) { return ''; } } hasPackage($package); } public function download(PackageInterface $package, ?PackageInterface $prevPackage = null) { return \React\Promise\resolve(null); } public function prepare($type, PackageInterface $package, ?PackageInterface $prevPackage = null) { return \React\Promise\resolve(null); } public function cleanup($type, PackageInterface $package, ?PackageInterface $prevPackage = null) { return \React\Promise\resolve(null); } public function install(InstalledRepositoryInterface $repo, PackageInterface $package) { if (!$repo->hasPackage($package)) { $repo->addPackage(clone $package); } return \React\Promise\resolve(null); } public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) { if (!$repo->hasPackage($initial)) { throw new \InvalidArgumentException('Package is not installed: '.$initial); } $repo->removePackage($initial); if (!$repo->hasPackage($target)) { $repo->addPackage(clone $target); } return \React\Promise\resolve(null); } public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) { if (!$repo->hasPackage($package)) { throw new \InvalidArgumentException('Package is not installed: '.$package); } $repo->removePackage($package); return \React\Promise\resolve(null); } public function getInstallPath(PackageInterface $package) { $targetDir = $package->getTargetDir(); return $package->getPrettyName() . ($targetDir ? '/'.$targetDir : ''); } } composer = $composer; $this->io = $io; $this->devMode = $devMode; $this->localRepo = $localRepo; $this->operations = $operations; $this->operation = $operation; } public function getComposer(): Composer { return $this->composer; } public function getIO(): IOInterface { return $this->io; } public function isDevMode(): bool { return $this->devMode; } public function getLocalRepo(): RepositoryInterface { return $this->localRepo; } public function getOperations(): array { return $this->operations; } public function getOperation(): OperationInterface { return $this->operation; } } getPluginManager()->arePluginsDisabled('local')) { $this->getPluginManager()->isPluginAllowed($package->getName(), false); } return parent::prepare($type, $package, $prevPackage); } public function download(PackageInterface $package, ?PackageInterface $prevPackage = null) { $extra = $package->getExtra(); if (empty($extra['class'])) { throw new \UnexpectedValueException('Error while installing '.$package->getPrettyName().', composer-plugin packages should have a class defined in their extra key to be usable.'); } return parent::download($package, $prevPackage); } public function install(InstalledRepositoryInterface $repo, PackageInterface $package) { $promise = parent::install($repo, $package); if (!$promise instanceof PromiseInterface) { $promise = \React\Promise\resolve(null); } return $promise->then(function () use ($package, $repo): void { try { Platform::workaroundFilesystemIssues(); $this->getPluginManager()->registerPackage($package, true); } catch (\Exception $e) { $this->rollbackInstall($e, $repo, $package); } }); } public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) { $promise = parent::update($repo, $initial, $target); if (!$promise instanceof PromiseInterface) { $promise = \React\Promise\resolve(null); } return $promise->then(function () use ($initial, $target, $repo): void { try { Platform::workaroundFilesystemIssues(); $this->getPluginManager()->deactivatePackage($initial); $this->getPluginManager()->registerPackage($target, true); } catch (\Exception $e) { $this->rollbackInstall($e, $repo, $target); } }); } public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) { $this->getPluginManager()->uninstallPackage($package); return parent::uninstall($repo, $package); } private function rollbackInstall(\Exception $e, InstalledRepositoryInterface $repo, PackageInterface $package): void { $this->io->writeError('Plugin initialization failed ('.$e->getMessage().'), uninstalling plugin'); parent::uninstall($repo, $package); throw $e; } protected function getPluginManager(): PluginManager { assert($this->composer instanceof Composer, new \LogicException(self::class.' should be initialized with a fully loaded Composer instance.')); $pluginManager = $this->composer->getPluginManager(); return $pluginManager; } } installPath = rtrim(strtr($installPath, '\\', '/'), '/').'/'; $this->downloadManager = $dm; $this->filesystem = $fs; } public function supports(string $packageType): bool { return true; } public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package): bool { return false; } public function download(PackageInterface $package, ?PackageInterface $prevPackage = null): ?PromiseInterface { $installPath = $this->installPath; if (file_exists($installPath) && !$this->filesystem->isDirEmpty($installPath)) { throw new \InvalidArgumentException("Project directory $installPath is not empty."); } if (!is_dir($installPath)) { mkdir($installPath, 0777, true); } return $this->downloadManager->download($package, $installPath, $prevPackage); } public function prepare($type, PackageInterface $package, ?PackageInterface $prevPackage = null): ?PromiseInterface { return $this->downloadManager->prepare($type, $package, $this->installPath, $prevPackage); } public function cleanup($type, PackageInterface $package, ?PackageInterface $prevPackage = null): ?PromiseInterface { return $this->downloadManager->cleanup($type, $package, $this->installPath, $prevPackage); } public function install(InstalledRepositoryInterface $repo, PackageInterface $package): ?PromiseInterface { return $this->downloadManager->install($package, $this->installPath); } public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target): ?PromiseInterface { throw new \InvalidArgumentException("not supported"); } public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package): ?PromiseInterface { throw new \InvalidArgumentException("not supported"); } public function getInstallPath(PackageInterface $package): string { return $this->installPath; } } io = $io; } public function getPackages(): array { return $this->suggestedPackages; } public function addPackage(string $source, string $target, string $reason): SuggestedPackagesReporter { $this->suggestedPackages[] = [ 'source' => $source, 'target' => $target, 'reason' => $reason, ]; return $this; } public function addSuggestionsFromPackage(PackageInterface $package): SuggestedPackagesReporter { $source = $package->getPrettyName(); foreach ($package->getSuggests() as $target => $reason) { $this->addPackage( $source, $target, $reason ); } return $this; } public function output(int $mode, ?InstalledRepository $installedRepo = null, ?PackageInterface $onlyDependentsOf = null): void { $suggestedPackages = $this->getFilteredSuggestions($installedRepo, $onlyDependentsOf); $suggesters = []; $suggested = []; foreach ($suggestedPackages as $suggestion) { $suggesters[$suggestion['source']][$suggestion['target']] = $suggestion['reason']; $suggested[$suggestion['target']][$suggestion['source']] = $suggestion['reason']; } ksort($suggesters); ksort($suggested); if ($mode & self::MODE_LIST) { foreach (array_keys($suggested) as $name) { $this->io->write(sprintf('%s', $name)); } return; } if ($mode & self::MODE_BY_PACKAGE) { foreach ($suggesters as $suggester => $suggestions) { $this->io->write(sprintf('%s suggests:', $suggester)); foreach ($suggestions as $suggestion => $reason) { $this->io->write(sprintf(' - %s' . ($reason ? ': %s' : ''), $suggestion, $this->escapeOutput($reason))); } $this->io->write(''); } } if ($mode & self::MODE_BY_SUGGESTION) { if ($mode & self::MODE_BY_PACKAGE) { $this->io->write(str_repeat('-', 78)); } foreach ($suggested as $suggestion => $suggesters) { $this->io->write(sprintf('%s is suggested by:', $suggestion)); foreach ($suggesters as $suggester => $reason) { $this->io->write(sprintf(' - %s' . ($reason ? ': %s' : ''), $suggester, $this->escapeOutput($reason))); } $this->io->write(''); } } if ($onlyDependentsOf) { $allSuggestedPackages = $this->getFilteredSuggestions($installedRepo); $diff = count($allSuggestedPackages) - count($suggestedPackages); if ($diff) { $this->io->write(''.$diff.' additional suggestions by transitive dependencies can be shown with --all'); } } } public function outputMinimalistic(?InstalledRepository $installedRepo = null, ?PackageInterface $onlyDependentsOf = null): void { $suggestedPackages = $this->getFilteredSuggestions($installedRepo, $onlyDependentsOf); if ($suggestedPackages) { $this->io->writeError(''.count($suggestedPackages).' package suggestions were added by new dependencies, use `composer suggest` to see details.'); } } private function getFilteredSuggestions(?InstalledRepository $installedRepo = null, ?PackageInterface $onlyDependentsOf = null): array { $suggestedPackages = $this->getPackages(); $installedNames = []; if (null !== $installedRepo && !empty($suggestedPackages)) { foreach ($installedRepo->getPackages() as $package) { $installedNames = array_merge( $installedNames, $package->getNames() ); } } $sourceFilter = []; if ($onlyDependentsOf) { $sourceFilter = array_map(static function ($link): string { return $link->getTarget(); }, array_merge($onlyDependentsOf->getRequires(), $onlyDependentsOf->getDevRequires())); $sourceFilter[] = $onlyDependentsOf->getName(); } $suggestions = []; foreach ($suggestedPackages as $suggestion) { if (in_array($suggestion['target'], $installedNames) || ($sourceFilter && !in_array($suggestion['source'], $sourceFilter))) { continue; } $suggestions[] = $suggestion; } return $suggestions; } private function escapeOutput(string $string): string { return OutputFormatter::escape( $this->removeControlCharacters($string) ); } private function removeControlCharacters(string $string): string { return Preg::replace( '/[[:cntrl:]]/', '', str_replace("\n", ' ', $string) ); } } path = $path; if (null === $httpDownloader && Preg::isMatch('{^https?://}i', $path)) { throw new \InvalidArgumentException('http urls require a HttpDownloader instance to be passed'); } $this->httpDownloader = $httpDownloader; $this->io = $io; } public function getPath(): string { return $this->path; } public function exists(): bool { return is_file($this->path); } public function read() { try { if ($this->httpDownloader) { $json = $this->httpDownloader->get($this->path)->getBody(); } else { if ($this->io && $this->io->isDebug()) { $realpathInfo = ''; $realpath = realpath($this->path); if (false !== $realpath && $realpath !== $this->path) { $realpathInfo = ' (' . $realpath . ')'; } $this->io->writeError('Reading ' . $this->path . $realpathInfo); } $json = file_get_contents($this->path); } } catch (TransportException $e) { throw new \RuntimeException($e->getMessage(), 0, $e); } catch (\Exception $e) { throw new \RuntimeException('Could not read '.$this->path."\n\n".$e->getMessage()); } if ($json === false) { throw new \RuntimeException('Could not read '.$this->path); } return static::parseJson($json, $this->path); } public function write(array $hash, int $options = JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) { if ($this->path === 'php://memory') { file_put_contents($this->path, static::encode($hash, $options)); return; } $dir = dirname($this->path); if (!is_dir($dir)) { if (file_exists($dir)) { throw new \UnexpectedValueException( realpath($dir).' exists and is not a directory.' ); } if (!@mkdir($dir, 0777, true)) { throw new \UnexpectedValueException( $dir.' does not exist and could not be created.' ); } } $retries = 3; while ($retries--) { try { $this->filePutContentsIfModified($this->path, static::encode($hash, $options). ($options & JSON_PRETTY_PRINT ? "\n" : '')); break; } catch (\Exception $e) { if ($retries > 0) { usleep(500000); continue; } throw $e; } } } private function filePutContentsIfModified(string $path, string $content) { $currentContent = @file_get_contents($path); if (false === $currentContent || $currentContent !== $content) { return file_put_contents($path, $content); } return 0; } public function validateSchema(int $schema = self::STRICT_SCHEMA, ?string $schemaFile = null): bool { $content = file_get_contents($this->path); $data = json_decode($content); if (null === $data && 'null' !== $content) { self::validateSyntax($content, $this->path); } return self::validateJsonSchema($this->path, $data, $schema, $schemaFile); } public static function validateJsonSchema(string $source, $data, int $schema, ?string $schemaFile = null): bool { $isComposerSchemaFile = false; if (null === $schemaFile) { $isComposerSchemaFile = true; $schemaFile = self::COMPOSER_SCHEMA_PATH; } if (false === strpos($schemaFile, '://')) { $schemaFile = 'file://' . $schemaFile; } $schemaData = (object) ['$ref' => $schemaFile]; if ($schema === self::LAX_SCHEMA) { $schemaData->additionalProperties = true; $schemaData->required = []; } elseif ($schema === self::STRICT_SCHEMA && $isComposerSchemaFile) { $schemaData->additionalProperties = false; $schemaData->required = ['name', 'description']; } elseif ($schema === self::AUTH_SCHEMA && $isComposerSchemaFile) { $schemaData = (object) ['$ref' => $schemaFile.'#/properties/config', '$schema' => "https://json-schema.org/draft-04/schema#"]; } $validator = new Validator(); $validator->check($data, $schemaData); if (!$validator->isValid()) { $errors = []; foreach ((array) $validator->getErrors() as $error) { $errors[] = ($error['property'] ? $error['property'].' : ' : '').$error['message']; } throw new JsonValidationException('"'.$source.'" does not match the expected JSON schema', $errors); } return true; } public static function encode($data, int $options = 448) { $json = json_encode($data, $options); if (false === $json) { self::throwEncodeError(json_last_error()); } return $json; } private static function throwEncodeError(int $code): void { switch ($code) { case JSON_ERROR_DEPTH: $msg = 'Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: $msg = 'Underflow or the modes mismatch'; break; case JSON_ERROR_CTRL_CHAR: $msg = 'Unexpected control character found'; break; case JSON_ERROR_UTF8: $msg = 'Malformed UTF-8 characters, possibly incorrectly encoded'; break; default: $msg = 'Unknown error'; } throw new \RuntimeException('JSON encoding failed: '.$msg); } public static function parseJson(?string $json, ?string $file = null) { if (null === $json) { return null; } $data = json_decode($json, true); if (null === $data && JSON_ERROR_NONE !== json_last_error()) { self::validateSyntax($json, $file); } return $data; } protected static function validateSyntax(string $json, ?string $file = null): bool { $parser = new JsonParser(); $result = $parser->lint($json); if (null === $result) { if (defined('JSON_ERROR_UTF8') && JSON_ERROR_UTF8 === json_last_error()) { throw new \UnexpectedValueException('"'.$file.'" is not UTF-8, could not parse as JSON'); } return true; } throw new ParsingException('"'.$file.'" does not contain valid JSON'."\n".$result->getMessage(), $result->getDetails()); } } = $code) { return $match[0]; } return str_repeat('\\', $l - 1) . mb_convert_encoding( pack('H*', $match[2]), 'UTF-8', 'UCS-2BE' ); } return $match[0]; }, $buffer); } $result .= $buffer.$char; $buffer = ''; continue; } if (':' === $char) { $char .= ' '; } elseif ('}' === $char || ']' === $char) { $pos--; $prevChar = substr($json, $i - 1, 1); if ('{' !== $prevChar && '[' !== $prevChar) { $result .= $newLine; $result .= str_repeat($indentStr, $pos); } else { $result = rtrim($result); } } $result .= $char; if (',' === $char || '{' === $char || '[' === $char) { $result .= $newLine; if ('{' === $char || '[' === $char) { $pos++; } $result .= str_repeat($indentStr, $pos); } } return $result; } } -? (?= [1-9]|0(?!\d) ) \d++ (\.\d++)? ([eE] [+-]?+ \d++)? ) (? true | false | null ) (? " ([^"\\\\]*+ | \\\\ ["\\\\bfnrt\/] | \\\\ u [0-9A-Fa-f]{4} )* " ) (? \[ (?: (?&json) \s*+ (?: , (?&json) \s*+ )*+ )?+ \s*+ \] ) (? \s*+ (?&string) \s*+ : (?&json) \s*+ ) (? \{ (?: (?&pair) (?: , (?&pair) )*+ )?+ \s*+ \} ) (? \s*+ (?: (?&number) | (?&boolean) | (?&string) | (?&array) | (?&object) ) ) )'; private $contents; private $newline; private $indent; public function __construct(string $contents) { $contents = trim($contents); if ($contents === '') { $contents = '{}'; } if (!Preg::isMatch('#^\{(.*)\}$#s', $contents)) { throw new \InvalidArgumentException('The json file must be an object ({})'); } $this->newline = false !== strpos($contents, "\r\n") ? "\r\n" : "\n"; $this->contents = $contents === '{}' ? '{' . $this->newline . '}' : $contents; $this->detectIndenting(); } public function getContents(): string { return $this->contents . $this->newline; } public function addLink(string $type, string $package, string $constraint, bool $sortPackages = false): bool { $decoded = JsonFile::parseJson($this->contents); if (!isset($decoded[$type])) { return $this->addMainKey($type, [$package => $constraint]); } $regex = '{'.self::$DEFINES.'^(?P\s*\{\s*(?:(?&string)\s*:\s*(?&json)\s*,\s*)*?)'. '(?P'.preg_quote(JsonFile::encode($type)).'\s*:\s*)(?P(?&json))(?P.*)}sx'; if (!Preg::isMatch($regex, $this->contents, $matches)) { return false; } $links = $matches['value']; $packageRegex = str_replace('/', '\\\\?/', preg_quote($package)); $regex = '{'.self::$DEFINES.'"(?P'.$packageRegex.')"(\s*:\s*)(?&string)}ix'; if (Preg::isMatch($regex, $links, $packageMatches)) { $existingPackage = $packageMatches['package']; $packageRegex = str_replace('/', '\\\\?/', preg_quote($existingPackage)); $links = Preg::replaceCallback('{'.self::$DEFINES.'"'.$packageRegex.'"(?P\s*:\s*)(?&string)}ix', static function ($m) use ($existingPackage, $constraint): string { return JsonFile::encode(str_replace('\\/', '/', $existingPackage)) . $m['separator'] . '"' . $constraint . '"'; }, $links); } else { if (Preg::isMatch('#^\s*\{\s*\S+.*?(\s*\}\s*)$#s', $links, $match)) { $links = Preg::replace( '{'.preg_quote($match[1]).'$}', addcslashes(',' . $this->newline . $this->indent . $this->indent . JsonFile::encode($package).': '.JsonFile::encode($constraint) . $match[1], '\\$'), $links ); } else { $links = '{' . $this->newline . $this->indent . $this->indent . JsonFile::encode($package).': '.JsonFile::encode($constraint) . $this->newline . $this->indent . '}'; } } if (true === $sortPackages) { $requirements = json_decode($links, true); $this->sortPackages($requirements); $links = $this->format($requirements); } $this->contents = $matches['start'] . $matches['property'] . $links . $matches['end']; return true; } private function sortPackages(array &$packages = []): void { $prefix = static function ($requirement): string { if (PlatformRepository::isPlatformPackage($requirement)) { return Preg::replace( [ '/^php/', '/^hhvm/', '/^ext/', '/^lib/', '/^\D/', ], [ '0-$0', '1-$0', '2-$0', '3-$0', '4-$0', ], $requirement ); } return '5-'.$requirement; }; uksort($packages, static function ($a, $b) use ($prefix): int { return strnatcmp($prefix($a), $prefix($b)); }); } public function addRepository(string $name, $config, bool $append = true): bool { return $this->addSubNode('repositories', $name, $config, $append); } public function removeRepository(string $name): bool { return $this->removeSubNode('repositories', $name); } public function addConfigSetting(string $name, $value): bool { return $this->addSubNode('config', $name, $value); } public function removeConfigSetting(string $name): bool { return $this->removeSubNode('config', $name); } public function addProperty(string $name, $value): bool { if (strpos($name, 'suggest.') === 0) { return $this->addSubNode('suggest', substr($name, 8), $value); } if (strpos($name, 'extra.') === 0) { return $this->addSubNode('extra', substr($name, 6), $value); } if (strpos($name, 'scripts.') === 0) { return $this->addSubNode('scripts', substr($name, 8), $value); } return $this->addMainKey($name, $value); } public function removeProperty(string $name): bool { if (strpos($name, 'suggest.') === 0) { return $this->removeSubNode('suggest', substr($name, 8)); } if (strpos($name, 'extra.') === 0) { return $this->removeSubNode('extra', substr($name, 6)); } if (strpos($name, 'scripts.') === 0) { return $this->removeSubNode('scripts', substr($name, 8)); } return $this->removeMainKey($name); } public function addSubNode(string $mainNode, string $name, $value, bool $append = true): bool { $decoded = JsonFile::parseJson($this->contents); $subName = null; if (in_array($mainNode, ['config', 'extra', 'scripts']) && false !== strpos($name, '.')) { [$name, $subName] = explode('.', $name, 2); } if (!isset($decoded[$mainNode])) { if ($subName !== null) { $this->addMainKey($mainNode, [$name => [$subName => $value]]); } else { $this->addMainKey($mainNode, [$name => $value]); } return true; } $nodeRegex = '{'.self::$DEFINES.'^(?P \s* \{ \s* (?: (?&string) \s* : (?&json) \s* , \s* )*?'. preg_quote(JsonFile::encode($mainNode)).'\s*:\s*)(?P(?&object))(?P.*)}sx'; try { if (!Preg::isMatch($nodeRegex, $this->contents, $match)) { return false; } } catch (\RuntimeException $e) { if ($e->getCode() === PREG_BACKTRACK_LIMIT_ERROR) { return false; } throw $e; } $children = $match['content']; if (!@json_decode($children)) { return false; } $childRegex = '{'.self::$DEFINES.'(?P"'.preg_quote($name).'"\s*:\s*)(?P(?&json))(?P,?)}x'; if (Preg::isMatch($childRegex, $children, $matches)) { $children = Preg::replaceCallback($childRegex, function ($matches) use ($subName, $value): string { if ($subName !== null) { $curVal = json_decode($matches['content'], true); if (!is_array($curVal)) { $curVal = []; } $curVal[$subName] = $value; $value = $curVal; } return $matches['start'] . $this->format($value, 1) . $matches['end']; }, $children); } else { Preg::match('#^{ (?P\s*?) (?P\S+.*?)? (?P\s*) }$#sx', $children, $match); $whitespace = ''; if (!empty($match['trailingspace'])) { $whitespace = $match['trailingspace']; } if (!empty($match['content'])) { if ($subName !== null) { $value = [$subName => $value]; } if ($append) { $children = Preg::replace( '#'.$whitespace.'}$#', addcslashes(',' . $this->newline . $this->indent . $this->indent . JsonFile::encode($name).': '.$this->format($value, 1) . $whitespace . '}', '\\$'), $children ); } else { $whitespace = ''; if (!empty($match['leadingspace'])) { $whitespace = $match['leadingspace']; } $children = Preg::replace( '#^{'.$whitespace.'#', addcslashes('{' . $whitespace . JsonFile::encode($name).': '.$this->format($value, 1) . ',' . $this->newline . $this->indent . $this->indent, '\\$'), $children ); } } else { if ($subName !== null) { $value = [$subName => $value]; } $children = '{' . $this->newline . $this->indent . $this->indent . JsonFile::encode($name).': '.$this->format($value, 1) . $whitespace . '}'; } } $this->contents = Preg::replaceCallback($nodeRegex, static function ($m) use ($children): string { return $m['start'] . $children . $m['end']; }, $this->contents); return true; } public function removeSubNode(string $mainNode, string $name): bool { $decoded = JsonFile::parseJson($this->contents); if (empty($decoded[$mainNode])) { return true; } $nodeRegex = '{'.self::$DEFINES.'^(?P \s* \{ \s* (?: (?&string) \s* : (?&json) \s* , \s* )*?'. preg_quote(JsonFile::encode($mainNode)).'\s*:\s*)(?P(?&object))(?P.*)}sx'; try { if (!Preg::isMatch($nodeRegex, $this->contents, $match)) { return false; } } catch (\RuntimeException $e) { if ($e->getCode() === PREG_BACKTRACK_LIMIT_ERROR) { return false; } throw $e; } $children = $match['content']; if (!@json_decode($children, true)) { return false; } $subName = null; if (in_array($mainNode, ['config', 'extra', 'scripts']) && false !== strpos($name, '.')) { [$name, $subName] = explode('.', $name, 2); } if (!isset($decoded[$mainNode][$name]) || ($subName && !isset($decoded[$mainNode][$name][$subName]))) { return true; } $keyRegex = str_replace('/', '\\\\?/', preg_quote($name)); if (Preg::isMatch('{"'.$keyRegex.'"\s*:}i', $children)) { if (Preg::isMatchAll('{'.self::$DEFINES.'"'.$keyRegex.'"\s*:\s*(?:(?&json))}x', $children, $matches)) { $bestMatch = ''; foreach ($matches[0] as $match) { if (strlen($bestMatch) < strlen($match)) { $bestMatch = $match; } } $childrenClean = Preg::replace('{,\s*'.preg_quote($bestMatch).'}i', '', $children, -1, $count); if (1 !== $count) { $childrenClean = Preg::replace('{'.preg_quote($bestMatch).'\s*,?\s*}i', '', $childrenClean, -1, $count); if (1 !== $count) { return false; } } } } else { $childrenClean = $children; } if (!isset($childrenClean)) { throw new \InvalidArgumentException("JsonManipulator: \$childrenClean is not defined. Please report at https://github.com/composer/composer/issues/new."); } unset($match); Preg::match('#^{ \s*? (?P\S+.*?)? (?P\s*) }$#sx', $childrenClean, $match); if (empty($match['content'])) { $newline = $this->newline; $indent = $this->indent; $this->contents = Preg::replaceCallback($nodeRegex, static function ($matches) use ($indent, $newline): string { return $matches['start'] . '{' . $newline . $indent . '}' . $matches['end']; }, $this->contents); if ($subName !== null) { $curVal = json_decode($children, true); unset($curVal[$name][$subName]); $this->addSubNode($mainNode, $name, $curVal[$name]); } return true; } $this->contents = Preg::replaceCallback($nodeRegex, function ($matches) use ($name, $subName, $childrenClean): string { if ($subName !== null) { $curVal = json_decode($matches['content'], true); unset($curVal[$name][$subName]); $childrenClean = $this->format($curVal); } return $matches['start'] . $childrenClean . $matches['end']; }, $this->contents); return true; } public function addMainKey(string $key, $content): bool { $decoded = JsonFile::parseJson($this->contents); $content = $this->format($content); $regex = '{'.self::$DEFINES.'^(?P\s*\{\s*(?:(?&string)\s*:\s*(?&json)\s*,\s*)*?)'. '(?P'.preg_quote(JsonFile::encode($key)).'\s*:\s*(?&json))(?P.*)}sx'; if (isset($decoded[$key]) && Preg::isMatch($regex, $this->contents, $matches)) { if (!@json_decode('{'.$matches['key'].'}')) { return false; } $this->contents = $matches['start'] . JsonFile::encode($key).': '.$content . $matches['end']; return true; } if (Preg::isMatch('#[^{\s](\s*)\}$#', $this->contents, $match)) { $this->contents = Preg::replace( '#'.$match[1].'\}$#', addcslashes(',' . $this->newline . $this->indent . JsonFile::encode($key). ': '. $content . $this->newline . '}', '\\$'), $this->contents ); return true; } $this->contents = Preg::replace( '#\}$#', addcslashes($this->indent . JsonFile::encode($key). ': '.$content . $this->newline . '}', '\\$'), $this->contents ); return true; } public function removeMainKey(string $key): bool { $decoded = JsonFile::parseJson($this->contents); if (!array_key_exists($key, $decoded)) { return true; } $regex = '{'.self::$DEFINES.'^(?P\s*\{\s*(?:(?&string)\s*:\s*(?&json)\s*,\s*)*?)'. '(?P'.preg_quote(JsonFile::encode($key)).'\s*:\s*(?&json))\s*,?\s*(?P.*)}sx'; if (Preg::isMatch($regex, $this->contents, $matches)) { if (!@json_decode('{'.$matches['removal'].'}')) { return false; } if (Preg::isMatch('#,\s*$#', $matches['start']) && Preg::isMatch('#^\}$#', $matches['end'])) { $matches['start'] = rtrim(Preg::replace('#,(\s*)$#', '$1', $matches['start']), $this->indent); } $this->contents = $matches['start'] . $matches['end']; if (Preg::isMatch('#^\{\s*\}\s*$#', $this->contents)) { $this->contents = "{\n}"; } return true; } return false; } public function removeMainKeyIfEmpty(string $key): bool { $decoded = JsonFile::parseJson($this->contents); if (!array_key_exists($key, $decoded)) { return true; } if (is_array($decoded[$key]) && count($decoded[$key]) === 0) { return $this->removeMainKey($key); } return true; } public function format($data, int $depth = 0): string { if (is_array($data)) { reset($data); if (is_numeric(key($data))) { foreach ($data as $key => $val) { $data[$key] = $this->format($val, $depth + 1); } return '['.implode(', ', $data).']'; } $out = '{' . $this->newline; $elems = []; foreach ($data as $key => $val) { $elems[] = str_repeat($this->indent, $depth + 2) . JsonFile::encode($key). ': '.$this->format($val, $depth + 1); } return $out . implode(','.$this->newline, $elems) . $this->newline . str_repeat($this->indent, $depth + 1) . '}'; } return JsonFile::encode($data); } protected function detectIndenting(): void { if (Preg::isMatch('{^([ \t]+)"}m', $this->contents, $match)) { $this->indent = $match[1]; } else { $this->indent = ' '; } } } errors = $errors; parent::__construct((string) $message, 0, $previous); } public function getErrors(): array { return $this->errors; } } $conf) { $type = $this->parseType($conf, $prop); $this->properties[$prop] = $type; } } public function getClass(): string { return Config::class; } public function isMethodSupported(MethodReflection $methodReflection): bool { return strtolower($methodReflection->getName()) === 'get'; } public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type { $args = $methodCall->getArgs(); if (count($args) < 1) { return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); } $keyType = $scope->getType($args[0]->value); if ($keyType instanceof ConstantStringType) { if (isset($this->properties[$keyType->getValue()])) { return $this->properties[$keyType->getValue()]; } } return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); } private function parseType(array $def, string $path): Type { if (isset($def['type'])) { $types = []; foreach ((array) $def['type'] as $type) { switch ($type) { case 'integer': if (in_array($path, ['process-timeout', 'cache-ttl', 'cache-files-ttl', 'cache-files-maxsize'], true)) { $types[] = IntegerRangeType::createAllGreaterThanOrEqualTo(0); } else { $types[] = new IntegerType(); } break; case 'string': if ($path === 'cache-files-maxsize') { } elseif ($path === 'discard-changes') { $types[] = new ConstantStringType('stash'); } elseif ($path === 'use-parent-dir') { $types[] = new ConstantStringType('prompt'); } elseif ($path === 'store-auths') { $types[] = new ConstantStringType('prompt'); } elseif ($path === 'platform-check') { $types[] = new ConstantStringType('php-only'); } elseif ($path === 'github-protocols') { $types[] = new UnionType([new ConstantStringType('git'), new ConstantStringType('https'), new ConstantStringType('ssh'), new ConstantStringType('http')]); } elseif (str_starts_with($path, 'preferred-install')) { $types[] = new UnionType([new ConstantStringType('source'), new ConstantStringType('dist'), new ConstantStringType('auto')]); } else { $types[] = new StringType(); } break; case 'boolean': if ($path === 'platform.additionalProperties') { $types[] = new ConstantBooleanType(false); } else { $types[] = new BooleanType(); } break; case 'object': $addlPropType = null; if (isset($def['additionalProperties'])) { $addlPropType = $this->parseType($def['additionalProperties'], $path.'.additionalProperties'); } if (isset($def['properties'])) { $keyNames = []; $valTypes = []; $optionalKeys = []; $propIndex = 0; foreach ($def['properties'] as $propName => $propdef) { $keyNames[] = new ConstantStringType($propName); $valType = $this->parseType($propdef, $path.'.'.$propName); if (!isset($def['required']) || !in_array($propName, $def['required'], true)) { $valType = TypeCombinator::addNull($valType); $optionalKeys[] = $propIndex; } $valTypes[] = $valType; $propIndex++; } if ($addlPropType !== null) { $types[] = new ArrayType(TypeCombinator::union(new StringType(), ...$keyNames), TypeCombinator::union($addlPropType, ...$valTypes)); } else { $types[] = new ConstantArrayType($keyNames, $valTypes, [0], $optionalKeys); } } else { $types[] = new ArrayType(new StringType(), $addlPropType ?? new MixedType()); } break; case 'array': if (isset($def['items'])) { $valType = $this->parseType($def['items'], $path.'.items'); } else { $valType = new MixedType(); } $types[] = new ArrayType(new IntegerType(), $valType); break; default: $types[] = new MixedType(); } } $type = TypeCombinator::union(...$types); } elseif (isset($def['enum'])) { $type = TypeCombinator::union(...array_map(static function (string $value): ConstantStringType { return new ConstantStringType($value); }, $def['enum'])); } else { $type = new MixedType(); } if ($path === 'allow-plugins' && time() < strtotime('2022-07-01')) { $type = TypeCombinator::addNull($type); } if (in_array($path, ['autoloader-suffix', 'gitlab-protocol'], true)) { $type = TypeCombinator::addNull($type); } return $type; } } getName()) === 'getreasondata'; } public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type { $reasonType = $scope->getType(new MethodCall($methodCall->var, new Identifier('getReason'))); $types = [ Rule::RULE_ROOT_REQUIRE => new ConstantArrayType([new ConstantStringType('packageName'), new ConstantStringType('constraint')], [new StringType, new ObjectType(ConstraintInterface::class)]), Rule::RULE_FIXED => new ConstantArrayType([new ConstantStringType('package')], [new ObjectType(BasePackage::class)]), Rule::RULE_PACKAGE_CONFLICT => new ObjectType(Link::class), Rule::RULE_PACKAGE_REQUIRES => new ObjectType(Link::class), Rule::RULE_PACKAGE_SAME_NAME => TypeCombinator::intersect(new StringType, new AccessoryNonEmptyStringType()), Rule::RULE_LEARNED => new IntegerType(), Rule::RULE_PACKAGE_ALIAS => new ObjectType(BasePackage::class), Rule::RULE_PACKAGE_INVERSE_ALIAS => new ObjectType(BasePackage::class), ]; foreach ($types as $const => $type) { if ((new ConstantIntegerType($const))->isSuperTypeOf($reasonType)->yes()) { return $type; } } return TypeCombinator::union(...$types); } } getName()); $this->version = $version; $this->prettyVersion = $prettyVersion; $this->aliasOf = $aliasOf; $this->stability = VersionParser::parseStability($version); $this->dev = $this->stability === 'dev'; foreach (Link::$TYPES as $type) { $links = $aliasOf->{'get' . ucfirst($type)}(); $this->{$type} = $this->replaceSelfVersionDependencies($links, $type); } } public function getAliasOf() { return $this->aliasOf; } public function getVersion(): string { return $this->version; } public function getStability(): string { return $this->stability; } public function getPrettyVersion(): string { return $this->prettyVersion; } public function isDev(): bool { return $this->dev; } public function getRequires(): array { return $this->requires; } public function getConflicts(): array { return $this->conflicts; } public function getProvides(): array { return $this->provides; } public function getReplaces(): array { return $this->replaces; } public function getDevRequires(): array { return $this->devRequires; } public function setRootPackageAlias(bool $value): void { $this->rootPackageAlias = $value; } public function isRootPackageAlias(): bool { return $this->rootPackageAlias; } protected function replaceSelfVersionDependencies(array $links, $linkType): array { $prettyVersion = $this->prettyVersion; if ($prettyVersion === VersionParser::DEFAULT_BRANCH_ALIAS) { $prettyVersion = $this->aliasOf->getPrettyVersion(); } if (\in_array($linkType, [Link::TYPE_CONFLICT, Link::TYPE_PROVIDE, Link::TYPE_REPLACE], true)) { $newLinks = []; foreach ($links as $link) { if ('self.version' === $link->getPrettyConstraint()) { $newLinks[] = new Link($link->getSource(), $link->getTarget(), $constraint = new Constraint('=', $this->version), $linkType, $prettyVersion); $constraint->setPrettyString($prettyVersion); } } $links = array_merge($links, $newLinks); } else { foreach ($links as $index => $link) { if ('self.version' === $link->getPrettyConstraint()) { if ($linkType === Link::TYPE_REQUIRE) { $this->hasSelfVersionRequires = true; } $links[$index] = new Link($link->getSource(), $link->getTarget(), $constraint = new Constraint('=', $this->version), $linkType, $prettyVersion); $constraint->setPrettyString($prettyVersion); } } } return $links; } public function hasSelfVersionRequires(): bool { return $this->hasSelfVersionRequires; } public function __toString(): string { return parent::__toString().' ('.($this->rootPackageAlias ? 'root ' : ''). 'alias of '.$this->aliasOf->getVersion().')'; } public function getType(): string { return $this->aliasOf->getType(); } public function getTargetDir(): ?string { return $this->aliasOf->getTargetDir(); } public function getExtra(): array { return $this->aliasOf->getExtra(); } public function setInstallationSource(?string $type): void { $this->aliasOf->setInstallationSource($type); } public function getInstallationSource(): ?string { return $this->aliasOf->getInstallationSource(); } public function getSourceType(): ?string { return $this->aliasOf->getSourceType(); } public function getSourceUrl(): ?string { return $this->aliasOf->getSourceUrl(); } public function getSourceUrls(): array { return $this->aliasOf->getSourceUrls(); } public function getSourceReference(): ?string { return $this->aliasOf->getSourceReference(); } public function setSourceReference(?string $reference): void { $this->aliasOf->setSourceReference($reference); } public function setSourceMirrors(?array $mirrors): void { $this->aliasOf->setSourceMirrors($mirrors); } public function getSourceMirrors(): ?array { return $this->aliasOf->getSourceMirrors(); } public function getDistType(): ?string { return $this->aliasOf->getDistType(); } public function getDistUrl(): ?string { return $this->aliasOf->getDistUrl(); } public function getDistUrls(): array { return $this->aliasOf->getDistUrls(); } public function getDistReference(): ?string { return $this->aliasOf->getDistReference(); } public function setDistReference(?string $reference): void { $this->aliasOf->setDistReference($reference); } public function getDistSha1Checksum(): ?string { return $this->aliasOf->getDistSha1Checksum(); } public function setTransportOptions(array $options): void { $this->aliasOf->setTransportOptions($options); } public function getTransportOptions(): array { return $this->aliasOf->getTransportOptions(); } public function setDistMirrors(?array $mirrors): void { $this->aliasOf->setDistMirrors($mirrors); } public function getDistMirrors(): ?array { return $this->aliasOf->getDistMirrors(); } public function getAutoload(): array { return $this->aliasOf->getAutoload(); } public function getDevAutoload(): array { return $this->aliasOf->getDevAutoload(); } public function getIncludePaths(): array { return $this->aliasOf->getIncludePaths(); } public function getReleaseDate(): ?\DateTimeInterface { return $this->aliasOf->getReleaseDate(); } public function getBinaries(): array { return $this->aliasOf->getBinaries(); } public function getSuggests(): array { return $this->aliasOf->getSuggests(); } public function getNotificationUrl(): ?string { return $this->aliasOf->getNotificationUrl(); } public function isDefaultBranch(): bool { return $this->aliasOf->isDefaultBranch(); } public function setDistUrl(?string $url): void { $this->aliasOf->setDistUrl($url); } public function setDistType(?string $type): void { $this->aliasOf->setDistType($type); } public function setSourceDistReferences(string $reference): void { $this->aliasOf->setSourceDistReferences($reference); } } getInnerIterator()->current(); if ($file->isDir()) { $this->dirs[] = (string) $file; return false; } return true; } public function addEmptyDir(PharData $phar, string $sources): void { foreach ($this->dirs as $filepath) { $localname = str_replace($sources . "/", '', $filepath); $phar->addEmptyDir($localname); } } } normalizePath(realpath($sources)); if ($ignoreFilters) { $filters = []; } else { $filters = [ new GitExcludeFilter($sources), new ComposerExcludeFilter($sources, $excludes), ]; } $this->finder = new Finder(); $filter = static function (\SplFileInfo $file) use ($sources, $filters, $fs): bool { if ($file->isLink() && ($file->getRealPath() === false || strpos($file->getRealPath(), $sources) !== 0)) { return false; } $relativePath = Preg::replace( '#^'.preg_quote($sources, '#').'#', '', $fs->normalizePath($file->getRealPath()) ); $exclude = false; foreach ($filters as $filter) { $exclude = $filter->filter($relativePath, $exclude); } return !$exclude; }; if (method_exists($filter, 'bindTo')) { $filter = $filter->bindTo(null); } $this->finder ->in($sources) ->filter($filter) ->ignoreVCS(true) ->ignoreDotFiles(false) ->sortByName(); parent::__construct($this->finder->getIterator()); } public function accept(): bool { $current = $this->getInnerIterator()->current(); if (!$current->isDir()) { return true; } $iterator = new FilesystemIterator((string) $current, FilesystemIterator::SKIP_DOTS); return !$iterator->valid(); } } downloadManager = $downloadManager; $this->loop = $loop; } public function addArchiver(ArchiverInterface $archiver): void { $this->archivers[] = $archiver; } public function setOverwriteFiles(bool $overwriteFiles): self { $this->overwriteFiles = $overwriteFiles; return $this; } public function getPackageFilename(CompletePackageInterface $package): string { if ($package->getArchiveName()) { $baseName = $package->getArchiveName(); } else { $baseName = Preg::replace('#[^a-z0-9-_]#i', '-', $package->getName()); } $nameParts = [$baseName]; if (null !== $package->getDistReference() && Preg::isMatch('{^[a-f0-9]{40}$}', $package->getDistReference())) { array_push($nameParts, $package->getDistReference(), $package->getDistType()); } else { array_push($nameParts, $package->getPrettyVersion(), $package->getDistReference()); } if ($package->getSourceReference()) { $nameParts[] = substr(sha1($package->getSourceReference()), 0, 6); } $name = implode('-', array_filter($nameParts, static function ($p): bool { return !empty($p); })); return str_replace('/', '-', $name); } public function archive(CompletePackageInterface $package, string $format, string $targetDir, ?string $fileName = null, bool $ignoreFilters = false): string { if (empty($format)) { throw new \InvalidArgumentException('Format must be specified'); } $usableArchiver = null; foreach ($this->archivers as $archiver) { if ($archiver->supports($format, $package->getSourceType())) { $usableArchiver = $archiver; break; } } if (null === $usableArchiver) { throw new \RuntimeException(sprintf('No archiver found to support %s format', $format)); } $filesystem = new Filesystem(); if ($package instanceof RootPackageInterface) { $sourcePath = realpath('.'); } else { $sourcePath = sys_get_temp_dir().'/composer_archive'.uniqid(); $filesystem->ensureDirectoryExists($sourcePath); try { $promise = $this->downloadManager->download($package, $sourcePath); SyncHelper::await($this->loop, $promise); $promise = $this->downloadManager->install($package, $sourcePath); SyncHelper::await($this->loop, $promise); } catch (\Exception $e) { $filesystem->removeDirectory($sourcePath); throw $e; } if (file_exists($composerJsonPath = $sourcePath.'/composer.json')) { $jsonFile = new JsonFile($composerJsonPath); $jsonData = $jsonFile->read(); if (!empty($jsonData['archive']['name'])) { $package->setArchiveName($jsonData['archive']['name']); } if (!empty($jsonData['archive']['exclude'])) { $package->setArchiveExcludes($jsonData['archive']['exclude']); } } } if (null === $fileName) { $packageName = $this->getPackageFilename($package); } else { $packageName = $fileName; } $filesystem->ensureDirectoryExists($targetDir); $target = realpath($targetDir).'/'.$packageName.'.'.$format; $filesystem->ensureDirectoryExists(dirname($target)); if (!$this->overwriteFiles && file_exists($target)) { return $target; } $tempTarget = sys_get_temp_dir().'/composer_archive'.uniqid().'.'.$format; $filesystem->ensureDirectoryExists(dirname($tempTarget)); $archivePath = $usableArchiver->archive($sourcePath, $tempTarget, $format, $package->getArchiveExcludes(), $ignoreFilters); $filesystem->rename($archivePath, $target); if (!$package instanceof RootPackageInterface) { $filesystem->removeDirectory($sourcePath); } $filesystem->remove($tempTarget); return $target; } } sourcePath = $sourcePath; $this->excludePatterns = []; } public function filter(string $relativePath, bool $exclude): bool { foreach ($this->excludePatterns as $patternData) { [$pattern, $negate, $stripLeadingSlash] = $patternData; if ($stripLeadingSlash) { $path = substr($relativePath, 1); } else { $path = $relativePath; } try { if (Preg::isMatch($pattern, $path)) { $exclude = !$negate; } } catch (\RuntimeException $e) { } } return $exclude; } protected function parseLines(array $lines, callable $lineParser): array { return array_filter( array_map( static function ($line) use ($lineParser) { $line = trim($line); if (!$line || 0 === strpos($line, '#')) { return null; } return $lineParser($line); }, $lines ), static function ($pattern): bool { return $pattern !== null; } ); } protected function generatePatterns(array $rules): array { $patterns = []; foreach ($rules as $rule) { $patterns[] = $this->generatePattern($rule); } return $patterns; } protected function generatePattern(string $rule): array { $negate = false; $pattern = ''; if ($rule !== '' && $rule[0] === '!') { $negate = true; $rule = ltrim($rule, '!'); } $firstSlashPosition = strpos($rule, '/'); if (0 === $firstSlashPosition) { $pattern = '^/'; } elseif (false === $firstSlashPosition || strlen($rule) - 1 === $firstSlashPosition) { $pattern = '/'; } $rule = trim($rule, '/'); $rule = substr(Finder\Glob::toRegex($rule), 2, -2); return ['{'.$pattern.$rule.'(?=$|/)}', $negate, false]; } } excludePatterns = $this->generatePatterns($excludeRules); } } excludePatterns = array_merge( $this->excludePatterns, $this->parseLines( file($sourcePath.'/.gitattributes'), [$this, 'parseGitAttributesLine'] ) ); } } public function parseGitAttributesLine(string $line): ?array { $parts = Preg::split('#\s+#', $line); if (count($parts) === 2 && $parts[1] === 'export-ignore') { return $this->generatePattern($parts[0]); } if (count($parts) === 2 && $parts[1] === '-export-ignore') { return $this->generatePattern('!'.$parts[0]); } return null; } } \Phar::ZIP, 'tar' => \Phar::TAR, 'tar.gz' => \Phar::TAR, 'tar.bz2' => \Phar::TAR, ]; protected static $compressFormats = [ 'tar.gz' => \Phar::GZ, 'tar.bz2' => \Phar::BZ2, ]; public function archive(string $sources, string $target, string $format, array $excludes = [], bool $ignoreFilters = false): string { $sources = realpath($sources); if (file_exists($target)) { unlink($target); } try { $filename = substr($target, 0, strrpos($target, $format) - 1); if (isset(static::$compressFormats[$format])) { $target = $filename . '.tar'; } $phar = new \PharData( $target, \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO, '', static::$formats[$format] ); $files = new ArchivableFilesFinder($sources, $excludes, $ignoreFilters); $filesOnly = new ArchivableFilesFilter($files); $phar->buildFromIterator($filesOnly, $sources); $filesOnly->addEmptyDir($phar, $sources); if (isset(static::$compressFormats[$format])) { if (!$phar->canCompress(static::$compressFormats[$format])) { throw new \RuntimeException(sprintf('Can not compress to %s format', $format)); } unlink($target); $phar->compress(static::$compressFormats[$format]); $target = $filename . '.' . $format; } return $target; } catch (\UnexpectedValueException $e) { $message = sprintf( "Could not create archive '%s' from '%s': %s", $target, $sources, $e->getMessage() ); throw new \RuntimeException($message, $e->getCode(), $e); } } public function supports(string $format, ?string $sourceType): bool { return isset(static::$formats[$format]); } } true, ]; public function archive(string $sources, string $target, string $format, array $excludes = [], bool $ignoreFilters = false): string { $fs = new Filesystem(); $sources = $fs->normalizePath($sources); $zip = new ZipArchive(); $res = $zip->open($target, ZipArchive::CREATE); if ($res === true) { $files = new ArchivableFilesFinder($sources, $excludes, $ignoreFilters); foreach ($files as $file) { $filepath = strtr($file->getPath()."/".$file->getFilename(), '\\', '/'); $localname = $filepath; if (strpos($localname, $sources . '/') === 0) { $localname = substr($localname, strlen($sources . '/')); } if ($file->isDir()) { $zip->addEmptyDir($localname); } else { $zip->addFile($filepath, $localname); } if (method_exists($zip, 'setExternalAttributesName')) { $perms = fileperms($filepath); $zip->setExternalAttributesName($localname, ZipArchive::OPSYS_UNIX, $perms << 16); } } if ($zip->close()) { return $target; } } $message = sprintf( "Could not create archive '%s' from '%s': %s", $target, $sources, $zip->getStatusString() ); throw new \RuntimeException($message); } public function supports(string $format, ?string $sourceType): bool { return isset(static::$formats[$format]) && $this->compressionAvailable(); } private function compressionAvailable(): bool { return class_exists('ZipArchive'); } } ['description' => 'requires', 'method' => Link::TYPE_REQUIRE], 'conflict' => ['description' => 'conflicts', 'method' => Link::TYPE_CONFLICT], 'provide' => ['description' => 'provides', 'method' => Link::TYPE_PROVIDE], 'replace' => ['description' => 'replaces', 'method' => Link::TYPE_REPLACE], 'require-dev' => ['description' => 'requires (for development)', 'method' => Link::TYPE_DEV_REQUIRE], ]; public const STABILITY_STABLE = 0; public const STABILITY_RC = 5; public const STABILITY_BETA = 10; public const STABILITY_ALPHA = 15; public const STABILITY_DEV = 20; public static $stabilities = [ 'stable' => self::STABILITY_STABLE, 'RC' => self::STABILITY_RC, 'beta' => self::STABILITY_BETA, 'alpha' => self::STABILITY_ALPHA, 'dev' => self::STABILITY_DEV, ]; public $id; protected $name; protected $prettyName; protected $repository = null; public function __construct(string $name) { $this->prettyName = $name; $this->name = strtolower($name); $this->id = -1; } public function getName(): string { return $this->name; } public function getPrettyName(): string { return $this->prettyName; } public function getNames($provides = true): array { $names = [ $this->getName() => true, ]; if ($provides) { foreach ($this->getProvides() as $link) { $names[$link->getTarget()] = true; } } foreach ($this->getReplaces() as $link) { $names[$link->getTarget()] = true; } return array_keys($names); } public function setId(int $id): void { $this->id = $id; } public function getId(): int { return $this->id; } public function setRepository(RepositoryInterface $repository): void { if ($this->repository && $repository !== $this->repository) { throw new \LogicException(sprintf( 'Package "%s" cannot be added to repository "%s" as it is already in repository "%s".', $this->getPrettyName(), $repository->getRepoName(), $this->repository->getRepoName() )); } $this->repository = $repository; } public function getRepository(): ?RepositoryInterface { return $this->repository; } public function isPlatform(): bool { return $this->getRepository() instanceof PlatformRepository; } public function getUniqueName(): string { return $this->getName().'-'.$this->getVersion(); } public function equals(PackageInterface $package): bool { $self = $this; if ($this instanceof AliasPackage) { $self = $this->getAliasOf(); } if ($package instanceof AliasPackage) { $package = $package->getAliasOf(); } return $package === $self; } public function __toString(): string { return $this->getUniqueName(); } public function getPrettyString(): string { return $this->getPrettyName().' '.$this->getPrettyVersion(); } public function getFullPrettyVersion(bool $truncate = true, int $displayMode = PackageInterface::DISPLAY_SOURCE_REF_IF_DEV): string { if ($displayMode === PackageInterface::DISPLAY_SOURCE_REF_IF_DEV && (!$this->isDev() || !\in_array($this->getSourceType(), ['hg', 'git'])) ) { return $this->getPrettyVersion(); } switch ($displayMode) { case PackageInterface::DISPLAY_SOURCE_REF_IF_DEV: case PackageInterface::DISPLAY_SOURCE_REF: $reference = $this->getSourceReference(); break; case PackageInterface::DISPLAY_DIST_REF: $reference = $this->getDistReference(); break; default: throw new \UnexpectedValueException('Display mode '.$displayMode.' is not supported'); } if (null === $reference) { return $this->getPrettyVersion(); } if ($truncate && \strlen($reference) === 40 && $this->getSourceType() !== 'svn') { return $this->getPrettyVersion() . ' ' . substr($reference, 0, 7); } return $this->getPrettyVersion() . ' ' . $reference; } public function getStabilityPriority(): int { return self::$stabilities[$this->getStability()]; } public function __clone() { $this->repository = null; $this->id = -1; } public static function packageNameToRegexp(string $allowPattern, string $wrap = '{^%s$}i'): string { $cleanedAllowPattern = str_replace('\\*', '.*', preg_quote($allowPattern)); return sprintf($wrap, $cleanedAllowPattern); } public static function packageNamesToRegexp(array $packageNames, string $wrap = '{^(?:%s)$}iD'): string { $packageNames = array_map( static function ($packageName): string { return BasePackage::packageNameToRegexp($packageName, '%s'); }, $packageNames ); return sprintf($wrap, implode('|', $packageNames)); } } source = $source; } public function setUpdate(string $update): void { $this->update = $update; } public function getChanged(bool $explicated = false) { $changed = $this->changed; if (!count($changed)) { return false; } if ($explicated) { foreach ($changed as $sectionKey => $itemSection) { foreach ($itemSection as $itemKey => $item) { $changed[$sectionKey][$itemKey] = $item.' ('.$sectionKey.')'; } } } return $changed; } public function getChangedAsString(bool $toString = false, bool $explicated = false): string { $changed = $this->getChanged($explicated); if (false === $changed) { return ''; } $strings = []; foreach ($changed as $sectionKey => $itemSection) { foreach ($itemSection as $itemKey => $item) { $strings[] = $item."\r\n"; } } return trim(implode("\r\n", $strings)); } public function doCompare(): void { $source = []; $destination = []; $this->changed = []; $currentDirectory = Platform::getCwd(); chdir($this->source); $source = $this->doTree('.', $source); if (!is_array($source)) { return; } chdir($currentDirectory); chdir($this->update); $destination = $this->doTree('.', $destination); if (!is_array($destination)) { exit; } chdir($currentDirectory); foreach ($source as $dir => $value) { foreach ($value as $file => $hash) { if (isset($destination[$dir][$file])) { if ($hash !== $destination[$dir][$file]) { $this->changed['changed'][] = $dir.'/'.$file; } } else { $this->changed['removed'][] = $dir.'/'.$file; } } } foreach ($destination as $dir => $value) { foreach ($value as $file => $hash) { if (!isset($source[$dir][$file])) { $this->changed['added'][] = $dir.'/'.$file; } } } } private function doTree(string $dir, array &$array) { if ($dh = opendir($dir)) { while ($file = readdir($dh)) { if ($file !== '.' && $file !== '..') { if (is_link($dir.'/'.$file)) { $array[$dir][$file] = readlink($dir.'/'.$file); } elseif (is_dir($dir.'/'.$file)) { if (!count($array)) { $array[0] = 'Temp'; } if (!$this->doTree($dir.'/'.$file, $array)) { return false; } } elseif (is_file($dir.'/'.$file) && filesize($dir.'/'.$file)) { $array[$dir][$file] = md5_file($dir.'/'.$file); } } } if (count($array) > 1 && isset($array['0'])) { unset($array['0']); } return $array; } return false; } } aliasOf; } public function getScripts(): array { return $this->aliasOf->getScripts(); } public function setScripts(array $scripts): void { $this->aliasOf->setScripts($scripts); } public function getRepositories(): array { return $this->aliasOf->getRepositories(); } public function setRepositories(array $repositories): void { $this->aliasOf->setRepositories($repositories); } public function getLicense(): array { return $this->aliasOf->getLicense(); } public function setLicense(array $license): void { $this->aliasOf->setLicense($license); } public function getKeywords(): array { return $this->aliasOf->getKeywords(); } public function setKeywords(array $keywords): void { $this->aliasOf->setKeywords($keywords); } public function getDescription(): ?string { return $this->aliasOf->getDescription(); } public function setDescription(?string $description): void { $this->aliasOf->setDescription($description); } public function getHomepage(): ?string { return $this->aliasOf->getHomepage(); } public function setHomepage(?string $homepage): void { $this->aliasOf->setHomepage($homepage); } public function getAuthors(): array { return $this->aliasOf->getAuthors(); } public function setAuthors(array $authors): void { $this->aliasOf->setAuthors($authors); } public function getSupport(): array { return $this->aliasOf->getSupport(); } public function setSupport(array $support): void { $this->aliasOf->setSupport($support); } public function getFunding(): array { return $this->aliasOf->getFunding(); } public function setFunding(array $funding): void { $this->aliasOf->setFunding($funding); } public function isAbandoned(): bool { return $this->aliasOf->isAbandoned(); } public function getReplacementPackage(): ?string { return $this->aliasOf->getReplacementPackage(); } public function setAbandoned($abandoned): void { $this->aliasOf->setAbandoned($abandoned); } public function getArchiveName(): ?string { return $this->aliasOf->getArchiveName(); } public function setArchiveName(?string $name): void { $this->aliasOf->setArchiveName($name); } public function getArchiveExcludes(): array { return $this->aliasOf->getArchiveExcludes(); } public function setArchiveExcludes(array $excludes): void { $this->aliasOf->setArchiveExcludes($excludes); } } scripts = $scripts; } public function getScripts(): array { return $this->scripts; } public function setRepositories(array $repositories): void { $this->repositories = $repositories; } public function getRepositories(): array { return $this->repositories; } public function setLicense(array $license): void { $this->license = $license; } public function getLicense(): array { return $this->license; } public function setKeywords(array $keywords): void { $this->keywords = $keywords; } public function getKeywords(): array { return $this->keywords; } public function setAuthors(array $authors): void { $this->authors = $authors; } public function getAuthors(): array { return $this->authors; } public function setDescription(?string $description): void { $this->description = $description; } public function getDescription(): ?string { return $this->description; } public function setHomepage(?string $homepage): void { $this->homepage = $homepage; } public function getHomepage(): ?string { return $this->homepage; } public function setSupport(array $support): void { $this->support = $support; } public function getSupport(): array { return $this->support; } public function setFunding(array $funding): void { $this->funding = $funding; } public function getFunding(): array { return $this->funding; } public function isAbandoned(): bool { return (bool) $this->abandoned; } public function setAbandoned($abandoned): void { $this->abandoned = $abandoned; } public function getReplacementPackage(): ?string { return \is_string($this->abandoned) ? $this->abandoned : null; } public function setArchiveName(?string $name): void { $this->archiveName = $name; } public function getArchiveName(): ?string { return $this->archiveName; } public function setArchiveExcludes(array $excludes): void { $this->archiveExcludes = $excludes; } public function getArchiveExcludes(): array { return $this->archiveExcludes; } } 'bin', 'type', 'extra', 'installationSource' => 'installation-source', 'autoload', 'devAutoload' => 'autoload-dev', 'notificationUrl' => 'notification-url', 'includePaths' => 'include-path', ]; $data = []; $data['name'] = $package->getPrettyName(); $data['version'] = $package->getPrettyVersion(); $data['version_normalized'] = $package->getVersion(); if ($package->getTargetDir()) { $data['target-dir'] = $package->getTargetDir(); } if ($package->getSourceType()) { $data['source']['type'] = $package->getSourceType(); $data['source']['url'] = $package->getSourceUrl(); if (null !== ($value = $package->getSourceReference())) { $data['source']['reference'] = $value; } if ($mirrors = $package->getSourceMirrors()) { $data['source']['mirrors'] = $mirrors; } } if ($package->getDistType()) { $data['dist']['type'] = $package->getDistType(); $data['dist']['url'] = $package->getDistUrl(); if (null !== ($value = $package->getDistReference())) { $data['dist']['reference'] = $value; } if (null !== ($value = $package->getDistSha1Checksum())) { $data['dist']['shasum'] = $value; } if ($mirrors = $package->getDistMirrors()) { $data['dist']['mirrors'] = $mirrors; } } foreach (BasePackage::$supportedLinkTypes as $type => $opts) { if ($links = $package->{'get'.ucfirst($opts['method'])}()) { foreach ($links as $link) { $data[$type][$link->getTarget()] = $link->getPrettyConstraint(); } ksort($data[$type]); } } if ($packages = $package->getSuggests()) { ksort($packages); $data['suggest'] = $packages; } if ($package->getReleaseDate() instanceof \DateTimeInterface) { $data['time'] = $package->getReleaseDate()->format(DATE_RFC3339); } if ($package->isDefaultBranch()) { $data['default-branch'] = true; } $data = $this->dumpValues($package, $keys, $data); if ($package instanceof CompletePackageInterface) { if ($package->getArchiveName()) { $data['archive']['name'] = $package->getArchiveName(); } if ($package->getArchiveExcludes()) { $data['archive']['exclude'] = $package->getArchiveExcludes(); } $keys = [ 'scripts', 'license', 'authors', 'description', 'homepage', 'keywords', 'repositories', 'support', 'funding', ]; $data = $this->dumpValues($package, $keys, $data); if (isset($data['keywords']) && \is_array($data['keywords'])) { sort($data['keywords']); } if ($package->isAbandoned()) { $data['abandoned'] = $package->getReplacementPackage() ?: true; } } if ($package instanceof RootPackageInterface) { $minimumStability = $package->getMinimumStability(); if ($minimumStability) { $data['minimum-stability'] = $minimumStability; } } if (\count($package->getTransportOptions()) > 0) { $data['transport-options'] = $package->getTransportOptions(); } return $data; } private function dumpValues(PackageInterface $package, array $keys, array $data): array { foreach ($keys as $method => $key) { if (is_numeric($method)) { $method = $key; } $getter = 'get'.ucfirst($method); $value = $package->{$getter}(); if (null !== $value && !(\is_array($value) && 0 === \count($value))) { $data[$key] = $value; } } return $data; } } source = strtolower($source); $this->target = strtolower($target); $this->constraint = $constraint; $this->description = self::TYPE_DEV_REQUIRE === $description ? 'requires (for development)' : $description; $this->prettyConstraint = $prettyConstraint; } public function getDescription(): string { return $this->description; } public function getSource(): string { return $this->source; } public function getTarget(): string { return $this->target; } public function getConstraint(): ConstraintInterface { return $this->constraint; } public function getPrettyConstraint(): string { if (null === $this->prettyConstraint) { throw new \UnexpectedValueException(sprintf('Link %s has been misconfigured and had no prettyConstraint given.', $this)); } return $this->prettyConstraint; } public function __toString(): string { return $this->source.' '.$this->description.' '.$this->target.' ('.$this->constraint.')'; } public function getPrettyString(PackageInterface $sourcePackage): string { return $sourcePackage->getPrettyString().' '.$this->description.' '.$this->target.' '.$this->constraint->getPrettyString(); } } versionParser = $parser; $this->loadOptions = $loadOptions; } public function load(array $config, string $class = 'Composer\Package\CompletePackage'): BasePackage { if ($class !== 'Composer\Package\CompletePackage' && $class !== 'Composer\Package\RootPackage') { trigger_error('The $class arg is deprecated, please reach out to Composer maintainers ASAP if you still need this.', E_USER_DEPRECATED); } $package = $this->createObject($config, $class); foreach (BasePackage::$supportedLinkTypes as $type => $opts) { if (!isset($config[$type]) || !is_array($config[$type])) { continue; } $method = 'set'.ucfirst($opts['method']); $package->{$method}( $this->parseLinks( $package->getName(), $package->getPrettyVersion(), $opts['method'], $config[$type] ) ); } $package = $this->configureObject($package, $config); return $package; } public function loadPackages(array $versions): array { $packages = []; $linkCache = []; foreach ($versions as $version) { $package = $this->createObject($version, 'Composer\Package\CompletePackage'); $this->configureCachedLinks($linkCache, $package, $version); $package = $this->configureObject($package, $version); $packages[] = $package; } return $packages; } private function createObject(array $config, string $class): CompletePackage { if (!isset($config['name'])) { throw new \UnexpectedValueException('Unknown package has no name defined ('.json_encode($config).').'); } if (!isset($config['version']) || !is_scalar($config['version'])) { throw new \UnexpectedValueException('Package '.$config['name'].' has no version defined.'); } if (!is_string($config['version'])) { $config['version'] = (string) $config['version']; } if (isset($config['version_normalized']) && is_string($config['version_normalized'])) { $version = $config['version_normalized']; if ($version === VersionParser::DEFAULT_BRANCH_ALIAS) { $version = $this->versionParser->normalize($config['version']); } } else { $version = $this->versionParser->normalize($config['version']); } return new $class($config['name'], $version, $config['version']); } private function configureObject(PackageInterface $package, array $config): BasePackage { if (!$package instanceof CompletePackage) { throw new \LogicException('ArrayLoader expects instances of the Composer\Package\CompletePackage class to function correctly'); } $package->setType(isset($config['type']) ? strtolower($config['type']) : 'library'); if (isset($config['target-dir'])) { $package->setTargetDir($config['target-dir']); } if (isset($config['extra']) && \is_array($config['extra'])) { $package->setExtra($config['extra']); } if (isset($config['bin'])) { if (!\is_array($config['bin'])) { $config['bin'] = [$config['bin']]; } foreach ($config['bin'] as $key => $bin) { $config['bin'][$key] = ltrim($bin, '/'); } $package->setBinaries($config['bin']); } if (isset($config['installation-source'])) { $package->setInstallationSource($config['installation-source']); } if (isset($config['default-branch']) && $config['default-branch'] === true) { $package->setIsDefaultBranch(true); } if (isset($config['source'])) { if (!isset($config['source']['type'], $config['source']['url'], $config['source']['reference'])) { throw new \UnexpectedValueException(sprintf( "Package %s's source key should be specified as {\"type\": ..., \"url\": ..., \"reference\": ...},\n%s given.", $config['name'], json_encode($config['source']) )); } $package->setSourceType($config['source']['type']); $package->setSourceUrl($config['source']['url']); $package->setSourceReference(isset($config['source']['reference']) ? (string) $config['source']['reference'] : null); if (isset($config['source']['mirrors'])) { $package->setSourceMirrors($config['source']['mirrors']); } } if (isset($config['dist'])) { if (!isset($config['dist']['type'], $config['dist']['url'])) { throw new \UnexpectedValueException(sprintf( "Package %s's dist key should be specified as ". "{\"type\": ..., \"url\": ..., \"reference\": ..., \"shasum\": ...},\n%s given.", $config['name'], json_encode($config['dist']) )); } $package->setDistType($config['dist']['type']); $package->setDistUrl($config['dist']['url']); $package->setDistReference(isset($config['dist']['reference']) ? (string) $config['dist']['reference'] : null); $package->setDistSha1Checksum($config['dist']['shasum'] ?? null); if (isset($config['dist']['mirrors'])) { $package->setDistMirrors($config['dist']['mirrors']); } } if (isset($config['suggest']) && \is_array($config['suggest'])) { foreach ($config['suggest'] as $target => $reason) { if ('self.version' === trim($reason)) { $config['suggest'][$target] = $package->getPrettyVersion(); } } $package->setSuggests($config['suggest']); } if (isset($config['autoload'])) { $package->setAutoload($config['autoload']); } if (isset($config['autoload-dev'])) { $package->setDevAutoload($config['autoload-dev']); } if (isset($config['include-path'])) { $package->setIncludePaths($config['include-path']); } if (!empty($config['time'])) { $time = Preg::isMatch('/^\d++$/D', $config['time']) ? '@'.$config['time'] : $config['time']; try { $date = new \DateTime($time, new \DateTimeZone('UTC')); $package->setReleaseDate($date); } catch (\Exception $e) { } } if (!empty($config['notification-url'])) { $package->setNotificationUrl($config['notification-url']); } if ($package instanceof CompletePackageInterface) { if (!empty($config['archive']['name'])) { $package->setArchiveName($config['archive']['name']); } if (!empty($config['archive']['exclude'])) { $package->setArchiveExcludes($config['archive']['exclude']); } if (isset($config['scripts']) && \is_array($config['scripts'])) { foreach ($config['scripts'] as $event => $listeners) { $config['scripts'][$event] = (array) $listeners; } foreach (['composer', 'php', 'putenv'] as $reserved) { if (isset($config['scripts'][$reserved])) { trigger_error('The `'.$reserved.'` script name is reserved for internal use, please avoid defining it', E_USER_DEPRECATED); } } $package->setScripts($config['scripts']); } if (!empty($config['description']) && \is_string($config['description'])) { $package->setDescription($config['description']); } if (!empty($config['homepage']) && \is_string($config['homepage'])) { $package->setHomepage($config['homepage']); } if (!empty($config['keywords']) && \is_array($config['keywords'])) { $package->setKeywords(array_map('strval', $config['keywords'])); } if (!empty($config['license'])) { $package->setLicense(\is_array($config['license']) ? $config['license'] : [$config['license']]); } if (!empty($config['authors']) && \is_array($config['authors'])) { $package->setAuthors($config['authors']); } if (isset($config['support'])) { $package->setSupport($config['support']); } if (!empty($config['funding']) && \is_array($config['funding'])) { $package->setFunding($config['funding']); } if (isset($config['abandoned'])) { $package->setAbandoned($config['abandoned']); } } if ($this->loadOptions && isset($config['transport-options'])) { $package->setTransportOptions($config['transport-options']); } if ($aliasNormalized = $this->getBranchAlias($config)) { $prettyAlias = Preg::replace('{(\.9{7})+}', '.x', $aliasNormalized); if ($package instanceof RootPackage) { return new RootAliasPackage($package, $aliasNormalized, $prettyAlias); } return new CompleteAliasPackage($package, $aliasNormalized, $prettyAlias); } return $package; } private function configureCachedLinks(array &$linkCache, PackageInterface $package, array $config): void { $name = $package->getName(); $prettyVersion = $package->getPrettyVersion(); foreach (BasePackage::$supportedLinkTypes as $type => $opts) { if (isset($config[$type])) { $method = 'set'.ucfirst($opts['method']); $links = []; foreach ($config[$type] as $prettyTarget => $constraint) { $target = strtolower($prettyTarget); if ($target === $name) { continue; } if ($constraint === 'self.version') { $links[$target] = $this->createLink($name, $prettyVersion, $opts['method'], $target, $constraint); } else { if (!isset($linkCache[$name][$type][$target][$constraint])) { $linkCache[$name][$type][$target][$constraint] = [$target, $this->createLink($name, $prettyVersion, $opts['method'], $target, $constraint)]; } [$target, $link] = $linkCache[$name][$type][$target][$constraint]; $links[$target] = $link; } } $package->{$method}($links); } } } public function parseLinks(string $source, string $sourceVersion, string $description, array $links): array { $res = []; foreach ($links as $target => $constraint) { if (!is_string($constraint)) { continue; } $target = strtolower((string) $target); $res[$target] = $this->createLink($source, $sourceVersion, $description, $target, $constraint); } return $res; } private function createLink(string $source, string $sourceVersion, string $description, string $target, string $prettyConstraint): Link { if (!\is_string($prettyConstraint)) { throw new \UnexpectedValueException('Link constraint in '.$source.' '.$description.' > '.$target.' should be a string, got '.\gettype($prettyConstraint) . ' (' . var_export($prettyConstraint, true) . ')'); } if ('self.version' === $prettyConstraint) { $parsedConstraint = $this->versionParser->parseConstraints($sourceVersion); } else { $parsedConstraint = $this->versionParser->parseConstraints($prettyConstraint); } return new Link($source, $target, $parsedConstraint, $description, $prettyConstraint); } public function getBranchAlias(array $config): ?string { if (!isset($config['version']) || !is_scalar($config['version'])) { throw new \UnexpectedValueException('no/invalid version defined'); } if (!is_string($config['version'])) { $config['version'] = (string) $config['version']; } if (strpos($config['version'], 'dev-') !== 0 && '-dev' !== substr($config['version'], -4)) { return null; } if (isset($config['extra']['branch-alias']) && \is_array($config['extra']['branch-alias'])) { foreach ($config['extra']['branch-alias'] as $sourceBranch => $targetBranch) { $sourceBranch = (string) $sourceBranch; if ('-dev' !== substr($targetBranch, -4)) { continue; } if ($targetBranch === VersionParser::DEFAULT_BRANCH_ALIAS) { $validatedTargetBranch = VersionParser::DEFAULT_BRANCH_ALIAS; } else { $validatedTargetBranch = $this->versionParser->normalizeBranch(substr($targetBranch, 0, -4)); } if ('-dev' !== substr($validatedTargetBranch, -4)) { continue; } if (strtolower($config['version']) !== strtolower($sourceBranch)) { continue; } if (($sourcePrefix = $this->versionParser->parseNumericAliasPrefix($sourceBranch)) && ($targetPrefix = $this->versionParser->parseNumericAliasPrefix($targetBranch)) && (stripos($targetPrefix, $sourcePrefix) !== 0) ) { continue; } return $validatedTargetBranch; } } if ( isset($config['default-branch']) && $config['default-branch'] === true && false === $this->versionParser->parseNumericAliasPrefix($config['version']) ) { return VersionParser::DEFAULT_BRANCH_ALIAS; } return null; } } errors = $errors; $this->warnings = $warnings; $this->data = $data; parent::__construct("Invalid package information: \n".implode("\n", array_merge($errors, $warnings))); } public function getData(): array { return $this->data; } public function getErrors(): array { return $this->errors; } public function getWarnings(): array { return $this->warnings; } } loader = $loader; } public function load($json): BasePackage { if ($json instanceof JsonFile) { $config = $json->read(); } elseif (file_exists($json)) { $config = JsonFile::parseJson(file_get_contents($json), $json); } elseif (is_string($json)) { $config = JsonFile::parseJson($json); } else { throw new \InvalidArgumentException(sprintf( "JsonLoader: Unknown \$json parameter %s. Please report at https://github.com/composer/composer/issues/new.", gettype($json) )); } return $this->loader->load($config); } } manager = $manager; $this->config = $config; $this->versionGuesser = $versionGuesser ?: new VersionGuesser($config, new ProcessExecutor($io), $this->versionParser); } public function load(array $config, string $class = 'Composer\Package\RootPackage', ?string $cwd = null): BasePackage { if ($class !== 'Composer\Package\RootPackage') { trigger_error('The $class arg is deprecated, please reach out to Composer maintainers ASAP if you still need this.', E_USER_DEPRECATED); } if (!isset($config['name'])) { $config['name'] = '__root__'; } elseif ($err = ValidatingArrayLoader::hasPackageNamingError($config['name'])) { throw new \RuntimeException('Your package name '.$err); } $autoVersioned = false; if (!isset($config['version'])) { $commit = null; if (Platform::getEnv('COMPOSER_ROOT_VERSION')) { $config['version'] = Platform::getEnv('COMPOSER_ROOT_VERSION'); } else { $versionData = $this->versionGuesser->guessVersion($config, $cwd ?? Platform::getCwd(true)); if ($versionData) { $config['version'] = $versionData['pretty_version']; $config['version_normalized'] = $versionData['version']; $commit = $versionData['commit']; } } if (!isset($config['version'])) { $config['version'] = '1.0.0'; $autoVersioned = true; } if ($commit) { $config['source'] = [ 'type' => '', 'url' => '', 'reference' => $commit, ]; $config['dist'] = [ 'type' => '', 'url' => '', 'reference' => $commit, ]; } } $package = parent::load($config, $class); if ($package instanceof RootAliasPackage) { $realPackage = $package->getAliasOf(); } else { $realPackage = $package; } if (!$realPackage instanceof RootPackage) { throw new \LogicException('Expecting a Composer\Package\RootPackage at this point'); } if ($autoVersioned) { $realPackage->replaceVersion($realPackage->getVersion(), RootPackage::DEFAULT_PRETTY_VERSION); } if (isset($config['minimum-stability'])) { $realPackage->setMinimumStability(VersionParser::normalizeStability($config['minimum-stability'])); } $aliases = []; $stabilityFlags = []; $references = []; foreach (['require', 'require-dev'] as $linkType) { if (isset($config[$linkType])) { $linkInfo = BasePackage::$supportedLinkTypes[$linkType]; $method = 'get'.ucfirst($linkInfo['method']); $links = []; foreach ($realPackage->{$method}() as $link) { $links[$link->getTarget()] = $link->getConstraint()->getPrettyString(); } $aliases = $this->extractAliases($links, $aliases); $stabilityFlags = self::extractStabilityFlags($links, $realPackage->getMinimumStability(), $stabilityFlags); $references = self::extractReferences($links, $references); if (isset($links[$config['name']])) { throw new \RuntimeException(sprintf('Root package \'%s\' cannot require itself in its composer.json' . PHP_EOL . 'Did you accidentally name your root package after an external package?', $config['name'])); } } } foreach (array_keys(BasePackage::$supportedLinkTypes) as $linkType) { if (isset($config[$linkType])) { foreach ($config[$linkType] as $linkName => $constraint) { if ($err = ValidatingArrayLoader::hasPackageNamingError($linkName, true)) { throw new \RuntimeException($linkType.'.'.$err); } } } } $realPackage->setAliases($aliases); $realPackage->setStabilityFlags($stabilityFlags); $realPackage->setReferences($references); if (isset($config['prefer-stable'])) { $realPackage->setPreferStable((bool) $config['prefer-stable']); } if (isset($config['config'])) { $realPackage->setConfig($config['config']); } $repos = RepositoryFactory::defaultRepos(null, $this->config, $this->manager); foreach ($repos as $repo) { $this->manager->addRepository($repo); } $realPackage->setRepositories($this->config->getRepositories()); return $package; } private function extractAliases(array $requires, array $aliases): array { foreach ($requires as $reqName => $reqVersion) { if (Preg::isMatch('{^([^,\s#]+)(?:#[^ ]+)? +as +([^,\s]+)$}', $reqVersion, $match)) { $aliases[] = [ 'package' => strtolower($reqName), 'version' => $this->versionParser->normalize($match[1], $reqVersion), 'alias' => $match[2], 'alias_normalized' => $this->versionParser->normalize($match[2], $reqVersion), ]; } elseif (strpos($reqVersion, ' as ') !== false) { throw new \UnexpectedValueException('Invalid alias definition in "'.$reqName.'": "'.$reqVersion.'". Aliases should be in the form "exact-version as other-exact-version".'); } } return $aliases; } public static function extractStabilityFlags(array $requires, string $minimumStability, array $stabilityFlags): array { $stabilities = BasePackage::$stabilities; $minimumStability = $stabilities[$minimumStability]; foreach ($requires as $reqName => $reqVersion) { $constraints = []; $orSplit = Preg::split('{\s*\|\|?\s*}', trim($reqVersion)); foreach ($orSplit as $orConstraint) { $andSplit = Preg::split('{(?< ,]) *(? $stability) { continue; } $stabilityFlags[$name] = $stability; $matched = true; } } if ($matched) { continue; } foreach ($constraints as $constraint) { $reqVersion = Preg::replace('{^([^,\s@]+) as .+$}', '$1', $constraint); if (Preg::isMatch('{^[^,\s@]+$}', $reqVersion) && 'stable' !== ($stabilityName = VersionParser::parseStability($reqVersion))) { $name = strtolower($reqName); $stability = $stabilities[$stabilityName]; if ((isset($stabilityFlags[$name]) && $stabilityFlags[$name] > $stability) || ($minimumStability > $stability)) { continue; } $stabilityFlags[$name] = $stability; } } } return $stabilityFlags; } public static function extractReferences(array $requires, array $references): array { foreach ($requires as $reqName => $reqVersion) { $reqVersion = Preg::replace('{^([^,\s@]+) as .+$}', '$1', $reqVersion); if (Preg::isMatch('{^[^,\s@]+?#([a-f0-9]+)$}', $reqVersion, $match) && 'dev' === VersionParser::parseStability($reqVersion)) { $name = strtolower($reqName); $references[$name] = $match[1]; } } return $references; } } loader = $loader; $this->versionParser = $parser ?? new VersionParser(); $this->flags = $flags; if ($strictName !== true) { trigger_error('$strictName must be set to true in ValidatingArrayLoader\'s constructor as of 2.2, and it will be removed in 3.0', E_USER_DEPRECATED); } } public function load(array $config, string $class = 'Composer\Package\CompletePackage'): BasePackage { $this->errors = []; $this->warnings = []; $this->config = $config; $this->validateString('name', true); if (isset($config['name']) && null !== ($err = self::hasPackageNamingError($config['name']))) { $this->errors[] = 'name : '.$err; } if (isset($this->config['version'])) { if (!is_scalar($this->config['version'])) { $this->validateString('version'); } else { if (!is_string($this->config['version'])) { $this->config['version'] = (string) $this->config['version']; } try { $this->versionParser->normalize($this->config['version']); } catch (\Exception $e) { $this->errors[] = 'version : invalid value ('.$this->config['version'].'): '.$e->getMessage(); unset($this->config['version']); } } } if (isset($this->config['config']['platform'])) { foreach ((array) $this->config['config']['platform'] as $key => $platform) { if (false === $platform) { continue; } if (!is_string($platform)) { $this->errors[] = 'config.platform.' . $key . ' : invalid value ('.gettype($platform).' '.var_export($platform, true).'): expected string or false'; continue; } try { $this->versionParser->normalize($platform); } catch (\Exception $e) { $this->errors[] = 'config.platform.' . $key . ' : invalid value ('.$platform.'): '.$e->getMessage(); } } } $this->validateRegex('type', '[A-Za-z0-9-]+'); $this->validateString('target-dir'); $this->validateArray('extra'); if (isset($this->config['bin'])) { if (is_string($this->config['bin'])) { $this->validateString('bin'); } else { $this->validateFlatArray('bin'); } } $this->validateArray('scripts'); $this->validateString('description'); $this->validateUrl('homepage'); $this->validateFlatArray('keywords', '[\p{N}\p{L} ._-]+'); $releaseDate = null; $this->validateString('time'); if (isset($this->config['time'])) { try { $releaseDate = new \DateTime($this->config['time'], new \DateTimeZone('UTC')); } catch (\Exception $e) { $this->errors[] = 'time : invalid value ('.$this->config['time'].'): '.$e->getMessage(); unset($this->config['time']); } } if (isset($this->config['license']) && (null === $releaseDate || $releaseDate->getTimestamp() >= strtotime('-8days'))) { if (is_array($this->config['license']) || is_string($this->config['license'])) { $licenses = (array) $this->config['license']; $licenseValidator = new SpdxLicenses(); foreach ($licenses as $license) { if ('proprietary' === $license) { continue; } $licenseToValidate = str_replace('proprietary', 'MIT', $license); if (!$licenseValidator->validate($licenseToValidate)) { if ($licenseValidator->validate(trim($licenseToValidate))) { $this->warnings[] = sprintf( 'License %s must not contain extra spaces, make sure to trim it.', json_encode($license) ); } else { $this->warnings[] = sprintf( 'License %s is not a valid SPDX license identifier, see https://spdx.org/licenses/ if you use an open license.' . PHP_EOL . 'If the software is closed-source, you may use "proprietary" as license.', json_encode($license) ); } } } } } if ($this->validateArray('authors')) { foreach ($this->config['authors'] as $key => $author) { if (!is_array($author)) { $this->errors[] = 'authors.'.$key.' : should be an array, '.gettype($author).' given'; unset($this->config['authors'][$key]); continue; } foreach (['homepage', 'email', 'name', 'role'] as $authorData) { if (isset($author[$authorData]) && !is_string($author[$authorData])) { $this->errors[] = 'authors.'.$key.'.'.$authorData.' : invalid value, must be a string'; unset($this->config['authors'][$key][$authorData]); } } if (isset($author['homepage']) && !$this->filterUrl($author['homepage'])) { $this->warnings[] = 'authors.'.$key.'.homepage : invalid value ('.$author['homepage'].'), must be an http/https URL'; unset($this->config['authors'][$key]['homepage']); } if (isset($author['email']) && false === filter_var($author['email'], FILTER_VALIDATE_EMAIL)) { $this->warnings[] = 'authors.'.$key.'.email : invalid value ('.$author['email'].'), must be a valid email address'; unset($this->config['authors'][$key]['email']); } if (\count($this->config['authors'][$key]) === 0) { unset($this->config['authors'][$key]); } } if (\count($this->config['authors']) === 0) { unset($this->config['authors']); } } if ($this->validateArray('support') && !empty($this->config['support'])) { foreach (['issues', 'forum', 'wiki', 'source', 'email', 'irc', 'docs', 'rss', 'chat'] as $key) { if (isset($this->config['support'][$key]) && !is_string($this->config['support'][$key])) { $this->errors[] = 'support.'.$key.' : invalid value, must be a string'; unset($this->config['support'][$key]); } } if (isset($this->config['support']['email']) && !filter_var($this->config['support']['email'], FILTER_VALIDATE_EMAIL)) { $this->warnings[] = 'support.email : invalid value ('.$this->config['support']['email'].'), must be a valid email address'; unset($this->config['support']['email']); } if (isset($this->config['support']['irc']) && !$this->filterUrl($this->config['support']['irc'], ['irc', 'ircs'])) { $this->warnings[] = 'support.irc : invalid value ('.$this->config['support']['irc'].'), must be a irc:/// or ircs:// URL'; unset($this->config['support']['irc']); } foreach (['issues', 'forum', 'wiki', 'source', 'docs', 'chat'] as $key) { if (isset($this->config['support'][$key]) && !$this->filterUrl($this->config['support'][$key])) { $this->warnings[] = 'support.'.$key.' : invalid value ('.$this->config['support'][$key].'), must be an http/https URL'; unset($this->config['support'][$key]); } } if (empty($this->config['support'])) { unset($this->config['support']); } } if ($this->validateArray('funding') && !empty($this->config['funding'])) { foreach ($this->config['funding'] as $key => $fundingOption) { if (!is_array($fundingOption)) { $this->errors[] = 'funding.'.$key.' : should be an array, '.gettype($fundingOption).' given'; unset($this->config['funding'][$key]); continue; } foreach (['type', 'url'] as $fundingData) { if (isset($fundingOption[$fundingData]) && !is_string($fundingOption[$fundingData])) { $this->errors[] = 'funding.'.$key.'.'.$fundingData.' : invalid value, must be a string'; unset($this->config['funding'][$key][$fundingData]); } } if (isset($fundingOption['url']) && !$this->filterUrl($fundingOption['url'])) { $this->warnings[] = 'funding.'.$key.'.url : invalid value ('.$fundingOption['url'].'), must be an http/https URL'; unset($this->config['funding'][$key]['url']); } if (empty($this->config['funding'][$key])) { unset($this->config['funding'][$key]); } } if (empty($this->config['funding'])) { unset($this->config['funding']); } } $unboundConstraint = new Constraint('=', '10000000-dev'); $stableConstraint = new Constraint('=', '1.0.0'); foreach (array_keys(BasePackage::$supportedLinkTypes) as $linkType) { if ($this->validateArray($linkType) && isset($this->config[$linkType])) { foreach ($this->config[$linkType] as $package => $constraint) { $package = (string) $package; if (0 === strcasecmp($package, $this->config['name'])) { $this->errors[] = $linkType.'.'.$package.' : a package cannot set a '.$linkType.' on itself'; unset($this->config[$linkType][$package]); continue; } if ($err = self::hasPackageNamingError($package, true)) { $this->warnings[] = $linkType.'.'.$err; } elseif (!Preg::isMatch('{^[A-Za-z0-9_./-]+$}', $package)) { $this->errors[] = $linkType.'.'.$package.' : invalid key, package names must be strings containing only [A-Za-z0-9_./-]'; } if (!is_string($constraint)) { $this->errors[] = $linkType.'.'.$package.' : invalid value, must be a string containing a version constraint'; unset($this->config[$linkType][$package]); } elseif ('self.version' !== $constraint) { try { $linkConstraint = $this->versionParser->parseConstraints($constraint); } catch (\Exception $e) { $this->errors[] = $linkType.'.'.$package.' : invalid version constraint ('.$e->getMessage().')'; unset($this->config[$linkType][$package]); continue; } if ( ($this->flags & self::CHECK_UNBOUND_CONSTRAINTS) && 'require' === $linkType && $linkConstraint->matches($unboundConstraint) && !PlatformRepository::isPlatformPackage($package) ) { $this->warnings[] = $linkType.'.'.$package.' : unbound version constraints ('.$constraint.') should be avoided'; } elseif ( ($this->flags & self::CHECK_STRICT_CONSTRAINTS) && 'require' === $linkType && $linkConstraint instanceof Constraint && in_array($linkConstraint->getOperator(), ['==', '='], true) && (new Constraint('>=', '1.0.0.0-dev'))->matches($linkConstraint) ) { $this->warnings[] = $linkType.'.'.$package.' : exact version constraints ('.$constraint.') should be avoided if the package follows semantic versioning'; } } if ($linkType === 'conflict' && isset($this->config['replace']) && $keys = array_intersect_key($this->config['replace'], $this->config['conflict'])) { $this->errors[] = $linkType.'.'.$package.' : you cannot conflict with a package that is also replaced, as replace already creates an implicit conflict rule'; unset($this->config[$linkType][$package]); } } } } if ($this->validateArray('suggest') && isset($this->config['suggest'])) { foreach ($this->config['suggest'] as $package => $description) { if (!is_string($description)) { $this->errors[] = 'suggest.'.$package.' : invalid value, must be a string describing why the package is suggested'; unset($this->config['suggest'][$package]); } } } if ($this->validateString('minimum-stability') && isset($this->config['minimum-stability'])) { if (!isset(BasePackage::$stabilities[strtolower($this->config['minimum-stability'])]) && $this->config['minimum-stability'] !== 'RC') { $this->errors[] = 'minimum-stability : invalid value ('.$this->config['minimum-stability'].'), must be one of '.implode(', ', array_keys(BasePackage::$stabilities)); unset($this->config['minimum-stability']); } } if ($this->validateArray('autoload') && isset($this->config['autoload'])) { $types = ['psr-0', 'psr-4', 'classmap', 'files', 'exclude-from-classmap']; foreach ($this->config['autoload'] as $type => $typeConfig) { if (!in_array($type, $types)) { $this->errors[] = 'autoload : invalid value ('.$type.'), must be one of '.implode(', ', $types); unset($this->config['autoload'][$type]); } if ($type === 'psr-4') { foreach ($typeConfig as $namespace => $dirs) { if ($namespace !== '' && '\\' !== substr((string) $namespace, -1)) { $this->errors[] = 'autoload.psr-4 : invalid value ('.$namespace.'), namespaces must end with a namespace separator, should be '.$namespace.'\\\\'; } } } } } if (isset($this->config['autoload']['psr-4']) && isset($this->config['target-dir'])) { $this->errors[] = 'target-dir : this can not be used together with the autoload.psr-4 setting, remove target-dir to upgrade to psr-4'; unset($this->config['autoload']['psr-4']); } foreach (['source', 'dist'] as $srcType) { if ($this->validateArray($srcType) && !empty($this->config[$srcType])) { if (!isset($this->config[$srcType]['type'])) { $this->errors[] = $srcType . '.type : must be present'; } if (!isset($this->config[$srcType]['url'])) { $this->errors[] = $srcType . '.url : must be present'; } if ($srcType === 'source' && !isset($this->config[$srcType]['reference'])) { $this->errors[] = $srcType . '.reference : must be present'; } if (isset($this->config[$srcType]['type']) && !is_string($this->config[$srcType]['type'])) { $this->errors[] = $srcType . '.type : should be a string, '.gettype($this->config[$srcType]['type']).' given'; } if (isset($this->config[$srcType]['url']) && !is_string($this->config[$srcType]['url'])) { $this->errors[] = $srcType . '.url : should be a string, '.gettype($this->config[$srcType]['url']).' given'; } if (isset($this->config[$srcType]['reference']) && !is_string($this->config[$srcType]['reference']) && !is_int($this->config[$srcType]['reference'])) { $this->errors[] = $srcType . '.reference : should be a string or int, '.gettype($this->config[$srcType]['reference']).' given'; } if (isset($this->config[$srcType]['reference']) && Preg::isMatch('{^\s*-}', (string) $this->config[$srcType]['reference'])) { $this->errors[] = $srcType . '.reference : must not start with a "-", "'.$this->config[$srcType]['reference'].'" given'; } if (isset($this->config[$srcType]['url']) && Preg::isMatch('{^\s*-}', (string) $this->config[$srcType]['url'])) { $this->errors[] = $srcType . '.url : must not start with a "-", "'.$this->config[$srcType]['url'].'" given'; } } } $this->validateFlatArray('include-path'); $this->validateArray('transport-options'); if (isset($this->config['extra']['branch-alias'])) { if (!is_array($this->config['extra']['branch-alias'])) { $this->errors[] = 'extra.branch-alias : must be an array of versions => aliases'; } else { foreach ($this->config['extra']['branch-alias'] as $sourceBranch => $targetBranch) { if (!is_string($targetBranch)) { $this->warnings[] = 'extra.branch-alias.'.$sourceBranch.' : the target branch ('.json_encode($targetBranch).') must be a string, "'.gettype($targetBranch).'" received.'; unset($this->config['extra']['branch-alias'][$sourceBranch]); continue; } if ('-dev' !== substr($targetBranch, -4)) { $this->warnings[] = 'extra.branch-alias.'.$sourceBranch.' : the target branch ('.$targetBranch.') must end in -dev'; unset($this->config['extra']['branch-alias'][$sourceBranch]); continue; } $validatedTargetBranch = $this->versionParser->normalizeBranch(substr($targetBranch, 0, -4)); if ('-dev' !== substr($validatedTargetBranch, -4)) { $this->warnings[] = 'extra.branch-alias.'.$sourceBranch.' : the target branch ('.$targetBranch.') must be a parseable number like 2.0-dev'; unset($this->config['extra']['branch-alias'][$sourceBranch]); continue; } if (($sourcePrefix = $this->versionParser->parseNumericAliasPrefix($sourceBranch)) && ($targetPrefix = $this->versionParser->parseNumericAliasPrefix($targetBranch)) && (stripos($targetPrefix, $sourcePrefix) !== 0) ) { $this->warnings[] = 'extra.branch-alias.'.$sourceBranch.' : the target branch ('.$targetBranch.') is not a valid numeric alias for this version'; unset($this->config['extra']['branch-alias'][$sourceBranch]); } } } } if ($this->errors) { throw new InvalidPackageException($this->errors, $this->warnings, $config); } $package = $this->loader->load($this->config, $class); $this->config = []; return $package; } public function getWarnings(): array { return $this->warnings; } public function getErrors(): array { return $this->errors; } public static function hasPackageNamingError(string $name, bool $isLink = false): ?string { if (PlatformRepository::isPlatformPackage($name)) { return null; } if (!Preg::isMatch('{^[a-z0-9](?:[_.-]?[a-z0-9]++)*+/[a-z0-9](?:(?:[_.]|-{1,2})?[a-z0-9]++)*+$}iD', $name)) { return $name.' is invalid, it should have a vendor name, a forward slash, and a package name. The vendor and package name can be words separated by -, . or _. The complete name should match "^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]?|-{0,2})[a-z0-9]+)*$".'; } $reservedNames = ['nul', 'con', 'prn', 'aux', 'com1', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9', 'lpt1', 'lpt2', 'lpt3', 'lpt4', 'lpt5', 'lpt6', 'lpt7', 'lpt8', 'lpt9']; $bits = explode('/', strtolower($name)); if (in_array($bits[0], $reservedNames, true) || in_array($bits[1], $reservedNames, true)) { return $name.' is reserved, package and vendor names can not match any of: '.implode(', ', $reservedNames).'.'; } if (Preg::isMatch('{\.json$}', $name)) { return $name.' is invalid, package names can not end in .json, consider renaming it or perhaps using a -json suffix instead.'; } if (Preg::isMatch('{[A-Z]}', $name)) { if ($isLink) { return $name.' is invalid, it should not contain uppercase characters. Please use '.strtolower($name).' instead.'; } $suggestName = Preg::replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $name); $suggestName = strtolower($suggestName); return $name.' is invalid, it should not contain uppercase characters. We suggest using '.$suggestName.' instead.'; } return null; } private function validateRegex(string $property, string $regex, bool $mandatory = false): bool { if (!$this->validateString($property, $mandatory)) { return false; } if (!Preg::isMatch('{^'.$regex.'$}u', $this->config[$property])) { $message = $property.' : invalid value ('.$this->config[$property].'), must match '.$regex; if ($mandatory) { $this->errors[] = $message; } else { $this->warnings[] = $message; } unset($this->config[$property]); return false; } return true; } private function validateString(string $property, bool $mandatory = false): bool { if (isset($this->config[$property]) && !is_string($this->config[$property])) { $this->errors[] = $property.' : should be a string, '.gettype($this->config[$property]).' given'; unset($this->config[$property]); return false; } if (!isset($this->config[$property]) || trim($this->config[$property]) === '') { if ($mandatory) { $this->errors[] = $property.' : must be present'; } unset($this->config[$property]); return false; } return true; } private function validateArray(string $property, bool $mandatory = false): bool { if (isset($this->config[$property]) && !is_array($this->config[$property])) { $this->errors[] = $property.' : should be an array, '.gettype($this->config[$property]).' given'; unset($this->config[$property]); return false; } if (!isset($this->config[$property]) || !count($this->config[$property])) { if ($mandatory) { $this->errors[] = $property.' : must be present and contain at least one element'; } unset($this->config[$property]); return false; } return true; } private function validateFlatArray(string $property, ?string $regex = null, bool $mandatory = false): bool { if (!$this->validateArray($property, $mandatory)) { return false; } $pass = true; foreach ($this->config[$property] as $key => $value) { if (!is_string($value) && !is_numeric($value)) { $this->errors[] = $property.'.'.$key.' : must be a string or int, '.gettype($value).' given'; unset($this->config[$property][$key]); $pass = false; continue; } if ($regex && !Preg::isMatch('{^'.$regex.'$}u', (string) $value)) { $this->warnings[] = $property.'.'.$key.' : invalid value ('.$value.'), must match '.$regex; unset($this->config[$property][$key]); $pass = false; } } return $pass; } private function validateUrl(string $property, bool $mandatory = false): bool { if (!$this->validateString($property, $mandatory)) { return false; } if (!$this->filterUrl($this->config[$property])) { $this->warnings[] = $property.' : invalid value ('.$this->config[$property].'), must be an http/https URL'; unset($this->config[$property]); return false; } return true; } private function filterUrl($value, array $schemes = ['http', 'https']): bool { if ($value === '') { return true; } $bits = parse_url($value); if (empty($bits['scheme']) || empty($bits['host'])) { return false; } if (!in_array($bits['scheme'], $schemes, true)) { return false; } return true; } } lockFile = $lockFile; $this->installationManager = $installationManager; $this->hash = md5($composerFileContents); $this->contentHash = self::getContentHash($composerFileContents); $this->loader = new ArrayLoader(null, true); $this->dumper = new ArrayDumper(); $this->process = $process ?? new ProcessExecutor($io); } public static function getContentHash(string $composerFileContents): string { $content = JsonFile::parseJson($composerFileContents, 'composer.json'); $relevantKeys = [ 'name', 'version', 'require', 'require-dev', 'conflict', 'replace', 'provide', 'minimum-stability', 'prefer-stable', 'repositories', 'extra', ]; $relevantContent = []; foreach (array_intersect($relevantKeys, array_keys($content)) as $key) { $relevantContent[$key] = $content[$key]; } if (isset($content['config']['platform'])) { $relevantContent['config']['platform'] = $content['config']['platform']; } ksort($relevantContent); return md5(JsonFile::encode($relevantContent, 0)); } public function isLocked(): bool { if (!$this->virtualFileWritten && !$this->lockFile->exists()) { return false; } $data = $this->getLockData(); return isset($data['packages']); } public function isFresh(): bool { $lock = $this->lockFile->read(); if (!empty($lock['content-hash'])) { return $this->contentHash === $lock['content-hash']; } if (!empty($lock['hash'])) { return $this->hash === $lock['hash']; } return false; } public function getLockedRepository(bool $withDevReqs = false): LockArrayRepository { $lockData = $this->getLockData(); $packages = new LockArrayRepository(); $lockedPackages = $lockData['packages']; if ($withDevReqs) { if (isset($lockData['packages-dev'])) { $lockedPackages = array_merge($lockedPackages, $lockData['packages-dev']); } else { throw new \RuntimeException('The lock file does not contain require-dev information, run install with the --no-dev option or delete it and run composer update to generate a new lock file.'); } } if (empty($lockedPackages)) { return $packages; } if (isset($lockedPackages[0]['name'])) { $packageByName = []; foreach ($lockedPackages as $info) { $package = $this->loader->load($info); $packages->addPackage($package); $packageByName[$package->getName()] = $package; if ($package instanceof AliasPackage) { $packageByName[$package->getAliasOf()->getName()] = $package->getAliasOf(); } } if (isset($lockData['aliases'])) { foreach ($lockData['aliases'] as $alias) { if (isset($packageByName[$alias['package']])) { $aliasPkg = new CompleteAliasPackage($packageByName[$alias['package']], $alias['alias_normalized'], $alias['alias']); $aliasPkg->setRootPackageAlias(true); $packages->addPackage($aliasPkg); } } } return $packages; } throw new \RuntimeException('Your composer.lock is invalid. Run "composer update" to generate a new one.'); } public function getDevPackageNames(): array { $names = []; $lockData = $this->getLockData(); if (isset($lockData['packages-dev'])) { foreach ($lockData['packages-dev'] as $package) { $names[] = strtolower($package['name']); } } return $names; } public function getPlatformRequirements(bool $withDevReqs = false): array { $lockData = $this->getLockData(); $requirements = []; if (!empty($lockData['platform'])) { $requirements = $this->loader->parseLinks( '__root__', '1.0.0', Link::TYPE_REQUIRE, $lockData['platform'] ?? [] ); } if ($withDevReqs && !empty($lockData['platform-dev'])) { $devRequirements = $this->loader->parseLinks( '__root__', '1.0.0', Link::TYPE_REQUIRE, $lockData['platform-dev'] ?? [] ); $requirements = array_merge($requirements, $devRequirements); } return $requirements; } public function getMinimumStability(): string { $lockData = $this->getLockData(); return $lockData['minimum-stability'] ?? 'stable'; } public function getStabilityFlags(): array { $lockData = $this->getLockData(); return $lockData['stability-flags'] ?? []; } public function getPreferStable(): ?bool { $lockData = $this->getLockData(); return $lockData['prefer-stable'] ?? null; } public function getPreferLowest(): ?bool { $lockData = $this->getLockData(); return $lockData['prefer-lowest'] ?? null; } public function getPlatformOverrides(): array { $lockData = $this->getLockData(); return $lockData['platform-overrides'] ?? []; } public function getAliases(): array { $lockData = $this->getLockData(); return $lockData['aliases'] ?? []; } public function getPluginApi() { $lockData = $this->getLockData(); return $lockData['plugin-api-version'] ?? '1.1.0'; } public function getLockData(): array { if (null !== $this->lockDataCache) { return $this->lockDataCache; } if (!$this->lockFile->exists()) { throw new \LogicException('No lockfile found. Unable to read locked packages'); } return $this->lockDataCache = $this->lockFile->read(); } public function setLockData(array $packages, ?array $devPackages, array $platformReqs, array $platformDevReqs, array $aliases, string $minimumStability, array $stabilityFlags, bool $preferStable, bool $preferLowest, array $platformOverrides, bool $write = true): bool { $aliases = array_map(static function ($alias): array { if (in_array($alias['version'], ['dev-master', 'dev-trunk', 'dev-default'], true)) { $alias['version'] = VersionParser::DEFAULT_BRANCH_ALIAS; } return $alias; }, $aliases); $lock = [ '_readme' => ['This file locks the dependencies of your project to a known state', 'Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies', 'This file is @gener'.'ated automatically', ], 'content-hash' => $this->contentHash, 'packages' => null, 'packages-dev' => null, 'aliases' => $aliases, 'minimum-stability' => $minimumStability, 'stability-flags' => $stabilityFlags, 'prefer-stable' => $preferStable, 'prefer-lowest' => $preferLowest, ]; $lock['packages'] = $this->lockPackages($packages); if (null !== $devPackages) { $lock['packages-dev'] = $this->lockPackages($devPackages); } $lock['platform'] = $platformReqs; $lock['platform-dev'] = $platformDevReqs; if (\count($platformOverrides) > 0) { $lock['platform-overrides'] = $platformOverrides; } $lock['plugin-api-version'] = PluginInterface::PLUGIN_API_VERSION; try { $isLocked = $this->isLocked(); } catch (ParsingException $e) { $isLocked = false; } if (!$isLocked || $lock !== $this->getLockData()) { if ($write) { $this->lockFile->write($lock); $this->lockDataCache = null; $this->virtualFileWritten = false; } else { $this->virtualFileWritten = true; $this->lockDataCache = JsonFile::parseJson(JsonFile::encode($lock)); } return true; } return false; } private function lockPackages(array $packages): array { $locked = []; foreach ($packages as $package) { if ($package instanceof AliasPackage) { continue; } $name = $package->getPrettyName(); $version = $package->getPrettyVersion(); if (!$name || !$version) { throw new \LogicException(sprintf( 'Package "%s" has no version or name and can not be locked', $package )); } $spec = $this->dumper->dump($package); unset($spec['version_normalized']); $time = $spec['time'] ?? null; unset($spec['time']); if ($package->isDev() && $package->getInstallationSource() === 'source') { $time = $this->getPackageTime($package) ?: $time; } if (null !== $time) { $spec['time'] = $time; } unset($spec['installation-source']); $locked[] = $spec; } usort($locked, static function ($a, $b) { $comparison = strcmp($a['name'], $b['name']); if (0 !== $comparison) { return $comparison; } return strcmp($a['version'], $b['version']); }); return $locked; } private function getPackageTime(PackageInterface $package): ?string { if (!function_exists('proc_open')) { return null; } $path = realpath($this->installationManager->getInstallPath($package)); $sourceType = $package->getSourceType(); $datetime = null; if ($path && in_array($sourceType, ['git', 'hg'])) { $sourceRef = $package->getSourceReference() ?: $package->getDistReference(); switch ($sourceType) { case 'git': GitUtil::cleanEnv(); if (0 === $this->process->execute('git log -n1 --pretty=%ct '.ProcessExecutor::escape($sourceRef).GitUtil::getNoShowSignatureFlag($this->process), $output, $path) && Preg::isMatch('{^\s*\d+\s*$}', $output)) { $datetime = new \DateTime('@'.trim($output), new \DateTimeZone('UTC')); } break; case 'hg': if (0 === $this->process->execute('hg log --template "{date|hgdate}" -r '.ProcessExecutor::escape($sourceRef), $output, $path) && Preg::isMatch('{^\s*(\d+)\s*}', $output, $match)) { $datetime = new \DateTime('@'.$match[1], new \DateTimeZone('UTC')); } break; } } return $datetime ? $datetime->format(DATE_RFC3339) : null; } } version = $version; $this->prettyVersion = $prettyVersion; $this->stability = VersionParser::parseStability($version); $this->dev = $this->stability === 'dev'; } public function isDev(): bool { return $this->dev; } public function setType(string $type): void { $this->type = $type; } public function getType(): string { return $this->type ?: 'library'; } public function getStability(): string { return $this->stability; } public function setTargetDir(?string $targetDir): void { $this->targetDir = $targetDir; } public function getTargetDir(): ?string { if (null === $this->targetDir) { return null; } return ltrim(Preg::replace('{ (?:^|[\\\\/]+) \.\.? (?:[\\\\/]+|$) (?:\.\.? (?:[\\\\/]+|$) )*}x', '/', $this->targetDir), '/'); } public function setExtra(array $extra): void { $this->extra = $extra; } public function getExtra(): array { return $this->extra; } public function setBinaries(array $binaries): void { $this->binaries = $binaries; } public function getBinaries(): array { return $this->binaries; } public function setInstallationSource(?string $type): void { $this->installationSource = $type; } public function getInstallationSource(): ?string { return $this->installationSource; } public function setSourceType(?string $type): void { $this->sourceType = $type; } public function getSourceType(): ?string { return $this->sourceType; } public function setSourceUrl(?string $url): void { $this->sourceUrl = $url; } public function getSourceUrl(): ?string { return $this->sourceUrl; } public function setSourceReference(?string $reference): void { $this->sourceReference = $reference; } public function getSourceReference(): ?string { return $this->sourceReference; } public function setSourceMirrors(?array $mirrors): void { $this->sourceMirrors = $mirrors; } public function getSourceMirrors(): ?array { return $this->sourceMirrors; } public function getSourceUrls(): array { return $this->getUrls($this->sourceUrl, $this->sourceMirrors, $this->sourceReference, $this->sourceType, 'source'); } public function setDistType(?string $type): void { $this->distType = $type; } public function getDistType(): ?string { return $this->distType; } public function setDistUrl(?string $url): void { $this->distUrl = $url; } public function getDistUrl(): ?string { return $this->distUrl; } public function setDistReference(?string $reference): void { $this->distReference = $reference; } public function getDistReference(): ?string { return $this->distReference; } public function setDistSha1Checksum(?string $sha1checksum): void { $this->distSha1Checksum = $sha1checksum; } public function getDistSha1Checksum(): ?string { return $this->distSha1Checksum; } public function setDistMirrors(?array $mirrors): void { $this->distMirrors = $mirrors; } public function getDistMirrors(): ?array { return $this->distMirrors; } public function getDistUrls(): array { return $this->getUrls($this->distUrl, $this->distMirrors, $this->distReference, $this->distType, 'dist'); } public function getTransportOptions(): array { return $this->transportOptions; } public function setTransportOptions(array $options): void { $this->transportOptions = $options; } public function getVersion(): string { return $this->version; } public function getPrettyVersion(): string { return $this->prettyVersion; } public function setReleaseDate(?\DateTimeInterface $releaseDate): void { $this->releaseDate = $releaseDate; } public function getReleaseDate(): ?\DateTimeInterface { return $this->releaseDate; } public function setRequires(array $requires): void { if (isset($requires[0])) { $requires = $this->convertLinksToMap($requires, 'setRequires'); } $this->requires = $requires; } public function getRequires(): array { return $this->requires; } public function setConflicts(array $conflicts): void { if (isset($conflicts[0])) { $conflicts = $this->convertLinksToMap($conflicts, 'setConflicts'); } $this->conflicts = $conflicts; } public function getConflicts(): array { return $this->conflicts; } public function setProvides(array $provides): void { if (isset($provides[0])) { $provides = $this->convertLinksToMap($provides, 'setProvides'); } $this->provides = $provides; } public function getProvides(): array { return $this->provides; } public function setReplaces(array $replaces): void { if (isset($replaces[0])) { $replaces = $this->convertLinksToMap($replaces, 'setReplaces'); } $this->replaces = $replaces; } public function getReplaces(): array { return $this->replaces; } public function setDevRequires(array $devRequires): void { if (isset($devRequires[0])) { $devRequires = $this->convertLinksToMap($devRequires, 'setDevRequires'); } $this->devRequires = $devRequires; } public function getDevRequires(): array { return $this->devRequires; } public function setSuggests(array $suggests): void { $this->suggests = $suggests; } public function getSuggests(): array { return $this->suggests; } public function setAutoload(array $autoload): void { $this->autoload = $autoload; } public function getAutoload(): array { return $this->autoload; } public function setDevAutoload(array $devAutoload): void { $this->devAutoload = $devAutoload; } public function getDevAutoload(): array { return $this->devAutoload; } public function setIncludePaths(array $includePaths): void { $this->includePaths = $includePaths; } public function getIncludePaths(): array { return $this->includePaths; } public function setNotificationUrl(string $notificationUrl): void { $this->notificationUrl = $notificationUrl; } public function getNotificationUrl(): ?string { return $this->notificationUrl; } public function setIsDefaultBranch(bool $defaultBranch): void { $this->isDefaultBranch = $defaultBranch; } public function isDefaultBranch(): bool { return $this->isDefaultBranch; } public function setSourceDistReferences(string $reference): void { $this->setSourceReference($reference); if ( $this->getDistUrl() !== null && Preg::isMatch('{^https?://(?:(?:www\.)?bitbucket\.org|(api\.)?github\.com|(?:www\.)?gitlab\.com)/}i', $this->getDistUrl()) ) { $this->setDistReference($reference); $this->setDistUrl(Preg::replace('{(?<=/|sha=)[a-f0-9]{40}(?=/|$)}i', $reference, $this->getDistUrl())); } elseif ($this->getDistReference()) { $this->setDistReference($reference); } } public function replaceVersion(string $version, string $prettyVersion): void { $this->version = $version; $this->prettyVersion = $prettyVersion; $this->stability = VersionParser::parseStability($version); $this->dev = $this->stability === 'dev'; } protected function getUrls(?string $url, ?array $mirrors, ?string $ref, ?string $type, string $urlType): array { if (!$url) { return []; } if ($urlType === 'dist' && false !== strpos($url, '%')) { $url = ComposerMirror::processUrl($url, $this->name, $this->version, $ref, $type, $this->prettyVersion); } $urls = [$url]; if ($mirrors) { foreach ($mirrors as $mirror) { if ($urlType === 'dist') { $mirrorUrl = ComposerMirror::processUrl($mirror['url'], $this->name, $this->version, $ref, $type, $this->prettyVersion); } elseif ($urlType === 'source' && $type === 'git') { $mirrorUrl = ComposerMirror::processGitUrl($mirror['url'], $this->name, $url, $type); } elseif ($urlType === 'source' && $type === 'hg') { $mirrorUrl = ComposerMirror::processHgUrl($mirror['url'], $this->name, $url, $type); } else { continue; } if (!\in_array($mirrorUrl, $urls)) { $func = $mirror['preferred'] ? 'array_unshift' : 'array_push'; $func($urls, $mirrorUrl); } } } return $urls; } private function convertLinksToMap(array $links, string $source): array { trigger_error('Package::'.$source.' must be called with a map of lowercased package name => Link object, got a indexed array, this is deprecated and you should fix your usage.'); $newLinks = []; foreach ($links as $link) { $newLinks[$link->getTarget()] = $link; } return $newLinks; } } aliasOf; } public function getAliases(): array { return $this->aliasOf->getAliases(); } public function getMinimumStability(): string { return $this->aliasOf->getMinimumStability(); } public function getStabilityFlags(): array { return $this->aliasOf->getStabilityFlags(); } public function getReferences(): array { return $this->aliasOf->getReferences(); } public function getPreferStable(): bool { return $this->aliasOf->getPreferStable(); } public function getConfig(): array { return $this->aliasOf->getConfig(); } public function setRequires(array $requires): void { $this->requires = $this->replaceSelfVersionDependencies($requires, Link::TYPE_REQUIRE); $this->aliasOf->setRequires($requires); } public function setDevRequires(array $devRequires): void { $this->devRequires = $this->replaceSelfVersionDependencies($devRequires, Link::TYPE_DEV_REQUIRE); $this->aliasOf->setDevRequires($devRequires); } public function setConflicts(array $conflicts): void { $this->conflicts = $this->replaceSelfVersionDependencies($conflicts, Link::TYPE_CONFLICT); $this->aliasOf->setConflicts($conflicts); } public function setProvides(array $provides): void { $this->provides = $this->replaceSelfVersionDependencies($provides, Link::TYPE_PROVIDE); $this->aliasOf->setProvides($provides); } public function setReplaces(array $replaces): void { $this->replaces = $this->replaceSelfVersionDependencies($replaces, Link::TYPE_REPLACE); $this->aliasOf->setReplaces($replaces); } public function setAutoload(array $autoload): void { $this->aliasOf->setAutoload($autoload); } public function setDevAutoload(array $devAutoload): void { $this->aliasOf->setDevAutoload($devAutoload); } public function setStabilityFlags(array $stabilityFlags): void { $this->aliasOf->setStabilityFlags($stabilityFlags); } public function setMinimumStability(string $minimumStability): void { $this->aliasOf->setMinimumStability($minimumStability); } public function setPreferStable(bool $preferStable): void { $this->aliasOf->setPreferStable($preferStable); } public function setConfig(array $config): void { $this->aliasOf->setConfig($config); } public function setReferences(array $references): void { $this->aliasOf->setReferences($references); } public function setAliases(array $aliases): void { $this->aliasOf->setAliases($aliases); } public function setSuggests(array $suggests): void { $this->aliasOf->setSuggests($suggests); } public function setExtra(array $extra): void { $this->aliasOf->setExtra($extra); } public function __clone() { parent::__clone(); $this->aliasOf = clone $this->aliasOf; } } minimumStability = $minimumStability; } public function getMinimumStability(): string { return $this->minimumStability; } public function setStabilityFlags(array $stabilityFlags): void { $this->stabilityFlags = $stabilityFlags; } public function getStabilityFlags(): array { return $this->stabilityFlags; } public function setPreferStable(bool $preferStable): void { $this->preferStable = $preferStable; } public function getPreferStable(): bool { return $this->preferStable; } public function setConfig(array $config): void { $this->config = $config; } public function getConfig(): array { return $this->config; } public function setReferences(array $references): void { $this->references = $references; } public function getReferences(): array { return $this->references; } public function setAliases(array $aliases): void { $this->aliases = $aliases; } public function getAliases(): array { return $this->aliases; } } getPrettyString(); if (str_starts_with($constraint->getPrettyString(), 'dev-')) { return $prettyConstraint; } $version = $package->getVersion(); if (str_starts_with($package->getVersion(), 'dev-')) { $loader = new ArrayLoader($parser); $dumper = new ArrayDumper(); $extra = $loader->getBranchAlias($dumper->dump($package)); if (null === $extra || $extra === VersionParser::DEFAULT_BRANCH_ALIAS) { return $prettyConstraint; } $version = $extra; } $intervals = Intervals::get($constraint); if (\count($intervals['branches']['names']) > 0) { return $prettyConstraint; } $major = Preg::replace('{^(\d+).*}', '$1', $version); $newPrettyConstraint = '^'.Preg::replace('{(?:\.(?:0|9999999))+(-dev)?$}', '', $version); if (!Preg::isMatch('{^\^\d+(\.\d+)*$}', $newPrettyConstraint)) { return $prettyConstraint; } $pattern = '{ (?<=,|\ |\||^) # leading separator (?P \^'.$major.'(?:\.\d+)* # e.g. ^2.anything | ~'.$major.'(?:\.\d+)? # e.g. ~2 or ~2.2 but no more | '.$major.'(?:\.[*x])+ # e.g. 2.* or 2.*.* or 2.x.x.x etc ) (?=,|$|\ |\||@) # trailing separator }x'; if (Preg::isMatchAllWithOffsets($pattern, $prettyConstraint, $matches)) { $modified = $prettyConstraint; foreach (array_reverse($matches['constraint']) as $match) { $modified = substr_replace($modified, $newPrettyConstraint, $match[1], Platform::strlen($match[0])); } $newConstraint = $parser->parseConstraints($modified); if (Intervals::isSubsetOf($newConstraint, $constraint) && Intervals::isSubsetOf($constraint, $newConstraint)) { return $prettyConstraint; } return $modified; } return $prettyConstraint; } } config = $config; $this->process = $process; $this->versionParser = $versionParser; } public function guessVersion(array $packageConfig, string $path): ?array { if (!function_exists('proc_open')) { return null; } if (Platform::isInputCompletionProcess()) { return null; } $versionData = $this->guessGitVersion($packageConfig, $path); if (null !== $versionData && null !== $versionData['version']) { return $this->postprocess($versionData); } $versionData = $this->guessHgVersion($packageConfig, $path); if (null !== $versionData && null !== $versionData['version']) { return $this->postprocess($versionData); } $versionData = $this->guessFossilVersion($path); if (null !== $versionData && null !== $versionData['version']) { return $this->postprocess($versionData); } $versionData = $this->guessSvnVersion($packageConfig, $path); if (null !== $versionData && null !== $versionData['version']) { return $this->postprocess($versionData); } return null; } private function postprocess(array $versionData): array { if (!empty($versionData['feature_version']) && $versionData['feature_version'] === $versionData['version'] && $versionData['feature_pretty_version'] === $versionData['pretty_version']) { unset($versionData['feature_version'], $versionData['feature_pretty_version']); } if ('-dev' === substr($versionData['version'], -4) && Preg::isMatch('{\.9{7}}', $versionData['version'])) { $versionData['pretty_version'] = Preg::replace('{(\.9{7})+}', '.x', $versionData['version']); } if (!empty($versionData['feature_version']) && '-dev' === substr($versionData['feature_version'], -4) && Preg::isMatch('{\.9{7}}', $versionData['feature_version'])) { $versionData['feature_pretty_version'] = Preg::replace('{(\.9{7})+}', '.x', $versionData['feature_version']); } return $versionData; } private function guessGitVersion(array $packageConfig, string $path): array { GitUtil::cleanEnv(); $commit = null; $version = null; $prettyVersion = null; $featureVersion = null; $featurePrettyVersion = null; $isDetached = false; if (0 === $this->process->execute(['git', 'branch', '-a', '--no-color', '--no-abbrev', '-v'], $output, $path)) { $branches = []; $isFeatureBranch = false; foreach ($this->process->splitLines($output) as $branch) { if ($branch && Preg::isMatch('{^(?:\* ) *(\(no branch\)|\(detached from \S+\)|\(HEAD detached at \S+\)|\S+) *([a-f0-9]+) .*$}', $branch, $match)) { if ( $match[1] === '(no branch)' || strpos($match[1], '(detached ') === 0 || strpos($match[1], '(HEAD detached at') === 0 ) { $version = 'dev-' . $match[2]; $prettyVersion = $version; $isFeatureBranch = true; $isDetached = true; } else { $version = $this->versionParser->normalizeBranch($match[1]); $prettyVersion = 'dev-' . $match[1]; $isFeatureBranch = $this->isFeatureBranch($packageConfig, $match[1]); } if ($match[2]) { $commit = $match[2]; } } if ($branch && !Preg::isMatch('{^ *.+/HEAD }', $branch)) { if (Preg::isMatch('{^(?:\* )? *((?:remotes/(?:origin|upstream)/)?[^\s/]+) *([a-f0-9]+) .*$}', $branch, $match)) { $branches[] = $match[1]; } } } if ($isFeatureBranch) { $featureVersion = $version; $featurePrettyVersion = $prettyVersion; $result = $this->guessFeatureVersion($packageConfig, $version, $branches, 'git rev-list %candidate%..%branch%', $path); $version = $result['version']; $prettyVersion = $result['pretty_version']; } } if (!$version || $isDetached) { $result = $this->versionFromGitTags($path); if ($result) { $version = $result['version']; $prettyVersion = $result['pretty_version']; $featureVersion = null; $featurePrettyVersion = null; } } if (!$commit) { $command = 'git log --pretty="%H" -n1 HEAD'.GitUtil::getNoShowSignatureFlag($this->process); if (0 === $this->process->execute($command, $output, $path)) { $commit = trim($output) ?: null; } } if ($featureVersion) { return ['version' => $version, 'commit' => $commit, 'pretty_version' => $prettyVersion, 'feature_version' => $featureVersion, 'feature_pretty_version' => $featurePrettyVersion]; } return ['version' => $version, 'commit' => $commit, 'pretty_version' => $prettyVersion]; } private function versionFromGitTags(string $path): ?array { if (0 === $this->process->execute('git describe --exact-match --tags', $output, $path)) { try { $version = $this->versionParser->normalize(trim($output)); return ['version' => $version, 'pretty_version' => trim($output)]; } catch (\Exception $e) { } } return null; } private function guessHgVersion(array $packageConfig, string $path): ?array { if (0 === $this->process->execute('hg branch', $output, $path)) { $branch = trim($output); $version = $this->versionParser->normalizeBranch($branch); $isFeatureBranch = 0 === strpos($version, 'dev-'); if (VersionParser::DEFAULT_BRANCH_ALIAS === $version) { return ['version' => $version, 'commit' => null, 'pretty_version' => 'dev-'.$branch]; } if (!$isFeatureBranch) { return ['version' => $version, 'commit' => null, 'pretty_version' => $version]; } $io = new NullIO(); $driver = new HgDriver(['url' => $path], $io, $this->config, new HttpDownloader($io, $this->config), $this->process); $branches = array_map('strval', array_keys($driver->getBranches())); $result = $this->guessFeatureVersion($packageConfig, $version, $branches, 'hg log -r "not ancestors(\'%candidate%\') and ancestors(\'%branch%\')" --template "{node}\\n"', $path); $result['commit'] = ''; $result['feature_version'] = $version; $result['feature_pretty_version'] = $version; return $result; } return null; } private function guessFeatureVersion(array $packageConfig, ?string $version, array $branches, string $scmCmdline, string $path): array { $prettyVersion = $version; if (!isset($packageConfig['extra']['branch-alias'][$version]) || strpos(json_encode($packageConfig), '"self.version"') ) { $branch = Preg::replace('{^dev-}', '', $version); $length = PHP_INT_MAX; if (!$this->isFeatureBranch($packageConfig, $branch)) { return ['version' => $version, 'pretty_version' => $prettyVersion]; } usort($branches, static function ($a, $b): int { $aRemote = 0 === strpos($a, 'remotes/'); $bRemote = 0 === strpos($b, 'remotes/'); if ($aRemote !== $bRemote) { return $aRemote ? 1 : -1; } return strnatcasecmp($b, $a); }); $promises = []; $this->process->setMaxJobs(30); try { foreach ($branches as $candidate) { $candidateVersion = Preg::replace('{^remotes/\S+/}', '', $candidate); if ($candidate === $branch || $this->isFeatureBranch($packageConfig, $candidateVersion)) { continue; } $cmdLine = str_replace(['%candidate%', '%branch%'], [$candidate, $branch], $scmCmdline); $promises[] = $this->process->executeAsync($cmdLine, $path)->then(function (Process $process) use (&$length, &$version, &$prettyVersion, $candidateVersion, &$promises): void { if (!$process->isSuccessful()) { return; } $output = $process->getOutput(); if (strlen($output) < $length) { $length = strlen($output); $version = $this->versionParser->normalizeBranch($candidateVersion); $prettyVersion = 'dev-' . $candidateVersion; if ($length === 0) { foreach ($promises as $promise) { if ($promise instanceof CancellablePromiseInterface) { $promise->cancel(); } } } } }); } $this->process->wait(); } finally { $this->process->resetMaxJobs(); } } return ['version' => $version, 'pretty_version' => $prettyVersion]; } private function isFeatureBranch(array $packageConfig, ?string $branchName): bool { $nonFeatureBranches = ''; if (!empty($packageConfig['non-feature-branches'])) { $nonFeatureBranches = implode('|', $packageConfig['non-feature-branches']); } return !Preg::isMatch('{^(' . $nonFeatureBranches . '|master|main|latest|next|current|support|tip|trunk|default|develop|\d+\..+)$}', $branchName, $match); } private function guessFossilVersion(string $path): array { $version = null; $prettyVersion = null; if (0 === $this->process->execute('fossil branch list', $output, $path)) { $branch = trim($output); $version = $this->versionParser->normalizeBranch($branch); $prettyVersion = 'dev-' . $branch; } if (0 === $this->process->execute('fossil tag list', $output, $path)) { try { $version = $this->versionParser->normalize(trim($output)); $prettyVersion = trim($output); } catch (\Exception $e) { } } return ['version' => $version, 'commit' => '', 'pretty_version' => $prettyVersion]; } private function guessSvnVersion(array $packageConfig, string $path): ?array { SvnUtil::cleanEnv(); if (0 === $this->process->execute('svn info --xml', $output, $path)) { $trunkPath = isset($packageConfig['trunk-path']) ? preg_quote($packageConfig['trunk-path'], '#') : 'trunk'; $branchesPath = isset($packageConfig['branches-path']) ? preg_quote($packageConfig['branches-path'], '#') : 'branches'; $tagsPath = isset($packageConfig['tags-path']) ? preg_quote($packageConfig['tags-path'], '#') : 'tags'; $urlPattern = '#.*/(' . $trunkPath . '|(' . $branchesPath . '|' . $tagsPath . ')/(.*))#'; if (Preg::isMatch($urlPattern, $output, $matches)) { if (isset($matches[2]) && ($branchesPath === $matches[2] || $tagsPath === $matches[2])) { $version = $this->versionParser->normalizeBranch($matches[3]); $prettyVersion = 'dev-' . $matches[3]; return ['version' => $version, 'commit' => '', 'pretty_version' => $prettyVersion]; } $prettyVersion = trim($matches[1]); if ($prettyVersion === 'trunk') { $version = 'dev-trunk'; } else { $version = $this->versionParser->normalize($prettyVersion); } return ['version' => $version, 'commit' => '', 'pretty_version' => $prettyVersion]; } } return null; } } $name, 'version' => $version]; } else { $result[] = ['name' => $pair]; } } return $result; } public static function isUpgrade(string $normalizedFrom, string $normalizedTo): bool { if ($normalizedFrom === $normalizedTo) { return true; } if (in_array($normalizedFrom, ['dev-master', 'dev-trunk', 'dev-default'], true)) { $normalizedFrom = VersionParser::DEFAULT_BRANCH_ALIAS; } if (in_array($normalizedTo, ['dev-master', 'dev-trunk', 'dev-default'], true)) { $normalizedTo = VersionParser::DEFAULT_BRANCH_ALIAS; } if (strpos($normalizedFrom, 'dev-') === 0 || strpos($normalizedTo, 'dev-') === 0) { return true; } $sorted = Semver::sort([$normalizedTo, $normalizedFrom]); return $sorted[0] === $normalizedFrom; } } repositorySet = $repositorySet; if ($platformRepo) { foreach ($platformRepo->getPackages() as $package) { $this->platformConstraints[$package->getName()][] = new Constraint('==', $package->getVersion()); } } } public function findBestCandidate(string $packageName, ?string $targetPackageVersion = null, string $preferredStability = 'stable', $platformRequirementFilter = null, int $repoSetFlags = 0, ?IOInterface $io = null) { if (!isset(BasePackage::$stabilities[$preferredStability])) { throw new \UnexpectedValueException('Expected a valid stability name as 3rd argument, got '.$preferredStability); } if (null === $platformRequirementFilter) { $platformRequirementFilter = PlatformRequirementFilterFactory::ignoreNothing(); } elseif (!($platformRequirementFilter instanceof PlatformRequirementFilterInterface)) { trigger_error('VersionSelector::findBestCandidate with ignored platform reqs as bool|array is deprecated since Composer 2.2, use an instance of PlatformRequirementFilterInterface instead.', E_USER_DEPRECATED); $platformRequirementFilter = PlatformRequirementFilterFactory::fromBoolOrList($platformRequirementFilter); } $constraint = $targetPackageVersion ? $this->getParser()->parseConstraints($targetPackageVersion) : null; $candidates = $this->repositorySet->findPackages(strtolower($packageName), $constraint, $repoSetFlags); $minPriority = BasePackage::$stabilities[$preferredStability]; usort($candidates, static function (PackageInterface $a, PackageInterface $b) use ($minPriority) { $aPriority = $a->getStabilityPriority(); $bPriority = $b->getStabilityPriority(); if ($minPriority < $aPriority && $bPriority < $aPriority) { return 1; } if ($minPriority < $aPriority && $aPriority < $bPriority) { return -1; } if ($minPriority >= $aPriority && $minPriority < $bPriority) { return -1; } return version_compare($b->getVersion(), $a->getVersion()); }); if (count($this->platformConstraints) > 0 && !($platformRequirementFilter instanceof IgnoreAllPlatformRequirementFilter)) { $alreadyWarnedNames = []; foreach ($candidates as $pkg) { $reqs = $pkg->getRequires(); foreach ($reqs as $name => $link) { if (!PlatformRepository::isPlatformPackage($name) || $platformRequirementFilter->isIgnored($name)) { continue; } if (isset($this->platformConstraints[$name])) { foreach ($this->platformConstraints[$name] as $providedConstraint) { if ($link->getConstraint()->matches($providedConstraint)) { continue 2; } } $reason = 'is not satisfied by your platform'; } else { $reason = 'is missing from your platform'; } if ($io !== null) { $isFirst = !isset($alreadyWarnedNames[$pkg->getName()]); $alreadyWarnedNames[$pkg->getName()] = true; $latest = $isFirst ? "'s latest version" : ''; $io->writeError( 'Cannot use '.$pkg->getPrettyName().$latest.' '.$pkg->getPrettyVersion().' as it '.$link->getDescription().' '.$link->getTarget().' '.$link->getPrettyConstraint().' which '.$reason.'.', true, $isFirst ? IOInterface::NORMAL : IOInterface::VERBOSE ); } continue 2; } $package = $pkg; break; } } else { $package = count($candidates) > 0 ? $candidates[0] : null; } if (!isset($package)) { return false; } if ($package instanceof AliasPackage && $package->getVersion() === VersionParser::DEFAULT_BRANCH_ALIAS) { $package = $package->getAliasOf(); } return $package; } public function findRecommendedRequireVersion(PackageInterface $package): string { if (0 === strpos($package->getName(), 'ext-')) { $phpVersion = PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION; $extVersion = implode('.', array_slice(explode('.', $package->getVersion()), 0, 3)); if ($phpVersion === $extVersion) { return '*'; } } $version = $package->getVersion(); if (!$package->isDev()) { return $this->transformVersion($version, $package->getPrettyVersion(), $package->getStability()); } $loader = new ArrayLoader($this->getParser()); $dumper = new ArrayDumper(); $extra = $loader->getBranchAlias($dumper->dump($package)); if ($extra && $extra !== VersionParser::DEFAULT_BRANCH_ALIAS) { $extra = Preg::replace('{^(\d+\.\d+\.\d+)(\.9999999)-dev$}', '$1.0', $extra, -1, $count); if ($count) { $extra = str_replace('.9999999', '.0', $extra); return $this->transformVersion($extra, $extra, 'dev'); } } return $package->getPrettyVersion(); } private function transformVersion(string $version, string $prettyVersion, string $stability): string { $semanticVersionParts = explode('.', $version); if (count($semanticVersionParts) === 4 && Preg::isMatch('{^0\D?}', $semanticVersionParts[3])) { if ($semanticVersionParts[0] === '0') { unset($semanticVersionParts[3]); } else { unset($semanticVersionParts[2], $semanticVersionParts[3]); } $version = implode('.', $semanticVersionParts); } else { return $prettyVersion; } if ($stability !== 'stable') { $version .= '@'.$stability; } return '^' . $version; } private function getParser(): VersionParser { if ($this->parser === null) { $this->parser = new VersionParser(); } return $this->parser; } } package = $package; } public function getPackage(): RootPackageInterface { return $this->package; } public function setConfig(Config $config): void { $this->config = $config; } public function getConfig(): Config { return $this->config; } public function setLoop(Loop $loop): void { $this->loop = $loop; } public function getLoop(): Loop { return $this->loop; } public function setRepositoryManager(RepositoryManager $manager): void { $this->repositoryManager = $manager; } public function getRepositoryManager(): RepositoryManager { return $this->repositoryManager; } public function setInstallationManager(InstallationManager $manager): void { $this->installationManager = $manager; } public function getInstallationManager(): InstallationManager { return $this->installationManager; } public function setEventDispatcher(EventDispatcher $eventDispatcher): void { $this->eventDispatcher = $eventDispatcher; } public function getEventDispatcher(): EventDispatcher { return $this->eventDispatcher; } } executableFinder = $executableFinder; $this->processExecutor = $processExecutor; } public function reset(): void { self::$hhvmVersion = null; } public function getVersion(): ?string { if (null !== self::$hhvmVersion) { return self::$hhvmVersion ?: null; } self::$hhvmVersion = defined('HHVM_VERSION') ? HHVM_VERSION : null; if (self::$hhvmVersion === null && !Platform::isWindows()) { self::$hhvmVersion = false; $this->executableFinder = $this->executableFinder ?: new ExecutableFinder(); $hhvmPath = $this->executableFinder->find('hhvm'); if ($hhvmPath !== null) { $this->processExecutor = $this->processExecutor ?? new ProcessExecutor(); $exitCode = $this->processExecutor->execute( ProcessExecutor::escape($hhvmPath). ' --php -d hhvm.jit=0 -r "echo HHVM_VERSION;" 2>/dev/null', self::$hhvmVersion ); if ($exitCode !== 0) { self::$hhvmVersion = false; } } } return self::$hhvmVersion ?: null; } } newInstanceArgs($arguments); } public function getExtensions(): array { return get_loaded_extensions(); } public function getExtensionVersion(string $extension): string { $version = phpversion($extension); if ($version === false) { $version = '0'; } return $version; } public function getExtensionInfo(string $extension): string { $reflector = new \ReflectionExtension($extension); ob_start(); $reflector->info(); return ob_get_clean(); } } [0-9.]+)(?[a-z]{0,2})?(?(?:-?(?:dev|pre|alpha|beta|rc|fips)[\d]*)*)?(?-\w+)?(? \(.+?\))?$/', $opensslVersion, $matches)) { return null; } $patch = ''; if (version_compare($matches['version'], '3.0.0', '<')) { $patch = '.'.self::convertAlphaVersionToIntVersion($matches['patch']); } $isFips = strpos($matches['suffix'], 'fips') !== false; $suffix = strtr('-'.ltrim($matches['suffix'], '-'), ['-fips' => '', '-pre' => '-alpha']); return rtrim($matches['version'].$patch.$suffix, '-'); } public static function parseLibjpeg(string $libjpegVersion): ?string { if (!Preg::isMatch('/^(?\d+)(?[a-z]*)$/', $libjpegVersion, $matches)) { return null; } return $matches['major'].'.'.self::convertAlphaVersionToIntVersion($matches['minor']); } public static function parseZoneinfoVersion(string $zoneinfoVersion): ?string { if (!Preg::isMatch('/^(?\d{4})(?[a-z]*)$/', $zoneinfoVersion, $matches)) { return null; } return $matches['year'].'.'.self::convertAlphaVersionToIntVersion($matches['revision']); } private static function convertAlphaVersionToIntVersion(string $alpha): int { return strlen($alpha) * (-ord('a') + 1) + array_sum(array_map('ord', str_split($alpha))); } public static function convertLibxpmVersionId(int $versionId): string { return self::convertVersionId($versionId, 100); } public static function convertOpenldapVersionId(int $versionId): string { return self::convertVersionId($versionId, 100); } private static function convertVersionId(int $versionId, int $base): string { return sprintf( '%d.%d.%d', $versionId / ($base * $base), (int) ($versionId / $base) % $base, $versionId % $base ); } } commandName = $commandName; $this->input = $input; $this->output = $output; } public function getInput(): InputInterface { return $this->input; } public function getOutput(): OutputInterface { return $this->output; } public function getCommandName(): string { return $this->commandName; } } io = $io; $this->composer = $composer; $this->globalComposer = $globalComposer; $this->versionParser = new VersionParser(); $this->disablePlugins = $disablePlugins; $this->allowPluginRules = $this->parseAllowedPlugins($composer->getConfig()->get('allow-plugins'), $composer->getLocker()); $this->allowGlobalPluginRules = $this->parseAllowedPlugins($globalComposer !== null ? $globalComposer->getConfig()->get('allow-plugins') : false); } public function setRunningInGlobalDir(bool $runningInGlobalDir): void { $this->runningInGlobalDir = $runningInGlobalDir; } public function loadInstalledPlugins(): void { if (!$this->arePluginsDisabled('local')) { $repo = $this->composer->getRepositoryManager()->getLocalRepository(); $this->loadRepository($repo, false); } if ($this->globalComposer !== null && !$this->arePluginsDisabled('global')) { $this->loadRepository($this->globalComposer->getRepositoryManager()->getLocalRepository(), true); } } public function deactivateInstalledPlugins(): void { if (!$this->arePluginsDisabled('local')) { $repo = $this->composer->getRepositoryManager()->getLocalRepository(); $this->deactivateRepository($repo, false); } if ($this->globalComposer !== null && !$this->arePluginsDisabled('global')) { $this->deactivateRepository($this->globalComposer->getRepositoryManager()->getLocalRepository(), true); } } public function getPlugins(): array { return $this->plugins; } public function getGlobalComposer(): ?PartialComposer { return $this->globalComposer; } public function registerPackage(PackageInterface $package, bool $failOnMissingClasses = false, bool $isGlobalPlugin = false): void { if ($this->arePluginsDisabled($isGlobalPlugin ? 'global' : 'local')) { return; } if ($package->getType() === 'composer-plugin') { $requiresComposer = null; foreach ($package->getRequires() as $link) { if ('composer-plugin-api' === $link->getTarget()) { $requiresComposer = $link->getConstraint(); break; } } if (!$requiresComposer) { throw new \RuntimeException("Plugin ".$package->getName()." is missing a require statement for a version of the composer-plugin-api package."); } $currentPluginApiVersion = $this->getPluginApiVersion(); $currentPluginApiConstraint = new Constraint('==', $this->versionParser->normalize($currentPluginApiVersion)); if ($requiresComposer->getPrettyString() === $this->getPluginApiVersion()) { $this->io->writeError('The "' . $package->getName() . '" plugin requires composer-plugin-api '.$this->getPluginApiVersion().', this *WILL* break in the future and it should be fixed ASAP (require ^'.$this->getPluginApiVersion().' instead for example).'); } elseif (!$requiresComposer->matches($currentPluginApiConstraint)) { $this->io->writeError('The "' . $package->getName() . '" plugin '.($isGlobalPlugin || $this->runningInGlobalDir ? '(installed globally) ' : '').'was skipped because it requires a Plugin API version ("' . $requiresComposer->getPrettyString() . '") that does not match your Composer installation ("' . $currentPluginApiVersion . '"). You may need to run composer update with the "--no-plugins" option.'); return; } if ($package->getName() === 'symfony/flex' && Preg::isMatch('{^[0-9.]+$}', $package->getVersion()) && version_compare($package->getVersion(), '1.9.8', '<')) { $this->io->writeError('The "' . $package->getName() . '" plugin '.($isGlobalPlugin || $this->runningInGlobalDir ? '(installed globally) ' : '').'was skipped because it is not compatible with Composer 2+. Make sure to update it to version 1.9.8 or greater.'); return; } } if (!$this->isPluginAllowed($package->getName(), $isGlobalPlugin)) { $this->io->writeError('Skipped loading "'.$package->getName() . '" '.($isGlobalPlugin || $this->runningInGlobalDir ? '(installed globally) ' : '').'as it is not in config.allow-plugins', true, IOInterface::DEBUG); return; } $oldInstallerPlugin = ($package->getType() === 'composer-installer'); if (isset($this->registeredPlugins[$package->getName()])) { return; } $extra = $package->getExtra(); if (empty($extra['class'])) { throw new \UnexpectedValueException('Error while installing '.$package->getPrettyName().', composer-plugin packages should have a class defined in their extra key to be usable.'); } $classes = is_array($extra['class']) ? $extra['class'] : [$extra['class']]; $localRepo = $this->composer->getRepositoryManager()->getLocalRepository(); $globalRepo = $this->globalComposer !== null ? $this->globalComposer->getRepositoryManager()->getLocalRepository() : null; $rootPackage = clone $this->composer->getPackage(); $rootPackageAutoloads = $rootPackage->getAutoload(); $rootPackageAutoloads['files'] = []; $rootPackage->setAutoload($rootPackageAutoloads); $rootPackageAutoloads = $rootPackage->getDevAutoload(); $rootPackageAutoloads['files'] = []; $rootPackage->setDevAutoload($rootPackageAutoloads); unset($rootPackageAutoloads); $rootPackageRepo = new RootPackageRepository($rootPackage); $installedRepo = new InstalledRepository([$localRepo, $rootPackageRepo]); if ($globalRepo) { $installedRepo->addRepository($globalRepo); } $autoloadPackages = [$package->getName() => $package]; $autoloadPackages = $this->collectDependencies($installedRepo, $autoloadPackages, $package); $generator = $this->composer->getAutoloadGenerator(); $autoloads = [[$rootPackage, '']]; foreach ($autoloadPackages as $autoloadPackage) { if ($autoloadPackage === $rootPackage) { continue; } $downloadPath = $this->getInstallPath($autoloadPackage, $globalRepo && $globalRepo->hasPackage($autoloadPackage)); $autoloads[] = [$autoloadPackage, $downloadPath]; } $map = $generator->parseAutoloads($autoloads, $rootPackage); $classLoader = $generator->createLoader($map, $this->composer->getConfig()->get('vendor-dir')); $classLoader->register(false); foreach ($map['files'] as $fileIdentifier => $file) { if ($fileIdentifier === '7e9bd612cc444b3eed788ebbe46263a0') { continue; } \Composer\Autoload\composerRequire($fileIdentifier, $file); } foreach ($classes as $class) { if (class_exists($class, false)) { $class = trim($class, '\\'); $path = $classLoader->findFile($class); $code = file_get_contents($path); $separatorPos = strrpos($class, '\\'); $className = $class; if ($separatorPos) { $className = substr($class, $separatorPos + 1); } $code = Preg::replace('{^((?:final\s+)?(?:\s*))class\s+('.preg_quote($className).')}mi', '$1class $2_composer_tmp'.self::$classCounter, $code, 1); $code = strtr($code, [ '__FILE__' => var_export($path, true), '__DIR__' => var_export(dirname($path), true), '__CLASS__' => var_export($class, true), ]); $code = Preg::replace('/^\s*<\?(php)?/i', '', $code, 1); eval($code); $class .= '_composer_tmp'.self::$classCounter; self::$classCounter++; } if ($oldInstallerPlugin) { if (!is_a($class, 'Composer\Installer\InstallerInterface', true)) { throw new \RuntimeException('Could not activate plugin "'.$package->getName().'" as "'.$class.'" does not implement Composer\Installer\InstallerInterface'); } $this->io->writeError('Loading "'.$package->getName() . '" '.($isGlobalPlugin || $this->runningInGlobalDir ? '(installed globally) ' : '').'which is a legacy composer-installer built for Composer 1.x, it is likely to cause issues as you are running Composer 2.x.'); $installer = new $class($this->io, $this->composer); $this->composer->getInstallationManager()->addInstaller($installer); $this->registeredPlugins[$package->getName()] = $installer; } elseif (class_exists($class)) { if (!is_a($class, 'Composer\Plugin\PluginInterface', true)) { throw new \RuntimeException('Could not activate plugin "'.$package->getName().'" as "'.$class.'" does not implement Composer\Plugin\PluginInterface'); } $plugin = new $class(); $this->addPlugin($plugin, $isGlobalPlugin, $package); $this->registeredPlugins[$package->getName()] = $plugin; } elseif ($failOnMissingClasses) { throw new \UnexpectedValueException('Plugin '.$package->getName().' could not be initialized, class not found: '.$class); } } } public function deactivatePackage(PackageInterface $package): void { if (!isset($this->registeredPlugins[$package->getName()])) { return; } $plugin = $this->registeredPlugins[$package->getName()]; unset($this->registeredPlugins[$package->getName()]); if ($plugin instanceof InstallerInterface) { $this->composer->getInstallationManager()->removeInstaller($plugin); } else { $this->removePlugin($plugin); } } public function uninstallPackage(PackageInterface $package): void { if (!isset($this->registeredPlugins[$package->getName()])) { return; } $plugin = $this->registeredPlugins[$package->getName()]; if ($plugin instanceof InstallerInterface) { $this->deactivatePackage($package); } else { unset($this->registeredPlugins[$package->getName()]); $this->removePlugin($plugin); $this->uninstallPlugin($plugin); } } protected function getPluginApiVersion(): string { return PluginInterface::PLUGIN_API_VERSION; } public function addPlugin(PluginInterface $plugin, bool $isGlobalPlugin = false, ?PackageInterface $sourcePackage = null): void { if ($this->arePluginsDisabled($isGlobalPlugin ? 'global' : 'local')) { return; } if ($sourcePackage === null) { trigger_error('Calling PluginManager::addPlugin without $sourcePackage is deprecated, if you are using this please get in touch with us to explain the use case', E_USER_DEPRECATED); } elseif (!$this->isPluginAllowed($sourcePackage->getName(), $isGlobalPlugin)) { $this->io->writeError('Skipped loading "'.get_class($plugin).' from '.$sourcePackage->getName() . '" '.($isGlobalPlugin || $this->runningInGlobalDir ? '(installed globally) ' : '').' as it is not in config.allow-plugins', true, IOInterface::DEBUG); return; } $details = []; if ($sourcePackage) { $details[] = 'from '.$sourcePackage->getName(); } if ($isGlobalPlugin || $this->runningInGlobalDir) { $details[] = 'installed globally'; } $this->io->writeError('Loading plugin '.get_class($plugin).($details ? ' ('.implode(', ', $details).')' : ''), true, IOInterface::DEBUG); $this->plugins[] = $plugin; $plugin->activate($this->composer, $this->io); if ($plugin instanceof EventSubscriberInterface) { $this->composer->getEventDispatcher()->addSubscriber($plugin); } } public function removePlugin(PluginInterface $plugin): void { $index = array_search($plugin, $this->plugins, true); if ($index === false) { return; } $this->io->writeError('Unloading plugin '.get_class($plugin), true, IOInterface::DEBUG); unset($this->plugins[$index]); $plugin->deactivate($this->composer, $this->io); $this->composer->getEventDispatcher()->removeListener($plugin); } public function uninstallPlugin(PluginInterface $plugin): void { $this->io->writeError('Uninstalling plugin '.get_class($plugin), true, IOInterface::DEBUG); $plugin->uninstall($this->composer, $this->io); } private function loadRepository(RepositoryInterface $repo, bool $isGlobalRepo): void { $packages = $repo->getPackages(); $weights = []; foreach ($packages as $package) { if ($package->getType() === 'composer-plugin') { $extra = $package->getExtra(); if ($package->getName() === 'composer/installers' || true === ($extra['plugin-modifies-install-path'] ?? false)) { $weights[$package->getName()] = -10000; } } } $sortedPackages = PackageSorter::sortPackages($packages, $weights); foreach ($sortedPackages as $package) { if (!($package instanceof CompletePackage)) { continue; } if ('composer-plugin' === $package->getType()) { $this->registerPackage($package, false, $isGlobalRepo); } elseif ('composer-installer' === $package->getType()) { $this->registerPackage($package, false, $isGlobalRepo); } } } private function deactivateRepository(RepositoryInterface $repo, bool $isGlobalRepo): void { $packages = $repo->getPackages(); $sortedPackages = array_reverse(PackageSorter::sortPackages($packages)); foreach ($sortedPackages as $package) { if (!($package instanceof CompletePackage)) { continue; } if ('composer-plugin' === $package->getType()) { $this->deactivatePackage($package); } elseif ('composer-installer' === $package->getType()) { $this->deactivatePackage($package); } } } private function collectDependencies(InstalledRepository $installedRepo, array $collected, PackageInterface $package): array { foreach ($package->getRequires() as $requireLink) { foreach ($installedRepo->findPackagesWithReplacersAndProviders($requireLink->getTarget()) as $requiredPackage) { if (!isset($collected[$requiredPackage->getName()])) { $collected[$requiredPackage->getName()] = $requiredPackage; $collected = $this->collectDependencies($installedRepo, $collected, $requiredPackage); } } } return $collected; } private function getInstallPath(PackageInterface $package, bool $global = false): string { if (!$global) { return $this->composer->getInstallationManager()->getInstallPath($package); } assert(null !== $this->globalComposer); return $this->globalComposer->getInstallationManager()->getInstallPath($package); } protected function getCapabilityImplementationClassName(PluginInterface $plugin, string $capability): ?string { if (!($plugin instanceof Capable)) { return null; } $capabilities = (array) $plugin->getCapabilities(); if (!empty($capabilities[$capability]) && is_string($capabilities[$capability]) && trim($capabilities[$capability])) { return trim($capabilities[$capability]); } if ( array_key_exists($capability, $capabilities) && (empty($capabilities[$capability]) || !is_string($capabilities[$capability]) || !trim($capabilities[$capability])) ) { throw new \UnexpectedValueException('Plugin '.get_class($plugin).' provided invalid capability class name(s), got '.var_export($capabilities[$capability], true)); } return null; } public function getPluginCapability(PluginInterface $plugin, $capabilityClassName, array $ctorArgs = []): ?Capability { if ($capabilityClass = $this->getCapabilityImplementationClassName($plugin, $capabilityClassName)) { if (!class_exists($capabilityClass)) { throw new \RuntimeException("Cannot instantiate Capability, as class $capabilityClass from plugin ".get_class($plugin)." does not exist."); } $ctorArgs['plugin'] = $plugin; $capabilityObj = new $capabilityClass($ctorArgs); if (!$capabilityObj instanceof Capability || !$capabilityObj instanceof $capabilityClassName) { throw new \RuntimeException( 'Class ' . $capabilityClass . ' must implement both Composer\Plugin\Capability\Capability and '. $capabilityClassName . '.' ); } return $capabilityObj; } return null; } public function getPluginCapabilities($capabilityClassName, array $ctorArgs = []): array { $capabilities = []; foreach ($this->getPlugins() as $plugin) { $capability = $this->getPluginCapability($plugin, $capabilityClassName, $ctorArgs); if (null !== $capability) { $capabilities[] = $capability; } } return $capabilities; } private function parseAllowedPlugins($allowPluginsConfig, ?Locker $locker = null): ?array { if ([] === $allowPluginsConfig && $locker !== null && $locker->isLocked() && version_compare($locker->getPluginApi(), '2.2.0', '<')) { return null; } if (true === $allowPluginsConfig) { return ['{}' => true]; } if (false === $allowPluginsConfig) { return ['{}' => false]; } $rules = []; foreach ($allowPluginsConfig as $pattern => $allow) { $rules[BasePackage::packageNameToRegexp($pattern)] = $allow; } return $rules; } public function arePluginsDisabled($type) { return $this->disablePlugins === true || $this->disablePlugins === $type; } public function isPluginAllowed(string $package, bool $isGlobalPlugin): bool { if ($isGlobalPlugin) { $rules = &$this->allowGlobalPluginRules; } else { $rules = &$this->allowPluginRules; } if ($rules === null) { if (!$this->io->isInteractive()) { $this->io->writeError('For additional security you should declare the allow-plugins config with a list of packages names that are allowed to run code. See https://getcomposer.org/allow-plugins'); $this->io->writeError('This warning will become an exception once you run composer update!'); $rules = ['{}' => true]; return true; } $rules = []; } foreach ($rules as $pattern => $allow) { if (Preg::isMatch($pattern, $package)) { return $allow === true; } } if ($package === 'composer/package-versions-deprecated') { return false; } if ($this->io->isInteractive()) { $composer = $isGlobalPlugin && $this->globalComposer !== null ? $this->globalComposer : $this->composer; $this->io->writeError(''.$package.($isGlobalPlugin || $this->runningInGlobalDir ? ' (installed globally)' : '').' contains a Composer plugin which is currently not in your allow-plugins config. See https://getcomposer.org/allow-plugins'); $attempts = 0; while (true) { $default = '?'; if ($attempts > 5) { $this->io->writeError('Too many failed prompts, aborting.'); break; } switch ($answer = $this->io->ask('Do you trust "'.$package.'" to execute code and wish to enable it now? (writes "allow-plugins" to composer.json) [y,n,d,?] ', $default)) { case 'y': case 'n': case 'd': $allow = $answer === 'y'; $rules[BasePackage::packageNameToRegexp($package)] = $allow; if ($answer === 'y' || $answer === 'n') { $composer->getConfig()->getConfigSource()->addConfigSetting('allow-plugins.'.$package, $allow); } return $allow; case '?': default: $attempts++; $this->io->writeError([ 'y - add package to allow-plugins in composer.json and let it run immediately', 'n - add package (as disallowed) to allow-plugins in composer.json to suppress further prompts', 'd - discard this, do not change composer.json and do not allow the plugin to run', '? - print help', ]); break; } } } throw new PluginBlockedException( $package.($isGlobalPlugin || $this->runningInGlobalDir ? ' (installed globally)' : '').' contains a Composer plugin which is blocked by your allow-plugins config. You may add it to the list if you consider it safe.'.PHP_EOL. 'You can run "composer '.($isGlobalPlugin || $this->runningInGlobalDir ? 'global ' : '').'config --no-plugins allow-plugins.'.$package.' [true|false]" to enable it (true) or disable it explicitly and suppress this exception (false)'.PHP_EOL. 'See https://getcomposer.org/allow-plugins' ); } } fileName = $fileName; $this->checksum = $checksum; $this->url = $url; $this->context = $context; $this->type = $type; } public function getFileName(): ?string { return $this->fileName; } public function getChecksum(): ?string { return $this->checksum; } public function getUrl(): string { return $this->url; } public function getContext() { return $this->context; } public function getPackage(): ?PackageInterface { trigger_error('PostFileDownloadEvent::getPackage is deprecated since Composer 2.1, use getContext instead.', E_USER_DEPRECATED); $context = $this->getContext(); return $context instanceof PackageInterface ? $context : null; } public function getType(): string { return $this->type; } } input = $input; $this->command = $command; } public function getInput(): InputInterface { return $this->input; } public function getCommand(): string { return $this->command; } } httpDownloader = $httpDownloader; $this->processedUrl = $processedUrl; $this->type = $type; $this->context = $context; } public function getHttpDownloader(): HttpDownloader { return $this->httpDownloader; } public function getProcessedUrl(): string { return $this->processedUrl; } public function setProcessedUrl(string $processedUrl): void { $this->processedUrl = $processedUrl; } public function getCustomCacheKey(): ?string { return $this->customCacheKey; } public function setCustomCacheKey(?string $customCacheKey): void { $this->customCacheKey = $customCacheKey; } public function getType(): string { return $this->type; } public function getContext() { return $this->context; } public function getTransportOptions(): array { return $this->transportOptions; } public function setTransportOptions(array $options): void { $this->transportOptions = $options; } } repositories = $repositories; $this->request = $request; $this->acceptableStabilities = $acceptableStabilities; $this->stabilityFlags = $stabilityFlags; $this->rootAliases = $rootAliases; $this->rootReferences = $rootReferences; $this->packages = $packages; $this->unacceptableFixedPackages = $unacceptableFixedPackages; } public function getRepositories(): array { return $this->repositories; } public function getRequest(): Request { return $this->request; } public function getAcceptableStabilities(): array { return $this->acceptableStabilities; } public function getStabilityFlags(): array { return $this->stabilityFlags; } public function getRootAliases(): array { return $this->rootAliases; } public function getRootReferences(): array { return $this->rootReferences; } public function getPackages(): array { return $this->packages; } public function getUnacceptableFixedPackages(): array { return $this->unacceptableFixedPackages; } public function setPackages(array $packages): void { $this->packages = $packages; } public function setUnacceptableFixedPackages(array $packages): void { $this->unacceptableFixedPackages = $packages; } } trueAnswerRegex = $trueAnswerRegex; $this->falseAnswerRegex = $falseAnswerRegex; $this->setNormalizer($this->getDefaultNormalizer()); $this->setValidator($this->getDefaultValidator()); } private function getDefaultNormalizer(): callable { $default = $this->getDefault(); $trueRegex = $this->trueAnswerRegex; $falseRegex = $this->falseAnswerRegex; return static function ($answer) use ($default, $trueRegex, $falseRegex) { if (is_bool($answer)) { return $answer; } if (empty($answer) && !empty($default)) { return $default; } if (Preg::isMatch($trueRegex, $answer)) { return true; } if (Preg::isMatch($falseRegex, $answer)) { return false; } return null; }; } private function getDefaultValidator(): callable { return static function ($answer): bool { if (!is_bool($answer)) { throw new InvalidArgumentException('Please answer yes, y, no, or n.'); } return $answer; }; } } addPackage($package); } } public function getRepoName() { return 'array repo (defining '.$this->count().' package'.($this->count() > 1 ? 's' : '').')'; } public function loadPackages(array $packageNameMap, array $acceptableStabilities, array $stabilityFlags, array $alreadyLoaded = []) { $packages = $this->getPackages(); $result = []; $namesFound = []; foreach ($packages as $package) { if (array_key_exists($package->getName(), $packageNameMap)) { if ( (!$packageNameMap[$package->getName()] || $packageNameMap[$package->getName()]->matches(new Constraint('==', $package->getVersion()))) && StabilityFilter::isPackageAcceptable($acceptableStabilities, $stabilityFlags, $package->getNames(), $package->getStability()) && !isset($alreadyLoaded[$package->getName()][$package->getVersion()]) ) { $result[spl_object_hash($package)] = $package; if ($package instanceof AliasPackage && !isset($result[spl_object_hash($package->getAliasOf())])) { $result[spl_object_hash($package->getAliasOf())] = $package->getAliasOf(); } } $namesFound[$package->getName()] = true; } } foreach ($packages as $package) { if ($package instanceof AliasPackage) { if (isset($result[spl_object_hash($package->getAliasOf())])) { $result[spl_object_hash($package)] = $package; } } } return ['namesFound' => array_keys($namesFound), 'packages' => $result]; } public function findPackage(string $name, $constraint) { $name = strtolower($name); if (!$constraint instanceof ConstraintInterface) { $versionParser = new VersionParser(); $constraint = $versionParser->parseConstraints($constraint); } foreach ($this->getPackages() as $package) { if ($name === $package->getName()) { $pkgConstraint = new Constraint('==', $package->getVersion()); if ($constraint->matches($pkgConstraint)) { return $package; } } } return null; } public function findPackages(string $name, $constraint = null) { $name = strtolower($name); $packages = []; if (null !== $constraint && !$constraint instanceof ConstraintInterface) { $versionParser = new VersionParser(); $constraint = $versionParser->parseConstraints($constraint); } foreach ($this->getPackages() as $package) { if ($name === $package->getName()) { if (null === $constraint || $constraint->matches(new Constraint('==', $package->getVersion()))) { $packages[] = $package; } } } return $packages; } public function search(string $query, int $mode = 0, ?string $type = null) { if ($mode === self::SEARCH_FULLTEXT) { $regex = '{(?:'.implode('|', Preg::split('{\s+}', preg_quote($query))).')}i'; } else { $regex = '{(?:'.implode('|', Preg::split('{\s+}', $query)).')}i'; } $matches = []; foreach ($this->getPackages() as $package) { $name = $package->getName(); if ($mode === self::SEARCH_VENDOR) { [$name] = explode('/', $name); } if (isset($matches[$name])) { continue; } if (null !== $type && $package->getType() !== $type) { continue; } if (Preg::isMatch($regex, $name) || ($mode === self::SEARCH_FULLTEXT && $package instanceof CompletePackageInterface && Preg::isMatch($regex, implode(' ', (array) $package->getKeywords()) . ' ' . $package->getDescription())) ) { if ($mode === self::SEARCH_VENDOR) { $matches[$name] = [ 'name' => $name, 'description' => null, ]; } else { $matches[$name] = [ 'name' => $package->getPrettyName(), 'description' => $package instanceof CompletePackageInterface ? $package->getDescription() : null, ]; if ($package instanceof CompletePackageInterface && $package->isAbandoned()) { $matches[$name]['abandoned'] = $package->getReplacementPackage() ?: true; } } } } return array_values($matches); } public function hasPackage(PackageInterface $package) { if ($this->packageMap === null) { $this->packageMap = []; foreach ($this->getPackages() as $repoPackage) { $this->packageMap[$repoPackage->getUniqueName()] = $repoPackage; } } return isset($this->packageMap[$package->getUniqueName()]); } public function addPackage(PackageInterface $package) { if (!$package instanceof BasePackage) { throw new \InvalidArgumentException('Only subclasses of BasePackage are supported'); } if (null === $this->packages) { $this->initialize(); } $package->setRepository($this); $this->packages[] = $package; if ($package instanceof AliasPackage) { $aliasedPackage = $package->getAliasOf(); if (null === $aliasedPackage->getRepository()) { $this->addPackage($aliasedPackage); } } $this->packageMap = null; } public function getProviders(string $packageName) { $result = []; foreach ($this->getPackages() as $candidate) { if (isset($result[$candidate->getName()])) { continue; } foreach ($candidate->getProvides() as $link) { if ($packageName === $link->getTarget()) { $result[$candidate->getName()] = [ 'name' => $candidate->getName(), 'description' => $candidate instanceof CompletePackageInterface ? $candidate->getDescription() : null, 'type' => $candidate->getType(), ]; continue 2; } } } return $result; } protected function createAliasPackage(BasePackage $package, string $alias, string $prettyAlias) { while ($package instanceof AliasPackage) { $package = $package->getAliasOf(); } if ($package instanceof CompletePackage) { return new CompleteAliasPackage($package, $alias, $prettyAlias); } return new AliasPackage($package, $alias, $prettyAlias); } public function removePackage(PackageInterface $package) { $packageId = $package->getUniqueName(); foreach ($this->getPackages() as $key => $repoPackage) { if ($packageId === $repoPackage->getUniqueName()) { array_splice($this->packages, $key, 1); $this->packageMap = null; return; } } } public function getPackages() { if (null === $this->packages) { $this->initialize(); } if (null === $this->packages) { throw new \LogicException('initialize failed to initialize the packages array'); } return $this->packages; } public function count(): int { if (null === $this->packages) { $this->initialize(); } return count($this->packages); } protected function initialize() { $this->packages = []; } } loader = new ArrayLoader(); $this->lookup = $repoConfig['url']; $this->io = $io; $this->repoConfig = $repoConfig; } public function getRepoName() { return 'artifact repo ('.$this->lookup.')'; } public function getRepoConfig() { return $this->repoConfig; } protected function initialize() { parent::initialize(); $this->scanDirectory($this->lookup); } private function scanDirectory(string $path): void { $io = $this->io; $directory = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS); $iterator = new \RecursiveIteratorIterator($directory); $regex = new \RegexIterator($iterator, '/^.+\.(zip|tar|gz|tgz)$/i'); foreach ($regex as $file) { if (!$file->isFile()) { continue; } $package = $this->getComposerInformation($file); if (!$package) { $io->writeError("File {$file->getBasename()} doesn't seem to hold a package", true, IOInterface::VERBOSE); continue; } $template = 'Found package %s (%s) in file %s'; $io->writeError(sprintf($template, $package->getName(), $package->getPrettyVersion(), $file->getBasename()), true, IOInterface::VERBOSE); $this->addPackage($package); } } private function getComposerInformation(\SplFileInfo $file): ?BasePackage { $json = null; $fileType = null; $fileExtension = pathinfo($file->getPathname(), PATHINFO_EXTENSION); if (in_array($fileExtension, ['gz', 'tar', 'tgz'], true)) { $fileType = 'tar'; } elseif ($fileExtension === 'zip') { $fileType = 'zip'; } else { throw new \RuntimeException('Files with "'.$fileExtension.'" extensions aren\'t supported. Only ZIP and TAR/TAR.GZ/TGZ archives are supported.'); } try { if ($fileType === 'tar') { $json = Tar::getComposerJson($file->getPathname()); } else { $json = Zip::getComposerJson($file->getPathname()); } } catch (\Exception $exception) { $this->io->write('Failed loading package '.$file->getPathname().': '.$exception->getMessage(), false, IOInterface::VERBOSE); } if (null === $json) { return null; } $package = JsonFile::parseJson($json, $file->getPathname().'#composer.json'); $package['dist'] = [ 'type' => $fileType, 'url' => strtr($file->getPathname(), '\\', '/'), 'shasum' => sha1_file($file->getRealPath()), ]; try { $package = $this->loader->load($package); } catch (\UnexpectedValueException $e) { throw new \UnexpectedValueException('Failed loading package in '.$file.': '.$e->getMessage(), 0, $e); } return $package; } } getPackages(); $packagesByName = []; foreach ($packages as $package) { if (!isset($packagesByName[$package->getName()]) || $packagesByName[$package->getName()] instanceof AliasPackage) { $packagesByName[$package->getName()] = $package; } } $canonicalPackages = []; foreach ($packagesByName as $package) { while ($package instanceof AliasPackage) { $package = $package->getAliasOf(); } $canonicalPackages[] = $package; } return $canonicalPackages; } } allowSslDowngrade = true; } $this->options = $repoConfig['options']; $this->url = $repoConfig['url']; if (Preg::isMatch('{^(?Phttps?)://packagist\.org/?$}i', $this->url, $match)) { $this->url = $match['proto'].'://repo.packagist.org'; } $this->baseUrl = rtrim(Preg::replace('{(?:/[^/\\\\]+\.json)?(?:[?#].*)?$}', '', $this->url), '/'); $this->io = $io; $this->cache = new Cache($io, $config->get('cache-repo-dir').'/'.Preg::replace('{[^a-z0-9.]}i', '-', Url::sanitize($this->url)), 'a-z0-9.$~'); $this->cache->setReadOnly($config->get('cache-read-only')); $this->versionParser = new VersionParser(); $this->loader = new ArrayLoader($this->versionParser); $this->httpDownloader = $httpDownloader; $this->eventDispatcher = $eventDispatcher; $this->repoConfig = $repoConfig; $this->loop = new Loop($this->httpDownloader); } public function getRepoName() { return 'composer repo ('.Url::sanitize($this->url).')'; } public function getRepoConfig() { return $this->repoConfig; } public function findPackage(string $name, $constraint) { $hasProviders = $this->hasProviders(); $name = strtolower($name); if (!$constraint instanceof ConstraintInterface) { $constraint = $this->versionParser->parseConstraints($constraint); } if ($this->lazyProvidersUrl) { if ($this->hasPartialPackages() && isset($this->partialPackagesByName[$name])) { return $this->filterPackages($this->whatProvides($name), $constraint, true); } if ($this->hasAvailablePackageList && !$this->lazyProvidersRepoContains($name)) { return null; } $packages = $this->loadAsyncPackages([$name => $constraint]); if (count($packages['packages']) > 0) { return reset($packages['packages']); } return null; } if ($hasProviders) { foreach ($this->getProviderNames() as $providerName) { if ($name === $providerName) { return $this->filterPackages($this->whatProvides($providerName), $constraint, true); } } return null; } return parent::findPackage($name, $constraint); } public function findPackages(string $name, $constraint = null) { $hasProviders = $this->hasProviders(); $name = strtolower($name); if (null !== $constraint && !$constraint instanceof ConstraintInterface) { $constraint = $this->versionParser->parseConstraints($constraint); } if ($this->lazyProvidersUrl) { if ($this->hasPartialPackages() && isset($this->partialPackagesByName[$name])) { return $this->filterPackages($this->whatProvides($name), $constraint); } if ($this->hasAvailablePackageList && !$this->lazyProvidersRepoContains($name)) { return []; } $result = $this->loadAsyncPackages([$name => $constraint]); return $result['packages']; } if ($hasProviders) { foreach ($this->getProviderNames() as $providerName) { if ($name === $providerName) { return $this->filterPackages($this->whatProvides($providerName), $constraint); } } return []; } return parent::findPackages($name, $constraint); } private function filterPackages(array $packages, ?ConstraintInterface $constraint = null, bool $returnFirstMatch = false) { if (null === $constraint) { if ($returnFirstMatch) { return reset($packages); } return $packages; } $filteredPackages = []; foreach ($packages as $package) { $pkgConstraint = new Constraint('==', $package->getVersion()); if ($constraint->matches($pkgConstraint)) { if ($returnFirstMatch) { return $package; } $filteredPackages[] = $package; } } if ($returnFirstMatch) { return null; } return $filteredPackages; } public function getPackages() { $hasProviders = $this->hasProviders(); if ($this->lazyProvidersUrl) { if (is_array($this->availablePackages) && !$this->availablePackagePatterns) { $packageMap = []; foreach ($this->availablePackages as $name) { $packageMap[$name] = new MatchAllConstraint(); } $result = $this->loadAsyncPackages($packageMap); return array_values($result['packages']); } if ($this->hasPartialPackages()) { if (!is_array($this->partialPackagesByName)) { throw new \LogicException('hasPartialPackages failed to initialize $this->partialPackagesByName'); } return $this->createPackages($this->partialPackagesByName, 'packages.json inline packages'); } throw new \LogicException('Composer repositories that have lazy providers and no available-packages list can not load the complete list of packages, use getPackageNames instead.'); } if ($hasProviders) { throw new \LogicException('Composer repositories that have providers can not load the complete list of packages, use getPackageNames instead.'); } return parent::getPackages(); } public function getPackageNames(?string $packageFilter = null) { $hasProviders = $this->hasProviders(); $filterResults = static function (array $results): array { return $results; } ; if (null !== $packageFilter && '' !== $packageFilter) { $packageFilterRegex = BasePackage::packageNameToRegexp($packageFilter); $filterResults = static function (array $results) use ($packageFilterRegex): array { return Preg::grep($packageFilterRegex, $results); } ; } if ($this->lazyProvidersUrl) { if (is_array($this->availablePackages)) { return $filterResults(array_keys($this->availablePackages)); } if ($this->listUrl) { return $this->loadPackageList($packageFilter); } if ($this->hasPartialPackages() && $this->partialPackagesByName !== null) { return $filterResults(array_keys($this->partialPackagesByName)); } return []; } if ($hasProviders) { return $filterResults($this->getProviderNames()); } $names = []; foreach ($this->getPackages() as $package) { $names[] = $package->getPrettyName(); } return $filterResults($names); } private function getVendorNames(): array { $cacheKey = 'vendor-list.txt'; $cacheAge = $this->cache->getAge($cacheKey); if (false !== $cacheAge && $cacheAge < 600 && ($cachedData = $this->cache->read($cacheKey)) !== false) { $cachedData = explode("\n", $cachedData); return $cachedData; } $names = $this->getPackageNames(); $uniques = []; foreach ($names as $name) { $uniques[substr($name, 0, strpos($name, '/'))] = true; } $vendors = array_keys($uniques); if (!$this->cache->isReadOnly()) { $this->cache->write($cacheKey, implode("\n", $vendors)); } return $vendors; } private function loadPackageList(?string $packageFilter = null): array { if (null === $this->listUrl) { throw new \LogicException('Make sure to call loadRootServerFile before loadPackageList'); } $url = $this->listUrl; if (is_string($packageFilter) && $packageFilter !== '') { $url .= '?filter='.urlencode($packageFilter); $result = $this->httpDownloader->get($url, $this->options)->decodeJson(); return $result['packageNames']; } $cacheKey = 'package-list.txt'; $cacheAge = $this->cache->getAge($cacheKey); if (false !== $cacheAge && $cacheAge < 600 && ($cachedData = $this->cache->read($cacheKey)) !== false) { $cachedData = explode("\n", $cachedData); return $cachedData; } $result = $this->httpDownloader->get($url, $this->options)->decodeJson(); if (!$this->cache->isReadOnly()) { $this->cache->write($cacheKey, implode("\n", $result['packageNames'])); } return $result['packageNames']; } public function loadPackages(array $packageNameMap, array $acceptableStabilities, array $stabilityFlags, array $alreadyLoaded = []) { $hasProviders = $this->hasProviders(); if (!$hasProviders && !$this->hasPartialPackages() && null === $this->lazyProvidersUrl) { return parent::loadPackages($packageNameMap, $acceptableStabilities, $stabilityFlags, $alreadyLoaded); } $packages = []; $namesFound = []; if ($hasProviders || $this->hasPartialPackages()) { foreach ($packageNameMap as $name => $constraint) { $matches = []; if (!$hasProviders && !isset($this->partialPackagesByName[$name])) { continue; } $candidates = $this->whatProvides($name, $acceptableStabilities, $stabilityFlags, $alreadyLoaded); foreach ($candidates as $candidate) { if ($candidate->getName() !== $name) { throw new \LogicException('whatProvides should never return a package with a different name than the requested one'); } $namesFound[$name] = true; if (!$constraint || $constraint->matches(new Constraint('==', $candidate->getVersion()))) { $matches[spl_object_hash($candidate)] = $candidate; if ($candidate instanceof AliasPackage && !isset($matches[spl_object_hash($candidate->getAliasOf())])) { $matches[spl_object_hash($candidate->getAliasOf())] = $candidate->getAliasOf(); } } } foreach ($candidates as $candidate) { if ($candidate instanceof AliasPackage) { if (isset($matches[spl_object_hash($candidate->getAliasOf())])) { $matches[spl_object_hash($candidate)] = $candidate; } } } $packages = array_merge($packages, $matches); unset($packageNameMap[$name]); } } if ($this->lazyProvidersUrl && count($packageNameMap)) { if ($this->hasAvailablePackageList) { foreach ($packageNameMap as $name => $constraint) { if (!$this->lazyProvidersRepoContains(strtolower($name))) { unset($packageNameMap[$name]); } } } $result = $this->loadAsyncPackages($packageNameMap, $acceptableStabilities, $stabilityFlags, $alreadyLoaded); $packages = array_merge($packages, $result['packages']); $namesFound = array_merge($namesFound, $result['namesFound']); } return ['namesFound' => array_keys($namesFound), 'packages' => $packages]; } public function search(string $query, int $mode = 0, ?string $type = null) { $this->loadRootServerFile(600); if ($this->searchUrl && $mode === self::SEARCH_FULLTEXT) { $url = str_replace(['%query%', '%type%'], [urlencode($query), $type], $this->searchUrl); $search = $this->httpDownloader->get($url, $this->options)->decodeJson(); if (empty($search['results'])) { return []; } $results = []; foreach ($search['results'] as $result) { if (!empty($result['virtual'])) { continue; } $results[] = $result; } return $results; } if ($mode === self::SEARCH_VENDOR) { $results = []; $regex = '{(?:'.implode('|', Preg::split('{\s+}', $query)).')}i'; $vendorNames = $this->getVendorNames(); foreach (Preg::grep($regex, $vendorNames) as $name) { $results[] = ['name' => $name, 'description' => '']; } return $results; } if ($this->hasProviders() || $this->lazyProvidersUrl) { if (Preg::isMatch('{^\^(?P(?P[a-z0-9_.-]+)/[a-z0-9_.-]*)\*?$}i', $query, $match) && $this->listUrl !== null) { $url = $this->listUrl . '?vendor='.urlencode($match['vendor']).'&filter='.urlencode($match['query'].'*'); $result = $this->httpDownloader->get($url, $this->options)->decodeJson(); $results = []; foreach ($result['packageNames'] as $name) { $results[] = ['name' => $name, 'description' => '']; } return $results; } $results = []; $regex = '{(?:'.implode('|', Preg::split('{\s+}', $query)).')}i'; $packageNames = $this->getPackageNames(); foreach (Preg::grep($regex, $packageNames) as $name) { $results[] = ['name' => $name, 'description' => '']; } return $results; } return parent::search($query, $mode); } public function hasSecurityAdvisories(): bool { $this->loadRootServerFile(600); return $this->securityAdvisoryConfig !== null && ($this->securityAdvisoryConfig['metadata'] || $this->securityAdvisoryConfig['api-url'] !== null); } public function getSecurityAdvisories(array $packageConstraintMap, bool $allowPartialAdvisories = false): array { $this->loadRootServerFile(600); if (null === $this->securityAdvisoryConfig) { return ['namesFound' => [], 'advisories' => []]; } $advisories = []; $namesFound = []; $apiUrl = $this->securityAdvisoryConfig['api-url']; $parser = new VersionParser(); $create = function (array $data, string $name) use ($parser, $allowPartialAdvisories, &$packageConstraintMap): ?PartialSecurityAdvisory { $advisory = PartialSecurityAdvisory::create($name, $data, $parser); if (!$allowPartialAdvisories && !$advisory instanceof SecurityAdvisory) { throw new \RuntimeException('Advisory for '.$name.' could not be loaded as a full advisory from '.$this->getRepoName() . PHP_EOL . var_export($data, true)); } if (!$advisory->affectedVersions->matches($packageConstraintMap[$name])) { return null; } return $advisory; }; if ($this->securityAdvisoryConfig['metadata'] && ($allowPartialAdvisories || $apiUrl === null)) { $promises = []; foreach ($packageConstraintMap as $name => $constraint) { $name = strtolower($name); if (PlatformRepository::isPlatformPackage($name) || '__root__' === $name) { continue; } $promises[] = $this->startCachedAsyncDownload($name, $name) ->then(static function (array $spec) use (&$advisories, &$namesFound, &$packageConstraintMap, $name, $create): void { [$response, ] = $spec; if (!isset($response['security-advisories']) || !is_array($response['security-advisories'])) { return; } $namesFound[$name] = true; if (count($response['security-advisories']) > 0) { $advisories[$name] = array_filter(array_map( static function ($data) use ($name, $create) { return $create($data, $name); }, $response['security-advisories'] )); } unset($packageConstraintMap[$name]); }); } $this->loop->wait($promises); } if ($apiUrl !== null && count($packageConstraintMap) > 0) { $options = [ 'http' => [ 'method' => 'POST', 'header' => ['Content-type: application/x-www-form-urlencoded'], 'timeout' => 10, 'content' => http_build_query(['packages' => array_keys($packageConstraintMap)]), ], ]; $response = $this->httpDownloader->get($apiUrl, $options); foreach ($response->decodeJson()['advisories'] as $name => $list) { if (count($list) > 0) { $advisories[$name] = array_filter(array_map( static function ($data) use ($name, $create) { return $create($data, $name); }, $list )); } $namesFound[$name] = true; } } return ['namesFound' => array_keys($namesFound), 'advisories' => array_filter($advisories)]; } public function getProviders(string $packageName) { $this->loadRootServerFile(); $result = []; if ($this->providersApiUrl) { try { $apiResult = $this->httpDownloader->get(str_replace('%package%', $packageName, $this->providersApiUrl), $this->options)->decodeJson(); } catch (TransportException $e) { if ($e->getStatusCode() === 404) { return $result; } throw $e; } foreach ($apiResult['providers'] as $provider) { $result[$provider['name']] = $provider; } return $result; } if ($this->hasPartialPackages()) { if (!is_array($this->partialPackagesByName)) { throw new \LogicException('hasPartialPackages failed to initialize $this->partialPackagesByName'); } foreach ($this->partialPackagesByName as $versions) { foreach ($versions as $candidate) { if (isset($result[$candidate['name']]) || !isset($candidate['provide'][$packageName])) { continue; } $result[$candidate['name']] = [ 'name' => $candidate['name'], 'description' => $candidate['description'] ?? '', 'type' => $candidate['type'] ?? '', ]; } } } if ($this->packages) { $result = array_merge($result, parent::getProviders($packageName)); } return $result; } private function getProviderNames(): array { $this->loadRootServerFile(); if (null === $this->providerListing) { $data = $this->loadRootServerFile(); if (is_array($data)) { $this->loadProviderListings($data); } } if ($this->lazyProvidersUrl) { return []; } if (null !== $this->providersUrl && null !== $this->providerListing) { return array_keys($this->providerListing); } return []; } protected function configurePackageTransportOptions(PackageInterface $package): void { foreach ($package->getDistUrls() as $url) { if (strpos($url, $this->baseUrl) === 0) { $package->setTransportOptions($this->options); return; } } } private function hasProviders(): bool { $this->loadRootServerFile(); return $this->hasProviders; } private function whatProvides(string $name, ?array $acceptableStabilities = null, ?array $stabilityFlags = null, array $alreadyLoaded = []): array { $packagesSource = null; if (!$this->hasPartialPackages() || !isset($this->partialPackagesByName[$name])) { if (PlatformRepository::isPlatformPackage($name) || '__root__' === $name) { return []; } if (null === $this->providerListing) { $data = $this->loadRootServerFile(); if (is_array($data)) { $this->loadProviderListings($data); } } $useLastModifiedCheck = false; if ($this->lazyProvidersUrl && !isset($this->providerListing[$name])) { $hash = null; $url = str_replace('%package%', $name, $this->lazyProvidersUrl); $cacheKey = 'provider-'.strtr($name, '/', '$').'.json'; $useLastModifiedCheck = true; } elseif ($this->providersUrl) { if (!isset($this->providerListing[$name])) { return []; } $hash = $this->providerListing[$name]['sha256']; $url =