#!/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. */ Phar::mapPhar('composer.phar'); require 'phar://composer.phar/bin/composer'; __HALT_COMPILER(); ?> N\ composer.pharsrc/bootstrap.phpT҄QC/src/Composer/IO/ConsoleIO.phpT҄QUPsrc/Composer/IO/NullIO.php|T҄Q|N߶src/Composer/IO/IOInterface.phpT҄QDtsrc/Composer/IO/BufferIO.php*T҄Q*o%)src/Composer/Command/RunScriptCommand.php.T҄Q.҈W(src/Composer/Command/DiagnoseCommand.php"T҄Q" I'src/Composer/Command/ArchiveCommand.php,T҄Q,b src/Composer/Command/Command.phpT҄QI-src/Composer/Command/CreateProjectCommand.php$T҄Q$>%src/Composer/Command/AboutCommand.phpT҄QM*$src/Composer/Command/ShowCommand.phpN+T҄QN+Ս˶&src/Composer/Command/UpdateCommand.php T҄Q Uö&src/Composer/Command/ConfigCommand.php'T҄Q'/d'src/Composer/Command/InstallCommand.php T҄Q ܒ(src/Composer/Command/ValidateCommand.phpGT҄QG;r'src/Composer/Command/DependsCommand.phpo T҄Qo T.r&src/Composer/Command/SearchCommand.phpT҄Q *src/Composer/Command/SelfUpdateCommand.phpT҄Qީ'src/Composer/Command/RequireCommand.phpT҄Qf%,src/Composer/Command/DumpAutoloadCommand.phpT҄Qa%,src/Composer/Command/Helper/DialogHelper.phpT҄Q&&src/Composer/Command/StatusCommand.php`T҄Q`J$src/Composer/Command/InitCommand.php0T҄Q0eǶ)src/Composer/Downloader/VcsDownloader.php4T҄Q4 Ͳ*src/Composer/Downloader/FileDownloader.phpT҄Q7q)src/Composer/Downloader/SvnDownloader.php T҄Q 0src/Composer/Downloader/PearPackageExtractor.phpT҄Q~z]B+src/Composer/Downloader/DownloadManager.phpT҄Qֹ\/src/Composer/Downloader/DownloaderInterface.phpT҄Qgs!l.src/Composer/Downloader/TransportException.phpT҄Q8͊*src/Composer/Downloader/PharDownloader.phpT҄Q)src/Composer/Downloader/TarDownloader.phpT҄Q͒X?(src/Composer/Downloader/HgDownloader.phpT҄QJ-src/Composer/Downloader/ArchiveDownloader.phpR T҄QR xaE)src/Composer/Downloader/ZipDownloader.php T҄Q p)src/Composer/Downloader/GitDownloader.phpi-T҄Qi-PI6src/Composer/Repository/InvalidRepositoryException.phpnT҄Qn똶+src/Composer/Repository/ArrayRepository.php T҄Q kb0src/Composer/Repository/FilesystemRepository.phpT҄QYz7src/Composer/Repository/WritableRepositoryInterface.phpT҄Q/s*src/Composer/Repository/PearRepository.phpT҄QbE-src/Composer/Repository/RepositoryManager.phpT҄Qr_3src/Composer/Repository/WritableArrayRepository.phpT҄QG*,src/Composer/Repository/Vcs/GitHubDriver.php+ T҄Q+ X2src/Composer/Repository/Vcs/GitBitbucketDriver.php= T҄Q= ǎ)src/Composer/Repository/Vcs/GitDriver.php{T҄Q{[mm)src/Composer/Repository/Vcs/SvnDriver.phpT҄Qx!2src/Composer/Repository/Vcs/VcsDriverInterface.phpCT҄QCF(src/Composer/Repository/Vcs/HgDriver.php T҄Q n0)src/Composer/Repository/Vcs/VcsDriver.phppT҄Qpfj1src/Composer/Repository/Vcs/HgBitbucketDriver.php T҄Q +A4src/Composer/Repository/InstalledArrayRepository.phpT҄Q/~>7src/Composer/Repository/RepositorySecurityException.phpnT҄QnB_9src/Composer/Repository/StreamableRepositoryInterface.phpT҄Q8)src/Composer/Repository/VcsRepository.phpT҄Q/ʖ.src/Composer/Repository/PlatformRepository.php T҄Q |9src/Composer/Repository/InstalledFilesystemRepository.phpT҄QV _/src/Composer/Repository/CompositeRepository.phpT҄QN]8src/Composer/Repository/InstalledRepositoryInterface.phpT҄Q9p.src/Composer/Repository/ComposerRepository.php7T҄Q7nH/src/Composer/Repository/Pear/DependencyInfo.phpqT҄QqfT8src/Composer/Repository/Pear/PackageDependencyParser.php!T҄Q!=F,src/Composer/Repository/Pear/ChannelInfo.phpT҄Q:T*ɶ.src/Composer/Repository/Pear/ChannelReader.phpmT҄QmN',src/Composer/Repository/Pear/PackageInfo.phpT҄Q 5src/Composer/Repository/Pear/DependencyConstraint.phpqT҄Qq9=4src/Composer/Repository/Pear/ChannelRest11Reader.php& T҄Q& Ub,src/Composer/Repository/Pear/ReleaseInfo.phpT҄Qoö2src/Composer/Repository/Pear/BaseChannelReader.php4T҄Q4D?4src/Composer/Repository/Pear/ChannelRest10Reader.php T҄Q ]1޶/src/Composer/Repository/RepositoryInterface.phpT҄Qɶ.src/Composer/Repository/ArtifactRepository.phpT҄Q4-src/Composer/Repository/PackageRepository.phpGT҄QG:k(src/Composer/Package/CompletePackage.phpT҄Q8Gl]+src/Composer/Package/Dumper/ArrayDumper.phpe T҄Qe 7src/Composer/Package/Loader/InvalidPackageException.phpET҄QExb*src/Composer/Package/Loader/JsonLoader.phpT҄Q!~{/src/Composer/Package/Loader/LoaderInterface.phpT҄Q}ζ+src/Composer/Package/Loader/ArrayLoader.phpT҄Q05src/Composer/Package/Loader/ValidatingArrayLoader.php0)T҄Q0)_Ӷ1src/Composer/Package/Loader/RootPackageLoader.phpT҄Qsrc/Composer/Package/Locker.php T҄Q  @˶)src/Composer/Package/PackageInterface.phpgT҄Qg`XĶ$src/Composer/Package/BasePackage.php[ T҄Q[ $Ҷ.src/Composer/Package/Version/VersionParser.php$T҄Q$i1src/Composer/Package/CompletePackageInterface.phpT҄Q2-src/Composer/Package/RootPackageInterface.phpT҄QqKж$src/Composer/Package/RootPackage.phpnT҄QnACO3src/Composer/Package/Archiver/ArchiverInterface.phpT҄Q7src/Composer/Package/Archiver/ComposerExcludeFilter.phpT҄QSZ0.src/Composer/Package/Archiver/PharArchiver.phpT҄Q&c3src/Composer/Package/Archiver/BaseExcludeFilter.phpT҄Q(2src/Composer/Package/Archiver/GitExcludeFilter.phpwT҄QwLgU7src/Composer/Package/Archiver/ArchivableFilesFinder.phpLT҄QL%iĶ0src/Composer/Package/Archiver/ArchiveManager.php T҄Q |_1src/Composer/Package/Archiver/HgExcludeFilter.phpT҄Q@.)src/Composer/Package/RootAliasPackage.phpBT҄QBeG src/Composer/Package/Package.phpT҄QZζ%src/Composer/Package/AliasPackage.phpCT҄QCLsrc/Composer/Package/Link.phpQT҄QQ97src/Composer/Package/LinkConstraint/EmptyConstraint.phpT҄Q7src/Composer/Package/LinkConstraint/MultiConstraint.phpgT҄Qgx89src/Composer/Package/LinkConstraint/VersionConstraint.phpT҄Q)mζ:src/Composer/Package/LinkConstraint/SpecificConstraint.phpqT҄QqS?src/Composer/Package/LinkConstraint/LinkConstraintInterface.phpT҄Qsrc/Composer/Cache.php T҄Q }03src/Composer/DependencyResolver/PolicyInterface.phpT҄QB+src/Composer/DependencyResolver/RuleSet.php T҄Q Jy 6src/Composer/DependencyResolver/SolverBugException.phpT҄Q"qN1src/Composer/DependencyResolver/DefaultPolicy.phpzT҄Qz'TĶ-src/Composer/DependencyResolver/Decisions.phpQT҄QQ?$1src/Composer/DependencyResolver/RuleWatchNode.phpT҄Q]$;src/Composer/DependencyResolver/SolverProblemsException.php%T҄Q%TP/src/Composer/DependencyResolver/Transaction.phpT҄Qއ@src/Composer/DependencyResolver/Operation/UninstallOperation.phpIT҄QIFɶ=src/Composer/DependencyResolver/Operation/UpdateOperation.phphT҄QhS]Isrc/Composer/DependencyResolver/Operation/MarkAliasInstalledOperation.phpT҄QxUZa>src/Composer/DependencyResolver/Operation/InstallOperation.phpCT҄QC\*=src/Composer/DependencyResolver/Operation/SolverOperation.phpT҄QħݔKsrc/Composer/DependencyResolver/Operation/MarkAliasUninstalledOperation.phpT҄Q_iǫ@src/Composer/DependencyResolver/Operation/OperationInterface.phpT҄Q&(src/Composer/DependencyResolver/Pool.php &T҄Q &(src/Composer/DependencyResolver/Rule.phpT҄Qe4src/Composer/DependencyResolver/RuleSetGenerator.phpOT҄QO_<=/src/Composer/DependencyResolver/DebugSolver.phpT҄Qҭ3src/Composer/DependencyResolver/RuleSetIterator.phpT҄Q}2src/Composer/DependencyResolver/RuleWatchChain.phpiT҄Qih,*src/Composer/DependencyResolver/Solver.php5T҄Q5‘+src/Composer/DependencyResolver/Request.phpT҄Q1I+src/Composer/DependencyResolver/Problem.phpnT҄Qn^2src/Composer/DependencyResolver/RuleWatchGraph.phpT҄Qrv-src/Composer/Config/ConfigSourceInterface.phpT҄Q!p(src/Composer/Config/JsonConfigSource.phpT҄Qsrc/Composer/Factory.phpZ$T҄QZ$^ src/Composer/Util/Filesystem.phpT҄Qsrc/Composer/Util/GitHub.php T҄Q KŶ%src/Composer/Util/ProcessExecutor.phpT҄Q-&src/Composer/Util/RemoteFilesystem.phpT҄QVo=*src/Composer/Util/StreamContextFactory.php$ T҄Q$ k:%src/Composer/Util/ConfigValidator.php T҄Q r8"src/Composer/Util/ErrorHandler.phpT҄Q@+src/Composer/Util/SpdxLicenseIdentifier.php6 T҄Q6 6osrc/Composer/Util/Svn.phpY T҄QY src/Composer/Composer.phpT҄QC%src/Composer/Json/JsonManipulator.phpT҄Q_ֶsrc/Composer/Json/JsonFile.phpT҄QJ߶-src/Composer/Json/JsonValidationException.php2T҄Q29b1src/Composer/Config.phpT҄Q>src/Composer/Script/Event.phpuT҄Qu7$src/Composer/Script/ScriptEvents.phpT҄Q]$src/Composer/Script/CommandEvent.phpoT҄Qo y$src/Composer/Script/PackageEvent.phpT҄Qa 'src/Composer/Script/EventDispatcher.phpX T҄QX V.˶(src/Composer/Installer/NoopInstaller.php5T҄Q58qa/src/Composer/Installer/MetapackageInstaller.phpT҄Qfζ(src/Composer/Installer/PearInstaller.phpT҄QWu+src/Composer/Installer/ProjectInstaller.phpT҄QQ+src/Composer/Installer/LibraryInstaller.php.T҄Q..src/Composer/Installer/InstallationManager.php;T҄Q;RӶ-src/Composer/Installer/InstallerInterface.phpT҄QHS-src/Composer/Installer/InstallerInstaller.php T҄Q m $src/Composer/Console/Application.phpGT҄QG؄WҶ,src/Composer/Console/HtmlOutputFormatter.phpT҄QF+src/Composer/Autoload/AutoloadGenerator.phpO7T҄QO7hm+src/Composer/Autoload/ClassMapGenerator.php T҄Q 4&src/Composer/Installer.php[T҄Q[Lz%src/Composer/Autoload/ClassLoader.phpT҄QJRڶres/spdx-identifier.jsoni T҄Qi Rres/composer-schema.json=T҄Q=F$*nsrc/Composer/IO/hiddeninput.exe$T҄Q$v?vendor/symfony/process/Symfony/Component/Process/PhpProcess.phpT҄Q8ZԷEvendor/symfony/process/Symfony/Component/Process/ExecutableFinder.phpT҄Q J<vendor/symfony/process/Symfony/Component/Process/Process.php}DT҄Q}DQn8Cvendor/symfony/process/Symfony/Component/Process/ProcessBuilder.phpT҄QAvendor/symfony/process/Symfony/Component/Process/ProcessUtils.phpGT҄QGS8}Qvendor/symfony/process/Symfony/Component/Process/Exception/ExceptionInterface.phpfT҄Qf]>TOvendor/symfony/process/Symfony/Component/Process/Exception/RuntimeException.phpT҄Q:Mvendor/symfony/process/Symfony/Component/Process/Exception/LogicException.phpT҄Q Uvendor/symfony/process/Symfony/Component/Process/Exception/ProcessFailedException.phpT҄Q|窶Wvendor/symfony/process/Symfony/Component/Process/Exception/InvalidArgumentException.phpT҄Q+_Hvendor/symfony/process/Symfony/Component/Process/PhpExecutableFinder.phpT҄QDEHvendor/symfony/console/Symfony/Component/Console/Command/HelpCommand.php T҄Q 71XDvendor/symfony/console/Symfony/Component/Console/Command/Command.php_T҄Q_!Hvendor/symfony/console/Symfony/Component/Console/Command/ListCommand.phpWT҄QWMvendor/symfony/console/Symfony/Component/Console/Tester/ApplicationTester.phpgT҄Qg7-GƶIvendor/symfony/console/Symfony/Component/Console/Tester/CommandTester.phpT҄QSmXvendor/symfony/console/Symfony/Component/Console/Formatter/OutputFormatterStyleStack.phpT҄QyI'Svendor/symfony/console/Symfony/Component/Console/Formatter/OutputFormatterStyle.phpU T҄QU z\vendor/symfony/console/Symfony/Component/Console/Formatter/OutputFormatterStyleInterface.phpT҄Q=Nvendor/symfony/console/Symfony/Component/Console/Formatter/OutputFormatter.php T҄Q >WWvendor/symfony/console/Symfony/Component/Console/Formatter/OutputFormatterInterface.phpT҄Q3l~@vendor/symfony/console/Symfony/Component/Console/Application.phpKT҄QKizxHvendor/symfony/console/Symfony/Component/Console/Input/InputArgument.phpT҄QK]i@vendor/symfony/console/Symfony/Component/Console/Input/Input.php4 T҄Q4 ݮFvendor/symfony/console/Symfony/Component/Console/Input/StringInput.phpT҄Q]thFvendor/symfony/console/Symfony/Component/Console/Input/InputOption.php T҄Q 1Dvendor/symfony/console/Symfony/Component/Console/Input/ArgvInput.phpT҄Q~Ivendor/symfony/console/Symfony/Component/Console/Input/InputInterface.php T҄Q 9ǶEvendor/symfony/console/Symfony/Component/Console/Input/ArrayInput.php T҄Q "9Jvendor/symfony/console/Symfony/Component/Console/Input/InputDefinition.phpT҄QI :vendor/symfony/console/Symfony/Component/Console/Shell.phptT҄Qt-Bvendor/symfony/console/Symfony/Component/Console/Output/Output.phpxT҄QxFvendor/symfony/console/Symfony/Component/Console/Output/NullOutput.phpT҄QǢRvendor/symfony/console/Symfony/Component/Console/Output/ConsoleOutputInterface.phpT҄QHvendor/symfony/console/Symfony/Component/Console/Output/StreamOutput.phpT҄Q.fKvendor/symfony/console/Symfony/Component/Console/Output/OutputInterface.php#T҄Q#LYIvendor/symfony/console/Symfony/Component/Console/Output/ConsoleOutput.php;T҄Q;PF,Svendor/symfony/console/Symfony/Component/Console/Descriptor/DescriptorInterface.phpT҄QeNvendor/symfony/console/Symfony/Component/Console/Descriptor/TextDescriptor.phpiT҄QiȶMvendor/symfony/console/Symfony/Component/Console/Descriptor/XmlDescriptor.php4T҄Q4[Rvendor/symfony/console/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php T҄Q )Vvendor/symfony/console/Symfony/Component/Console/Descriptor/ApplicationDescription.phpT҄Q)IJvendor/symfony/console/Symfony/Component/Console/Descriptor/Descriptor.phpT҄Q nNvendor/symfony/console/Symfony/Component/Console/Descriptor/JsonDescriptor.php* T҄Q* EKvendor/symfony/console/Symfony/Component/Console/Helper/HelperInterface.phpT҄Q=e Gvendor/symfony/console/Symfony/Component/Console/Helper/TableHelper.phpT҄Q]7Lvendor/symfony/console/Symfony/Component/Console/Helper/DescriptorHelper.phpT҄QKvendor/symfony/console/Symfony/Component/Console/Helper/FormatterHelper.phpT҄QIEvendor/symfony/console/Symfony/Component/Console/Helper/HelperSet.phpT҄Q^9tHvendor/symfony/console/Symfony/Component/Console/Helper/DialogHelper.phpgT҄QgpIȶBvendor/symfony/console/Symfony/Component/Console/Helper/Helper.php"T҄Q"UJvendor/symfony/console/Symfony/Component/Console/Helper/ProgressHelper.phpT҄Q}XGvendor/symfony/console/Symfony/Component/Console/Event/ConsoleEvent.phpT҄Qx\Pvendor/symfony/console/Symfony/Component/Console/Event/ConsoleTerminateEvent.phptT҄Qt~ҶSvendor/symfony/console/Symfony/Component/Console/Event/ConsoleForExceptionEvent.phpT҄QjNvendor/symfony/console/Symfony/Component/Console/Event/ConsoleCommandEvent.phpT҄QI=Bvendor/symfony/console/Symfony/Component/Console/ConsoleEvents.phpT҄QRe9vendor/symfony/finder/Symfony/Component/Finder/Finder.phpT҄QA?@vendor/symfony/finder/Symfony/Component/Finder/Shell/Command.phpT҄Qcv>vendor/symfony/finder/Symfony/Component/Finder/Shell/Shell.phpT҄QDA Cvendor/symfony/finder/Symfony/Component/Finder/Expression/Regex.phpZT҄QZ@~Bvendor/symfony/finder/Symfony/Component/Finder/Expression/Glob.phpT҄Q VHvendor/symfony/finder/Symfony/Component/Finder/Expression/Expression.php{T҄Q{'ȼLvendor/symfony/finder/Symfony/Component/Finder/Expression/ValueInterface.php;T҄Q; ӶKvendor/symfony/finder/Symfony/Component/Finder/Adapter/AdapterInterface.phpsT҄Qsrj]Ivendor/symfony/finder/Symfony/Component/Finder/Adapter/BsdFindAdapter.phprT҄QrUmIvendor/symfony/finder/Symfony/Component/Finder/Adapter/GnuFindAdapter.phpUT҄QU$矶Jvendor/symfony/finder/Symfony/Component/Finder/Adapter/AbstractAdapter.php T҄Q JNvendor/symfony/finder/Symfony/Component/Finder/Adapter/AbstractFindAdapter.phpDT҄QD=FEvendor/symfony/finder/Symfony/Component/Finder/Adapter/PhpAdapter.phpT҄Q87vendor/symfony/finder/Symfony/Component/Finder/Glob.php T҄Q z Tvendor/symfony/finder/Symfony/Component/Finder/Iterator/DepthRangeFilterIterator.phpT҄Q0Lvendor/symfony/finder/Symfony/Component/Finder/Iterator/SortableIterator.phpT҄QǶUvendor/symfony/finder/Symfony/Component/Finder/Iterator/FilecontentFilterIterator.php#T҄Q#_VǶMvendor/symfony/finder/Symfony/Component/Finder/Iterator/FilePathsIterator.phpT҄QQSvendor/symfony/finder/Symfony/Component/Finder/Iterator/SizeRangeFilterIterator.phpgT҄Qg!ԗZvendor/symfony/finder/Symfony/Component/Finder/Iterator/ExcludeDirectoryFilterIterator.phpT҄Qz`.Jvendor/symfony/finder/Symfony/Component/Finder/Iterator/FilterIterator.phpT҄Q Vvendor/symfony/finder/Symfony/Component/Finder/Iterator/MultiplePcreFilterIterator.phpT҄QSCPvendor/symfony/finder/Symfony/Component/Finder/Iterator/CustomFilterIterator.php]T҄Q]t౵Rvendor/symfony/finder/Symfony/Component/Finder/Iterator/FilenameFilterIterator.phpT҄QBZSvendor/symfony/finder/Symfony/Component/Finder/Iterator/DateRangeFilterIterator.phpgT҄QgeRvendor/symfony/finder/Symfony/Component/Finder/Iterator/FileTypeFilterIterator.php\T҄Q\p'Vvendor/symfony/finder/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php7T҄Q7P7Nvendor/symfony/finder/Symfony/Component/Finder/Iterator/PathFilterIterator.phpT҄Q_ALvendor/symfony/finder/Symfony/Component/Finder/Comparator/DateComparator.php&T҄Q&hdNvendor/symfony/finder/Symfony/Component/Finder/Comparator/NumberComparator.phpyT҄Qy"`۶Hvendor/symfony/finder/Symfony/Component/Finder/Comparator/Comparator.phpT҄QwTOvendor/symfony/finder/Symfony/Component/Finder/Exception/ExceptionInterface.php{T҄Q{Zvendor/symfony/finder/Symfony/Component/Finder/Exception/OperationNotPermitedException.phpT҄QU88Tvendor/symfony/finder/Symfony/Component/Finder/Exception/AdapterFailureException.phpT҄Qm_,Yvendor/symfony/finder/Symfony/Component/Finder/Exception/ShellCommandFailureException.php$T҄Q$CsӶ>vendor/symfony/finder/Symfony/Component/Finder/SplFileInfo.phpT҄QhŶ4vendor/seld/jsonlint/src/Seld/JsonLint/Undefined.php>T҄Q>q5vendor/seld/jsonlint/src/Seld/JsonLint/JsonParser.php,T҄Q,V\ 0vendor/seld/jsonlint/src/Seld/JsonLint/Lexer.phpT҄Q Y;vendor/seld/jsonlint/src/Seld/JsonLint/ParsingException.phpT҄QIvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Undefined.phpxT҄QxhqDvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Type.phpT҄Q+FFvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Schema.phpT҄Q:fMFvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Number.phpWT҄QW.1Fvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Object.phpT҄QcZSvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/ConstraintInterface.php$T҄Q$gJvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php T҄Q >Jvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Collection.phpVT҄QV<Dvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Enum.phpT҄Q$(mFvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/String.phpT҄Qk=vendor/justinrainbow/json-schema/src/JsonSchema/Validator.php[T҄Q[8vendor/autoload.phpT҄Q'vendor/composer/autoload_namespaces.phpT҄QǶ%vendor/composer/autoload_classmap.phpdT҄QdZH!vendor/composer/autoload_real.phpT҄QTevendor/composer/ClassLoader.php T҄Q >I bin/composernT҄QnWLICENSE3T҄Q3 2 input = $input; $this->output = $output; $this->helperSet = $helperSet; } public function enableDebugging($startTime) { $this->startTime = $startTime; } public function isInteractive() { return $this->input->isInteractive(); } public function isDecorated() { return $this->output->isDecorated(); } public function isVerbose() { return $this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE; } public function isVeryVerbose() { return $this->output->getVerbosity() >= 3; } public function isDebug() { return $this->output->getVerbosity() >= 4; } public function write($messages, $newline = true) { if (null !== $this->startTime) { $messages = (array) $messages; $messages[0] = sprintf( '[%.1fMB/%.2fs] %s', memory_get_usage() / 1024 / 1024, microtime(true) - $this->startTime, $messages[0] ); } $this->output->write($messages, $newline); $this->lastMessage = join($newline ? "\n" : '', (array) $messages); } public function overwrite($messages, $newline = true, $size = null) { $messages = join($newline ? "\n" : '', (array) $messages); if (!isset($size)) { $size = strlen(strip_tags($this->lastMessage)); } $this->write(str_repeat("\x08", $size), false); $this->write($messages, false); $fill = $size - strlen(strip_tags($messages)); if ($fill > 0) { $this->write(str_repeat(' ', $fill), false); $this->write(str_repeat("\x08", $fill), false); } if ($newline) { $this->write(''); } $this->lastMessage = $messages; } public function ask($question, $default = null) { return $this->helperSet->get('dialog')->ask($this->output, $question, $default); } public function askConfirmation($question, $default = true) { return $this->helperSet->get('dialog')->askConfirmation($this->output, $question, $default); } public function askAndValidate($question, $validator, $attempts = false, $default = null) { return $this->helperSet->get('dialog')->askAndValidate($this->output, $question, $validator, $attempts, $default); } public function askAndHideAnswer($question) { if (defined('PHP_WINDOWS_VERSION_BUILD')) { $exe = __DIR__.'\\hiddeninput.exe'; if ('phar:' === substr(__FILE__, 0, 5)) { $tmpExe = sys_get_temp_dir().'/hiddeninput.exe'; $source = fopen(__DIR__.'\\hiddeninput.exe', 'r'); $target = fopen($tmpExe, 'w+'); stream_copy_to_stream($source, $target); fclose($source); fclose($target); unset($source, $target); $exe = $tmpExe; } $this->write($question, false); $value = rtrim(shell_exec($exe)); $this->write(''); if (isset($tmpExe)) { unlink($tmpExe); } return $value; } if (file_exists('/usr/bin/env')) { $test = "/usr/bin/env %s -c 'echo OK' 2> /dev/null"; foreach (array('bash', 'zsh', 'ksh', 'csh') as $sh) { if ('OK' === rtrim(shell_exec(sprintf($test, $sh)))) { $shell = $sh; break; } } if (isset($shell)) { $this->write($question, false); $readCmd = ($shell === 'csh') ? 'set mypassword = $<' : 'read -r mypassword'; $command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd); $value = rtrim(shell_exec($command)); $this->write(''); return $value; } } return $this->ask($question); } public function getAuthentications() { return $this->authentications; } public function hasAuthentication($repositoryName) { $auths = $this->getAuthentications(); return isset($auths[$repositoryName]); } public function getAuthentication($repositoryName) { $auths = $this->getAuthentications(); return isset($auths[$repositoryName]) ? $auths[$repositoryName] : array('username' => null, 'password' => null); } public function setAuthentication($repositoryName, $username, $password = null) { $this->authentications[$repositoryName] = array('username' => $username, 'password' => $password); } } null, 'password' => null); } public function setAuthentication($repositoryName, $username, $password = null) { } } setInteractive(false); $output = new StreamOutput(fopen('php://memory', 'rw'), $verbosity === null ? StreamOutput::VERBOSITY_NORMAL : $verbosity, !empty($formatter), $formatter); parent::__construct($input, $output, new HelperSet(array())); } public function getOutput() { fseek($this->output->getStream(), 0); $output = stream_get_contents($this->output->getStream()); $output = preg_replace_callback("{(?<=^|\n|\x08)(.+?)(\x08+)}", function ($matches) { $pre = strip_tags($matches[1]); if (strlen($pre) === strlen($matches[2])) { return ''; } return rtrim($matches[1])."\n"; }, $output); return $output; } } setName('run-script') ->setDescription('Run the scripts defined in composer.json.') ->setDefinition(array( new InputArgument('script', InputArgument::REQUIRED, 'Script name to run.'), new InputOption('dev', null, InputOption::VALUE_NONE, 'Sets the dev mode.'), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables the dev mode.'), )) ->setHelp(<<run-script command runs scripts defined in composer.json: php composer.phar run-script post-update-cmd EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $script = $input->getArgument('script'); if (!in_array($script, array( ScriptEvents::PRE_INSTALL_CMD, ScriptEvents::POST_INSTALL_CMD, ScriptEvents::PRE_UPDATE_CMD, ScriptEvents::POST_UPDATE_CMD, ))) { if (defined('Composer\Script\ScriptEvents::'.str_replace('-', '_', strtoupper($script)))) { throw new \InvalidArgumentException(sprintf('Script "%s" cannot be run with this command', $script)); } throw new \InvalidArgumentException(sprintf('Script "%s" does not exist', $script)); } $this->getComposer()->getEventDispatcher()->dispatchCommandEvent($script, $input->getOption('dev') || !$input->getOption('no-dev')); } } setName('diagnose') ->setDescription('Diagnoses the system to identify common errors.') ->setHelp(<<diagnose command checks common errors to help debugging problems. EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $this->rfs = new RemoteFilesystem($this->getIO()); $output->write('Checking platform settings: '); $this->outputResult($output, $this->checkPlatform()); $output->write('Checking http connectivity: '); $this->outputResult($output, $this->checkHttp()); $opts = stream_context_get_options(StreamContextFactory::getContext()); if (!empty($opts['http']['proxy'])) { $output->write('Checking HTTP proxy: '); $this->outputResult($output, $this->checkHttpProxy()); $output->write('Checking HTTPS proxy support for request_fulluri: '); $this->outputResult($output, $this->checkHttpsProxyFullUriRequestParam()); } $composer = $this->getComposer(false); if ($composer) { $output->write('Checking composer.json: '); $this->outputResult($output, $this->checkComposerSchema()); } if ($composer) { $config = $composer->getConfig(); } else { $config = Factory::createConfig(); } if ($oauth = $config->get('github-oauth')) { foreach ($oauth as $domain => $token) { $output->write('Checking '.$domain.' oauth access: '); $this->outputResult($output, $this->checkGithubOauth($domain, $token)); } } $output->write('Checking composer version: '); $this->outputResult($output, $this->checkVersion()); return $this->failures; } private function checkComposerSchema() { $validator = new ConfigValidator($this->getIO()); list($errors, $publishErrors, $warnings) = $validator->validate(Factory::getComposerFile()); if ($errors || $publishErrors || $warnings) { $messages = array( 'error' => array_merge($errors, $publishErrors), 'warning' => $warnings, ); $output = ''; foreach ($messages as $style => $msgs) { foreach ($msgs as $msg) { $output .= '<' . $style . '>' . $msg . '' . PHP_EOL; } } return rtrim($output); } return true; } private function checkHttp() { $protocol = extension_loaded('openssl') ? 'https' : 'http'; try { $json = $this->rfs->getContents('packagist.org', $protocol . '://packagist.org/packages.json', false); } catch (\Exception $e) { return $e; } return true; } private function checkHttpProxy() { $protocol = extension_loaded('openssl') ? 'https' : 'http'; try { $json = json_decode($this->rfs->getContents('packagist.org', $protocol . '://packagist.org/packages.json', false), true); $hash = reset($json['provider-includes']); $hash = $hash['sha256']; $path = str_replace('%hash%', $hash, key($json['provider-includes'])); $provider = $this->rfs->getContents('packagist.org', $protocol . '://packagist.org/'.$path, false); 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 checkHttpsProxyFullUriRequestParam() { $url = 'https://api.github.com/repos/Seldaek/jsonlint/zipball/1.0.0 '; try { $rfcResult = $this->rfs->getContents('api.github.com', $url, false); } catch (TransportException $e) { if (!extension_loaded('openssl')) { return 'You need the openssl extension installed for this check'; } try { $this->rfs->getContents('api.github.com', $url, false, array('http' => array('request_fulluri' => false))); } catch (TransportException $e) { return 'Unable to assert the situation, maybe github is down ('.$e->getMessage().')'; } return 'It seems there is a problem with your proxy server, try setting the "HTTP_PROXY_REQUEST_FULLURI" environment variable to "false"'; } return true; } private function checkGithubOauth($domain, $token) { $this->getIO()->setAuthentication($domain, $token, 'x-oauth-basic'); try { $url = $domain === 'github.com' ? 'https://api.'.$domain.'/user/repos' : 'https://'.$domain.'/api/v3/user/repos'; return $this->rfs->getContents($domain, $url, false) ? true : 'Unexpected error'; } 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 checkVersion() { $protocol = extension_loaded('openssl') ? 'https' : 'http'; $latest = trim($this->rfs->getContents('getcomposer.org', $protocol . '://getcomposer.org/version', false)); if (Composer::VERSION !== $latest && Composer::VERSION !== '1.0.0-alpha7') { return 'Your are not running the latest version'; } return true; } private function outputResult(OutputInterface $output, $result) { if (true === $result) { $output->writeln('OK'); } else { $this->failures++; $output->writeln('FAIL'); if ($result instanceof \Exception) { $output->writeln('['.get_class($result).'] '.$result->getMessage()); } elseif ($result) { $output->writeln($result); } } } private function checkPlatform() { $output = ''; $out = function ($msg, $style) use (&$output) { $output .= '<'.$style.'>'.$msg.''; }; $errors = array(); $warnings = array(); $iniPath = php_ini_loaded_file(); $displayIniMessage = false; if ($iniPath) { $iniMessage = PHP_EOL.PHP_EOL.'The php.ini used by your command-line PHP is: ' . $iniPath; } else { $iniMessage = PHP_EOL.PHP_EOL.'A php.ini file does not exist. You will have to create one.'; } $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 (!ini_get('allow_url_fopen')) { $errors['allow_url_fopen'] = true; } if (version_compare(PHP_VERSION, '5.3.2', '<')) { $errors['php'] = PHP_VERSION; } if (!isset($errors['php']) && version_compare(PHP_VERSION, '5.3.4', '<')) { $warnings['php'] = PHP_VERSION; } if (!extension_loaded('openssl')) { $warnings['openssl'] = true; } if (ini_get('apc.enable_cli')) { $warnings['apc_cli'] = true; } ob_start(); phpinfo(INFO_GENERAL); $phpinfo = ob_get_clean(); if (preg_match('{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 (!empty($errors)) { foreach ($errors as $error => $current) { switch ($error) { case 'php': $text = PHP_EOL."Your PHP ({$current}) is too old, you must upgrade to PHP 5.3.2 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; } $out($text, 'error'); } $output .= PHP_EOL; } if (!empty($warnings)) { foreach ($warnings as $warning => $current) { switch ($warning) { case 'apc_cli': $text = PHP_EOL."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 'sigchild': $text = PHP_EOL."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_EOL."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': $text = PHP_EOL."The openssl extension is missing, which will reduce the security and stability of Composer.".PHP_EOL; $text .= "If possible you should enable it or recompile php with --with-openssl"; break; case 'php': $text = PHP_EOL."Your PHP ({$current}) is quite old, upgrading to PHP 5.3.4 or higher is recommended.".PHP_EOL; $text .= "Composer works with 5.3.2+ for most people, but there might be edge case issues."; break; } $out($text, 'warning'); } } if ($displayIniMessage) { $out($iniMessage, 'warning'); } return !$warnings && !$errors ? true : $output; } } setName('archive') ->setDescription('Create an archive of this composer package') ->setDefinition(array( new InputArgument('package', InputArgument::OPTIONAL, 'The package to archive instead of the current project'), new InputArgument('version', InputArgument::OPTIONAL, 'The package version to archive'), new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the resulting archive: tar or zip', 'tar'), new InputOption('dir', false, InputOption::VALUE_REQUIRED, 'Write the archive to this directory', '.'), )) ->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] [package [version]] EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { return $this->archive( $this->getIO(), $input->getArgument('package'), $input->getArgument('version'), $input->getOption('format'), $input->getOption('dir') ); } protected function archive(IOInterface $io, $packageName = null, $version = null, $format = 'tar', $dest = '.') { $config = Factory::createConfig(); $factory = new Factory; $archiveManager = $factory->createArchiveManager($config); if ($packageName) { $package = $this->selectPackage($io, $packageName, $version); if (!$package) { return 1; } } else { $package = $this->getComposer()->getPackage(); } $io->write('Creating the archive.'); $archiveManager->archive($package, $format, $dest); return 0; } protected function selectPackage(IOInterface $io, $packageName, $version = null) { $io->write('Searching for the specified package.'); if ($composer = $this->getComposer(false)) { $localRepo = $composer->getRepositoryManager()->getLocalRepository(); $repos = new CompositeRepository(array_merge(array($localRepo), $composer->getRepositoryManager()->getRepositories())); } else { $defaultRepos = Factory::createDefaultRepositories($this->getIO()); $output->writeln('No composer.json found in the current directory, searching packages from ' . implode(', ', array_keys($defaultRepos))); $repos = new CompositeRepository($defaultRepos); } $pool = new Pool(); $pool->addRepository($repos); $constraint = ($version) ? new VersionConstraint('>=', $version) : null; $packages = $pool->whatProvides($packageName, $constraint); if (count($packages) > 1) { $package = $packages[0]; $io->write('Found multiple matches, selected '.$package->getPrettyString().'.'); $io->write('Alternatives were '.implode(', ', array_map(function ($p) { return $p->getPrettyString(); }, $packages)).'.'); $io->write('Please use a more specific constraint to pick a different package.'); } elseif ($packages) { $package = $packages[0]; $io->write('Found an exact match '.$package->getPrettyString().'.'); } else { $io->write('Could not find a package matching '.$packageName.'.'); return false; } return $package; } } composer) { $application = $this->getApplication(); if ($application instanceof Application) { $this->composer = $application->getComposer($required); } elseif ($required) { 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 setComposer(Composer $composer) { $this->composer = $composer; } public function getIO() { if (null === $this->io) { $application = $this->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; } } setName('create-project') ->setDescription('Create new project from a package into given directory.') ->setDefinition(array( new InputArgument('package', InputArgument::REQUIRED, 'Package name to be installed'), new InputArgument('directory', InputArgument::OPTIONAL, 'Directory where the files should be created'), new InputArgument('version', InputArgument::OPTIONAL, 'Version, will defaults to latest'), new InputOption('stability', 's', InputOption::VALUE_REQUIRED, 'Minimum-stability allowed (unless a version is specified).', 'stable'), 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 even for dev versions.'), new InputOption('repository-url', null, InputOption::VALUE_REQUIRED, 'Pick a different repository url to look for the package.'), new InputOption('dev', null, InputOption::VALUE_NONE, 'Whether to install dependencies for development.'), new InputOption('no-custom-installers', null, InputOption::VALUE_NONE, 'Whether to disable custom installers.'), 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('keep-vcs', null, InputOption::VALUE_NONE, 'Whether to prevent deletion vcs folder.'), )) ->setHelp(<<create-project command creates a new project from a given package into a new directory. 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. 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. Also, it is advisable to install all dependencies required for development by appending the '--dev' flag. To install a package from another repository than the default one you can pass the '--repository-url=http://myrepository.org' flag. EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $config = Factory::createConfig(); $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->getOption('prefer-source') || $input->getOption('prefer-dist')) { $preferSource = $input->getOption('prefer-source'); $preferDist = $input->getOption('prefer-dist'); } return $this->installProject( $this->getIO(), $config, $input->getArgument('package'), $input->getArgument('directory'), $input->getArgument('version'), $input->getOption('stability'), $preferSource, $preferDist, $input->getOption('dev'), $input->getOption('repository-url'), $input->getOption('no-custom-installers'), $input->getOption('no-scripts'), $input->getOption('keep-vcs'), $input->getOption('no-progress') ); } public function installProject(IOInterface $io, $config, $packageName, $directory = null, $packageVersion = null, $stability = 'stable', $preferSource = false, $preferDist = false, $installDevPackages = false, $repositoryUrl = null, $disableCustomInstallers = false, $noScripts = false, $keepVcs = false, $noProgress = false) { $stability = strtolower($stability); if ($stability === 'rc') { $stability = 'RC'; } if (!isset(BasePackage::$stabilities[$stability])) { throw new \InvalidArgumentException('Invalid stability provided ('.$stability.'), must be one of: '.implode(', ', array_keys(BasePackage::$stabilities))); } if (null === $repositoryUrl) { $sourceRepo = new CompositeRepository(Factory::createDefaultRepositories($io, $config)); } elseif ("json" === pathinfo($repositoryUrl, PATHINFO_EXTENSION)) { $sourceRepo = new FilesystemRepository(new JsonFile($repositoryUrl, new RemoteFilesystem($io))); } elseif (0 === strpos($repositoryUrl, 'http')) { $sourceRepo = new ComposerRepository(array('url' => $repositoryUrl), $io, $config); } else { throw new \InvalidArgumentException("Invalid repository url given. Has to be a .json file or an http url."); } $parser = new VersionParser(); $candidates = array(); $requirements = $parser->parseNameVersionPairs(array($packageName)); $name = strtolower($requirements[0]['name']); if (!$packageVersion && isset($requirements[0]['version'])) { $packageVersion = $requirements[0]['version']; } $pool = new Pool($packageVersion ? 'dev' : $stability); $pool->addRepository($sourceRepo); $constraint = $packageVersion ? new VersionConstraint('=', $parser->normalize($packageVersion)) : null; $candidates = $pool->whatProvides($name, $constraint); foreach ($candidates as $key => $candidate) { if ($candidate->getName() !== $name) { unset($candidates[$key]); } } if (!$candidates) { throw new \InvalidArgumentException("Could not find package $name" . ($packageVersion ? " with version $packageVersion." : " with stability $stability.")); } if (null === $directory) { $parts = explode("/", $name, 2); $directory = getcwd() . DIRECTORY_SEPARATOR . array_pop($parts); } $package = $candidates[0]; foreach ($candidates as $candidate) { if (version_compare($package->getVersion(), $candidate->getVersion(), '<')) { $package = $candidate; } } unset($candidates); $io->write('Installing ' . $package->getName() . ' (' . VersionParser::formatVersion($package, false) . ')'); if ($disableCustomInstallers) { $io->write('Custom installers have been disabled.'); } if (0 === strpos($package->getPrettyVersion(), 'dev-') && in_array($package->getSourceType(), array('git', 'hg'))) { $package->setSourceReference(substr($package->getPrettyVersion(), 4)); } $dm = $this->createDownloadManager($io, $config); $dm->setPreferSource($preferSource) ->setPreferDist($preferDist) ->setOutputProgress(!$noProgress); $projectInstaller = new ProjectInstaller($directory, $dm); $im = $this->createInstallationManager(); $im->addInstaller($projectInstaller); $im->install(new InstalledFilesystemRepository(new JsonFile('php://memory')), new InstallOperation($package)); $im->notifyInstalls(); $installedFromVcs = 'source' === $package->getInstallationSource(); $io->write('Created project in ' . $directory . ''); chdir($directory); putenv('COMPOSER_ROOT_VERSION='.$package->getPrettyVersion()); unset($dm, $im, $config, $projectInstaller, $sourceRepo, $package); $composer = Factory::create($io); $installer = Installer::create($io, $composer); $installer->setPreferSource($preferSource) ->setPreferDist($preferDist) ->setDevMode($installDevPackages) ->setRunScripts( ! $noScripts); if ($disableCustomInstallers) { $installer->disableCustomInstallers(); } if (!$installer->run()) { return 1; } if (!$keepVcs && $installedFromVcs && ( !$io->isInteractive() || $io->askConfirmation('Do you want to remove the existing VCS (.git, .svn..) history? [Y,n]? ', true) ) ) { $finder = new Finder(); $finder->depth(0)->directories()->in(getcwd())->ignoreVCS(false)->ignoreDotFiles(false); foreach (array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg') as $vcsName) { $finder->name($vcsName); } try { $fs = new Filesystem(); $dirs = iterator_to_array($finder); unset($finder); foreach ($dirs as $dir) { if (!$fs->removeDirectory($dir)) { throw new \RuntimeException('Could not remove '.$dir); } } } catch (\Exception $e) { $io->write('An error occurred while removing the VCS metadata: '.$e->getMessage().''); } } return 0; } protected function createDownloadManager(IOInterface $io, Config $config) { $factory = new Factory(); return $factory->createDownloadManager($io, $config); } protected function createInstallationManager() { return new InstallationManager(); } } setName('about') ->setDescription('Short information about Composer') ->setHelp(<<php composer.phar about EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln(<<Composer - Package Management for PHP Composer is a dependency manager tracking local dependencies of your projects and libraries. See http://getcomposer.org/ for more information. EOT ); } } setName('show') ->setDescription('Show information about packages') ->setDefinition(array( new InputArgument('package', InputArgument::OPTIONAL, 'Package to inspect'), new InputArgument('version', InputArgument::OPTIONAL, 'Version or version constraint to inspect'), new InputOption('installed', 'i', InputOption::VALUE_NONE, 'List installed packages only'), 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'), )) ->setHelp(<<versionParser = new VersionParser; $platformRepo = new PlatformRepository; if ($input->getOption('self')) { $package = $this->getComposer(false)->getPackage(); $repos = $installedRepo = new ArrayRepository(array($package)); } elseif ($input->getOption('platform')) { $repos = $installedRepo = $platformRepo; } elseif ($input->getOption('installed')) { $repos = $installedRepo = $this->getComposer()->getRepositoryManager()->getLocalRepository(); } elseif ($input->getOption('available')) { $installedRepo = $platformRepo; if ($composer = $this->getComposer(false)) { $repos = new CompositeRepository($composer->getRepositoryManager()->getRepositories()); } else { $defaultRepos = Factory::createDefaultRepositories($this->getIO()); $repos = new CompositeRepository($defaultRepos); $output->writeln('No composer.json found in the current directory, showing available packages from ' . implode(', ', array_keys($defaultRepos))); } } elseif ($composer = $this->getComposer(false)) { $composer = $this->getComposer(); $localRepo = $composer->getRepositoryManager()->getLocalRepository(); $installedRepo = new CompositeRepository(array($localRepo, $platformRepo)); $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories())); } else { $defaultRepos = Factory::createDefaultRepositories($this->getIO()); $output->writeln('No composer.json found in the current directory, showing available packages from ' . implode(', ', array_keys($defaultRepos))); $installedRepo = $platformRepo; $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos)); } if ($input->getArgument('package') || !empty($package)) { $versions = array(); if (empty($package)) { list($package, $versions) = $this->getPackage($installedRepo, $repos, $input->getArgument('package'), $input->getArgument('version')); if (!$package) { throw new \InvalidArgumentException('Package '.$input->getArgument('package').' not found'); } } else { $versions = array($package->getPrettyVersion() => $package->getVersion()); } $this->printMeta($input, $output, $package, $versions, $installedRepo, $repos); $this->printLinks($input, $output, $package, 'requires'); $this->printLinks($input, $output, $package, 'devRequires', 'requires (dev)'); if ($package->getSuggests()) { $output->writeln("\nsuggests"); foreach ($package->getSuggests() as $suggested => $reason) { $output->writeln($suggested . ' ' . $reason . ''); } } $this->printLinks($input, $output, $package, 'provides'); $this->printLinks($input, $output, $package, 'conflicts'); $this->printLinks($input, $output, $package, 'replaces'); return; } $packages = array(); if ($repos instanceof CompositeRepository) { $repos = $repos->getRepositories(); } elseif (!is_array($repos)) { $repos = array($repos); } foreach ($repos as $repo) { if ($repo === $platformRepo) { $type = 'platform:'; } elseif ( $repo === $installedRepo || ($installedRepo instanceof CompositeRepository && in_array($repo, $installedRepo->getRepositories(), true)) ) { $type = 'installed:'; } else { $type = 'available:'; } if ($repo instanceof ComposerRepository && $repo->hasProviders()) { foreach ($repo->getProviderNames() 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(), '<') ) { $packages[$type][$package->getName()] = $package; } } } } $tree = !$input->getOption('platform') && !$input->getOption('installed') && !$input->getOption('available'); $indent = $tree ? ' ' : ''; foreach (array('platform:' => true, 'available:' => false, 'installed:' => true) as $type => $showVersion) { if (isset($packages[$type])) { if ($tree) { $output->writeln($type); } ksort($packages[$type]); $nameLength = $versionLength = 0; foreach ($packages[$type] as $package) { if (is_object($package)) { $nameLength = max($nameLength, strlen($package->getPrettyName())); $versionLength = max($versionLength, strlen($this->versionParser->formatVersion($package))); } else { $nameLength = max($nameLength, $package); } } list($width) = $this->getApplication()->getTerminalDimensions(); if (defined('PHP_WINDOWS_VERSION_BUILD')) { $width--; } $writeVersion = !$input->getOption('name-only') && $showVersion && ($nameLength + $versionLength + 3 <= $width); $writeDescription = !$input->getOption('name-only') && ($nameLength + ($showVersion ? $versionLength : 0) + 24 <= $width); foreach ($packages[$type] as $package) { if (is_object($package)) { $output->write($indent . str_pad($package->getPrettyName(), $nameLength, ' '), false); if ($writeVersion) { $output->write(' ' . str_pad($this->versionParser->formatVersion($package), $versionLength, ' '), false); } if ($writeDescription) { $description = strtok($package->getDescription(), "\r\n"); $remaining = $width - $nameLength - $versionLength - 4; if (strlen($description) > $remaining) { $description = substr($description, 0, $remaining - 3) . '...'; } $output->write(' ' . $description); } } else { $output->write($indent . $package); } $output->writeln(''); } if ($tree) { $output->writeln(''); } } } } protected function getPackage(RepositoryInterface $installedRepo, RepositoryInterface $repos, $name, $version = null) { $name = strtolower($name); $constraint = null; if ($version) { $constraint = $this->versionParser->parseConstraints($version); } $policy = new DefaultPolicy(); $pool = new Pool('dev'); $pool->addRepository($repos); $matchedPackage = null; $versions = array(); $matches = $pool->whatProvides($name, $constraint); foreach ($matches as $index => $package) { if ($package->getName() !== $name) { unset($matches[$index]); continue; } if (null === $version && $installedRepo->hasPackage($package)) { $matchedPackage = $package; } $versions[$package->getPrettyVersion()] = $package->getVersion(); $matches[$index] = $package->getId(); } if (!$matchedPackage && $matches && $prefered = $policy->selectPreferedPackages($pool, array(), $matches)) { $matchedPackage = $pool->literalToPackage($prefered[0]); } return array($matchedPackage, $versions); } protected function printMeta(InputInterface $input, OutputInterface $output, CompletePackageInterface $package, array $versions, RepositoryInterface $installedRepo, RepositoryInterface $repos) { $output->writeln('name : ' . $package->getPrettyName()); $output->writeln('descrip. : ' . $package->getDescription()); $output->writeln('keywords : ' . join(', ', $package->getKeywords() ?: array())); $this->printVersions($input, $output, $package, $versions, $installedRepo, $repos); $output->writeln('type : ' . $package->getType()); $output->writeln('license : ' . implode(', ', $package->getLicense())); $output->writeln('source : ' . sprintf('[%s] %s %s', $package->getSourceType(), $package->getSourceUrl(), $package->getSourceReference())); $output->writeln('dist : ' . sprintf('[%s] %s %s', $package->getDistType(), $package->getDistUrl(), $package->getDistReference())); $output->writeln('names : ' . implode(', ', $package->getNames())); if ($package->getSupport()) { $output->writeln("\nsupport"); foreach ($package->getSupport() as $type => $value) { $output->writeln('' . $type . ' : '.$value); } } if ($package->getAutoload()) { $output->writeln("\nautoload"); foreach ($package->getAutoload() as $type => $autoloads) { $output->writeln('' . $type . ''); if ($type === 'psr-0') { foreach ($autoloads as $name => $path) { $output->writeln(($name ?: '*') . ' => ' . ($path ?: '.')); } } elseif ($type === 'classmap') { $output->writeln(implode(', ', $autoloads)); } } if ($package->getIncludePaths()) { $output->writeln('include-path'); $output->writeln(implode(', ', $package->getIncludePaths())); } } } protected function printVersions(InputInterface $input, OutputInterface $output, CompletePackageInterface $package, array $versions, RepositoryInterface $installedRepo, RepositoryInterface $repos) { uasort($versions, 'version_compare'); $versions = array_keys(array_reverse($versions)); if ($installedRepo->hasPackage($package)) { $installedVersion = $package->getPrettyVersion(); $key = array_search($installedVersion, $versions); if (false !== $key) { $versions[$key] = '* ' . $installedVersion . ''; } } $versions = implode(', ', $versions); $output->writeln('versions : ' . $versions); } protected function printLinks(InputInterface $input, OutputInterface $output, CompletePackageInterface $package, $linkType, $title = null) { $title = $title ?: $linkType; if ($links = $package->{'get'.ucfirst($linkType)}()) { $output->writeln("\n" . $title . ""); foreach ($links as $link) { $output->writeln($link->getTarget() . ' ' . $link->getPrettyConstraint() . ''); } } } } setName('update') ->setDescription('Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file.') ->setDefinition(array( new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Packages that should be updated, if not provided all packages are.'), 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 even for dev versions.'), 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, 'Enables installation of require-dev packages (enabled by default, only present for sanity).'), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables installation of require-dev packages.'), new InputOption('no-custom-installers', null, InputOption::VALUE_NONE, 'Disables all custom installers.'), new InputOption('no-scripts', null, InputOption::VALUE_NONE, 'Skips the execution of all scripts defined in composer.json file.'), new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'), 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') )) ->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 [...] EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $composer = $this->getComposer(); $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress')); $io = $this->getIO(); $install = Installer::create($io, $composer); $preferSource = false; $preferDist = false; switch ($composer->getConfig()->get('preferred-install')) { case 'source': $preferSource = true; break; case 'dist': $preferDist = true; break; case 'auto': default: break; } if ($input->getOption('prefer-source') || $input->getOption('prefer-dist')) { $preferSource = $input->getOption('prefer-source'); $preferDist = $input->getOption('prefer-dist'); } $install ->setDryRun($input->getOption('dry-run')) ->setVerbose($input->getOption('verbose')) ->setPreferSource($preferSource) ->setPreferDist($preferDist) ->setDevMode(!$input->getOption('no-dev')) ->setRunScripts(!$input->getOption('no-scripts')) ->setOptimizeAutoloader($input->getOption('optimize-autoloader')) ->setUpdate(true) ->setUpdateWhitelist($input->getArgument('packages')) ; if ($input->getOption('no-custom-installers')) { $install->disableCustomInstallers(); } return $install->run() ? 0 : 1; } } setName('config') ->setDescription('Set config options') ->setDefinition(array( 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('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', 'composer.json'), new InputArgument('setting-key', null, 'Setting key'), new InputArgument('setting-value', InputArgument::IS_ARRAY, 'Setting value'), )) ->setHelp(<<%command.full_name% --global To add a repository: %command.full_name% repositories.foo vcs http://bar.com You can add a repository to the global config.json file by passing in the --global option. 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 EOT ) ; } protected function initialize(InputInterface $input, OutputInterface $output) { if ($input->getOption('global') && 'composer.json' !== $input->getOption('file')) { throw new \RuntimeException('--file and --global can not be combined'); } $this->config = Factory::createConfig(); $configFile = $input->getOption('global') ? ($this->config->get('home') . '/config.json') : $input->getOption('file'); $this->configFile = new JsonFile($configFile); $this->configSource = new JsonConfigSource($this->configFile); if ($input->getOption('global') && !$this->configFile->exists()) { touch($this->configFile->getPath()); $this->configFile->write(array('config' => new \ArrayObject)); chmod($this->configFile->getPath(), 0600); } if (!$this->configFile->exists()) { throw new \RuntimeException('No composer.json found in the current directory'); } } protected function execute(InputInterface $input, OutputInterface $output) { if ($input->getOption('editor')) { $editor = getenv('EDITOR'); if (!$editor) { if (defined('PHP_WINDOWS_VERSION_BUILD')) { $editor = 'notepad'; } else { foreach (array('vim', 'vi', 'nano', 'pico', 'ed') as $candidate) { if (exec('which '.$candidate)) { $editor = $candidate; break; } } } } system($editor . ' ' . $this->configFile->getPath() . (defined('PHP_WINDOWS_VERSION_BUILD') ? '': ' > `tty`')); return 0; } if (!$input->getOption('global')) { $this->config->merge($this->configFile->read()); } if ($input->getOption('list')) { $this->listConfiguration($this->config->all(), $this->config->raw(), $output); return 0; } $settingKey = $input->getArgument('setting-key'); if (!$settingKey) { return 0; } if (array() !== $input->getArgument('setting-value') && $input->getOption('unset')) { throw new \RuntimeException('You can not combine a setting value with --unset'); } if (array() === $input->getArgument('setting-value') && !$input->getOption('unset')) { $data = $this->config->all(); if (preg_match('/^repos?(?:itories)?(?:\.(.+))?/', $settingKey, $matches)) { if (empty($matches[1])) { $value = isset($data['repositories']) ? $data['repositories'] : array(); } 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); $data = $data['config']; foreach ($bits as $bit) { if (isset($data[$bit])) { $data = $data[$bit]; } elseif (isset($data[implode('.', $bits)])) { $data = $data[implode('.', $bits)]; break; } else { throw new \RuntimeException($settingKey.' is not defined'); } array_shift($bits); } $value = $data; } elseif (isset($data['config'][$settingKey])) { $value = $data['config'][$settingKey]; } else { throw new \RuntimeException($settingKey.' is not defined'); } if (is_array($value)) { $value = json_encode($value); } $output->writeln($value); return 0; } $values = $input->getArgument('setting-value'); if (preg_match('/^repos?(?:itories)?\.(.+)/', $settingKey, $matches)) { if ($input->getOption('unset')) { return $this->configSource->removeRepository($matches[1]); } if (2 !== count($values)) { throw new \RuntimeException('You must pass the type and a url. Example: php composer.phar config repositories.foo vcs http://bar.com'); } return $this->configSource->addRepository($matches[1], array( 'type' => $values[0], 'url' => $values[1], )); } if (preg_match('/^github-oauth\.(.+)/', $settingKey, $matches)) { if ($input->getOption('unset')) { return $this->configSource->removeConfigSetting('github-oauth.'.$matches[1]); } if (1 !== count($values)) { throw new \RuntimeException('Too many arguments, expected only one token'); } return $this->configSource->addConfigSetting('github-oauth.'.$matches[1], $values[0]); } $booleanValidator = function ($val) { return in_array($val, array('true', 'false', '1', '0'), true); }; $booleanNormalizer = function ($val) { return $val !== 'false' && (bool) $val; }; $uniqueConfigValues = array( 'process-timeout' => array('is_numeric', 'intval'), 'use-include-path' => array( $booleanValidator, $booleanNormalizer ), 'preferred-install' => array( function ($val) { return in_array($val, array('auto', 'source', 'dist'), true); }, function ($val) { return $val; } ), 'notify-on-install' => array( $booleanValidator, $booleanNormalizer ), 'vendor-dir' => array('is_string', function ($val) { return $val; }), 'bin-dir' => array('is_string', function ($val) { return $val; }), 'cache-dir' => array('is_string', function ($val) { return $val; }), 'cache-files-dir' => array('is_string', function ($val) { return $val; }), 'cache-repo-dir' => array('is_string', function ($val) { return $val; }), 'cache-vcs-dir' => array('is_string', function ($val) { return $val; }), 'cache-ttl' => array('is_numeric', 'intval'), 'cache-files-ttl' => array('is_numeric', 'intval'), 'cache-files-maxsize' => array( function ($val) { return preg_match('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', $val) > 0; }, function ($val) { return $val; } ), 'discard-changes' => array( function ($val) { return in_array($val, array('stash', 'true', 'false', '1', '0'), true); }, function ($val) { if ('stash' === $val) { return 'stash'; } return $val !== 'false' && (bool) $val; } ), ); $multiConfigValues = array( 'github-protocols' => array( function ($vals) { if (!is_array($vals)) { return 'array expected'; } foreach ($vals as $val) { if (!in_array($val, array('git', 'https', 'http'))) { return 'valid protocols include: git, https, http'; } } return true; }, function ($vals) { return $vals; } ), ); foreach ($uniqueConfigValues as $name => $callbacks) { if ($settingKey === $name) { if ($input->getOption('unset')) { return $this->configSource->removeConfigSetting($settingKey); } list($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] )); } return $this->configSource->addConfigSetting($settingKey, $normalizer($values[0])); } } foreach ($multiConfigValues as $name => $callbacks) { if ($settingKey === $name) { if ($input->getOption('unset')) { return $this->configSource->removeConfigSetting($settingKey); } list($validator, $normalizer) = $callbacks; if (true !== $validation = $validator($values)) { throw new \RuntimeException(sprintf( '%s is an invalid value'.($validation ? ' ('.$validation.')' : ''), json_encode($values) )); } return $this->configSource->addConfigSetting($settingKey, $normalizer($values)); } } throw new \InvalidArgumentException('Setting '.$settingKey.' does not exist or is not supported by this command'); } protected function listConfiguration(array $contents, array $rawContents, OutputInterface $output, $k = null) { $origK = $k; foreach ($contents as $key => $value) { if ($k === null && !in_array($key, array('config', 'repositories'))) { continue; } $rawVal = isset($rawContents[$key]) ? $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); if (substr_count($k, '.') > 1) { $k = str_split($k, strrpos($k, '.', -2)); $k = $k[0] . '.'; } else { $k = $origK; } continue; } if (is_array($value)) { $value = array_map(function ($val) { return is_array($val) ? json_encode($val) : $val; }, $value); $value = '['.implode(', ', $value).']'; } if (is_bool($value)) { $value = var_export($value, true); } if (is_string($rawVal) && $rawVal != $value) { $output->writeln('[' . $k . $key . '] ' . $rawVal . ' (' . $value . ')'); } else { $output->writeln('[' . $k . $key . '] ' . $value . ''); } } } } setName('install') ->setDescription('Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.') ->setDefinition(array( 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 even for dev versions.'), 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, 'Enables installation of require-dev packages.'), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables installation of require-dev packages (enabled by default, only present for sanity).'), new InputOption('no-custom-installers', null, InputOption::VALUE_NONE, 'Disables all custom installers.'), new InputOption('no-scripts', null, InputOption::VALUE_NONE, 'Skips the execution of all scripts defined in composer.json file.'), new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'), 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') )) ->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 EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $composer = $this->getComposer(); $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress')); $io = $this->getIO(); $install = Installer::create($io, $composer); $preferSource = false; $preferDist = false; switch ($composer->getConfig()->get('preferred-install')) { case 'source': $preferSource = true; break; case 'dist': $preferDist = true; break; case 'auto': default: break; } if ($input->getOption('prefer-source') || $input->getOption('prefer-dist')) { $preferSource = $input->getOption('prefer-source'); $preferDist = $input->getOption('prefer-dist'); } $install ->setDryRun($input->getOption('dry-run')) ->setVerbose($input->getOption('verbose')) ->setPreferSource($preferSource) ->setPreferDist($preferDist) ->setDevMode($input->getOption('dev')) ->setRunScripts(!$input->getOption('no-scripts')) ->setOptimizeAutoloader($input->getOption('optimize-autoloader')) ; if ($input->getOption('no-custom-installers')) { $install->disableCustomInstallers(); } return $install->run() ? 0 : 1; } } setName('validate') ->setDescription('Validates a composer.json') ->setDefinition(array( new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file', './composer.json') )) ->setHelp(<<getArgument('file'); if (!file_exists($file)) { $output->writeln('' . $file . ' not found.'); return 1; } if (!is_readable($file)) { $output->writeln('' . $file . ' is not readable.'); return 1; } $validator = new ConfigValidator($this->getIO()); list($errors, $publishErrors, $warnings) = $validator->validate($file); if (!$errors && !$publishErrors && !$warnings) { $output->writeln('' . $file . ' is valid'); } elseif (!$errors && !$publishErrors) { $output->writeln('' . $file . ' is valid, but with a few warnings'); $output->writeln('See http://getcomposer.org/doc/04-schema.md for details on the schema'); } elseif (!$errors) { $output->writeln('' . $file . ' is valid for simple usage with composer but has'); $output->writeln('strict errors that make it unable to be published as a package:'); $output->writeln('See http://getcomposer.org/doc/04-schema.md for details on the schema'); } else { $output->writeln('' . $file . ' is invalid, the following errors/warnings were found:'); } $messages = array( 'error' => array_merge($errors, $publishErrors), 'warning' => $warnings, ); foreach ($messages as $style => $msgs) { foreach ($msgs as $msg) { $output->writeln('<' . $style . '>' . $msg . ''); } } return $errors || $publishErrors ? 1 : 0; } } array('requires', 'requires'), 'require-dev' => array('devRequires', 'requires (dev)'), ); protected function configure() { $this ->setName('depends') ->setDescription('Shows which packages depend on the given package') ->setDefinition(array( new InputArgument('package', InputArgument::REQUIRED, 'Package to inspect'), new InputOption('link-type', '', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Link types to show (require, require-dev)', array_keys($this->linkTypes)), )) ->setHelp(<<php composer.phar depends composer/composer EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $repo = $this->getComposer()->getRepositoryManager()->getLocalRepository(); $needle = $input->getArgument('package'); $pool = new Pool(); $pool->addRepository($repo); $packages = $pool->whatProvides($needle); if (empty($packages)) { throw new \InvalidArgumentException('Could not find package "'.$needle.'" in your project.'); } $linkTypes = $this->linkTypes; $types = array_map(function ($type) use ($linkTypes) { $type = rtrim($type, 's'); if (!isset($linkTypes[$type])) { throw new \InvalidArgumentException('Unexpected link type: '.$type.', valid types: '.implode(', ', array_keys($linkTypes))); } return $type; }, $input->getOption('link-type')); $messages = array(); $outputPackages = array(); foreach ($repo->getPackages() as $package) { foreach ($types as $type) { foreach ($package->{'get'.$linkTypes[$type][0]}() as $link) { if ($link->getTarget() === $needle) { if (!isset($outputPackages[$package->getName()])) { $messages[] = ''.$package->getPrettyName() . ' ' . $linkTypes[$type][1] . ' ' . $needle .' (' . $link->getPrettyConstraint() . ')'; $outputPackages[$package->getName()] = true; } } } } } if ($messages) { sort($messages); $output->writeln($messages); } else { $output->writeln('There is no installed package depending on "'.$needle.'".'); } } } setName('search') ->setDescription('Search for packages') ->setDefinition(array( new InputOption('only-name', 'N', InputOption::VALUE_NONE, 'Search only in name'), new InputArgument('tokens', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'tokens to search for'), )) ->setHelp(<<php composer.phar search symfony composer EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $platformRepo = new PlatformRepository; if ($composer = $this->getComposer(false)) { $localRepo = $composer->getRepositoryManager()->getLocalRepository(); $installedRepo = new CompositeRepository(array($localRepo, $platformRepo)); $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories())); } else { $defaultRepos = Factory::createDefaultRepositories($this->getIO()); $output->writeln('No composer.json found in the current directory, showing packages from ' . implode(', ', array_keys($defaultRepos))); $installedRepo = $platformRepo; $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos)); } $onlyName = $input->getOption('only-name'); $flags = $onlyName ? RepositoryInterface::SEARCH_NAME : RepositoryInterface::SEARCH_FULLTEXT; $results = $repos->search(implode(' ', $input->getArgument('tokens')), $flags); foreach ($results as $result) { $output->writeln($result['name'] . (isset($result['description']) ? ' '. $result['description'] : '')); } } } setName('self-update') ->setAliases(array('selfupdate')) ->setDescription('Updates composer.phar to the latest version.') ->setHelp(<<self-update command checks getcomposer.org for newer versions of composer and if found, installs the latest. php composer.phar self-update EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $protocol = extension_loaded('openssl') ? 'https' : 'http'; $rfs = new RemoteFilesystem($this->getIO()); $latest = trim($rfs->getContents('getcomposer.org', $protocol . '://getcomposer.org/version', false)); if (Composer::VERSION !== $latest) { $output->writeln(sprintf("Updating to version %s.", $latest)); $remoteFilename = $protocol . '://getcomposer.org/composer.phar'; $localFilename = realpath($_SERVER['argv'][0]) ?: $_SERVER['argv'][0]; $tempFilename = dirname($localFilename) . '/' . basename($localFilename, '.phar').'-temp.phar'; $rfs->copy('getcomposer.org', $remoteFilename, $tempFilename); if (!file_exists($tempFilename)) { $output->writeln('The download of the new composer version failed for an unexpected reason'); return 1; } try { chmod($tempFilename, 0777 & ~umask()); $phar = new \Phar($tempFilename); unset($phar); rename($tempFilename, $localFilename); } catch (\Exception $e) { @unlink($tempFilename); if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) { throw $e; } $output->writeln('The download is corrupted ('.$e->getMessage().').'); $output->writeln('Please re-run the self-update command to try again.'); } } else { $output->writeln("You are using the latest composer version."); } } } setName('require') ->setDescription('Adds required packages to your composer.json and installs them') ->setDefinition(array( new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Required package with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or "foo/bar 1.0.0"'), new InputOption('dev', null, InputOption::VALUE_NONE, 'Add requirement to require-dev.'), 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 even for dev versions.'), 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.'), )) ->setHelp(<<writeln(''.$file.' could not be created.'); return 1; } if (!is_readable($file)) { $output->writeln(''.$file.' is not readable.'); return 1; } if (!is_writable($file)) { $output->writeln(''.$file.' is not writable.'); return 1; } $dialog = $this->getHelperSet()->get('dialog'); $json = new JsonFile($file); $composer = $json->read(); $composerBackup = file_get_contents($json->getPath()); $requirements = $this->determineRequirements($input, $output, $input->getArgument('packages')); $requireKey = $input->getOption('dev') ? 'require-dev' : 'require'; $baseRequirements = array_key_exists($requireKey, $composer) ? $composer[$requireKey] : array(); $requirements = $this->formatRequirements($requirements); if (!$this->updateFileCleanly($json, $baseRequirements, $requirements, $requireKey)) { foreach ($requirements as $package => $version) { $baseRequirements[$package] = $version; } $composer[$requireKey] = $baseRequirements; $json->write($composer); } $output->writeln(''.$file.' has been updated'); if ($input->getOption('no-update')) { return 0; } $composer = $this->getComposer(); $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress')); $io = $this->getIO(); $install = Installer::create($io, $composer); $install ->setVerbose($input->getOption('verbose')) ->setPreferSource($input->getOption('prefer-source')) ->setPreferDist($input->getOption('prefer-dist')) ->setDevMode(true) ->setUpdate(true) ->setUpdateWhitelist(array_keys($requirements)); ; if (!$install->run()) { $output->writeln("\n".'Installation failed, reverting '.$file.' to its original content.'); file_put_contents($json->getPath(), $composerBackup); return 1; } return 0; } private function updateFileCleanly($json, array $base, array $new, $requireKey) { $contents = file_get_contents($json->getPath()); $manipulator = new JsonManipulator($contents); foreach ($new as $package => $constraint) { if (!$manipulator->addLink($requireKey, $package, $constraint)) { return false; } } file_put_contents($json->getPath(), $manipulator->getContents()); return true; } protected function interact(InputInterface $input, OutputInterface $output) { return; } } setName('dump-autoload') ->setAliases(array('dumpautoload')) ->setDescription('Dumps the autoloader') ->setDefinition(array( new InputOption('optimize', 'o', InputOption::VALUE_NONE, 'Optimizes PSR0 packages to be loaded with classmaps too, good for production.'), )) ->setHelp(<<php composer.phar dump-autoload EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Generating autoload files'); $composer = $this->getComposer(); $installationManager = $composer->getInstallationManager(); $localRepo = $composer->getRepositoryManager()->getLocalRepository(); $package = $composer->getPackage(); $config = $composer->getConfig(); $composer->getAutoloadGenerator()->dump($config, $localRepo, $package, $installationManager, 'composer', $input->getOption('optimize')); } } %s [%s]%s ', $question, $default, $sep) : sprintf('%s%s ', $question, $sep); } } setName('status') ->setDescription('Show a list of locally modified packages') ->setDefinition(array( new InputOption('verbose', 'v|vv|vvv', InputOption::VALUE_NONE, 'Show modified files for each directory that contains changes.'), )) ->setHelp(<<getComposer(); $installedRepo = $composer->getRepositoryManager()->getLocalRepository(); $dm = $composer->getDownloadManager(); $im = $composer->getInstallationManager(); $errors = array(); foreach ($installedRepo->getPackages() as $package) { $downloader = $dm->getDownloaderForInstalledPackage($package); if ($downloader instanceof VcsDownloader) { $targetDir = $im->getInstallPath($package); if ($changes = $downloader->getLocalChanges($targetDir)) { $errors[$targetDir] = $changes; } } } if (!$errors) { $output->writeln('No local changes'); } else { $output->writeln('You have changes in the following dependencies:'); } foreach ($errors as $path => $changes) { if ($input->getOption('verbose')) { $indentedChanges = implode("\n", array_map(function ($line) { return ' ' . $line; }, explode("\n", $changes))); $output->writeln(''.$path.':'); $output->writeln($indentedChanges); } else { $output->writeln($path); } } if ($errors && !$input->getOption('verbose')) { $output->writeln('Use --verbose (-v) to see modified files'); } return $errors ? 1 : 0; } } [- \.,\w\'’]+) <(?P.+?)>$/u', $author, $match)) { if (!function_exists('filter_var') || version_compare(PHP_VERSION, '5.3.3', '<') || $match['email'] === filter_var($match['email'], FILTER_VALIDATE_EMAIL)) { return array( 'name' => trim($match['name']), 'email' => $match['email'] ); } } throw new \InvalidArgumentException( 'Invalid author string. Must be in the format: '. 'John Smith ' ); } protected function configure() { $this ->setName('init') ->setDescription('Creates a basic composer.json file in current directory.') ->setDefinition(array( 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('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"'), 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"'), 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'), )) ->setHelp(<<init command creates a basic composer.json file in the current directory. php composer.phar init EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $dialog = $this->getHelperSet()->get('dialog'); $whitelist = array('name', 'description', 'author', 'homepage', 'require', 'require-dev', 'stability', 'license'); $options = array_filter(array_intersect_key($input->getOptions(), array_flip($whitelist))); if (isset($options['author'])) { $options['authors'] = $this->formatAuthors($options['author']); unset($options['author']); } if (isset($options['stability'])) { $options['minimum-stability'] = $options['stability']; unset($options['stability']); } $options['require'] = isset($options['require']) ? $this->formatRequirements($options['require']) : new \stdClass; if (array() === $options['require']) { $options['require'] = new \stdClass; } if (isset($options['require-dev'])) { $options['require-dev'] = $this->formatRequirements($options['require-dev']) ; if (array() === $options['require-dev']) { $options['require-dev'] = new \stdClass; } } $file = new JsonFile('composer.json'); $json = $file->encode($options); if ($input->isInteractive()) { $output->writeln(array( '', $json, '' )); if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm generation', 'yes', '?'), true)) { $output->writeln('Command aborted'); return 1; } } $file->write($options); 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 ($dialog->askConfirmation($output, $question, true)) { $this->addVendorIgnore($ignoreFile); } } } } protected function interact(InputInterface $input, OutputInterface $output) { $git = $this->getGitConfig(); $dialog = $this->getHelperSet()->get('dialog'); $formatter = $this->getHelperSet()->get('formatter'); $output->writeln(array( '', $formatter->formatBlock('Welcome to the Composer config generator', 'bg=blue;fg=white', true), '' )); $output->writeln(array( '', 'This command will guide you through creating your composer.json config.', '', )); $cwd = realpath("."); if (!$name = $input->getOption('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 (isset($git['github.user'])) { $name = $git['github.user'] . '/' . $name; } elseif (!empty($_SERVER['USERNAME'])) { $name = $_SERVER['USERNAME'] . '/' . $name; } elseif (get_current_user()) { $name = get_current_user() . '/' . $name; } else { $name = $name . '/' . $name; } } else { if (!preg_match('{^[a-z0-9_.-]+/[a-z0-9_.-]+$}', $name)) { throw new \InvalidArgumentException( 'The package name '.$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_.-]+' ); } } $name = $dialog->askAndValidate( $output, $dialog->getQuestion('Package name (/)', $name), function ($value) use ($name) { if (null === $value) { return $name; } if (!preg_match('{^[a-z0-9_.-]+/[a-z0-9_.-]+$}', $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; } ); $input->setOption('name', $name); $description = $input->getOption('description') ?: false; $description = $dialog->ask( $output, $dialog->getQuestion('Description', $description) ); $input->setOption('description', $description); if (null === $author = $input->getOption('author')) { if (isset($git['user.name']) && isset($git['user.email'])) { $author = sprintf('%s <%s>', $git['user.name'], $git['user.email']); } } $self = $this; $author = $dialog->askAndValidate( $output, $dialog->getQuestion('Author', $author), function ($value) use ($self, $author) { if (null === $value) { return $author; } $author = $self->parseAuthorString($value); return sprintf('%s <%s>', $author['name'], $author['email']); } ); $input->setOption('author', $author); $minimumStability = $input->getOption('stability') ?: ''; $minimumStability = $dialog->askAndValidate( $output, $dialog->getQuestion('Minimum Stability', $minimumStability), function ($value) use ($self, $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; } ); $input->setOption('stability', $minimumStability); $license = $input->getOption('license') ?: false; $license = $dialog->ask( $output, $dialog->getQuestion('License', $license) ); $input->setOption('license', $license); $output->writeln(array( '', 'Define your dependencies.', '' )); $requirements = array(); if ($dialog->askConfirmation($output, $dialog->getQuestion('Would you like to define your dependencies (require) interactively', 'yes', '?'), true)) { $requirements = $this->determineRequirements($input, $output, $input->getOption('require')); } $input->setOption('require', $requirements); $devRequirements = array(); if ($dialog->askConfirmation($output, $dialog->getQuestion('Would you like to define your dev dependencies (require-dev) interactively', 'yes', '?'), true)) { $devRequirements = $this->determineRequirements($input, $output, $input->getOption('require-dev')); } $input->setOption('require-dev', $devRequirements); } protected function findPackages($name) { $packages = array(); if (!$this->repos) { $this->repos = new CompositeRepository(array_merge( array(new PlatformRepository), Factory::createDefaultRepositories($this->getIO()) )); } return $this->repos->search($name); } protected function determineRequirements(InputInterface $input, OutputInterface $output, $requires = array()) { $dialog = $this->getHelperSet()->get('dialog'); $prompt = $dialog->getQuestion('Search for a package', false, ':'); if ($requires) { $requires = $this->normalizeRequirements($requires); $result = array(); foreach ($requires as $key => $requirement) { if (!isset($requirement['version']) && $input->isInteractive()) { $question = $dialog->getQuestion('Please provide a version constraint for the '.$requirement['name'].' requirement'); if ($constraint = $dialog->ask($output, $question)) { $requirement['version'] = $constraint; } } if (!isset($requirement['version'])) { throw new \InvalidArgumentException('The requirement '.$requirement['name'].' must contain a version constraint'); } $result[] = $requirement['name'] . ' ' . $requirement['version']; } return $result; } while (null !== $package = $dialog->ask($output, $prompt)) { $matches = $this->findPackages($package); if (count($matches)) { $output->writeln(array( '', sprintf('Found %s packages matching %s', count($matches), $package), '' )); $exactMatch = null; $choices = array(); foreach ($matches as $position => $package) { $choices[] = sprintf(' %5s %s', "[$position]", $package['name']); if ($package['name'] === $package) { $exactMatch = true; break; } } if (!$exactMatch) { $output->writeln($choices); $output->writeln(''); $validator = function ($selection) use ($matches) { if ('' === $selection) { return false; } if (!is_numeric($selection) && preg_match('{^\s*(\S+)\s+(\S.*)\s*$}', $selection, $matches)) { return $matches[1].' '.$matches[2]; } if (!isset($matches[(int) $selection])) { throw new \Exception('Not a valid selection'); } $package = $matches[(int) $selection]; return $package['name']; }; $package = $dialog->askAndValidate($output, $dialog->getQuestion('Enter package # to add, or the complete package name if it is not listed', false, ':'), $validator, 3); } if (false !== $package && false === strpos($package, ' ')) { $validator = function ($input) { $input = trim($input); return $input ?: false; }; $constraint = $dialog->askAndValidate($output, $dialog->getQuestion('Enter the version constraint to require', false, ':'), $validator, 3); if (false === $constraint) { continue; } $package .= ' '.$constraint; } if (false !== $package) { $requires[] = $package; } } } return $requires; } protected function formatAuthors($author) { return array($this->parseAuthorString($author)); } protected function formatRequirements(array $requirements) { $requires = array(); $requirements = $this->normalizeRequirements($requirements); foreach ($requirements as $requirement) { $requires[$requirement['name']] = $requirement['version']; } return $requires; } protected function getGitConfig() { if (null !== $this->gitConfig) { return $this->gitConfig; } $finder = new ExecutableFinder(); $gitBin = $finder->find('git'); $cmd = new Process(sprintf('%s config -l', escapeshellarg($gitBin))); $cmd->run(); if ($cmd->isSuccessful()) { $this->gitConfig = array(); preg_match_all('{^([^=]+)=(.*)$}m', $cmd->getOutput(), $matches, PREG_SET_ORDER); foreach ($matches as $match) { $this->gitConfig[$match[1]] = $match[2]; } return $this->gitConfig; } return $this->gitConfig = array(); } protected function hasVendorIgnore($ignoreFile, $vendor = 'vendor') { 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_match($pattern, $line)) { return true; } } return false; } protected function normalizeRequirements(array $requirements) { $parser = new VersionParser(); return $parser->parseNameVersionPairs($requirements); } protected function addVendorIgnore($ignoreFile, $vendor = '/vendor/') { $contents = ""; if (file_exists($ignoreFile)) { $contents = file_get_contents($ignoreFile); if ("\n" !== substr($contents, 0, -1)) { $contents .= "\n"; } } file_put_contents($ignoreFile, $contents . $vendor. "\n"); } } io = $io; $this->config = $config; $this->process = $process ?: new ProcessExecutor($io); $this->filesystem = $fs ?: new Filesystem; } public function getInstallationSource() { return 'source'; } public function download(PackageInterface $package, $path) { if (!$package->getSourceReference()) { throw new \InvalidArgumentException('Package '.$package->getPrettyName().' is missing reference information'); } $this->io->write(" - Installing " . $package->getName() . " (" . VersionParser::formatVersion($package) . ")"); $this->filesystem->removeDirectory($path); $this->doDownload($package, $path); $this->io->write(''); } public function update(PackageInterface $initial, PackageInterface $target, $path) { if (!$target->getSourceReference()) { throw new \InvalidArgumentException('Package '.$target->getPrettyName().' is missing reference information'); } $name = $target->getName(); if ($initial->getPrettyVersion() == $target->getPrettyVersion()) { if ($target->getSourceType() === 'svn') { $from = $initial->getSourceReference(); $to = $target->getSourceReference(); } else { $from = substr($initial->getSourceReference(), 0, 7); $to = substr($target->getSourceReference(), 0, 7); } $name .= ' '.$initial->getPrettyVersion(); } else { $from = VersionParser::formatVersion($initial); $to = VersionParser::formatVersion($target); } $this->io->write(" - Updating " . $name . " (" . $from . " => " . $to . ")"); $this->cleanChanges($path, true); try { $this->doUpdate($initial, $target, $path); } catch (\Exception $e) { $this->reapplyChanges($path); throw $e; } $this->reapplyChanges($path); if ($this->io->isVerbose()) { $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(function ($line) { return ' ' . $line; }, explode("\n", $logs))); $this->io->write(' '.$message); $this->io->write($logs); } } $this->io->write(''); } public function remove(PackageInterface $package, $path) { $this->io->write(" - Removing " . $package->getName() . " (" . $package->getPrettyVersion() . ")"); $this->cleanChanges($path, false); if (!$this->filesystem->removeDirectory($path)) { if (!defined('PHP_WINDOWS_VERSION_BUILD') || (usleep(250) && !$this->filesystem->removeDirectory($path))) { throw new \RuntimeException('Could not completely delete '.$path.', aborting.'); } } } public function setOutputProgress($outputProgress) { return $this; } protected function cleanChanges($path, $update) { if (null !== $this->getLocalChanges($path)) { throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes.'); } } protected function reapplyChanges($path) { } abstract protected function doDownload(PackageInterface $package, $path); abstract protected function doUpdate(PackageInterface $initial, PackageInterface $target, $path); abstract public function getLocalChanges($path); abstract protected function getCommitLogs($fromReference, $toReference, $path); } io = $io; $this->config = $config; $this->rfs = $rfs ?: new RemoteFilesystem($io); $this->filesystem = $filesystem ?: new Filesystem(); $this->cache = $cache; if ($this->cache && !self::$cacheCollected && !mt_rand(0, 50)) { $this->cache->gc($config->get('cache-ttl'), $config->get('cache-files-maxsize')); } self::$cacheCollected = true; } public function getInstallationSource() { return 'dist'; } public function download(PackageInterface $package, $path) { $url = $package->getDistUrl(); if (!$url) { throw new \InvalidArgumentException('The given package is missing url information'); } $this->filesystem->ensureDirectoryExists($path); $fileName = $this->getFileName($package, $path); $this->io->write(" - Installing " . $package->getName() . " (" . VersionParser::formatVersion($package) . ")"); $processedUrl = $this->processUrl($package, $url); $hostname = parse_url($processedUrl, PHP_URL_HOST); if (strpos($hostname, '.github.com') === (strlen($hostname) - 11)) { $hostname = 'github.com'; } try { try { if (!$this->cache || !$this->cache->copyTo($this->getCacheKey($package), $fileName)) { if (!$this->outputProgress) { $this->io->write(' Downloading'); } $retries = 3; while ($retries--) { try { $this->rfs->copy($hostname, $processedUrl, $fileName, $this->outputProgress); break; } catch (TransportException $e) { if (0 !== $e->getCode() || !$retries) { throw $e; } if ($this->io->isVerbose()) { $this->io->write(' Download failed, retrying...'); } usleep(500000); } } if ($this->cache) { $this->cache->copyFrom($this->getCacheKey($package), $fileName); } } else { $this->io->write(' Loading from cache'); } } catch (TransportException $e) { if (in_array($e->getCode(), array(404, 403)) && 'github.com' === $hostname && !$this->io->hasAuthentication($hostname)) { $message = "\n".'Could not fetch '.$processedUrl.', enter your GitHub credentials '.($e->getCode() === 404 ? 'to access private repos' : 'to go over the API rate limit'); $gitHubUtil = new GitHub($this->io, $this->config, null, $this->rfs); if (!$gitHubUtil->authorizeOAuth($hostname) && (!$this->io->isInteractive() || !$gitHubUtil->authorizeOAuthInteractively($hostname, $message)) ) { throw $e; } $this->rfs->copy($hostname, $processedUrl, $fileName, $this->outputProgress); } else { throw $e; } } if (!file_exists($fileName)) { throw new \UnexpectedValueException($url.' could not be saved to '.$fileName.', make sure the' .' directory is writable and you have internet connectivity'); } $checksum = $package->getDistSha1Checksum(); if ($checksum && hash_file('sha1', $fileName) !== $checksum) { throw new \UnexpectedValueException('The checksum verification of the file failed (downloaded from '.$url.')'); } } catch (\Exception $e) { $this->filesystem->removeDirectory($path); $this->clearCache($package, $path); throw $e; } } public function setOutputProgress($outputProgress) { $this->outputProgress = $outputProgress; return $this; } protected function clearCache(PackageInterface $package, $path) { if ($this->cache) { $fileName = $this->getFileName($package, $path); $this->cache->remove($this->getCacheKey($package)); } } public function update(PackageInterface $initial, PackageInterface $target, $path) { $this->remove($initial, $path); $this->download($target, $path); } public function remove(PackageInterface $package, $path) { $this->io->write(" - Removing " . $package->getName() . " (" . VersionParser::formatVersion($package) . ")"); if (!$this->filesystem->removeDirectory($path)) { if (!defined('PHP_WINDOWS_VERSION_BUILD') || (usleep(250000) && !$this->filesystem->removeDirectory($path))) { throw new \RuntimeException('Could not completely delete '.$path.', aborting.'); } } } protected function getFileName(PackageInterface $package, $path) { return $path.'/'.pathinfo(parse_url($package->getDistUrl(), PHP_URL_PATH), PATHINFO_BASENAME); } protected function processUrl(PackageInterface $package, $url) { if (!extension_loaded('openssl') && 0 === strpos($url, 'https:')) { throw new \RuntimeException('You must enable the openssl extension to download files via https'); } return $url; } private function getCacheKey(PackageInterface $package) { if (preg_match('{^[a-f0-9]{40}$}', $package->getDistReference())) { return $package->getName().'/'.$package->getDistReference().'.'.$package->getDistType(); } return $package->getName().'/'.$package->getVersion().'-'.$package->getDistReference().'.'.$package->getDistType(); } } getSourceUrl(); $ref = $package->getSourceReference(); $this->io->write(" Checking out ".$package->getSourceReference()); $this->execute($url, "svn co", sprintf("%s/%s", $url, $ref), null, $path); } public function doUpdate(PackageInterface $initial, PackageInterface $target, $path) { $url = $target->getSourceUrl(); $ref = $target->getSourceReference(); $this->io->write(" Checking out " . $ref); $this->execute($url, "svn switch", sprintf("%s/%s", $url, $ref), $path); } public function getLocalChanges($path) { if (!is_dir($path.'/.svn')) { return; } $this->process->execute('svn status --ignore-externals', $output, $path); return preg_match('{^ *[^X ] +}m', $output) ? $output : null; } protected function execute($baseUrl, $command, $url, $cwd = null, $path = null) { $util = new SvnUtil($baseUrl, $this->io); try { return $util->execute($command, $url, $cwd, $path, $this->io->isVerbose()); } catch (\RuntimeException $e) { throw new \RuntimeException( 'Package could not be downloaded, '.$e->getMessage() ); } } protected function cleanChanges($path, $update) { if (!$changes = $this->getLocalChanges($path)) { return; } if (!$this->io->isInteractive()) { if (true === $this->config->get('discard-changes')) { return $this->discardChanges($path); } return parent::cleanChanges($path, $update); } $changes = array_map(function ($elem) { return ' '.$elem; }, preg_split('{\s*\r?\n\s*}', $changes)); $this->io->write(' The package has modified files:'); $this->io->write(array_slice($changes, 0, 10)); if (count($changes) > 10) { $this->io->write(' '.count($changes) - 10 . ' more files modified, choose "v" to view the full list'); } 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->write($changes); break; case '?': default: $this->io->write(array( ' 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; } } } protected function getCommitLogs($fromReference, $toReference, $path) { $fromRevision = preg_replace('{.*@(\d+)$}', '$1', $fromReference); $toRevision = preg_replace('{.*@(\d+)$}', '$1', $toReference); $command = sprintf('cd %s && svn log -r%s:%s --incremental', escapeshellarg($path), $fromRevision, $toRevision); if (0 !== $this->process->execute($command, $output)) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } return $output; } protected function discardChanges($path) { if (0 !== $this->process->execute('svn revert -R .', $output, $path)) { throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput()); } } } filesystem = new Filesystem(); $this->file = $file; } public function extractTo($target, array $roles = array('php' => '/', 'script' => '/bin'), $vars = array()) { $extractionPath = $target.'/tarball'; try { $archive = new \PharData($this->file); $archive->extractTo($extractionPath, null, true); if (!is_file($this->combine($extractionPath, '/package.xml'))) { throw new \RuntimeException('Invalid PEAR package. It must contain package.xml file.'); } $fileCopyActions = $this->buildCopyActions($extractionPath, $roles, $vars); $this->copyFiles($fileCopyActions, $extractionPath, $target, $roles, $vars); $this->filesystem->removeDirectory($extractionPath); } catch (\Exception $exception) { throw new \UnexpectedValueException(sprintf('Failed to extract PEAR package %s to %s. Reason: %s', $this->file, $target, $exception->getMessage()), 0, $exception); } } private function copyFiles($files, $source, $target, $roles, $vars) { foreach ($files as $file) { $from = $this->combine($source, $file['from']); $to = $this->combine($target, $roles[$file['role']]); $to = $this->combine($to, $file['to']); $tasks = $file['tasks']; $this->copyFile($from, $to, $tasks, $vars); } } private function copyFile($from, $to, $tasks, $vars) { if (!is_file($from)) { throw new \RuntimeException('Invalid PEAR package. package.xml defines file that is not located inside tarball.'); } $this->filesystem->ensureDirectoryExists(dirname($to)); if (0 == count($tasks)) { $copied = copy($from, $to); } else { $content = file_get_contents($from); $replacements = array(); foreach ($tasks as $task) { $pattern = $task['from']; $varName = $task['to']; if (isset($vars[$varName])) { if ($varName === 'php_bin' && false === strpos($to, '.bat')) { $replacements[$pattern] = preg_replace('{\.bat$}', '', $vars[$varName]); } else { $replacements[$pattern] = $vars[$varName]; } } } $content = strtr($content, $replacements); $copied = file_put_contents($to, $content); } if (false === $copied) { throw new \RuntimeException(sprintf('Failed to copy %s to %s', $from, $to)); } } private function buildCopyActions($source, array $roles, $vars) { $package = simplexml_load_file($this->combine($source, 'package.xml')); if(false === $package) throw new \RuntimeException('Package definition file is not valid.'); $packageSchemaVersion = $package['version']; if ('1.0' == $packageSchemaVersion) { $children = $package->release->filelist->children(); $packageName = (string) $package->name; $packageVersion = (string) $package->release->version; $sourceDir = $packageName . '-' . $packageVersion; $result = $this->buildSourceList10($children, $roles, $sourceDir, '', null, $packageName); } elseif ('2.0' == $packageSchemaVersion || '2.1' == $packageSchemaVersion) { $children = $package->contents->children(); $packageName = (string) $package->name; $packageVersion = (string) $package->version->release; $sourceDir = $packageName . '-' . $packageVersion; $result = $this->buildSourceList20($children, $roles, $sourceDir, '', null, $packageName); $namespaces = $package->getNamespaces(); $package->registerXPathNamespace('ns', $namespaces['']); $releaseNodes = $package->xpath('ns:phprelease'); $this->applyRelease($result, $releaseNodes, $vars); } else { throw new \RuntimeException('Unsupported schema version of package definition file.'); } return $result; } private function applyRelease(&$actions, $releaseNodes, $vars) { foreach ($releaseNodes as $releaseNode) { $requiredOs = $releaseNode->installconditions && $releaseNode->installconditions->os && $releaseNode->installconditions->os->name ? (string) $releaseNode->installconditions->os->name : ''; if ($requiredOs && $vars['os'] != $requiredOs) { continue; } if ($releaseNode->filelist) { foreach ($releaseNode->filelist->children() as $action) { if ('install' == $action->getName()) { $name = (string) $action['name']; $as = (string) $action['as']; if (isset($actions[$name])) { $actions[$name]['to'] = $as; } } elseif ('ignore' == $action->getName()) { $name = (string) $action['name']; unset($actions[$name]); } else { } } } break; } } private function buildSourceList10($children, $targetRoles, $source = '', $target = '', $role = null, $packageName) { $result = array(); foreach ($children as $child) { if ($child->getName() == 'dir') { $dirSource = $this->combine($source, (string) $child['name']); $dirTarget = $child['baseinstalldir'] ? : $target; $dirRole = $child['role'] ? : $role; $dirFiles = $this->buildSourceList10($child->children(), $targetRoles, $dirSource, $dirTarget, $dirRole, $packageName); $result = array_merge($result, $dirFiles); } elseif ($child->getName() == 'file') { $fileRole = (string) $child['role'] ? : $role; if (isset($targetRoles[$fileRole])) { $fileName = (string) ($child['name'] ? : $child[0]); $fileSource = $this->combine($source, $fileName); $fileTarget = $this->combine((string) $child['baseinstalldir'] ? : $target, $fileName); if (!in_array($fileRole, self::$rolesWithoutPackageNamePrefix)) { $fileTarget = $packageName . '/' . $fileTarget; } $result[(string) $child['name']] = array('from' => $fileSource, 'to' => $fileTarget, 'role' => $fileRole, 'tasks' => array()); } } } return $result; } private function buildSourceList20($children, $targetRoles, $source = '', $target = '', $role = null, $packageName) { $result = array(); foreach ($children as $child) { if ('dir' == $child->getName()) { $dirSource = $this->combine($source, $child['name']); $dirTarget = $child['baseinstalldir'] ? : $target; $dirRole = $child['role'] ? : $role; $dirFiles = $this->buildSourceList20($child->children(), $targetRoles, $dirSource, $dirTarget, $dirRole, $packageName); $result = array_merge($result, $dirFiles); } elseif ('file' == $child->getName()) { $fileRole = (string) $child['role'] ? : $role; if (isset($targetRoles[$fileRole])) { $fileSource = $this->combine($source, (string) $child['name']); $fileTarget = $this->combine((string) ($child['baseinstalldir'] ? : $target), (string) $child['name']); $fileTasks = array(); foreach ($child->children('http://pear.php.net/dtd/tasks-1.0') as $taskNode) { if ('replace' == $taskNode->getName()) { $fileTasks[] = array('from' => (string) $taskNode->attributes()->from, 'to' => (string) $taskNode->attributes()->to); } } if (!in_array($fileRole, self::$rolesWithoutPackageNamePrefix)) { $fileTarget = $packageName . '/' . $fileTarget; } $result[(string) $child['name']] = array('from' => $fileSource, 'to' => $fileTarget, 'role' => $fileRole, 'tasks' => $fileTasks); } } } return $result; } private function combine($left, $right) { return rtrim($left, '/') . '/' . ltrim($right, '/'); } } preferSource = $preferSource; $this->filesystem = $filesystem ?: new Filesystem(); } public function setPreferSource($preferSource) { $this->preferSource = $preferSource; return $this; } public function setPreferDist($preferDist) { $this->preferDist = $preferDist; return $this; } public function setOutputProgress($outputProgress) { foreach ($this->downloaders as $downloader) { $downloader->setOutputProgress($outputProgress); } return $this; } public function setDownloader($type, DownloaderInterface $downloader) { $type = strtolower($type); $this->downloaders[$type] = $downloader; return $this; } public function getDownloader($type) { $type = strtolower($type); if (!isset($this->downloaders[$type])) { throw new \InvalidArgumentException('Unknown downloader type: '.$type); } return $this->downloaders[$type]; } public function getDownloaderForInstalledPackage(PackageInterface $package) { $installationSource = $package->getInstallationSource(); if ('dist' === $installationSource) { $downloader = $this->getDownloader($package->getDistType()); } elseif ('source' === $installationSource) { $downloader = $this->getDownloader($package->getSourceType()); } else { throw new \InvalidArgumentException( 'Package '.$package.' seems not been installed properly' ); } if ($installationSource !== $downloader->getInstallationSource()) { throw new \LogicException(sprintf( 'Downloader "%s" is a %s type downloader and can not be used to download %s', get_class($downloader), $downloader->getInstallationSource(), $installationSource )); } return $downloader; } public function download(PackageInterface $package, $targetDir, $preferSource = null) { $preferSource = null !== $preferSource ? $preferSource : $this->preferSource; $sourceType = $package->getSourceType(); $distType = $package->getDistType(); if ((!$package->isDev() || $this->preferDist || !$sourceType) && !($preferSource && $sourceType) && $distType) { $package->setInstallationSource('dist'); } elseif ($sourceType) { $package->setInstallationSource('source'); } else { throw new \InvalidArgumentException('Package '.$package.' must have a source or dist specified'); } $this->filesystem->ensureDirectoryExists($targetDir); $downloader = $this->getDownloaderForInstalledPackage($package); $downloader->download($package, $targetDir); } public function update(PackageInterface $initial, PackageInterface $target, $targetDir) { $downloader = $this->getDownloaderForInstalledPackage($initial); $installationSource = $initial->getInstallationSource(); if ('dist' === $installationSource) { $initialType = $initial->getDistType(); $targetType = $target->getDistType(); } else { $initialType = $initial->getSourceType(); $targetType = $target->getSourceType(); } if ($target->isDev() && 'dist' === $installationSource) { $downloader->remove($initial, $targetDir); $this->download($target, $targetDir); return; } if ($initialType === $targetType) { $target->setInstallationSource($installationSource); $downloader->update($initial, $target, $targetDir); } else { $downloader->remove($initial, $targetDir); $this->download($target, $targetDir, 'source' === $installationSource); } } public function remove(PackageInterface $package, $targetDir) { $downloader = $this->getDownloaderForInstalledPackage($package); $downloader->remove($package, $targetDir); } } headers = $headers; } public function getHeaders() { return $this->headers; } } extractTo($path, null, true); } } extractTo($path, null, true); } } getSourceUrl()); $ref = escapeshellarg($package->getSourceReference()); $path = escapeshellarg($path); $this->io->write(" Cloning ".$package->getSourceReference()); $command = sprintf('hg clone %s %s && cd %2$s && hg up %s', $url, $path, $ref); if (0 !== $this->process->execute($command, $ignoredOutput)) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } } public function doUpdate(PackageInterface $initial, PackageInterface $target, $path) { $url = escapeshellarg($target->getSourceUrl()); $ref = escapeshellarg($target->getSourceReference()); $path = escapeshellarg($path); $this->io->write(" Updating to ".$target->getSourceReference()); $command = sprintf('cd %s && hg pull %s && hg up %s', $path, $url, $ref); if (0 !== $this->process->execute($command, $ignoredOutput)) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } } public function getLocalChanges($path) { if (!is_dir($path.'/.hg')) { return; } $this->process->execute(sprintf('cd %s && hg st', escapeshellarg($path)), $output); return trim($output) ?: null; } protected function getCommitLogs($fromReference, $toReference, $path) { $command = sprintf('cd %s && hg log -r %s:%s --style compact', escapeshellarg($path), $fromReference, $toReference); if (0 !== $this->process->execute($command, $output)) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } return $output; } } getFileName($package, $path); if ($this->io->isVerbose()) { $this->io->write(' Extracting archive'); } $temporaryDir = sys_get_temp_dir().'/cmp'.substr(md5(time().mt_rand()), 0, 5); try { $this->filesystem->ensureDirectoryExists($temporaryDir); try { $this->extract($fileName, $temporaryDir); } catch (\Exception $e) { parent::clearCache($package, $path); throw $e; } unlink($fileName); $contentDir = $this->listFiles($temporaryDir); if (1 === count($contentDir) && !is_file($contentDir[0])) { $contentDir = $this->listFiles($contentDir[0]); } foreach ($contentDir as $file) { $this->filesystem->rename($file, $path . '/' . basename($file)); } $this->filesystem->removeDirectory($temporaryDir); } catch (\Exception $e) { $this->filesystem->removeDirectory($path); $this->filesystem->removeDirectory($temporaryDir); throw $e; } $this->io->write(''); } protected function getFileName(PackageInterface $package, $path) { return rtrim($path.'/'.md5($path.spl_object_hash($package)).'.'.pathinfo(parse_url($package->getDistUrl(), PHP_URL_PATH), PATHINFO_EXTENSION), '.'); } protected function processUrl(PackageInterface $package, $url) { if ($package->getDistReference() && strpos($url, 'github.com')) { if (preg_match('{^https?://(?:www\.)?github\.com/([^/]+)/([^/]+)/(zip|tar)ball/(.+)$}i', $url, $match)) { $url = 'https://api.github.com/repos/' . $match[1] . '/'. $match[2] . '/' . $match[3] . 'ball/' . $package->getDistReference(); } elseif ($package->getDistReference() && preg_match('{^https?://(?:www\.)?github\.com/([^/]+)/([^/]+)/archive/.+\.(zip|tar)(?:\.gz)?$}i', $url, $match)) { $url = 'https://api.github.com/repos/' . $match[1] . '/'. $match[2] . '/' . $match[3] . 'ball/' . $package->getDistReference(); } elseif ($package->getDistReference() && preg_match('{^https?://api\.github\.com/repos/([^/]+)/([^/]+)/(zip|tar)ball(?:/.+)?$}i', $url, $match)) { $url = 'https://api.github.com/repos/' . $match[1] . '/'. $match[2] . '/' . $match[3] . 'ball/' . $package->getDistReference(); } } if (!extension_loaded('openssl') && (0 === strpos($url, 'https:') || 0 === strpos($url, 'http://github.com'))) { if (preg_match('{^https://api\.github\.com/repos/([^/]+/[^/]+)/(zip|tar)ball/([^/]+)$}i', $url, $match)) { $url = 'http://nodeload.github.com/'.$match[1].'/'.$match[2].'/'.$match[3]; } elseif (preg_match('{^https://github\.com/([^/]+/[^/]+)/(zip|tar)ball/([^/]+)$}i', $url, $match)) { $url = 'http://nodeload.github.com/'.$match[1].'/'.$match[2].'/'.$match[3]; } elseif (preg_match('{^https://github\.com/([^/]+/[^/]+)/archive/([^/]+)\.(zip|tar\.gz)$}i', $url, $match)) { $url = 'http://nodeload.github.com/'.$match[1].'/'.$match[3].'/'.$match[2]; } else { throw new \RuntimeException('You must enable the openssl extension to download files via https'); } } return parent::processUrl($package, $url); } abstract protected function extract($file, $path); private function listFiles($dir) { $files = array_merge(glob($dir . '/.*'), glob($dir . '/*')); return array_values(array_filter($files, function ($el) { return basename($el) !== '.' && basename($el) !== '..'; })); } } process = $process ?: new ProcessExecutor; parent::__construct($io, $config, $cache); } protected function extract($file, $path) { $processError = null; if (!defined('PHP_WINDOWS_VERSION_BUILD')) { $command = 'unzip '.escapeshellarg($file).' -d '.escapeshellarg($path); if (0 === $this->process->execute($command, $ignoredOutput)) { return; } $processError = 'Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput(); } if (!class_exists('ZipArchive')) { $iniPath = php_ini_loaded_file(); if ($iniPath) { $iniMessage = 'The php.ini used by your command-line PHP is: ' . $iniPath; } else { $iniMessage = 'A php.ini file does not exist. You will have to create one.'; } $error = "Could not decompress the archive, enable the PHP zip extension or install unzip.\n" . $iniMessage . "\n" . $processError; if (!defined('PHP_WINDOWS_VERSION_BUILD')) { $error = "Could not decompress the archive, enable the PHP zip extension.\n" . $iniMessage; } throw new \RuntimeException($error); } $zipArchive = new ZipArchive(); if (true !== ($retval = $zipArchive->open($file))) { throw new \UnexpectedValueException($this->getErrorMessage($retval, $file)); } if (true !== $zipArchive->extractTo($path)) { throw new \RuntimeException("There was an error extracting the ZIP file. Corrupt file?"); } $zipArchive->close(); } protected function getErrorMessage($retval, $file) { 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); default: return sprintf("'%s' is not a valid zip archive, got error code: %s", $file, $retval); } } } getSourceReference(); $command = 'git clone %s %s && cd %2$s && git remote add composer %1$s && git fetch composer'; $this->io->write(" Cloning ".$ref); putenv('GIT_ASKPASS=echo'); $commandCallable = function($url) use ($ref, $path, $command) { return sprintf($command, escapeshellarg($url), escapeshellarg($path), escapeshellarg($ref)); }; $this->runCommand($commandCallable, $package->getSourceUrl(), $path, true); $this->setPushUrl($package, $path); $this->updateToCommit($path, $ref, $package->getPrettyVersion(), $package->getReleaseDate()); } public function doUpdate(PackageInterface $initial, PackageInterface $target, $path) { $ref = $target->getSourceReference(); $this->io->write(" Checking out ".$ref); $command = 'git remote set-url composer %s && git fetch composer && git fetch --tags composer'; $this->process->execute('git remote -v', $output, $path); if (preg_match('{^(?:composer|origin)\s+https?://(.+):(.+)@([^/]+)}im', $output, $match)) { $this->io->setAuthentication($match[3], urldecode($match[1]), urldecode($match[2])); } putenv('GIT_ASKPASS=echo'); $commandCallable = function($url) use ($command) { return sprintf($command, escapeshellarg($url)); }; $this->runCommand($commandCallable, $target->getSourceUrl(), $path); $this->updateToCommit($path, $ref, $target->getPrettyVersion(), $target->getReleaseDate()); } public function getLocalChanges($path) { if (!is_dir($path.'/.git')) { return; } $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()); } return trim($output) ?: null; } protected function cleanChanges($path, $update) { if (!$changes = $this->getLocalChanges($path)) { return; } 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($path, $update); } return $this->stashChanges($path); } return parent::cleanChanges($path, $update); } $changes = array_map(function ($elem) { return ' '.$elem; }, preg_split('{\s*\r?\n\s*}', $changes)); $this->io->write(' The package has modified files:'); $this->io->write(array_slice($changes, 0, 10)); if (count($changes) > 10) { $this->io->write(' '.count($changes) - 10 . ' more files modified, choose "v" to view the full list'); } while (true) { switch ($this->io->ask(' Discard changes [y,n,v,'.($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->write($changes); break; case '?': default: help: $this->io->write(array( ' 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', )); if ($update) { $this->io->write(' s - stash changes and try to reapply them after the update'); } $this->io->write(' ? - print help'); break; } } } protected function reapplyChanges($path) { if ($this->hasStashedChanges) { $this->hasStashedChanges = false; $this->io->write(' 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()); } } } protected function updateToCommit($path, $reference, $branch, $date) { $template = 'git checkout %s && git reset --hard %1$s'; $branch = preg_replace('{(?:^dev-|(?:\.x)?-dev$)}i', '', $branch); $branches = null; if (0 === $this->process->execute('git branch -r', $output, $path)) { $branches = $output; } $gitRef = $reference; if (!preg_match('{^[a-f0-9]{40}$}', $reference) && $branches && preg_match('{^\s+composer/'.preg_quote($reference).'$}m', $output) ) { $command = sprintf('git checkout -B %s %s && git reset --hard %2$s', escapeshellarg($branch), escapeshellarg('composer/'.$reference)); if (0 === $this->process->execute($command, $output, $path)) { return; } } if (preg_match('{^[a-f0-9]{40}$}', $reference)) { if (!preg_match('{^\s+composer/'.preg_quote($branch).'$}m', $branches) && preg_match('{^\s+composer/v'.preg_quote($branch).'$}m', $branches)) { $branch = 'v' . $branch; } $command = sprintf('git checkout %s', escapeshellarg($branch)); $fallbackCommand = sprintf('git checkout -B %s %s', escapeshellarg($branch), escapeshellarg('composer/'.$branch)); if (0 === $this->process->execute($command, $output, $path) || 0 === $this->process->execute($fallbackCommand, $output, $path) ) { $command = sprintf('git reset --hard %s', escapeshellarg($reference)); if (0 === $this->process->execute($command, $output, $path)) { return; } } } $command = sprintf($template, escapeshellarg($gitRef)); if (0 === $this->process->execute($command, $output, $path)) { return; } if ($date && false !== strpos($this->process->getErrorOutput(), $reference)) { $date = $date->format('U'); $command = 'git branch -r'; if (0 !== $this->process->execute($command, $output, $path)) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } $guessTemplate = 'git log --until=%s --date=raw -n1 --pretty=%%H %s'; foreach ($this->process->splitLines($output) as $line) { if (preg_match('{^composer/'.preg_quote($branch).'(?:\.x)?$}i', trim($line))) { if (0 === $this->process->execute(sprintf($guessTemplate, $date, escapeshellarg(trim($line))), $output, $path)) { $newReference = trim($output); } break; } } if (empty($newReference)) { if (0 !== $this->process->execute(sprintf($guessTemplate, $date, '--all'), $output, $path)) { throw new \RuntimeException('Failed to execute ' . $this->sanitizeUrl($command) . "\n\n" . $this->process->getErrorOutput()); } $newReference = trim($output); } $command = sprintf($template, escapeshellarg($reference)); if (0 === $this->process->execute($command, $output, $path)) { $this->io->write(' '.$reference.' is gone (history was rewritten?), recovered by checking out '.$newReference); return; } } throw new \RuntimeException('Failed to execute ' . $this->sanitizeUrl($command) . "\n\n" . $this->process->getErrorOutput()); } protected function runCommand($commandCallable, $url, $cwd, $initialClone = false) { if ($initialClone) { $origCwd = $cwd; $cwd = null; } if (preg_match('{^ssh://[^@]+@[^:]+:[^0-9]+}', $url)) { throw new \InvalidArgumentException('The source URL '.$url.' is invalid, ssh URLs should have a port number after ":".'."\n".'Use ssh://git@example.com:22/path or just git@example.com:path if you do not want to provide a password or custom port.'); } if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match)) { $protocols = $this->config->get('github-protocols'); if (!is_array($protocols)) { throw new \RuntimeException('Config value "github-protocols" must be an array, got '.gettype($protocols)); } $messages = array(); foreach ($protocols as $protocol) { $url = $protocol . $match[1]; if (0 === $this->process->execute(call_user_func($commandCallable, $url), $ignoredOutput, $cwd)) { return; } $messages[] = '- ' . $url . "\n" . preg_replace('#^#m', ' ', $this->process->getErrorOutput()); if ($initialClone) { $this->filesystem->removeDirectory($origCwd); } } $this->throwException('Failed to clone ' . $this->sanitizeUrl($url) .' via git, https and http protocols, aborting.' . "\n\n" . implode("\n", $messages), $url); } $command = call_user_func($commandCallable, $url); if (0 !== $this->process->execute($command, $ignoredOutput, $cwd)) { if (preg_match('{^git@(github.com):(.+?)\.git$}i', $url, $match)) { if (!$this->io->hasAuthentication($match[1])) { $gitHubUtil = new GitHub($this->io, $this->config, $this->process); $message = 'Cloning failed using an ssh key for authentication, enter your GitHub credentials to access private repos'; if (!$gitHubUtil->authorizeOAuth($match[1]) && $this->io->isInteractive()) { $gitHubUtil->authorizeOAuthInteractively($match[1], $message); } } if ($this->io->hasAuthentication($match[1])) { $auth = $this->io->getAuthentication($match[1]); $url = 'https://'.urlencode($auth['username']) . ':' . urlencode($auth['password']) . '@'.$match[1].'/'.$match[2].'.git'; $command = call_user_func($commandCallable, $url); if (0 === $this->process->execute($command, $ignoredOutput, $cwd)) { return; } } } elseif ( $this->io->isInteractive() && preg_match('{(https?://)([^/]+)(.*)$}i', $url, $match) && strpos($this->process->getErrorOutput(), 'fatal: Authentication failed') !== false ) { if ($this->io->hasAuthentication($match[2])) { $auth = $this->io->getAuthentication($match[2]); } else { $this->io->write($url.' requires Authentication'); $auth = array( 'username' => $this->io->ask('Username: '), 'password' => $this->io->askAndHideAnswer('Password: '), ); } $url = $match[1].urlencode($auth['username']).':'.urlencode($auth['password']).'@'.$match[2].$match[3]; $command = call_user_func($commandCallable, $url); if (0 === $this->process->execute($command, $ignoredOutput, $cwd)) { $this->io->setAuthentication($match[2], $auth['username'], $auth['password']); return; } } if ($initialClone) { $this->filesystem->removeDirectory($origCwd); } $this->throwException('Failed to execute ' . $this->sanitizeUrl($command) . "\n\n" . $this->process->getErrorOutput(), $url); } } protected function throwException($message, $url) { if (0 !== $this->process->execute('git --version', $ignoredOutput)) { throw new \RuntimeException('Failed to clone '.$this->sanitizeUrl($url).', git was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput()); } throw new \RuntimeException($message); } protected function sanitizeUrl($message) { return preg_replace('{://(.+?):.+?@}', '://$1:***@', $message); } protected function setPushUrl(PackageInterface $package, $path) { if (preg_match('{^(?:https?|git)://github.com/([^/]+)/([^/]+?)(?:\.git)?$}', $package->getSourceUrl(), $match)) { $pushUrl = 'git@github.com:'.$match[1].'/'.$match[2].'.git'; $cmd = sprintf('git remote set-url --push origin %s', escapeshellarg($pushUrl)); $this->process->execute($cmd, $ignoredOutput, $path); } } protected function getCommitLogs($fromReference, $toReference, $path) { $command = sprintf('git log %s..%s --pretty=format:"%%h - %%an: %%s"', $fromReference, $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($path) { if (0 !== $this->process->execute('git reset --hard', $output, $path)) { throw new \RuntimeException("Could not reset changes\n\n:".$this->process->getErrorOutput()); } } protected function stashChanges($path) { if (0 !== $this->process->execute('git stash', $output, $path)) { throw new \RuntimeException("Could not stash changes\n\n:".$this->process->getErrorOutput()); } $this->hasStashedChanges = true; } } addPackage($package); } } public function findPackage($name, $version) { $versionParser = new VersionParser(); $version = $versionParser->normalize($version); $name = strtolower($name); foreach ($this->getPackages() as $package) { if ($name === $package->getName() && $version === $package->getVersion()) { return $package; } } } public function findPackages($name, $version = null) { $name = strtolower($name); if (null !== $version) { $versionParser = new VersionParser(); $version = $versionParser->normalize($version); } $packages = array(); foreach ($this->getPackages() as $package) { if ($package->getName() === $name && (null === $version || $version === $package->getVersion())) { $packages[] = $package; } } return $packages; } public function search($query, $mode = 0) { $regex = '{(?:'.implode('|', preg_split('{\s+}', $query)).')}i'; $matches = array(); foreach ($this->getPackages() as $package) { $name = $package->getName(); if (isset($matches[$name])) { continue; } if (preg_match($regex, $name) || ($mode === self::SEARCH_FULLTEXT && $package instanceof CompletePackageInterface && preg_match($regex, implode(' ', (array) $package->getKeywords()) . ' ' . $package->getDescription())) ) { $matches[$name] = array( 'name' => $package->getPrettyName(), 'description' => $package->getDescription(), ); } } return $matches; } public function hasPackage(PackageInterface $package) { $packageId = $package->getUniqueName(); foreach ($this->getPackages() as $repoPackage) { if ($packageId === $repoPackage->getUniqueName()) { return true; } } return false; } public function addPackage(PackageInterface $package) { 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); } } } protected function createAliasPackage(PackageInterface $package, $alias, $prettyAlias) { return new AliasPackage($package instanceof AliasPackage ? $package->getAliasOf() : $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); return; } } } public function getPackages() { if (null === $this->packages) { $this->initialize(); } return $this->packages; } public function count() { return count($this->packages); } protected function initialize() { $this->packages = array(); } } file = $repositoryFile; } protected function initialize() { parent::initialize(); if (!$this->file->exists()) { return; } try { $packages = $this->file->read(); if (!is_array($packages)) { throw new \UnexpectedValueException('Could not parse package list from the repository'); } } catch (\Exception $e) { throw new InvalidRepositoryException('Invalid repository data in '.$this->file->getPath().', packages could not be loaded: ['.get_class($e).'] '.$e->getMessage()); } $loader = new ArrayLoader(); foreach ($packages as $packageData) { $package = $loader->load($packageData); $this->addPackage($package); } } public function reload() { $this->packages = null; $this->initialize(); } public function write() { $data = array(); $dumper = new ArrayDumper(); foreach ($this->getCanonicalPackages() as $package) { $data[] = $dumper->dump($package); } $this->file->write($data); } } url = rtrim($repoConfig['url'], '/'); $this->io = $io; $this->rfs = $rfs ?: new RemoteFilesystem($this->io); $this->vendorAlias = isset($repoConfig['vendor-alias']) ? $repoConfig['vendor-alias'] : null; $this->versionParser = new VersionParser(); } protected function initialize() { parent::initialize(); $this->io->write('Initializing PEAR repository '.$this->url); $reader = new ChannelReader($this->rfs); try { $channelInfo = $reader->read($this->url); } catch (\Exception $e) { $this->io->write('PEAR repository from '.$this->url.' could not be loaded. '.$e->getMessage().''); return; } $packages = $this->buildComposerPackages($channelInfo, $this->versionParser); foreach ($packages as $package) { $this->addPackage($package); } } private function buildComposerPackages(ChannelInfo $channelInfo, VersionParser $versionParser) { $result = array(); foreach ($channelInfo->getPackages() as $packageDefinition) { foreach ($packageDefinition->getReleases() as $version => $releaseInfo) { try { $normalizedVersion = $versionParser->normalize($version); } catch (\UnexpectedValueException $e) { if ($this->io->isVerbose()) { $this->io->write('Could not load '.$packageDefinition->getPackageName().' '.$version.': '.$e->getMessage()); } continue; } $composerPackageName = $this->buildComposerPackageName($packageDefinition->getChannelName(), $packageDefinition->getPackageName()); $urlBits = parse_url($this->url); $scheme = (isset($urlBits['scheme']) && 'https' === $urlBits['scheme'] && extension_loaded('openssl')) ? 'https' : 'http'; $distUrl = "{$scheme}://{$packageDefinition->getChannelName()}/get/{$packageDefinition->getPackageName()}-{$version}.tgz"; $requires = array(); $suggests = array(); $conflicts = array(); $replaces = array(); if ($channelInfo->getName() == $packageDefinition->getChannelName()) { $composerPackageAlias = $this->buildComposerPackageName($channelInfo->getAlias(), $packageDefinition->getPackageName()); $aliasConstraint = new VersionConstraint('==', $normalizedVersion); $replaces[] = new Link($composerPackageName, $composerPackageAlias, $aliasConstraint, 'replaces', (string) $aliasConstraint); } if (!empty($this->vendorAlias) && ($this->vendorAlias != 'pear-'.$channelInfo->getAlias() || $channelInfo->getName() != $packageDefinition->getChannelName()) ) { $composerPackageAlias = "{$this->vendorAlias}/{$packageDefinition->getPackageName()}"; $aliasConstraint = new VersionConstraint('==', $normalizedVersion); $replaces[] = new Link($composerPackageName, $composerPackageAlias, $aliasConstraint, 'replaces', (string) $aliasConstraint); } foreach ($releaseInfo->getDependencyInfo()->getRequires() as $dependencyConstraint) { $dependencyPackageName = $this->buildComposerPackageName($dependencyConstraint->getChannelName(), $dependencyConstraint->getPackageName()); $constraint = $versionParser->parseConstraints($dependencyConstraint->getConstraint()); $link = new Link($composerPackageName, $dependencyPackageName, $constraint, $dependencyConstraint->getType(), $dependencyConstraint->getConstraint()); switch ($dependencyConstraint->getType()) { case 'required': $requires[] = $link; break; case 'conflicts': $conflicts[] = $link; break; case 'replaces': $replaces[] = $link; break; } } foreach ($releaseInfo->getDependencyInfo()->getOptionals() as $group => $dependencyConstraints) { foreach ($dependencyConstraints as $dependencyConstraint) { $dependencyPackageName = $this->buildComposerPackageName($dependencyConstraint->getChannelName(), $dependencyConstraint->getPackageName()); $suggests[$group.'-'.$dependencyPackageName] = $dependencyConstraint->getConstraint(); } } $package = new CompletePackage($composerPackageName, $normalizedVersion, $version); $package->setType('pear-library'); $package->setDescription($packageDefinition->getDescription()); $package->setDistType('file'); $package->setDistUrl($distUrl); $package->setAutoload(array('classmap' => array(''))); $package->setIncludePaths(array('/')); $package->setRequires($requires); $package->setConflicts($conflicts); $package->setSuggests($suggests); $package->setReplaces($replaces); $result[] = $package; } } return $result; } private function buildComposerPackageName($channelName, $packageName) { if ('php' === $channelName) { return "php"; } if ('ext' === $channelName) { return "ext-{$packageName}"; } return "pear-{$channelName}/{$packageName}"; } } io = $io; $this->config = $config; } public function findPackage($name, $version) { foreach ($this->repositories as $repository) { if ($package = $repository->findPackage($name, $version)) { return $package; } } } public function findPackages($name, $version) { $packages = array(); foreach ($this->repositories as $repository) { $packages = array_merge($packages, $repository->findPackages($name, $version)); } return $packages; } public function addRepository(RepositoryInterface $repository) { $this->repositories[] = $repository; } public function createRepository($type, $config) { if (!isset($this->repositoryClasses[$type])) { throw new \InvalidArgumentException('Repository type is not registered: '.$type); } $class = $this->repositoryClasses[$type]; return new $class($config, $this->io, $this->config); } public function setRepositoryClass($type, $class) { $this->repositoryClasses[$type] = $class; } public function getRepositories() { return $this->repositories; } public function setLocalRepository(WritableRepositoryInterface $repository) { $this->localRepository = $repository; } public function getLocalRepository() { return $this->localRepository; } public function getLocalRepositories() { trigger_error('This method is deprecated, use getLocalRepository instead since the getLocalDevRepository is now gone', E_USER_DEPRECATED); return array($this->localRepository); } } getPackages(); $packagesByName = array(); foreach ($packages as $package) { if (!isset($packagesByName[$package->getName()]) || $packagesByName[$package->getName()] instanceof AliasPackage) { $packagesByName[$package->getName()] = $package; } } $canonicalPackages = array(); foreach ($packagesByName as $package) { while ($package instanceof AliasPackage) { $package = $package->getAliasOf(); } $canonicalPackages[] = $package; } return $canonicalPackages; } } url, $match); $this->owner = $match[1]; $this->repository = $match[2]; $this->originUrl = 'github.com'; $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.$this->originUrl.'/'.$this->owner.'/'.$this->repository); $this->fetchRootIdentifier(); } public function getRootIdentifier() { if ($this->gitDriver) { return $this->gitDriver->getRootIdentifier(); } return $this->rootIdentifier; } public function getUrl() { if ($this->gitDriver) { return $this->gitDriver->getUrl(); } return 'https://github.com/'.$this->owner.'/'.$this->repository.'.git'; } public function getSource($identifier) { if ($this->gitDriver) { return $this->gitDriver->getSource($identifier); } $label = array_search($identifier, $this->getTags()) ?: $identifier; if ($this->isPrivate) { $url = $this->generateSshUrl(); } else { $url = $this->getUrl(); } return array('type' => 'git', 'url' => $url, 'reference' => $label); } public function getDist($identifier) { if ($this->gitDriver) { return $this->gitDriver->getDist($identifier); } $label = array_search($identifier, $this->getTags()) ?: $identifier; $url = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/zipball/'.$label; return array('type' => 'zip', 'url' => $url, 'reference' => $label, 'shasum' => ''); } public function getComposerInformation($identifier) { if ($this->gitDriver) { return $this->gitDriver->getComposerInformation($identifier); } if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier)) { $this->infoCache[$identifier] = JsonFile::parseJson($res); } if (!isset($this->infoCache[$identifier])) { try { $resource = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/contents/composer.json?ref='.urlencode($identifier); $composer = JsonFile::parseJson($this->getContents($resource)); if (empty($composer['content']) || $composer['encoding'] !== 'base64' || !($composer = base64_decode($composer['content']))) { throw new \RuntimeException('Could not retrieve composer.json from '.$resource); } } catch (TransportException $e) { if (404 !== $e->getCode()) { throw $e; } $composer = false; } if ($composer) { $composer = JsonFile::parseJson($composer, $resource); if (!isset($composer['time'])) { $resource = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/commits/'.urlencode($identifier); $commit = JsonFile::parseJson($this->getContents($resource), $resource); $composer['time'] = $commit['commit']['committer']['date']; } if (!isset($composer['support']['source'])) { $label = array_search($identifier, $this->getTags()) ?: array_search($identifier, $this->getBranches()) ?: $identifier; $composer['support']['source'] = sprintf('https://github.com/%s/%s/tree/%s', $this->owner, $this->repository, $label); } if (!isset($composer['support']['issues']) && $this->hasIssues) { $composer['support']['issues'] = sprintf('https://github.com/%s/%s/issues', $this->owner, $this->repository); } } if (preg_match('{[a-f0-9]{40}}i', $identifier)) { $this->cache->write($identifier, json_encode($composer)); } $this->infoCache[$identifier] = $composer; } return $this->infoCache[$identifier]; } public function getTags() { if ($this->gitDriver) { return $this->gitDriver->getTags(); } if (null === $this->tags) { $resource = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/tags'; $tagsData = JsonFile::parseJson($this->getContents($resource), $resource); $this->tags = array(); foreach ($tagsData as $tag) { $this->tags[$tag['name']] = $tag['commit']['sha']; } } return $this->tags; } public function getBranches() { if ($this->gitDriver) { return $this->gitDriver->getBranches(); } if (null === $this->branches) { $resource = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/git/refs/heads'; $branchData = JsonFile::parseJson($this->getContents($resource), $resource); $this->branches = array(); foreach ($branchData as $branch) { $name = substr($branch['ref'], 11); $this->branches[$name] = $branch['object']['sha']; } } return $this->branches; } public static function supports(IOInterface $io, $url, $deep = false) { if (!preg_match('#^((?:https?|git)://github\.com/|git@github\.com:)([^/]+)/(.+?)(?:\.git)?$#', $url)) { return false; } if (!extension_loaded('openssl')) { if ($io->isVerbose()) { $io->write('Skipping GitHub driver for '.$url.' because the OpenSSL PHP extension is missing.'); } return false; } return true; } protected function generateSshUrl() { return 'git@github.com:'.$this->owner.'/'.$this->repository.'.git'; } protected function getContents($url, $fetchingRepoData = false) { try { return parent::getContents($url); } catch (TransportException $e) { $gitHubUtil = new GitHub($this->io, $this->config, $this->process, $this->remoteFilesystem); switch ($e->getCode()) { case 401: case 404: if (!$fetchingRepoData) { throw $e; } if ($gitHubUtil->authorizeOAuth($this->originUrl)) { return parent::getContents($url); } if (!$this->io->isInteractive()) { return $this->attemptCloneFallback(); } $gitHubUtil->authorizeOAuthInteractively($this->originUrl, 'Your GitHub credentials are required to fetch private repository metadata ('.$this->url.')'); return parent::getContents($url); case 403: if (!$this->io->hasAuthentication($this->originUrl) && $gitHubUtil->authorizeOAuth($this->originUrl)) { return parent::getContents($url); } if (!$this->io->isInteractive() && $fetchingRepoData) { return $this->attemptCloneFallback(); } $rateLimited = false; foreach ($e->getHeaders() as $header) { if (preg_match('{^X-RateLimit-Remaining: *0$}i', trim($header))) { $rateLimited = true; } } if (!$this->io->hasAuthentication($this->originUrl)) { if (!$this->io->isInteractive()) { $this->io->write('GitHub API limit exhausted. Failed to get metadata for the '.$this->url.' repository, try running in interactive mode so that you can enter your GitHub credentials to increase the API limit'); throw $e; } $gitHubUtil->authorizeOAuthInteractively($this->originUrl, 'API limit exhausted. Enter your GitHub credentials to get a larger API limit ('.$this->url.')'); return parent::getContents($url); } if ($rateLimited) { $this->io->write('GitHub API limit exhausted. You are already authorized so you will have to wait a while before doing more requests'); } throw $e; default: throw $e; } } } protected function fetchRootIdentifier() { $repoDataUrl = 'https://api.github.com/repos/'.$this->owner.'/'.$this->repository; $repoData = JsonFile::parseJson($this->getContents($repoDataUrl, true), $repoDataUrl); if (null === $repoData && null !== $this->gitDriver) { return; } $this->isPrivate = !empty($repoData['private']); if (isset($repoData['default_branch'])) { $this->rootIdentifier = $repoData['default_branch']; } elseif (isset($repoData['master_branch'])) { $this->rootIdentifier = $repoData['master_branch']; } else { $this->rootIdentifier = 'master'; } $this->hasIssues = !empty($repoData['has_issues']); } protected function attemptCloneFallback() { $this->isPrivate = true; try { $this->gitDriver = new GitDriver( array('url' => $this->generateSshUrl()), $this->io, $this->config, $this->process, $this->remoteFilesystem ); $this->gitDriver->initialize(); return; } catch (\RuntimeException $e) { $this->gitDriver = null; $this->io->write('Failed to clone the '.$this->generateSshUrl().' repository, try running in interactive mode so that you can enter your GitHub credentials'); throw $e; } } } url, $match); $this->owner = $match[1]; $this->repository = $match[2]; $this->originUrl = 'bitbucket.org'; } public function getRootIdentifier() { if (null === $this->rootIdentifier) { $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository; $repoData = JsonFile::parseJson($this->getContents($resource), $resource); $this->rootIdentifier = !empty($repoData['main_branch']) ? $repoData['main_branch'] : 'master'; } return $this->rootIdentifier; } public function getUrl() { return $this->url; } public function getSource($identifier) { $label = array_search($identifier, $this->getTags()) ?: $identifier; return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $label); } public function getDist($identifier) { $label = array_search($identifier, $this->getTags()) ?: $identifier; $url = $this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/get/'.$label.'.zip'; return array('type' => 'zip', 'url' => $url, 'reference' => $label, 'shasum' => ''); } public function getComposerInformation($identifier) { if (!isset($this->infoCache[$identifier])) { $resource = $this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/raw/'.$identifier.'/composer.json'; $composer = $this->getContents($resource); if (!$composer) { return; } $composer = JsonFile::parseJson($composer, $resource); if (!isset($composer['time'])) { $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier; $changeset = JsonFile::parseJson($this->getContents($resource), $resource); $composer['time'] = $changeset['timestamp']; } $this->infoCache[$identifier] = $composer; } return $this->infoCache[$identifier]; } public function getTags() { if (null === $this->tags) { $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags'; $tagsData = JsonFile::parseJson($this->getContents($resource), $resource); $this->tags = array(); foreach ($tagsData as $tag => $data) { $this->tags[$tag] = $data['raw_node']; } } return $this->tags; } public function getBranches() { if (null === $this->branches) { $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches'; $branchData = JsonFile::parseJson($this->getContents($resource), $resource); $this->branches = array(); foreach ($branchData as $branch => $data) { $this->branches[$branch] = $data['raw_node']; } } return $this->branches; } public static function supports(IOInterface $io, $url, $deep = false) { if (!preg_match('#^https://bitbucket\.org/([^/]+)/(.+?)\.git$#', $url)) { return false; } if (!extension_loaded('openssl')) { if ($io->isVerbose()) { $io->write('Skipping Bitbucket git driver for '.$url.' because the OpenSSL PHP extension is missing.'); } return false; } return true; } } url)) { $this->repoDir = str_replace('file://', '', $this->url); } else { $this->repoDir = $this->config->get('cache-vcs-dir') . '/' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/'; $fs = new Filesystem(); $fs->ensureDirectoryExists(dirname($this->repoDir)); if (!is_writable(dirname($this->repoDir))) { throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.dirname($this->repoDir).'" directory is not writable by the current user.'); } if (preg_match('{^ssh://[^@]+@[^:]+:[^0-9]+}', $this->url)) { throw new \InvalidArgumentException('The source URL '.$this->url.' is invalid, ssh URLs should have a port number after ":".'."\n".'Use ssh://git@example.com:22/path or just git@example.com:path if you do not want to provide a password or custom port.'); } if (is_dir($this->repoDir) && 0 === $this->process->execute('git remote', $output, $this->repoDir)) { if (0 !== $this->process->execute('git remote update --prune origin', $output, $this->repoDir)) { $this->io->write('Failed to update '.$this->url.', package information from this repository may be outdated ('.$this->process->getErrorOutput().')'); } } else { $fs->removeDirectory($this->repoDir); putenv('GIT_ASKPASS=echo'); $command = sprintf('git clone --mirror %s %s', escapeshellarg($this->url), escapeshellarg($this->repoDir)); if (0 !== $this->process->execute($command, $output)) { $output = $this->process->getErrorOutput(); if (0 !== $this->process->execute('git --version', $ignoredOutput)) { throw new \RuntimeException('Failed to clone '.$this->url.', git was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput()); } throw new \RuntimeException('Failed to clone '.$this->url.', could not read packages from it' . "\n\n" .$output); } } } $this->getTags(); $this->getBranches(); } public function getRootIdentifier() { if (null === $this->rootIdentifier) { $this->rootIdentifier = 'master'; $this->process->execute('git branch --no-color', $output, $this->repoDir); $branches = $this->process->splitLines($output); if (!in_array('* master', $branches)) { foreach ($branches as $branch) { if ($branch && preg_match('{^\* +(\S+)}', $branch, $match)) { $this->rootIdentifier = $match[1]; break; } } } } return $this->rootIdentifier; } public function getUrl() { return $this->url; } public function getSource($identifier) { $label = array_search($identifier, (array) $this->tags) ?: $identifier; return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $label); } public function getDist($identifier) { return null; } public function getComposerInformation($identifier) { if (!isset($this->infoCache[$identifier])) { $resource = sprintf('%s:composer.json', escapeshellarg($identifier)); $this->process->execute(sprintf('git show %s', $resource), $composer, $this->repoDir); if (!trim($composer)) { return; } $composer = JsonFile::parseJson($composer, $resource); if (!isset($composer['time'])) { $this->process->execute(sprintf('git log -1 --format=%%at %s', escapeshellarg($identifier)), $output, $this->repoDir); $date = new \DateTime('@'.trim($output), new \DateTimeZone('UTC')); $composer['time'] = $date->format('Y-m-d H:i:s'); } $this->infoCache[$identifier] = $composer; } return $this->infoCache[$identifier]; } public function getTags() { if (null === $this->tags) { $this->process->execute('git tag', $output, $this->repoDir); $output = $this->process->splitLines($output); $this->tags = $output ? array_combine($output, $output) : array(); } return $this->tags; } public function getBranches() { if (null === $this->branches) { $branches = array(); $this->process->execute('git branch --no-color --no-abbrev -v', $output, $this->repoDir); foreach ($this->process->splitLines($output) as $branch) { if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) { if (preg_match('{^(?:\* )? *(\S+) *([a-f0-9]+) .*$}', $branch, $match)) { $branches[$match[1]] = $match[2]; } } } $this->branches = $branches; } return $this->branches; } public static function supports(IOInterface $io, $url, $deep = false) { if (preg_match('#(^git://|\.git$|git(?:olite)?@|//git\.|//github.com/)#i', $url)) { return true; } if (static::isLocalUrl($url)) { if (!is_dir($url)) { throw new \RuntimeException('Directory does not exist: '.$url); } $process = new ProcessExecutor(); $url = str_replace('file://', '', $url); if ($process->execute('git tag', $output, $url) === 0) { return true; } } if (!$deep) { return false; } return false; } } url = $this->baseUrl = rtrim(self::normalizeUrl($this->url), '/'); if (isset($this->repoConfig['trunk-path'])) { $this->trunkPath = $this->repoConfig['trunk-path']; } if (isset($this->repoConfig['branches-path'])) { $this->branchesPath = $this->repoConfig['branches-path']; } if (isset($this->repoConfig['tags-path'])) { $this->tagsPath = $this->repoConfig['tags-path']; } if (isset($this->repoConfig['package-path'])) { $this->packagePath = '/' . trim($this->repoConfig['package-path'], '/'); } if (false !== ($pos = strrpos($this->url, '/' . $this->trunkPath))) { $this->baseUrl = substr($this->url, 0, $pos); } $this->cache = new Cache($this->io, $this->config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $this->baseUrl)); $this->getBranches(); $this->getTags(); } public function getRootIdentifier() { return $this->rootIdentifier ?: $this->trunkPath; } public function getUrl() { return $this->url; } public function getSource($identifier) { return array('type' => 'svn', 'url' => $this->baseUrl, 'reference' => $identifier); } public function getDist($identifier) { return null; } public function getComposerInformation($identifier) { $identifier = '/' . trim($identifier, '/') . '/'; if ($res = $this->cache->read($identifier.'.json')) { $this->infoCache[$identifier] = JsonFile::parseJson($res); } if (!isset($this->infoCache[$identifier])) { preg_match('{^(.+?)(@\d+)?/$}', $identifier, $match); if (!empty($match[2])) { $path = $match[1]; $rev = $match[2]; } else { $path = $identifier; $rev = ''; } try { $resource = $path.'composer.json'; $output = $this->execute('svn cat', $this->baseUrl . $resource . $rev); if (!trim($output)) { return; } } catch (\RuntimeException $e) { throw new TransportException($e->getMessage()); } $composer = JsonFile::parseJson($output, $this->baseUrl . $resource . $rev); if (!isset($composer['time'])) { $output = $this->execute('svn info', $this->baseUrl . $path . $rev); foreach ($this->process->splitLines($output) as $line) { if ($line && preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) { $date = new \DateTime($match[1], new \DateTimeZone('UTC')); $composer['time'] = $date->format('Y-m-d H:i:s'); break; } } } $this->cache->write($identifier.'.json', json_encode($composer)); $this->infoCache[$identifier] = $composer; } return $this->infoCache[$identifier]; } public function getTags() { if (null === $this->tags) { $this->tags = array(); if ($this->tagsPath !== false) { $output = $this->execute('svn ls --verbose', $this->baseUrl . '/' . $this->tagsPath); if ($output) { foreach ($this->process->splitLines($output) as $line) { $line = trim($line); if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) { if (isset($match[1]) && isset($match[2]) && $match[2] !== './') { $this->tags[rtrim($match[2], '/')] = $this->buildIdentifier( '/' . $this->tagsPath . '/' . $match[2], $match[1] ); } } } } } } return $this->tags; } public function getBranches() { if (null === $this->branches) { $this->branches = array(); $output = $this->execute('svn ls --verbose', $this->baseUrl . '/'); if ($output) { foreach ($this->process->splitLines($output) as $line) { $line = trim($line); if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) { if (isset($match[1]) && isset($match[2]) && $match[2] === $this->trunkPath . '/') { $this->branches[$this->trunkPath] = $this->buildIdentifier( '/' . $this->trunkPath, $match[1] ); $this->rootIdentifier = $this->branches[$this->trunkPath]; break; } } } } unset($output); if ($this->branchesPath !== false) { $output = $this->execute('svn ls --verbose', $this->baseUrl . '/' . $this->branchesPath); if ($output) { foreach ($this->process->splitLines(trim($output)) as $line) { $line = trim($line); if ($line && preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match)) { if (isset($match[1]) && isset($match[2]) && $match[2] !== './') { $this->branches[rtrim($match[2], '/')] = $this->buildIdentifier( '/' . $this->branchesPath . '/' . $match[2], $match[1] ); } } } } } } return $this->branches; } public static function supports(IOInterface $io, $url, $deep = false) { $url = self::normalizeUrl($url); if (preg_match('#(^svn://|^svn\+ssh://|svn\.)#i', $url)) { return true; } if (!$deep && !static::isLocalUrl($url)) { return false; } $processExecutor = new ProcessExecutor(); $exit = $processExecutor->execute( "svn info --non-interactive {$url}", $ignoredOutput ); if ($exit === 0) { return true; } if (false !== stripos($processExecutor->getErrorOutput(), 'authorization failed:')) { return true; } return false; } protected static function normalizeUrl($url) { $fs = new Filesystem(); if ($fs->isAbsolutePath($url)) { return 'file://' . strtr($url, '\\', '/'); } return $url; } protected function execute($command, $url) { if (null === $this->util) { $this->util = new SvnUtil($this->baseUrl, $this->io, $this->process); } try { return $this->util->execute($command, $url); } catch (\RuntimeException $e) { if (0 !== $this->process->execute('svn --version', $ignoredOutput)) { throw new \RuntimeException('Failed to load '.$this->url.', svn was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput()); } throw new \RuntimeException( 'Repository '.$this->url.' could not be processed, '.$e->getMessage() ); } } protected function buildIdentifier($baseDir, $revision) { return rtrim($baseDir, '/') . $this->packagePath . '/@' . $revision; } } url)) { $this->repoDir = str_replace('file://', '', $this->url); } else { $cacheDir = $this->config->get('cache-vcs-dir'); $this->repoDir = $cacheDir . '/' . preg_replace('{[^a-z0-9]}i', '-', $this->url) . '/'; $fs = new Filesystem(); $fs->ensureDirectoryExists($cacheDir); if (!is_writable(dirname($this->repoDir))) { throw new \RuntimeException('Can not clone '.$this->url.' to access package information. The "'.$cacheDir.'" directory is not writable by the current user.'); } if (is_dir($this->repoDir) && 0 === $this->process->execute('hg summary', $output, $this->repoDir)) { if (0 !== $this->process->execute('hg pull -u', $output, $this->repoDir)) { $this->io->write('Failed to update '.$this->url.', package information from this repository may be outdated ('.$this->process->getErrorOutput().')'); } } else { $fs->removeDirectory($this->repoDir); if (0 !== $this->process->execute(sprintf('hg clone %s %s', escapeshellarg($this->url), escapeshellarg($this->repoDir)), $output, $cacheDir)) { $output = $this->process->getErrorOutput(); if (0 !== $this->process->execute('hg --version', $ignoredOutput)) { throw new \RuntimeException('Failed to clone '.$this->url.', hg was not found, check that it is installed and in your PATH env.' . "\n\n" . $this->process->getErrorOutput()); } throw new \RuntimeException('Failed to clone '.$this->url.', could not read packages from it' . "\n\n" .$output); } } } $this->getTags(); $this->getBranches(); } public function getRootIdentifier() { if (null === $this->rootIdentifier) { $this->process->execute(sprintf('hg tip --template "{node}"'), $output, $this->repoDir); $output = $this->process->splitLines($output); $this->rootIdentifier = $output[0]; } return $this->rootIdentifier; } public function getUrl() { return $this->url; } public function getSource($identifier) { $label = array_search($identifier, (array) $this->tags) ? : $identifier; return array('type' => 'hg', 'url' => $this->getUrl(), 'reference' => $label); } public function getDist($identifier) { return null; } public function getComposerInformation($identifier) { if (!isset($this->infoCache[$identifier])) { $this->process->execute(sprintf('hg cat -r %s composer.json', escapeshellarg($identifier)), $composer, $this->repoDir); if (!trim($composer)) { return; } $composer = JsonFile::parseJson($composer, $identifier); if (!isset($composer['time'])) { $this->process->execute(sprintf('hg log --template "{date|rfc822date}" -r %s', escapeshellarg($identifier)), $output, $this->repoDir); $date = new \DateTime(trim($output), new \DateTimeZone('UTC')); $composer['time'] = $date->format('Y-m-d H:i:s'); } $this->infoCache[$identifier] = $composer; } return $this->infoCache[$identifier]; } public function getTags() { if (null === $this->tags) { $tags = array(); $this->process->execute('hg tags', $output, $this->repoDir); foreach ($this->process->splitLines($output) as $tag) { if ($tag && preg_match('(^([^\s]+)\s+\d+:(.*)$)', $tag, $match)) { $tags[$match[1]] = $match[2]; } } unset($tags['tip']); $this->tags = $tags; } return $this->tags; } public function getBranches() { if (null === $this->branches) { $branches = array(); $bookmarks = array(); $this->process->execute('hg branches', $output, $this->repoDir); foreach ($this->process->splitLines($output) as $branch) { if ($branch && preg_match('(^([^\s]+)\s+\d+:([a-f0-9]+))', $branch, $match)) { $branches[$match[1]] = $match[2]; } } $this->process->execute('hg bookmarks', $output, $this->repoDir); foreach ($this->process->splitLines($output) as $branch) { if ($branch && preg_match('(^(?:[\s*]*)([^\s]+)\s+\d+:(.*)$)', $branch, $match)) { $bookmarks[$match[1]] = $match[2]; } } $this->branches = array_merge($bookmarks, $branches); } return $this->branches; } public static function supports(IOInterface $io, $url, $deep = false) { if (preg_match('#(^(?:https?|ssh)://(?:[^@]@)?bitbucket.org|https://(?:.*?)\.kilnhg.com)#i', $url)) { return true; } if (static::isLocalUrl($url)) { if (!is_dir($url)) { throw new \RuntimeException('Directory does not exist: '.$url); } $process = new ProcessExecutor(); $url = str_replace('file://', '', $url); if ($process->execute('hg summary', $output, $url) === 0) { return true; } } if (!$deep) { return false; } $processExecutor = new ProcessExecutor(); $exit = $processExecutor->execute(sprintf('hg identify %s', escapeshellarg($url)), $ignored); return $exit === 0; } } url = $repoConfig['url']; $this->originUrl = $repoConfig['url']; $this->repoConfig = $repoConfig; $this->io = $io; $this->config = $config; $this->process = $process ?: new ProcessExecutor; $this->remoteFilesystem = $remoteFilesystem ?: new RemoteFilesystem($io); } public function hasComposerFile($identifier) { try { return (bool) $this->getComposerInformation($identifier); } catch (TransportException $e) { } return false; } protected function getScheme() { if (extension_loaded('openssl')) { return 'https'; } return 'http'; } protected function getContents($url) { return $this->remoteFilesystem->getContents($this->originUrl, $url, false); } protected static function isLocalUrl($url) { return (bool) preg_match('{^(file://|/|[a-z]:[\\\\/])}i', $url); } } url, $match); $this->owner = $match[1]; $this->repository = $match[2]; $this->originUrl = 'bitbucket.org'; } public function getRootIdentifier() { if (null === $this->rootIdentifier) { $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags'; $repoData = JsonFile::parseJson($this->getContents($resource), $resource); if (array() === $repoData) { throw new \RuntimeException('This does not appear to be a mercurial repository, use '.$this->url.'.git if this is a git bitbucket repository'); } $this->rootIdentifier = $repoData['tip']['raw_node']; } return $this->rootIdentifier; } public function getUrl() { return $this->url; } public function getSource($identifier) { $label = array_search($identifier, $this->getTags()) ?: $identifier; return array('type' => 'hg', 'url' => $this->getUrl(), 'reference' => $label); } public function getDist($identifier) { $label = array_search($identifier, $this->getTags()) ?: $identifier; $url = $this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/get/'.$label.'.zip'; return array('type' => 'zip', 'url' => $url, 'reference' => $label, 'shasum' => ''); } public function getComposerInformation($identifier) { if (!isset($this->infoCache[$identifier])) { $resource = $this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/raw/'.$identifier.'/composer.json'; $composer = $this->getContents($resource); if (!$composer) { return; } $composer = JsonFile::parseJson($composer, $resource); if (!isset($composer['time'])) { $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier; $changeset = JsonFile::parseJson($this->getContents($resource), $resource); $composer['time'] = $changeset['timestamp']; } $this->infoCache[$identifier] = $composer; } return $this->infoCache[$identifier]; } public function getTags() { if (null === $this->tags) { $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags'; $tagsData = JsonFile::parseJson($this->getContents($resource), $resource); $this->tags = array(); foreach ($tagsData as $tag => $data) { $this->tags[$tag] = $data['raw_node']; } } return $this->tags; } public function getBranches() { if (null === $this->branches) { $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches'; $branchData = JsonFile::parseJson($this->getContents($resource), $resource); $this->branches = array(); foreach ($branchData as $branch => $data) { $this->branches[$branch] = $data['raw_node']; } } return $this->branches; } public static function supports(IOInterface $io, $url, $deep = false) { if (!preg_match('#^https://bitbucket\.org/([^/]+)/([^/]+)/?$#', $url)) { return false; } if (!extension_loaded('openssl')) { if ($io->isVerbose()) { $io->write('Skipping Bitbucket hg driver for '.$url.' because the OpenSSL PHP extension is missing.'); } return false; } return true; } } drivers = $drivers ?: array( 'github' => 'Composer\Repository\Vcs\GitHubDriver', 'git-bitbucket' => 'Composer\Repository\Vcs\GitBitbucketDriver', 'git' => 'Composer\Repository\Vcs\GitDriver', 'hg-bitbucket' => 'Composer\Repository\Vcs\HgBitbucketDriver', 'hg' => 'Composer\Repository\Vcs\HgDriver', 'svn' => 'Composer\Repository\Vcs\SvnDriver', ); $this->url = $repoConfig['url']; $this->io = $io; $this->type = isset($repoConfig['type']) ? $repoConfig['type'] : 'vcs'; $this->verbose = $io->isVerbose(); $this->config = $config; $this->repoConfig = $repoConfig; } public function setLoader(LoaderInterface $loader) { $this->loader = $loader; } public function getDriver() { if (isset($this->drivers[$this->type])) { $class = $this->drivers[$this->type]; $driver = new $class($this->repoConfig, $this->io, $this->config); $driver->initialize(); return $driver; } foreach ($this->drivers as $driver) { if ($driver::supports($this->io, $this->url)) { $driver = new $driver($this->repoConfig, $this->io, $this->config); $driver->initialize(); return $driver; } } foreach ($this->drivers as $driver) { if ($driver::supports($this->io, $this->url, true)) { $driver = new $driver($this->repoConfig, $this->io, $this->config); $driver->initialize(); return $driver; } } } public function hadInvalidBranches() { return $this->branchErrorOccurred; } protected function initialize() { parent::initialize(); $verbose = $this->verbose; $driver = $this->getDriver(); if (!$driver) { throw new \InvalidArgumentException('No driver found to handle VCS repository '.$this->url); } $this->versionParser = new VersionParser; if (!$this->loader) { $this->loader = new ArrayLoader($this->versionParser); } try { if ($driver->hasComposerFile($driver->getRootIdentifier())) { $data = $driver->getComposerInformation($driver->getRootIdentifier()); $this->packageName = !empty($data['name']) ? $data['name'] : null; } } catch (\Exception $e) { if ($verbose) { $this->io->write('Skipped parsing '.$driver->getRootIdentifier().', '.$e->getMessage().''); } } foreach ($driver->getTags() as $tag => $identifier) { $msg = 'Reading composer.json of ' . ($this->packageName ?: $this->url) . ' (' . $tag . ')'; if ($verbose) { $this->io->write($msg); } else { $this->io->overwrite($msg, false); } $tag = str_replace('release-', '', $tag); if (!$parsedTag = $this->validateTag($tag)) { if ($verbose) { $this->io->write('Skipped tag '.$tag.', invalid tag name'); } continue; } try { if (!$data = $driver->getComposerInformation($identifier)) { if ($verbose) { $this->io->write('Skipped tag '.$tag.', no composer file'); } continue; } if (isset($data['version'])) { $data['version_normalized'] = $this->versionParser->normalize($data['version']); } else { $data['version'] = $tag; $data['version_normalized'] = $parsedTag; } $data['version'] = preg_replace('{[.-]?dev$}i', '', $data['version']); $data['version_normalized'] = preg_replace('{(^dev-|[.-]?dev$)}i', '', $data['version_normalized']); if ($data['version_normalized'] !== $parsedTag) { if ($verbose) { $this->io->write('Skipped tag '.$tag.', tag ('.$parsedTag.') does not match version ('.$data['version_normalized'].') in composer.json'); } continue; } if ($verbose) { $this->io->write('Importing tag '.$tag.' ('.$data['version_normalized'].')'); } $this->addPackage($this->loader->load($this->preProcess($driver, $data, $identifier))); } catch (\Exception $e) { if ($verbose) { $this->io->write('Skipped tag '.$tag.', '.($e instanceof TransportException ? 'no composer file was found' : $e->getMessage()).''); } continue; } } if (!$verbose) { $this->io->overwrite('', false); } foreach ($driver->getBranches() as $branch => $identifier) { $msg = 'Reading composer.json of ' . ($this->packageName ?: $this->url) . ' (' . $branch . ')'; if ($verbose) { $this->io->write($msg); } else { $this->io->overwrite($msg, false); } if (!$parsedBranch = $this->validateBranch($branch)) { if ($verbose) { $this->io->write('Skipped branch '.$branch.', invalid name'); } continue; } try { if (!$data = $driver->getComposerInformation($identifier)) { if ($verbose) { $this->io->write('Skipped branch '.$branch.', no composer file'); } continue; } $data['version'] = $branch; $data['version_normalized'] = $parsedBranch; if ('dev-' === substr($parsedBranch, 0, 4) || '9999999-dev' === $parsedBranch) { $data['version'] = 'dev-' . $data['version']; } else { $data['version'] = preg_replace('{(\.9{7})+}', '.x', $parsedBranch); } if ($verbose) { $this->io->write('Importing branch '.$branch.' ('.$data['version'].')'); } $packageData = $this->preProcess($driver, $data, $identifier); $package = $this->loader->load($packageData); if ($this->loader instanceof ValidatingArrayLoader && $this->loader->getWarnings()) { throw new InvalidPackageException($this->loader->getErrors(), $this->loader->getWarnings(), $packageData); } $this->addPackage($package); } catch (TransportException $e) { if ($verbose) { $this->io->write('Skipped branch '.$branch.', no composer file was found'); } continue; } catch (\Exception $e) { if (!$verbose) { $this->io->write(''); } $this->branchErrorOccurred = true; $this->io->write('Skipped branch '.$branch.', '.$e->getMessage().''); $this->io->write(''); continue; } } if (!$verbose) { $this->io->overwrite('', false); } if (!$this->getPackages()) { throw new InvalidRepositoryException('No valid composer.json was found in any branch or tag of '.$this->url.', could not load a package from it.'); } } private function preProcess(VcsDriverInterface $driver, array $data, $identifier) { $data['name'] = $this->packageName ?: $data['name']; if (!isset($data['dist'])) { $data['dist'] = $driver->getDist($identifier); } if (!isset($data['source'])) { $data['source'] = $driver->getSource($identifier); } return $data; } private function validateBranch($branch) { try { return $this->versionParser->normalizeBranch($branch); } catch (\Exception $e) { } return false; } private function validateTag($version) { try { return $this->versionParser->normalize($version); } catch (\Exception $e) { } return false; } } normalize($prettyVersion); } catch (\UnexpectedValueException $e) { $prettyVersion = preg_replace('#^([^~+-]+).*$#', '$1', PHP_VERSION); $version = $versionParser->normalize($prettyVersion); } $php = new CompletePackage('php', $version, $prettyVersion); $php->setDescription('The PHP interpreter'); parent::addPackage($php); if (PHP_INT_SIZE === 8) { $php64 = new CompletePackage('php-64bit', $version, $prettyVersion); $php64->setDescription('The PHP interpreter (64bit)'); parent::addPackage($php64); } $loadedExtensions = get_loaded_extensions(); foreach ($loadedExtensions as $name) { if (in_array($name, array('standard', 'Core'))) { continue; } $reflExt = new \ReflectionExtension($name); try { $prettyVersion = $reflExt->getVersion(); $version = $versionParser->normalize($prettyVersion); } catch (\UnexpectedValueException $e) { $prettyVersion = '0'; $version = $versionParser->normalize($prettyVersion); } $ext = new CompletePackage('ext-'.$name, $version, $prettyVersion); $ext->setDescription('The '.$name.' PHP extension'); parent::addPackage($ext); } foreach ($loadedExtensions as $name) { $prettyVersion = null; switch ($name) { case 'curl': $curlVersion = curl_version(); $prettyVersion = $curlVersion['version']; break; case 'iconv': $prettyVersion = ICONV_VERSION; break; case 'intl': $name = 'ICU'; if (defined('INTL_ICU_VERSION')) { $prettyVersion = INTL_ICU_VERSION; } else { $reflector = new \ReflectionExtension('intl'); ob_start(); $reflector->info(); $output = ob_get_clean(); preg_match('/^ICU version => (.*)$/m', $output, $matches); $prettyVersion = $matches[1]; } break; case 'libxml': $prettyVersion = LIBXML_DOTTED_VERSION; break; case 'openssl': $prettyVersion = preg_replace_callback('{^(?:OpenSSL\s*)?([0-9.]+)([a-z]?).*}', function ($match) { return $match[1] . (empty($match[2]) ? '' : '.'.(ord($match[2]) - 96)); }, OPENSSL_VERSION_TEXT); break; case 'pcre': $prettyVersion = preg_replace('{^(\S+).*}', '$1', PCRE_VERSION); break; case 'uuid': $prettyVersion = phpversion('uuid'); break; case 'xsl': $prettyVersion = LIBXSLT_DOTTED_VERSION; break; default: continue 2; } try { $version = $versionParser->normalize($prettyVersion); } catch (\UnexpectedValueException $e) { continue; } $lib = new CompletePackage('lib-'.$name, $version, $prettyVersion); $lib->setDescription('The '.$name.' PHP library'); parent::addPackage($lib); } } } repositories = array(); foreach ($repositories as $repo) { $this->addRepository($repo); } } public function getRepositories() { return $this->repositories; } public function hasPackage(PackageInterface $package) { foreach ($this->repositories as $repository) { if ($repository->hasPackage($package)) { return true; } } return false; } public function findPackage($name, $version) { foreach ($this->repositories as $repository) { $package = $repository->findPackage($name, $version); if (null !== $package) { return $package; } } return null; } public function findPackages($name, $version = null) { $packages = array(); foreach ($this->repositories as $repository) { $packages[] = $repository->findPackages($name, $version); } return call_user_func_array('array_merge', $packages); } public function search($query, $mode = 0) { $matches = array(); foreach ($this->repositories as $repository) { $matches[] = $repository->search($query, $mode); } return call_user_func_array('array_merge', $matches); } public function filterPackages($callback, $class = 'Composer\Package\Package') { foreach ($this->repositories as $repository) { if (false === $repository->filterPackages($callback, $class)) { return false; } } return true; } public function getPackages() { $packages = array(); foreach ($this->repositories as $repository) { $packages[] = $repository->getPackages(); } return call_user_func_array('array_merge', $packages); } public function removePackage(PackageInterface $package) { foreach ($this->repositories as $repository) { $repository->removePackage($package); } } public function count() { $total = 0; foreach ($this->repositories as $repository) { $total += $repository->count(); } return $total; } public function addRepository(RepositoryInterface $repository) { if ($repository instanceof self) { foreach ($repository->getRepositories() as $repo) { $this->addRepository($repo); } } else { $this->repositories[] = $repository; } } } allowSslDowngrade = true; } $this->config = $config; $this->options = $repoConfig['options']; $this->url = $repoConfig['url']; $this->baseUrl = rtrim(preg_replace('{^(.*)(?:/packages.json)?(?:[?#].*)?$}', '$1', $this->url), '/'); $this->io = $io; $this->cache = new Cache($io, $config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url), 'a-z0-9.$'); $this->loader = new ArrayLoader(); $this->rfs = new RemoteFilesystem($this->io, $this->options); } public function setRootAliases(array $rootAliases) { $this->rootAliases = $rootAliases; } public function getPackages() { if ($this->hasProviders()) { throw new \LogicException('Composer repositories that have providers can not load the complete list of packages, use getProviderNames instead.'); } return parent::getPackages(); } public function getMinimalPackages() { if (isset($this->minimalPackages)) { return $this->minimalPackages; } if (null === $this->rawData) { $this->rawData = $this->loadDataFromServer(); } $this->minimalPackages = array(); $versionParser = new VersionParser; foreach ($this->rawData as $package) { $version = !empty($package['version_normalized']) ? $package['version_normalized'] : $versionParser->normalize($package['version']); $data = array( 'name' => strtolower($package['name']), 'repo' => $this, 'version' => $version, 'raw' => $package, ); if (!empty($package['replace'])) { $data['replace'] = $package['replace']; } if (!empty($package['provide'])) { $data['provide'] = $package['provide']; } if ($aliasNormalized = $this->loader->getBranchAlias($package)) { $data['alias'] = preg_replace('{(\.9{7})+}', '.x', $aliasNormalized); $data['alias_normalized'] = $aliasNormalized; } $this->minimalPackages[] = $data; } return $this->minimalPackages; } public function search($query, $mode = 0) { $this->loadRootServerFile(); if ($this->searchUrl && $mode === self::SEARCH_FULLTEXT) { $url = str_replace('%query%', $query, $this->searchUrl); $json = $this->rfs->getContents($url, $url, false); $results = JsonFile::parseJson($json, $url); return $results['results']; } if ($this->hasProviders()) { $results = array(); $regex = '{(?:'.implode('|', preg_split('{\s+}', $query)).')}i'; foreach ($this->getProviderNames() as $name) { if (preg_match($regex, $name)) { $results[] = array('name' => $name); } } return $results; } return parent::search($query, $mode); } public function getProviderNames() { $this->loadRootServerFile(); if (null === $this->providerListing) { $this->loadProviderListings($this->loadRootServerFile()); } if ($this->providersUrl) { return array_keys($this->providerListing); } $providers = array(); foreach (array_keys($this->providerListing) as $provider) { $providers[] = substr($provider, 2, -5); } return $providers; } public function loadPackage(array $data) { $package = $this->createPackage($data['raw'], 'Composer\Package\Package'); if ($package instanceof AliasPackage) { $package = $package->getAliasOf(); } $package->setRepository($this); return $package; } public function loadAliasPackage(array $data, PackageInterface $aliasOf) { $aliasPackage = $this->createAliasPackage($aliasOf, $data['version'], $data['alias']); $aliasPackage->setRepository($this); return $aliasPackage; } public function hasProviders() { $this->loadRootServerFile(); return $this->hasProviders; } public function resetPackageIds() { foreach ($this->providersByUid as $package) { if ($package instanceof AliasPackage) { $package->getAliasOf()->setId(-1); } $package->setId(-1); } } public function whatProvides(Pool $pool, $name) { if (isset($this->providers[$name])) { return $this->providers[$name]; } if (preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $name) || '__root__' === $name) { return array(); } if (null === $this->providerListing) { $this->loadProviderListings($this->loadRootServerFile()); } if ($this->providersUrl) { if (!isset($this->providerListing[$name])) { return array(); } $hash = $this->providerListing[$name]['sha256']; $url = str_replace(array('%package%', '%hash%'), array($name, $hash), $this->providersUrl); $cacheKey = 'provider-'.strtr($name, '/', '$').'.json'; } else { $url = 'p/'.$name.'.json'; if (!isset($this->providerListing[$url])) { return array(); } $hash = $this->providerListing[$url]['sha256']; $cacheKey = null; } if ($this->cache->sha256($cacheKey) === $hash) { $packages = json_decode($this->cache->read($cacheKey), true); } else { $packages = $this->fetchFile($url, $cacheKey, $hash); } $this->providers[$name] = array(); foreach ($packages['packages'] as $versions) { foreach ($versions as $version) { if (isset($this->providersByUid[$version['uid']])) { if (!isset($this->providers[$name][$version['uid']])) { if ($this->providersByUid[$version['uid']] instanceof AliasPackage) { $this->providers[$name][$version['uid']] = $this->providersByUid[$version['uid']]->getAliasOf(); $this->providers[$name][$version['uid'].'-alias'] = $this->providersByUid[$version['uid']]; } else { $this->providers[$name][$version['uid']] = $this->providersByUid[$version['uid']]; } if (isset($this->providersByUid[$version['uid'].'-root'])) { $this->providers[$name][$version['uid'].'-root'] = $this->providersByUid[$version['uid'].'-root']; } } } else { if (isset($version['provide']) || isset($version['replace'])) { $names = array( strtolower($version['name']) => true, ); if (isset($version['provide'])) { foreach ($version['provide'] as $target => $constraint) { $names[strtolower($target)] = true; } } if (isset($version['replace'])) { foreach ($version['replace'] as $target => $constraint) { $names[strtolower($target)] = true; } } $names = array_keys($names); } else { $names = array(strtolower($version['name'])); } if (!$pool->isPackageAcceptable(strtolower($version['name']), VersionParser::parseStability($version['version']))) { continue; } $package = $this->createPackage($version, 'Composer\Package\Package'); $package->setRepository($this); if ($package instanceof AliasPackage) { $aliased = $package->getAliasOf(); $aliased->setRepository($this); $this->providers[$name][$version['uid']] = $aliased; $this->providers[$name][$version['uid'].'-alias'] = $package; $this->providersByUid[$version['uid']] = $package; } else { $this->providers[$name][$version['uid']] = $package; $this->providersByUid[$version['uid']] = $package; } unset($rootAliasData); if (isset($this->rootAliases[$name][$package->getVersion()])) { $rootAliasData = $this->rootAliases[$name][$package->getVersion()]; } elseif ($package instanceof AliasPackage && isset($this->rootAliases[$name][$package->getAliasOf()->getVersion()])) { $rootAliasData = $this->rootAliases[$name][$package->getAliasOf()->getVersion()]; } if (isset($rootAliasData)) { $alias = $this->createAliasPackage($package, $rootAliasData['alias_normalized'], $rootAliasData['alias']); $alias->setRepository($this); $this->providers[$name][$version['uid'].'-root'] = $alias; $this->providersByUid[$version['uid'].'-root'] = $alias; } } } } return $this->providers[$name]; } protected function initialize() { parent::initialize(); $repoData = $this->loadDataFromServer(); foreach ($repoData as $package) { $this->addPackage($this->createPackage($package, 'Composer\Package\CompletePackage')); } } protected function loadRootServerFile() { if (null !== $this->rootData) { return $this->rootData; } if (!extension_loaded('openssl') && 'https' === substr($this->url, 0, 5)) { throw new \RuntimeException('You must enable the openssl extension in your php.ini to load information from '.$this->url); } $jsonUrlParts = parse_url($this->url); if (isset($jsonUrlParts['path']) && false !== strpos($jsonUrlParts['path'], '/packages.json')) { $jsonUrl = $this->url; } else { $jsonUrl = $this->url . '/packages.json'; } $data = $this->fetchFile($jsonUrl, 'packages.json'); if (!empty($data['notify-batch'])) { $this->notifyUrl = $this->canonicalizeUrl($data['notify-batch']); } elseif (!empty($data['notify_batch'])) { $this->notifyUrl = $this->canonicalizeUrl($data['notify_batch']); } elseif (!empty($data['notify'])) { $this->notifyUrl = $this->canonicalizeUrl($data['notify']); } if (!empty($data['search'])) { $this->searchUrl = $this->canonicalizeUrl($data['search']); } if ($this->allowSslDowngrade) { $this->url = str_replace('https://', 'http://', $this->url); } if (!empty($data['providers-url'])) { $this->providersUrl = $this->canonicalizeUrl($data['providers-url']); $this->hasProviders = true; } if (!empty($data['providers']) || !empty($data['providers-includes'])) { $this->hasProviders = true; } return $this->rootData = $data; } protected function canonicalizeUrl($url) { if ('/' === $url[0]) { return preg_replace('{(https?://[^/]+).*}i', '$1' . $url, $this->url); } return $url; } protected function loadDataFromServer() { $data = $this->loadRootServerFile(); return $this->loadIncludes($data); } protected function loadProviderListings($data) { if (isset($data['providers'])) { if (!is_array($this->providerListing)) { $this->providerListing = array(); } $this->providerListing = array_merge($this->providerListing, $data['providers']); } if ($this->providersUrl && isset($data['provider-includes'])) { $includes = $data['provider-includes']; foreach ($includes as $include => $metadata) { $url = $this->baseUrl . '/' . str_replace('%hash%', $metadata['sha256'], $include); $cacheKey = str_replace(array('%hash%','$'), '', $include); if ($this->cache->sha256($cacheKey) === $metadata['sha256']) { $includedData = json_decode($this->cache->read($cacheKey), true); } else { $includedData = $this->fetchFile($url, $cacheKey, $metadata['sha256']); } $this->loadProviderListings($includedData); } } elseif (isset($data['providers-includes'])) { $includes = $data['providers-includes']; foreach ($includes as $include => $metadata) { if ($this->cache->sha256($include) === $metadata['sha256']) { $includedData = json_decode($this->cache->read($include), true); } else { $includedData = $this->fetchFile($include, null, $metadata['sha256']); } $this->loadProviderListings($includedData); } } } protected function loadIncludes($data) { $packages = array(); if (!isset($data['packages']) && !isset($data['includes'])) { foreach ($data as $pkg) { foreach ($pkg['versions'] as $metadata) { $packages[] = $metadata; } } return $packages; } if (isset($data['packages'])) { foreach ($data['packages'] as $package => $versions) { foreach ($versions as $version => $metadata) { $packages[] = $metadata; } } } if (isset($data['includes'])) { foreach ($data['includes'] as $include => $metadata) { if ($this->cache->sha1($include) === $metadata['sha1']) { $includedData = json_decode($this->cache->read($include), true); } else { $includedData = $this->fetchFile($include); } $packages = array_merge($packages, $this->loadIncludes($includedData)); } } return $packages; } protected function createPackage(array $data, $class) { try { $data['notification-url'] = $this->notifyUrl; return $this->loader->load($data, 'Composer\Package\CompletePackage'); } catch (\Exception $e) { throw new \RuntimeException('Could not load package '.(isset($data['name']) ? $data['name'] : json_encode($data)).' in '.$this->url.': ['.get_class($e).'] '.$e->getMessage(), 0, $e); } } protected function fetchFile($filename, $cacheKey = null, $sha256 = null) { if (!$cacheKey) { $cacheKey = $filename; $filename = $this->baseUrl.'/'.$filename; } $retries = 3; while ($retries--) { try { $json = $this->rfs->getContents($filename, $filename, false); if ($sha256 && $sha256 !== hash('sha256', $json)) { if ($retries) { usleep(100000); continue; } throw new RepositorySecurityException('The contents of '.$filename.' do not match its signature. This should indicate a man-in-the-middle attack. Try running composer again and report this if you think it is a mistake.'); } $data = JsonFile::parseJson($json, $filename); $this->cache->write($cacheKey, $json); break; } catch (\Exception $e) { if ($retries) { usleep(100000); continue; } if ($e instanceof RepositorySecurityException) { throw $e; } if ($contents = $this->cache->read($cacheKey)) { if (!$this->degradedMode) { $this->io->write(''.$e->getMessage().''); $this->io->write(''.$this->url.' could not be fully loaded, package information was loaded from the local cache and may be out of date'); } $this->degradedMode = true; $data = JsonFile::parseJson($contents, $this->cache->getRoot().$cacheKey); break; } throw $e; } } return $data; } } requires = $requires; $this->optionals = $optionals; } public function getRequires() { return $this->requires; } public function getOptionals() { return $this->optionals; } } isHash($depArray)) { return new DependencyInfo($this->buildDependency10Info($depArray), array()); } return $this->buildDependency20Info($depArray); } private function buildDependency10Info($depArray) { static $dep10toOperatorMap = array('has'=>'==', 'eq' => '==', 'ge' => '>=', 'gt' => '>', 'le' => '<=', 'lt' => '<', 'not' => '!='); $result = array(); foreach ($depArray as $depItem) { if (empty($depItem['rel']) || !array_key_exists($depItem['rel'], $dep10toOperatorMap)) { continue; } $depType = !empty($depItem['optional']) && 'yes' == $depItem['optional'] ? 'optional' : 'required'; $depType = 'not' == $depItem['rel'] ? 'conflicts' : $depType; $depVersion = !empty($depItem['version']) ? $this->parseVersion($depItem['version']) : '*'; $depVersionConstraint = ('has' == $depItem['rel'] || 'not' == $depItem['rel']) && '*' == $depVersion ? '*' : $dep10toOperatorMap[$depItem['rel']] . $depVersion; switch ($depItem['type']) { case 'php': $depChannelName = 'php'; $depPackageName = ''; break; case 'pkg': $depChannelName = !empty($depItem['channel']) ? $depItem['channel'] : 'pear.php.net'; $depPackageName = $depItem['name']; break; case 'ext': $depChannelName = 'ext'; $depPackageName = $depItem['name']; break; case 'os': case 'sapi': $depChannelName = ''; $depPackageName = ''; break; default: $depChannelName = ''; $depPackageName = ''; break; } if ('' != $depChannelName) { $result[] = new DependencyConstraint( $depType, $depVersionConstraint, $depChannelName, $depPackageName ); } } return $result; } private function buildDependency20Info($depArray) { $result = array(); $optionals = array(); $defaultOptionals = array(); foreach ($depArray as $depType => $depTypeGroup) { if (!is_array($depTypeGroup)) { continue; } if ('required' == $depType || 'optional' == $depType) { foreach ($depTypeGroup as $depItemType => $depItem) { switch ($depItemType) { case 'php': $result[] = new DependencyConstraint( $depType, $this->parse20VersionConstraint($depItem), 'php', '' ); break; case 'package': $deps = $this->buildDepPackageConstraints($depItem, $depType); $result = array_merge($result, $deps); break; case 'extension': $deps = $this->buildDepExtensionConstraints($depItem, $depType); $result = array_merge($result, $deps); break; case 'subpackage': $deps = $this->buildDepPackageConstraints($depItem, 'replaces'); $defaultOptionals += $deps; break; case 'os': case 'pearinstaller': break; default: break; } } } elseif ('group' == $depType) { if ($this->isHash($depTypeGroup)) { $depTypeGroup = array($depTypeGroup); } foreach ($depTypeGroup as $depItem) { $groupName = $depItem['attribs']['name']; if (!isset($optionals[$groupName])) { $optionals[$groupName] = array(); } if (isset($depItem['subpackage'])) { $optionals[$groupName] += $this->buildDepPackageConstraints($depItem['subpackage'], 'replaces'); } else { $result += $this->buildDepPackageConstraints($depItem['package'], 'optional'); } } } } if (count($defaultOptionals) > 0) { $optionals['*'] = $defaultOptionals; } return new DependencyInfo($result, $optionals); } private function buildDepExtensionConstraints($depItem, $depType) { if ($this->isHash($depItem)) { $depItem = array($depItem); } $result = array(); foreach ($depItem as $subDepItem) { $depChannelName = 'ext'; $depPackageName = $subDepItem['name']; $depVersionConstraint = $this->parse20VersionConstraint($subDepItem); $result[] = new DependencyConstraint( $depType, $depVersionConstraint, $depChannelName, $depPackageName ); } return $result; } private function buildDepPackageConstraints($depItem, $depType) { if ($this->isHash($depItem)) { $depItem = array($depItem); } $result = array(); foreach ($depItem as $subDepItem) { $depChannelName = $subDepItem['channel']; $depPackageName = $subDepItem['name']; $depVersionConstraint = $this->parse20VersionConstraint($subDepItem); if (isset($subDepItem['conflicts'])) { $depType = 'conflicts'; } $result[] = new DependencyConstraint( $depType, $depVersionConstraint, $depChannelName, $depPackageName ); } return $result; } private function parse20VersionConstraint(array $data) { static $dep20toOperatorMap = array('has'=>'==', 'min' => '>=', 'max' => '<=', 'exclude' => '!='); $versions = array(); $values = array_intersect_key($data, $dep20toOperatorMap); if (0 == count($values)) { return '*'; } if (isset($values['min']) && isset($values['exclude']) && $data['min'] == $data['exclude']) { $versions[] = '>' . $this->parseVersion($values['min']); } elseif (isset($values['max']) && isset($values['exclude']) && $data['max'] == $data['exclude']) { $versions[] = '<' . $this->parseVersion($values['max']); } else { foreach ($values as $op => $version) { if ('exclude' == $op && is_array($version)) { foreach ($version as $versionPart) { $versions[] = $dep20toOperatorMap[$op] . $this->parseVersion($versionPart); } } else { $versions[] = $dep20toOperatorMap[$op] . $this->parseVersion($version); } } } return implode(',', $versions); } private function parseVersion($version) { if (preg_match('{^v?(\d{1,3})(\.\d+)?(\.\d+)?(\.\d+)?}i', $version, $matches)) { $version = $matches[1] .(!empty($matches[2]) ? $matches[2] : '.0') .(!empty($matches[3]) ? $matches[3] : '.0') .(!empty($matches[4]) ? $matches[4] : '.0'); return $version; } return null; } private function isHash(array $array) { return !array_key_exists(1, $array) && !array_key_exists(0, $array); } } name = $name; $this->alias = $alias; $this->packages = $packages; } public function getName() { return $this->name; } public function getAlias() { return $this->alias; } public function getPackages() { return $this->packages; } } readerMap = array( 'REST1.3' => $rest11reader, 'REST1.2' => $rest11reader, 'REST1.1' => $rest11reader, 'REST1.0' => $rest10reader, ); } public function read($url) { $xml = $this->requestXml($url, "/channel.xml"); $channelName = (string) $xml->name; $channelSummary = (string) $xml->summary; $channelAlias = (string) $xml->suggestedalias; $supportedVersions = array_keys($this->readerMap); $selectedRestVersion = $this->selectRestVersion($xml, $supportedVersions); if (!$selectedRestVersion) { throw new \UnexpectedValueException(sprintf('PEAR repository %s does not supports any of %s protocols.', $url, implode(', ', $supportedVersions))); } $reader = $this->readerMap[$selectedRestVersion['version']]; $packageDefinitions = $reader->read($selectedRestVersion['baseUrl']); return new ChannelInfo($channelName, $channelAlias, $packageDefinitions); } private function selectRestVersion($channelXml, $supportedVersions) { $channelXml->registerXPathNamespace('ns', self::CHANNEL_NS); foreach ($supportedVersions as $version) { $xpathTest = "ns:servers/ns:primary/ns:rest/ns:baseurl[@type='{$version}']"; $testResult = $channelXml->xpath($xpathTest); if (count($testResult) > 0) { return array('version' => $version, 'baseUrl' => (string) $testResult[0]); } } return null; } } channelName = $channelName; $this->packageName = $packageName; $this->license = $license; $this->shortDescription = $shortDescription; $this->description = $description; $this->releases = $releases; } public function getChannelName() { return $this->channelName; } public function getPackageName() { return $this->packageName; } public function getDescription() { return $this->description; } public function getShortDescription() { return $this->shortDescription; } public function getLicense() { return $this->license; } public function getReleases() { return $this->releases; } } type = $type; $this->constraint = $constraint; $this->channelName = $channelName; $this->packageName = $packageName; } public function getChannelName() { return $this->channelName; } public function getConstraint() { return $this->constraint; } public function getPackageName() { return $this->packageName; } public function getType() { return $this->type; } } dependencyReader = new PackageDependencyParser(); } public function read($baseUrl) { return $this->readChannelPackages($baseUrl); } private function readChannelPackages($baseUrl) { $result = array(); $xml = $this->requestXml($baseUrl, "/c/categories.xml"); $xml->registerXPathNamespace('ns', self::ALL_CATEGORIES_NS); foreach ($xml->xpath('ns:c') as $node) { $categoryName = (string) $node; $categoryPackages = $this->readCategoryPackages($baseUrl, $categoryName); $result = array_merge($result, $categoryPackages); } return $result; } private function readCategoryPackages($baseUrl, $categoryName) { $result = array(); $categoryPath = '/c/'.urlencode($categoryName).'/packagesinfo.xml'; $xml = $this->requestXml($baseUrl, $categoryPath); $xml->registerXPathNamespace('ns', self::CATEGORY_PACKAGES_INFO_NS); foreach ($xml->xpath('ns:pi') as $node) { $packageInfo = $this->parsePackage($node); $result[] = $packageInfo; } return $result; } private function parsePackage($packageInfo) { $packageInfo->registerXPathNamespace('ns', self::CATEGORY_PACKAGES_INFO_NS); $channelName = (string) $packageInfo->p->c; $packageName = (string) $packageInfo->p->n; $license = (string) $packageInfo->p->l; $shortDescription = (string) $packageInfo->p->s; $description = (string) $packageInfo->p->d; $dependencies = array(); foreach ($packageInfo->xpath('ns:deps') as $node) { $dependencyVersion = (string) $node->v; $dependencyArray = unserialize((string) $node->d); $dependencyInfo = $this->dependencyReader->buildDependencyInfo($dependencyArray); $dependencies[$dependencyVersion] = $dependencyInfo; } $releases = array(); $releasesInfo = $packageInfo->xpath('ns:a/ns:r'); if ($releasesInfo) { foreach ($releasesInfo as $node) { $releaseVersion = (string) $node->v; $releaseStability = (string) $node->s; $releases[$releaseVersion] = new ReleaseInfo( $releaseStability, isset($dependencies[$releaseVersion]) ? $dependencies[$releaseVersion] : new DependencyInfo(array(), array()) ); } } return new PackageInfo( $channelName, $packageName, $license, $shortDescription, $description, $releases ); } } stability = $stability; $this->dependencyInfo = $dependencyInfo; } public function getDependencyInfo() { return $this->dependencyInfo; } public function getStability() { return $this->stability; } } rfs = $rfs; } protected function requestContent($origin, $path) { $url = rtrim($origin, '/') . '/' . ltrim($path, '/'); $content = $this->rfs->getContents($origin, $url, false); if (!$content) { throw new \UnexpectedValueException('The PEAR channel at ' . $url . ' did not respond.'); } return $content; } protected function requestXml($origin, $path) { $xml = simplexml_load_string($this->requestContent($origin, $path), "SimpleXMLElement", LIBXML_NOERROR); if (false == $xml) { $url = rtrim($origin, '/') . '/' . ltrim($path, '/'); throw new \UnexpectedValueException(sprintf('The PEAR channel at ' . $origin . ' is broken. (Invalid XML at file `%s`)', $path)); } return $xml; } } dependencyReader = new PackageDependencyParser(); } public function read($baseUrl) { return $this->readPackages($baseUrl); } private function readPackages($baseUrl) { $result = array(); $xmlPath = '/p/packages.xml'; $xml = $this->requestXml($baseUrl, $xmlPath); $xml->registerXPathNamespace('ns', self::ALL_PACKAGES_NS); foreach ($xml->xpath('ns:p') as $node) { $packageName = (string) $node; $packageInfo = $this->readPackage($baseUrl, $packageName); $result[] = $packageInfo; } return $result; } private function readPackage($baseUrl, $packageName) { $xmlPath = '/p/' . strtolower($packageName) . '/info.xml'; $xml = $this->requestXml($baseUrl, $xmlPath); $xml->registerXPathNamespace('ns', self::PACKAGE_INFO_NS); $channelName = (string) $xml->c; $packageName = (string) $xml->n; $license = (string) $xml->l; $shortDescription = (string) $xml->s; $description = (string) $xml->d; return new PackageInfo( $channelName, $packageName, $license, $shortDescription, $description, $this->readPackageReleases($baseUrl, $packageName) ); } private function readPackageReleases($baseUrl, $packageName) { $result = array(); try { $xmlPath = '/r/' . strtolower($packageName) . '/allreleases.xml'; $xml = $this->requestXml($baseUrl, $xmlPath); $xml->registerXPathNamespace('ns', self::ALL_RELEASES_NS); foreach ($xml->xpath('ns:r') as $node) { $releaseVersion = (string) $node->v; $releaseStability = (string) $node->s; try { $result[$releaseVersion] = new ReleaseInfo( $releaseStability, $this->readPackageReleaseDependencies($baseUrl, $packageName, $releaseVersion) ); } catch (TransportException $exception) { if ($exception->getCode() != 404) { throw $exception; } } } } catch (TransportException $exception) { if ($exception->getCode() != 404) { throw $exception; } } return $result; } private function readPackageReleaseDependencies($baseUrl, $packageName, $version) { $dependencyReader = new PackageDependencyParser(); $depthPath = '/r/' . strtolower($packageName) . '/deps.' . $version . '.txt'; $content = $this->requestContent($baseUrl, $depthPath); $dependencyArray = unserialize($content); $result = $dependencyReader->buildDependencyInfo($dependencyArray); return $result; } } loader = new ArrayLoader(); $this->lookup = $repoConfig['url']; $this->io = $io; } protected function initialize() { parent::initialize(); $this->scanDirectory($this->lookup); } private function scanDirectory($path) { $io = $this->io; foreach (new \RecursiveDirectoryIterator($path) as $file) { if (!$file->isFile()) { continue; } $package = $this->getComposerInformation($file); if (!$package) { if ($io->isVerbose()) { $io->write("File {$file->getBasename()} doesn't seem to hold a package"); } continue; } if ($io->isVerbose()) { $template = 'Found package %s (%s) in file %s'; $io->write(sprintf($template, $package->getName(), $package->getPrettyVersion(), $file->getBasename())); } $this->addPackage($package); } } private function getComposerInformation(\SplFileInfo $file) { $zip = new \ZipArchive(); $zip->open($file->getPathname()); if (0 == $zip->numFiles) { return false; } $foundFileIndex = $zip->locateName('composer.json', \ZipArchive::FL_NODIR); if (false === $foundFileIndex) { return false; } $configurationFileName = $zip->getNameIndex($foundFileIndex); $composerFile = "zip://{$file->getPathname()}#$configurationFileName"; $json = file_get_contents($composerFile); $package = JsonFile::parseJson($json, $composerFile); $package['dist'] = array( 'type' => 'zip', 'url' => $file->getRealPath(), 'reference' => $file->getBasename(), 'shasum' => sha1_file($file->getRealPath()) ); $package = $this->loader->load($package); return $package; } } config = $config['package']; if (!is_numeric(key($this->config))) { $this->config = array($this->config); } } protected function initialize() { parent::initialize(); $loader = new ValidatingArrayLoader(new ArrayLoader, false); foreach ($this->config as $package) { try { $package = $loader->load($package); } catch (\Exception $e) { throw new InvalidRepositoryException('A repository of type "package" contains an invalid package definition: '.$e->getMessage()."\n\nInvalid package definition:\n".json_encode($package)); } $this->addPackage($package); } } } scripts = $scripts; } public function getScripts() { return $this->scripts; } public function setRepositories($repositories) { $this->repositories = $repositories; } public function getRepositories() { return $this->repositories; } public function setLicense(array $license) { $this->license = $license; } public function getLicense() { return $this->license; } public function setKeywords(array $keywords) { $this->keywords = $keywords; } public function getKeywords() { return $this->keywords; } public function setAuthors(array $authors) { $this->authors = $authors; } public function getAuthors() { return $this->authors; } public function setDescription($description) { $this->description = $description; } public function getDescription() { return $this->description; } public function setHomepage($homepage) { $this->homepage = $homepage; } public function getHomepage() { return $this->homepage; } public function setSupport(array $support) { $this->support = $support; } public function getSupport() { return $this->support; } } 'bin', 'type', 'extra', 'installationSource' => 'installation-source', 'autoload', 'notificationUrl' => 'notification-url', 'includePaths' => 'include-path', ); $data = array(); $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(); $data['source']['reference'] = $package->getSourceReference(); } if ($package->getDistType()) { $data['dist']['type'] = $package->getDistType(); $data['dist']['url'] = $package->getDistUrl(); $data['dist']['reference'] = $package->getDistReference(); $data['dist']['shasum'] = $package->getDistSha1Checksum(); } if ($package->getArchiveExcludes()) { $data['archive']['exclude'] = $package->getArchiveExcludes(); } 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()) { $data['time'] = $package->getReleaseDate()->format('Y-m-d H:i:s'); } $data = $this->dumpValues($package, $keys, $data); if ($package instanceof CompletePackageInterface) { $keys = array( 'scripts', 'license', 'authors', 'description', 'homepage', 'keywords', 'repositories', 'support', ); $data = $this->dumpValues($package, $keys, $data); if (isset($data['keywords']) && is_array($data['keywords'])) { sort($data['keywords']); } } if ($package instanceof RootPackageInterface) { $minimumStability = $package->getMinimumStability(); if ($minimumStability) { $data['minimum-stability'] = $minimumStability; } } return $data; } private function dumpValues(PackageInterface $package, array $keys, array $data) { 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; } } errors = $errors; $this->warnings = $warnings; $this->data = $data; parent::__construct("Invalid package information: \n".implode("\n", array_merge($errors, $warnings))); } public function getData() { return $this->data; } public function getErrors() { return $this->errors; } public function getWarnings() { return $this->warnings; } } loader = $loader; } public function load($json) { 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); } return $this->loader->load($config); } } versionParser = $parser; } public function load(array $config, $class = 'Composer\Package\CompletePackage') { if (!isset($config['name'])) { throw new \UnexpectedValueException('Unknown package has no name defined ('.json_encode($config).').'); } if (!isset($config['version'])) { throw new \UnexpectedValueException('Package '.$config['name'].' has no version defined.'); } if (isset($config['version_normalized'])) { $version = $config['version_normalized']; } else { $version = $this->versionParser->normalize($config['version']); } $package = new $class($config['name'], $version, $config['version']); $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'])) { throw new \UnexpectedValueException('Package '.$config['name'].'\'s bin key should be an array, '.gettype($config['bin']).' given.'); } 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['source'])) { if (!isset($config['source']['type']) || !isset($config['source']['url']) || !isset($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($config['source']['reference']); } if (isset($config['dist'])) { if (!isset($config['dist']['type']) || !isset($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']) ? $config['dist']['reference'] : null); $package->setDistSha1Checksum(isset($config['dist']['shasum']) ? $config['dist']['shasum'] : null); } foreach (Package\BasePackage::$supportedLinkTypes as $type => $opts) { if (isset($config[$type])) { $method = 'set'.ucfirst($opts['method']); $package->{$method}( $this->versionParser->parseLinks( $package->getName(), $package->getPrettyVersion(), $opts['description'], $config[$type] ) ); } } 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['include-path'])) { $package->setIncludePaths($config['include-path']); } if (!empty($config['time'])) { $time = ctype_digit($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 (!empty($config['archive']['exclude'])) { $package->setArchiveExcludes($config['archive']['exclude']); } if ($package instanceof Package\CompletePackageInterface) { if (isset($config['scripts']) && is_array($config['scripts'])) { foreach ($config['scripts'] as $event => $listeners) { $config['scripts'][$event] = (array) $listeners; } $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($config['keywords']); } if (!empty($config['license'])) { $package->setLicense(is_array($config['license']) ? $config['license'] : array($config['license'])); } if (!empty($config['authors']) && is_array($config['authors'])) { $package->setAuthors($config['authors']); } if (isset($config['support'])) { $package->setSupport($config['support']); } } if ($aliasNormalized = $this->getBranchAlias($config)) { if ($package instanceof RootPackageInterface) { $package = new RootAliasPackage($package, $aliasNormalized, preg_replace('{(\.9{7})+}', '.x', $aliasNormalized)); } else { $package = new AliasPackage($package, $aliasNormalized, preg_replace('{(\.9{7})+}', '.x', $aliasNormalized)); } } return $package; } public function getBranchAlias(array $config) { if ('dev-' !== substr($config['version'], 0, 4) || !isset($config['extra']['branch-alias']) || !is_array($config['extra']['branch-alias']) ) { return; } foreach ($config['extra']['branch-alias'] as $sourceBranch => $targetBranch) { if ('-dev' !== substr($targetBranch, -4)) { continue; } $validatedTargetBranch = $this->versionParser->normalizeBranch(substr($targetBranch, 0, -4)); if ('-dev' !== substr($validatedTargetBranch, -4)) { continue; } if (strtolower($config['version']) !== strtolower($sourceBranch)) { continue; } return $validatedTargetBranch; } } } loader = $loader; $this->versionParser = $parser ?: new VersionParser(); $this->strictName = $strictName; } public function load(array $config, $class = 'Composer\Package\CompletePackage') { $this->errors = array(); $this->warnings = array(); $this->config = $config; if ($this->strictName) { $this->validateRegex('name', '[A-Za-z0-9][A-Za-z0-9_.-]*/[A-Za-z0-9][A-Za-z0-9_.-]*', true); } else { $this->validateString('name', true); } if (!empty($this->config['version'])) { try { $this->versionParser->normalize($this->config['version']); } catch (\Exception $e) { unset($this->config['version']); $this->errors[] = 'version : invalid value ('.$this->config['version'].'): '.$e->getMessage(); } } $this->validateRegex('type', '[A-Za-z0-9-]+'); $this->validateString('target-dir'); $this->validateArray('extra'); $this->validateFlatArray('bin'); $this->validateArray('scripts'); $this->validateString('description'); $this->validateUrl('homepage'); $this->validateFlatArray('keywords', '[A-Za-z0-9 ._-]+'); if (isset($this->config['license'])) { if (is_string($this->config['license'])) { $this->validateRegex('license', '[A-Za-z0-9+. ()-]+'); } else { $this->validateFlatArray('license', '[A-Za-z0-9+. ()-]+'); } } $this->validateString('time'); if (!empty($this->config['time'])) { try { $date = 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 ($this->validateArray('authors') && !empty($this->config['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 (array('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']) && !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 (empty($this->config['authors'][$key])) { unset($this->config['authors'][$key]); } } if (empty($this->config['authors'])) { unset($this->config['authors']); } } if ($this->validateArray('support') && !empty($this->config['support'])) { foreach (array('issues', 'forum', 'wiki', 'source', 'email', 'irc') 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'], array('irc'))) { $this->warnings[] = 'support.irc : invalid value ('.$this->config['support']['irc'].'), must be a irc:/// URL'; unset($this->config['support']['irc']); } foreach (array('issues', 'forum', 'wiki', 'source') 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']); } } foreach (array_keys(BasePackage::$supportedLinkTypes) as $linkType) { if ($this->validateArray($linkType) && isset($this->config[$linkType])) { foreach ($this->config[$linkType] as $package => $constraint) { if (!preg_match('{^[A-Za-z0-9_./-]+$}', $package)) { $this->warnings[] = $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 { $this->versionParser->parseConstraints($constraint); } catch (\Exception $e) { $this->errors[] = $linkType.'.'.$package.' : invalid version constraint ('.$e->getMessage().')'; unset($this->config[$linkType][$package]); } } } } } if ($this->validateArray('suggest') && !empty($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') && !empty($this->config['minimum-stability'])) { if (!isset(BasePackage::$stabilities[$this->config['minimum-stability']])) { $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') && !empty($this->config['autoload'])) { $types = array('psr-0', 'classmap', 'files'); 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]); } } } $this->validateFlatArray('include-path'); 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 ('-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]); } } } } if ($this->errors) { throw new InvalidPackageException($this->errors, $this->warnings, $config); } $package = $this->loader->load($this->config, $class); $this->config = null; return $package; } public function getWarnings() { return $this->warnings; } public function getErrors() { return $this->errors; } private function validateRegex($property, $regex, $mandatory = false) { if (!$this->validateString($property, $mandatory)) { return false; } if (!preg_match('{^'.$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($property, $mandatory = false) { 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($property, $mandatory = false) { 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($property, $regex = null, $mandatory = false) { 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_match('{^'.$regex.'$}u', $value)) { $this->warnings[] = $property.'.'.$key.' : invalid value ('.$value.'), must match '.$regex; unset($this->config[$property][$key]); $pass = false; } } return $pass; } private function validateUrl($property, $mandatory = false) { 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 = array('http', 'https')) { 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; } } manager = $manager; $this->config = $config; $this->process = $process ?: new ProcessExecutor(); parent::__construct($parser); } public function load(array $config, $class = 'Composer\Package\RootPackage') { if (!isset($config['name'])) { $config['name'] = '__root__'; } if (!isset($config['version'])) { if (getenv('COMPOSER_ROOT_VERSION')) { $version = getenv('COMPOSER_ROOT_VERSION'); } else { $version = $this->guessVersion($config); } if (!$version) { $version = '1.0.0'; } $config['version'] = $version; } else { $version = $config['version']; } $realPackage = $package = parent::load($config, $class); if ($realPackage instanceof AliasPackage) { $realPackage = $package->getAliasOf(); } $aliases = array(); $stabilityFlags = array(); $references = array(); foreach (array('require', 'require-dev') as $linkType) { if (isset($config[$linkType])) { $linkInfo = BasePackage::$supportedLinkTypes[$linkType]; $method = 'get'.ucfirst($linkInfo['method']); $links = array(); foreach ($realPackage->$method() as $link) { $links[$link->getTarget()] = $link->getConstraint()->getPrettyString(); } $aliases = $this->extractAliases($links, $aliases); $stabilityFlags = $this->extractStabilityFlags($links, $stabilityFlags); $references = $this->extractReferences($links, $references); } } $realPackage->setAliases($aliases); $realPackage->setStabilityFlags($stabilityFlags); $realPackage->setReferences($references); if (isset($config['minimum-stability'])) { $realPackage->setMinimumStability(VersionParser::normalizeStability($config['minimum-stability'])); } if (isset($config['prefer-stable'])) { $realPackage->setPreferStable((bool) $config['prefer-stable']); } $repos = Factory::createDefaultRepositories(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) { foreach ($requires as $reqName => $reqVersion) { if (preg_match('{^([^,\s#]+)(?:#[^ ]+)? +as +([^,\s]+)$}', $reqVersion, $match)) { $aliases[] = array( 'package' => strtolower($reqName), 'version' => $this->versionParser->normalize($match[1], $reqVersion), 'alias' => $match[2], 'alias_normalized' => $this->versionParser->normalize($match[2], $reqVersion), ); } } return $aliases; } private function extractStabilityFlags(array $requires, array $stabilityFlags) { $stabilities = BasePackage::$stabilities; foreach ($requires as $reqName => $reqVersion) { if (preg_match('{^[^,\s]*?@('.implode('|', array_keys($stabilities)).')$}i', $reqVersion, $match)) { $name = strtolower($reqName); $stability = $stabilities[VersionParser::normalizeStability($match[1])]; if (isset($stabilityFlags[$name]) && $stabilityFlags[$name] > $stability) { continue; } $stabilityFlags[$name] = $stability; continue; } $reqVersion = preg_replace('{^([^,\s@]+) as .+$}', '$1', $reqVersion); if (preg_match('{^[^,\s@]+$}', $reqVersion) && 'stable' !== ($stabilityName = VersionParser::parseStability($reqVersion))) { $name = strtolower($reqName); $stability = $stabilities[$stabilityName]; if (isset($stabilityFlags[$name]) && $stabilityFlags[$name] > $stability) { continue; } $stabilityFlags[$name] = $stability; } } return $stabilityFlags; } private function extractReferences(array $requires, array $references) { foreach ($requires as $reqName => $reqVersion) { $reqVersion = preg_replace('{^([^,\s@]+) as .+$}', '$1', $reqVersion); if (preg_match('{^[^,\s@]+?#([a-f0-9]+)$}', $reqVersion, $match) && 'dev' === ($stabilityName = VersionParser::parseStability($reqVersion))) { $name = strtolower($reqName); $references[$name] = $match[1]; } } return $references; } private function guessVersion(array $config) { if (function_exists('proc_open')) { $version = $this->guessGitVersion($config); if (null !== $version) { return $version; } return $this->guessHgVersion($config); } } private function guessGitVersion(array $config) { if (0 === $this->process->execute('git branch --no-color --no-abbrev -v', $output)) { $branches = array(); $isFeatureBranch = false; $version = null; foreach ($this->process->splitLines($output) as $branch) { if ($branch && preg_match('{^(?:\* ) *(\S+|\(no branch\)) *([a-f0-9]+) .*$}', $branch, $match)) { if ($match[1] === '(no branch)') { $version = 'dev-'.$match[2]; $isFeatureBranch = true; } else { $version = $this->versionParser->normalizeBranch($match[1]); $isFeatureBranch = 0 === strpos($version, 'dev-'); if ('9999999-dev' === $version) { $version = 'dev-'.$match[1]; } } } if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) { if (preg_match('{^(?:\* )? *(\S+) *([a-f0-9]+) .*$}', $branch, $match)) { $branches[] = $match[1]; } } } if (!$isFeatureBranch) { return $version; } $version = $this->guessFeatureVersion($config, $version, $branches, 'git rev-list %candidate%..%branch%'); return $version; } } private function guessHgVersion(array $config) { if (0 === $this->process->execute('hg branch', $output)) { $branch = trim($output); $version = $this->versionParser->normalizeBranch($branch); $isFeatureBranch = 0 === strpos($version, 'dev-'); if ('9999999-dev' === $version) { $version = 'dev-'.$branch; } if (!$isFeatureBranch) { return $version; } $config = array('url' => getcwd()); $driver = new HgDriver($config, new NullIO(), $this->config, $this->process); $branches = array_keys($driver->getBranches()); $version = $this->guessFeatureVersion($config, $version, $branches, 'hg log -r "not ancestors(\'%candidate%\') and ancestors(\'%branch%\')" --template "{node}\\n"'); return $version; } } private function guessFeatureVersion(array $config, $version, array $branches, $scmCmdline) { if ((isset($config['extra']['branch-alias']) && !isset($config['extra']['branch-alias'][$version])) || strpos(json_encode($config), '"self.version"') ) { $branch = preg_replace('{^dev-}', '', $version); $length = PHP_INT_MAX; foreach ($branches as $candidate) { if ($candidate === $branch || !preg_match('{^(master|trunk|default|develop|\d+\..+)$}', $candidate, $match)) { continue; } $cmdLine = str_replace(array('%candidate%', '%branch%'), array($candidate, $branch), $scmCmdline); if (0 !== $this->process->execute($cmdLine, $output)) { continue; } if (strlen($output) < $length) { $length = strlen($output); $version = $this->versionParser->normalizeBranch($candidate); if ('9999999-dev' === $version) { $version = 'dev-'.$match[1]; } } } } return $version; } } lockFile = $lockFile; $this->repositoryManager = $repositoryManager; $this->installationManager = $installationManager; $this->hash = $hash; $this->loader = new ArrayLoader(); $this->dumper = new ArrayDumper(); } public function isLocked() { if (!$this->lockFile->exists()) { return false; } $data = $this->getLockData(); return isset($data['packages']); } public function isFresh() { $lock = $this->lockFile->read(); return $this->hash === $lock['hash']; } public function getLockedRepository($withDevReqs = false) { $lockData = $this->getLockData(); $packages = new ArrayRepository(); $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 without --dev or run update to install those packages.'); } } if (empty($lockedPackages)) { return $packages; } if (isset($lockedPackages[0]['name'])) { foreach ($lockedPackages as $info) { $packages->addPackage($this->loader->load($info)); } return $packages; } throw new \RuntimeException('Your composer.lock was created before 2012-09-15, and is not supported anymore. Run "composer update" to generate a new one.'); } public function getPlatformRequirements($withDevReqs = false) { $lockData = $this->getLockData(); $versionParser = new VersionParser(); $requirements = array(); if (!empty($lockData['platform'])) { $requirements = $versionParser->parseLinks( '__ROOT__', '1.0.0', 'requires', isset($lockData['platform']) ? $lockData['platform'] : array() ); } if ($withDevReqs && !empty($lockData['platform-dev'])) { $devRequirements = $versionParser->parseLinks( '__ROOT__', '1.0.0', 'requires', isset($lockData['platform-dev']) ? $lockData['platform-dev'] : array() ); $requirements = array_merge($requirements, $devRequirements); } return $requirements; } public function getMinimumStability() { $lockData = $this->getLockData(); return isset($lockData['minimum-stability']) ? $lockData['minimum-stability'] : 'stable'; } public function getStabilityFlags() { $lockData = $this->getLockData(); return isset($lockData['stability-flags']) ? $lockData['stability-flags'] : array(); } public function getAliases() { $lockData = $this->getLockData(); return isset($lockData['aliases']) ? $lockData['aliases'] : array(); } public function getLockData() { 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, $devPackages, array $platformReqs, $platformDevReqs, array $aliases, $minimumStability, array $stabilityFlags) { $lock = array( '_readme' => array('This file locks the dependencies of your project to a known state', 'Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file'), 'hash' => $this->hash, 'packages' => null, 'packages-dev' => null, 'aliases' => array(), 'minimum-stability' => $minimumStability, 'stability-flags' => $stabilityFlags, ); foreach ($aliases as $package => $versions) { foreach ($versions as $version => $alias) { $lock['aliases'][] = array( 'alias' => $alias['alias'], 'alias_normalized' => $alias['alias_normalized'], 'version' => $version, 'package' => $package, ); } } $lock['packages'] = $this->lockPackages($packages); if (null !== $devPackages) { $lock['packages-dev'] = $this->lockPackages($devPackages); } if (empty($lock['packages']) && empty($lock['packages-dev'])) { if ($this->lockFile->exists()) { unlink($this->lockFile->getPath()); } return false; } $lock['platform'] = $platformReqs; $lock['platform-dev'] = $platformDevReqs; if (!$this->isLocked() || $lock !== $this->getLockData()) { $this->lockFile->write($lock); $this->lockDataCache = null; return true; } return false; } private function lockPackages(array $packages) { $locked = array(); 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 = isset($spec['time']) ? $spec['time'] : null; unset($spec['time']); if ($package->isDev()) { $time = $this->getPackageTime($package) ?: $time; } if (null !== $time) { $spec['time'] = $time; } unset($spec['installation-source']); $locked[] = $spec; } usort($locked, 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) { if (!function_exists('proc_open')) { return null; } $path = $this->installationManager->getInstallPath($package); $sourceType = $package->getSourceType(); $datetime = null; if ($path && in_array($sourceType, array('git', 'hg'))) { $sourceRef = $package->getSourceReference() ?: $package->getDistReference(); $process = new ProcessExecutor(); switch ($sourceType) { case 'git': if (0 === $process->execute('git log -n1 --pretty=%ct '.escapeshellarg($sourceRef), $output, $path) && preg_match('{^\s*\d+\s*$}', $output)) { $datetime = new \DateTime('@'.trim($output), new \DateTimeZone('UTC')); } break; case 'hg': if (0 === $process->execute('hg log --template "{date|hgdate}" -r '.escapeshellarg($sourceRef), $output, $path) && preg_match('{^\s*(\d+)\s*}', $output, $match)) { $datetime = new \DateTime('@'.$match[1], new \DateTimeZone('UTC')); } break; } } return $datetime ? $datetime->format('Y-m-d H:i:s') : null; } } array('description' => 'requires', 'method' => 'requires'), 'conflict' => array('description' => 'conflicts', 'method' => 'conflicts'), 'provide' => array('description' => 'provides', 'method' => 'provides'), 'replace' => array('description' => 'replaces', 'method' => 'replaces'), 'require-dev' => array('description' => 'requires (for development)', 'method' => 'devRequires'), ); const STABILITY_STABLE = 0; const STABILITY_RC = 5; const STABILITY_BETA = 10; const STABILITY_ALPHA = 15; const STABILITY_DEV = 20; public static $stabilities = array( 'stable' => self::STABILITY_STABLE, 'RC' => self::STABILITY_RC, 'beta' => self::STABILITY_BETA, 'alpha' => self::STABILITY_ALPHA, 'dev' => self::STABILITY_DEV, ); protected $name; protected $prettyName; protected $repository; protected $id; public function __construct($name) { $this->prettyName = $name; $this->name = strtolower($name); $this->id = -1; } public function getName() { return $this->name; } public function getPrettyName() { return $this->prettyName; } public function getNames() { $names = array( $this->getName() => true, ); 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($id) { $this->id = $id; } public function getId() { return $this->id; } public function setRepository(RepositoryInterface $repository) { if ($this->repository && $repository !== $this->repository) { throw new \LogicException('A package can only be added to one repository'); } $this->repository = $repository; } public function getRepository() { return $this->repository; } public function isPlatform() { return $this->getRepository() instanceof PlatformRepository; } public function getUniqueName() { return $this->getName().'-'.$this->getVersion(); } public function equals(PackageInterface $package) { $self = $this; if ($this instanceof AliasPackage) { $self = $this->getAliasOf(); } if ($package instanceof AliasPackage) { $package = $package->getAliasOf(); } return $package === $self; } public function __toString() { return $this->getUniqueName(); } public function getPrettyString() { return $this->getPrettyName().' '.$this->getPrettyVersion(); } public function __clone() { $this->repository = null; $this->id = -1; } } isDev() || !in_array($package->getSourceType(), array('hg', 'git'))) { return $package->getPrettyVersion(); } if ($truncate && strlen($package->getSourceReference()) === 40) { return $package->getPrettyVersion() . ' ' . substr($package->getSourceReference(), 0, 7); } return $package->getPrettyVersion() . ' ' . $package->getSourceReference(); } public function normalize($version, $fullVersion = null) { $version = trim($version); if (null === $fullVersion) { $fullVersion = $version; } if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $version, $match)) { $version = $match[1]; } if (preg_match('{^(?:dev-)?(?:master|trunk|default)$}i', $version)) { return '9999999-dev'; } if ('dev-' === strtolower(substr($version, 0, 4))) { return 'dev-'.substr($version, 4); } if (preg_match('{^v?(\d{1,3})(\.\d+)?(\.\d+)?(\.\d+)?'.self::$modifierRegex.'$}i', $version, $matches)) { $version = $matches[1] .(!empty($matches[2]) ? $matches[2] : '.0') .(!empty($matches[3]) ? $matches[3] : '.0') .(!empty($matches[4]) ? $matches[4] : '.0'); $index = 5; } elseif (preg_match('{^v?(\d{4}(?:[.:-]?\d{2}){1,6}(?:[.:-]?\d{1,3})?)'.self::$modifierRegex.'$}i', $version, $matches)) { $version = preg_replace('{\D}', '-', $matches[1]); $index = 2; } if (isset($index)) { if (!empty($matches[$index])) { if ('stable' === $matches[$index]) { return $version; } $version .= '-' . $this->expandStability($matches[$index]) . (!empty($matches[$index+1]) ? $matches[$index+1] : ''); } if (!empty($matches[$index+2])) { $version .= '-dev'; } return $version; } if (preg_match('{(.*?)[.-]?dev$}i', $version, $match)) { try { return $this->normalizeBranch($match[1]); } catch (\Exception $e) {} } $extraMessage = ''; if (preg_match('{ +as +'.preg_quote($version).'$}', $fullVersion)) { $extraMessage = ' in "'.$fullVersion.'", the alias must be an exact version'; } elseif (preg_match('{^'.preg_quote($version).' +as +}', $fullVersion)) { $extraMessage = ' in "'.$fullVersion.'", the alias source must be an exact version, if it is a branch name you should prefix it with dev-'; } throw new \UnexpectedValueException('Invalid version string "'.$version.'"'.$extraMessage); } public function normalizeBranch($name) { $name = trim($name); if (in_array($name, array('master', 'trunk', 'default'))) { return $this->normalize($name); } if (preg_match('#^v?(\d+)(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?$#i', $name, $matches)) { $version = ''; for ($i = 1; $i < 5; $i++) { $version .= isset($matches[$i]) ? str_replace('*', 'x', $matches[$i]) : '.x'; } return str_replace('x', '9999999', $version).'-dev'; } return 'dev-'.$name; } public function parseLinks($source, $sourceVersion, $description, $links) { $res = array(); foreach ($links as $target => $constraint) { if ('self.version' === $constraint) { $parsedConstraint = $this->parseConstraints($sourceVersion); } else { $parsedConstraint = $this->parseConstraints($constraint); } $res[strtolower($target)] = new Link($source, $target, $parsedConstraint, $description, $constraint); } return $res; } public function parseConstraints($constraints) { $prettyConstraint = $constraints; if (preg_match('{^([^,\s]*?)@('.implode('|', array_keys(BasePackage::$stabilities)).')$}i', $constraints, $match)) { $constraints = empty($match[1]) ? '*' : $match[1]; } if (preg_match('{^(dev-[^,\s@]+?|[^,\s@]+?\.x-dev)#.+$}i', $constraints, $match)) { $constraints = $match[1]; } $orConstraints = preg_split('{\s*\|\s*}', trim($constraints)); $orGroups = array(); foreach ($orConstraints as $constraints) { $andConstraints = preg_split('{\s*,\s*}', $constraints); if (count($andConstraints) > 1) { $constraintObjects = array(); foreach ($andConstraints as $constraint) { $constraintObjects = array_merge($constraintObjects, $this->parseConstraint($constraint)); } } else { $constraintObjects = $this->parseConstraint($andConstraints[0]); } if (1 === count($constraintObjects)) { $constraint = $constraintObjects[0]; } else { $constraint = new MultiConstraint($constraintObjects); } $orGroups[] = $constraint; } if (1 === count($orGroups)) { $constraint = $orGroups[0]; } else { $constraint = new MultiConstraint($orGroups, false); } $constraint->setPrettyString($prettyConstraint); return $constraint; } private function parseConstraint($constraint) { if (preg_match('{^([^,\s]+?)@('.implode('|', array_keys(BasePackage::$stabilities)).')$}i', $constraint, $match)) { $constraint = $match[1]; if ($match[2] !== 'stable') { $stabilityModifier = $match[2]; } } if (preg_match('{^[x*](\.[x*])*$}i', $constraint)) { return array(new EmptyConstraint); } if (preg_match('{^~(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?'.self::$modifierRegex.'?$}i', $constraint, $matches)) { if (isset($matches[4]) && '' !== $matches[4]) { $highVersion = $matches[1] . '.' . $matches[2] . '.' . ($matches[3] + 1) . '.0-dev'; $lowVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3]. '.' . $matches[4]; } elseif (isset($matches[3]) && '' !== $matches[3]) { $highVersion = $matches[1] . '.' . ($matches[2] + 1) . '.0.0-dev'; $lowVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3]. '.0'; } else { $highVersion = ($matches[1] + 1) . '.0.0.0-dev'; if (isset($matches[2]) && '' !== $matches[2]) { $lowVersion = $matches[1] . '.' . $matches[2] . '.0.0'; } else { $lowVersion = $matches[1] . '.0.0.0'; } } if (!empty($matches[5])) { $lowVersion .= '-' . $this->expandStability($matches[5]) . (!empty($matches[6]) ? $matches[6] : ''); } if (!empty($matches[7])) { $lowVersion .= '-dev'; } return array( new VersionConstraint('>=', $lowVersion), new VersionConstraint('<', $highVersion), ); } if (preg_match('{^(\d+)(?:\.(\d+))?(?:\.(\d+))?\.[x*]$}', $constraint, $matches)) { if (isset($matches[3])) { $highVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3] . '.9999999'; if ($matches[3] === '0') { $lowVersion = $matches[1] . '.' . ($matches[2] - 1) . '.9999999.9999999'; } else { $lowVersion = $matches[1] . '.' . $matches[2] . '.' . ($matches[3] - 1). '.9999999'; } } elseif (isset($matches[2])) { $highVersion = $matches[1] . '.' . $matches[2] . '.9999999.9999999'; if ($matches[2] === '0') { $lowVersion = ($matches[1] - 1) . '.9999999.9999999.9999999'; } else { $lowVersion = $matches[1] . '.' . ($matches[2] - 1) . '.9999999.9999999'; } } else { $highVersion = $matches[1] . '.9999999.9999999.9999999'; if ($matches[1] === '0') { return array(new VersionConstraint('<', $highVersion)); } else { $lowVersion = ($matches[1] - 1) . '.9999999.9999999.9999999'; } } return array( new VersionConstraint('>', $lowVersion), new VersionConstraint('<', $highVersion), ); } if (preg_match('{^(<>|!=|>=?|<=?|==?)?\s*(.*)}', $constraint, $matches)) { try { $version = $this->normalize($matches[2]); if (!empty($stabilityModifier) && $this->parseStability($version) === 'stable') { $version .= '-' . $stabilityModifier; } elseif ('<' === $matches[1]) { if (!preg_match('/-stable$/', strtolower($matches[2]))) { $version .= '-dev'; } } return array(new VersionConstraint($matches[1] ?: '=', $version)); } catch (\Exception $e) { } } $message = 'Could not parse version constraint '.$constraint; if (isset($e)) { $message .= ': '.$e->getMessage(); } throw new \UnexpectedValueException($message); } private function expandStability($stability) { $stability = strtolower($stability); switch ($stability) { case 'a': return 'alpha'; case 'b': return 'beta'; case 'p': case 'pl': return 'patch'; case 'rc': return 'RC'; default: return $stability; } } public function parseNameVersionPairs(array $pairs) { $pairs = array_values($pairs); $result = array(); for ($i = 0, $count = count($pairs); $i < $count; $i++) { $pair = preg_replace('{^([^=: ]+)[=: ](.*)$}', '$1 $2', trim($pairs[$i])); if (false === strpos($pair, ' ') && isset($pairs[$i+1]) && false === strpos($pairs[$i+1], '/')) { $pair .= ' '.$pairs[$i+1]; $i++; } if (strpos($pair, ' ')) { list($name, $version) = explode(" ", $pair, 2); $result[] = array('name' => $name, 'version' => $version); } else { $result[] = array('name' => $pair); } } return $result; } } minimumStability = $minimumStability; } public function getMinimumStability() { return $this->minimumStability; } public function setStabilityFlags(array $stabilityFlags) { $this->stabilityFlags = $stabilityFlags; } public function getStabilityFlags() { return $this->stabilityFlags; } public function setPreferStable($preferStable) { $this->preferStable = $preferStable; } public function getPreferStable() { return $this->preferStable; } public function setReferences(array $references) { $this->references = $references; } public function getReferences() { return $this->references; } public function setAliases(array $aliases) { $this->aliases = $aliases; } public function getAliases() { return $this->aliases; } } excludePatterns = $this->generatePatterns($excludeRules); } } \Phar::ZIP, 'tar' => \Phar::TAR, ); public function archive($sources, $target, $format, array $excludes = array()) { $sources = realpath($sources); if (file_exists($target)) { unlink($target); } try { $phar = new \PharData($target, null, null, static::$formats[$format]); $files = new ArchivableFilesFinder($sources, $excludes); $phar->buildFromIterator($files, $sources); 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($format, $sourceType) { return isset(static::$formats[$format]); } } sourcePath = $sourcePath; $this->excludePatterns = array(); } public function filter($relativePath, $exclude) { foreach ($this->excludePatterns as $patternData) { list($pattern, $negate, $stripLeadingSlash) = $patternData; if ($stripLeadingSlash) { $path = substr($relativePath, 1); } else { $path = $relativePath; } if (preg_match($pattern, $path)) { $exclude = !$negate; } } return $exclude; } protected function parseLines(array $lines, $lineParser) { return array_filter( array_map( function ($line) use ($lineParser) { $line = trim($line); $commentHash = strpos($line, '#'); if ($commentHash !== false) { $line = substr($line, 0, $commentHash); } if ($line) { return call_user_func($lineParser, $line); } return null; }, $lines), function ($pattern) { return $pattern !== null; } ); } protected function generatePatterns($rules) { $patterns = array(); foreach ($rules as $rule) { $patterns[] = $this->generatePattern($rule); } return $patterns; } protected function generatePattern($rule) { $negate = false; $pattern = '#'; if (strlen($rule) && $rule[0] === '!') { $negate = true; $rule = substr($rule, 1); } if (strlen($rule) && $rule[0] === '/') { $pattern .= '^/'; $rule = substr($rule, 1); } elseif (false === strpos($rule, '/') || strlen($rule) - 1 === strpos($rule, '/')) { $pattern .= '/'; } $pattern .= substr(Finder\Glob::toRegex($rule), 2, -2); return array($pattern . '#', $negate, false); } } excludePatterns = $this->parseLines( file($sourcePath.'/.gitignore'), array($this, 'parseGitIgnoreLine') ); } if (file_exists($sourcePath.'/.gitattributes')) { $this->excludePatterns = array_merge( $this->excludePatterns, $this->parseLines( file($sourcePath.'/.gitattributes'), array($this, 'parseGitAttributesLine') )); } } public function parseGitIgnoreLine($line) { return $this->generatePattern($line); } public function parseGitAttributesLine($line) { $parts = preg_split('#\s+#', $line); if (count($parts) != 2) { return null; } if ($parts[1] === 'export-ignore') { return $this->generatePattern($parts[0]); } } } normalizePath($sources); $filters = array( new HgExcludeFilter($sources), new GitExcludeFilter($sources), new ComposerExcludeFilter($sources, $excludes), ); $this->finder = new Finder\Finder(); $this->finder ->in($sources) ->filter(function (\SplFileInfo $file) use ($sources, $filters, $fs) { $relativePath = preg_replace( '#^'.preg_quote($sources, '#').'#', '', $fs->normalizePath($file->getRealPath()) ); $exclude = false; foreach ($filters as $filter) { $exclude = $filter->filter($relativePath, $exclude); } return !$exclude; }) ->ignoreVCS(true) ->ignoreDotFiles(false); parent::__construct($this->finder->getIterator()); } public function accept() { return !$this->getInnerIterator()->current()->isDir(); } } downloadManager = $downloadManager; } public function addArchiver(ArchiverInterface $archiver) { $this->archivers[] = $archiver; } public function setOverwriteFiles($overwriteFiles) { $this->overwriteFiles = $overwriteFiles; return $this; } public function getPackageFilename(PackageInterface $package) { $nameParts = array(preg_replace('#[^a-z0-9-_.]#i', '-', $package->getName())); if (preg_match('{^[a-f0-9]{40}$}', $package->getDistReference())) { $nameParts = array_merge($nameParts, array($package->getDistReference(), $package->getDistType())); } else { $nameParts = array_merge($nameParts, array($package->getPrettyVersion(), $package->getDistReference())); } if ($package->getSourceReference()) { $nameParts[] = substr(sha1($package->getSourceReference()), 0, 6); } return implode('-', array_filter($nameParts, function ($p) { return !empty($p); })); } public function archive(PackageInterface $package, $format, $targetDir) { 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(); $packageName = $this->getPackageFilename($package); $filesystem->ensureDirectoryExists($targetDir); $target = realpath($targetDir).'/'.$packageName.'.'.$format; $filesystem->ensureDirectoryExists(dirname($target)); if (!$this->overwriteFiles && file_exists($target)) { return $target; } if ($package instanceof RootPackage) { $sourcePath = realpath('.'); } else { $sourcePath = sys_get_temp_dir().'/composer_archiver/'.$packageName; $filesystem->ensureDirectoryExists($sourcePath); $this->downloadManager->download($package, $sourcePath, true); } return $usableArchiver->archive($sourcePath, $target, $format, $package->getArchiveExcludes()); } } patternMode = self::HG_IGNORE_REGEX; if (file_exists($sourcePath.'/.hgignore')) { $this->excludePatterns = $this->parseLines( file($sourcePath.'/.hgignore'), array($this, 'parseHgIgnoreLine') ); } } public function parseHgIgnoreLine($line) { if (preg_match('#^syntax\s*:\s*(glob|regexp)$#', $line, $matches)) { if ($matches[1] === 'glob') { $this->patternMode = self::HG_IGNORE_GLOB; } else { $this->patternMode = self::HG_IGNORE_REGEX; } return null; } if ($this->patternMode == self::HG_IGNORE_GLOB) { return $this->patternFromGlob($line); } else { return $this->patternFromRegex($line); } } protected function patternFromGlob($line) { $pattern = '#'.substr(Finder\Glob::toRegex($line), 2, -1).'#'; $pattern = str_replace('[^/]*', '.*', $pattern); return array($pattern, false, true); } public function patternFromRegex($line) { $pattern = '#'.preg_replace('/((?:\\\\\\\\)*)(\\\\?)#/', '\1\2\2\\#', $line).'#'; return array($pattern, false, true); } } aliasOf->getAliases(); } public function getMinimumStability() { return $this->aliasOf->getMinimumStability(); } public function getStabilityFlags() { return $this->aliasOf->getStabilityFlags(); } public function getReferences() { return $this->aliasOf->getReferences(); } public function getPreferStable() { return $this->aliasOf->getPreferStable(); } public function setRequires(array $require) { return $this->aliasOf->setRequires($require); } public function setDevRequires(array $devRequire) { return $this->aliasOf->setDevRequires($devRequire); } public function __clone() { parent::__clone(); $this->aliasOf = clone $this->aliasOf; } } version = $version; $this->prettyVersion = $prettyVersion; $this->stability = VersionParser::parseStability($version); $this->dev = $this->stability === 'dev'; } public function isDev() { return $this->dev; } public function setType($type) { $this->type = $type; } public function getType() { return $this->type ?: 'library'; } public function getStability() { return $this->stability; } public function setTargetDir($targetDir) { $this->targetDir = $targetDir; } public function getTargetDir() { if (null === $this->targetDir) { return; } return ltrim(preg_replace('{ (?:^|[\\\\/]+) \.\.? (?:[\\\\/]+|$) (?:\.\.? (?:[\\\\/]+|$) )*}x', '/', $this->targetDir), '/'); } public function setExtra(array $extra) { $this->extra = $extra; } public function getExtra() { return $this->extra; } public function setBinaries(array $binaries) { $this->binaries = $binaries; } public function getBinaries() { return $this->binaries; } public function setInstallationSource($type) { $this->installationSource = $type; } public function getInstallationSource() { return $this->installationSource; } public function setSourceType($type) { $this->sourceType = $type; } public function getSourceType() { return $this->sourceType; } public function setSourceUrl($url) { $this->sourceUrl = $url; } public function getSourceUrl() { return $this->sourceUrl; } public function setSourceReference($reference) { $this->sourceReference = $reference; } public function getSourceReference() { return $this->sourceReference; } public function setDistType($type) { $this->distType = $type; } public function getDistType() { return $this->distType; } public function setDistUrl($url) { $this->distUrl = $url; } public function getDistUrl() { return $this->distUrl; } public function setDistReference($reference) { $this->distReference = $reference; } public function getDistReference() { return $this->distReference; } public function setDistSha1Checksum($sha1checksum) { $this->distSha1Checksum = $sha1checksum; } public function getDistSha1Checksum() { return $this->distSha1Checksum; } public function getVersion() { return $this->version; } public function getPrettyVersion() { return $this->prettyVersion; } public function setReleaseDate(\DateTime $releaseDate) { $this->releaseDate = $releaseDate; } public function getReleaseDate() { return $this->releaseDate; } public function setRequires(array $requires) { $this->requires = $requires; } public function getRequires() { return $this->requires; } public function setConflicts(array $conflicts) { $this->conflicts = $conflicts; } public function getConflicts() { return $this->conflicts; } public function setProvides(array $provides) { $this->provides = $provides; } public function getProvides() { return $this->provides; } public function setReplaces(array $replaces) { $this->replaces = $replaces; } public function getReplaces() { return $this->replaces; } public function setDevRequires(array $devRequires) { $this->devRequires = $devRequires; } public function getDevRequires() { return $this->devRequires; } public function setSuggests(array $suggests) { $this->suggests = $suggests; } public function getSuggests() { return $this->suggests; } public function setAutoload(array $autoload) { $this->autoload = $autoload; } public function getAutoload() { return $this->autoload; } public function setIncludePaths(array $includePaths) { $this->includePaths = $includePaths; } public function getIncludePaths() { return $this->includePaths; } public function setNotificationUrl($notificationUrl) { $this->notificationUrl = $notificationUrl; } public function getNotificationUrl() { return $this->notificationUrl; } public function setArchiveExcludes(array $excludes) { $this->archiveExcludes = $excludes; } public function getArchiveExcludes() { return $this->archiveExcludes; } } getName()); $this->version = $version; $this->prettyVersion = $prettyVersion; $this->aliasOf = $aliasOf; $this->stability = VersionParser::parseStability($version); $this->dev = $this->stability === 'dev'; foreach (array('requires', 'devRequires') as $type) { $links = $aliasOf->{'get'.ucfirst($type)}(); foreach ($links as $index => $link) { if ('self.version' === $link->getPrettyConstraint()) { $links[$index] = new Link($link->getSource(), $link->getTarget(), new VersionConstraint('=', $this->version), $type, $prettyVersion); } } $this->$type = $links; } foreach (array('conflicts', 'provides', 'replaces') as $type) { $links = $aliasOf->{'get'.ucfirst($type)}(); $newLinks = array(); foreach ($links as $link) { if ('self.version' === $link->getPrettyConstraint()) { $newLinks[] = new Link($link->getSource(), $link->getTarget(), new VersionConstraint('=', $this->version), $type, $prettyVersion); } } $this->$type = array_merge($links, $newLinks); } } public function getAliasOf() { return $this->aliasOf; } public function getVersion() { return $this->version; } public function getStability() { return $this->stability; } public function getPrettyVersion() { return $this->prettyVersion; } public function isDev() { return $this->dev; } public function getRequires() { return $this->requires; } public function getConflicts() { return $this->conflicts; } public function getProvides() { return $this->provides; } public function getReplaces() { return $this->replaces; } public function getDevRequires() { return $this->devRequires; } public function setRootPackageAlias($value) { return $this->rootPackageAlias = $value; } public function isRootPackageAlias() { return $this->rootPackageAlias; } public function getType() { return $this->aliasOf->getType(); } public function getTargetDir() { return $this->aliasOf->getTargetDir(); } public function getExtra() { return $this->aliasOf->getExtra(); } public function setInstallationSource($type) { $this->aliasOf->setInstallationSource($type); } public function getInstallationSource() { return $this->aliasOf->getInstallationSource(); } public function getSourceType() { return $this->aliasOf->getSourceType(); } public function getSourceUrl() { return $this->aliasOf->getSourceUrl(); } public function getSourceReference() { return $this->aliasOf->getSourceReference(); } public function setSourceReference($reference) { return $this->aliasOf->setSourceReference($reference); } public function getDistType() { return $this->aliasOf->getDistType(); } public function getDistUrl() { return $this->aliasOf->getDistUrl(); } public function getDistReference() { return $this->aliasOf->getDistReference(); } public function getDistSha1Checksum() { return $this->aliasOf->getDistSha1Checksum(); } public function getScripts() { return $this->aliasOf->getScripts(); } public function getLicense() { return $this->aliasOf->getLicense(); } public function getAutoload() { return $this->aliasOf->getAutoload(); } public function getIncludePaths() { return $this->aliasOf->getIncludePaths(); } public function getRepositories() { return $this->aliasOf->getRepositories(); } public function getReleaseDate() { return $this->aliasOf->getReleaseDate(); } public function getBinaries() { return $this->aliasOf->getBinaries(); } public function getKeywords() { return $this->aliasOf->getKeywords(); } public function getDescription() { return $this->aliasOf->getDescription(); } public function getHomepage() { return $this->aliasOf->getHomepage(); } public function getSuggests() { return $this->aliasOf->getSuggests(); } public function getAuthors() { return $this->aliasOf->getAuthors(); } public function getSupport() { return $this->aliasOf->getSupport(); } public function getNotificationUrl() { return $this->aliasOf->getNotificationUrl(); } public function getArchiveExcludes() { return $this->aliasOf->getArchiveExcludes(); } public function __toString() { return parent::__toString().' (alias of '.$this->aliasOf->getVersion().')'; } } source = strtolower($source); $this->target = strtolower($target); $this->constraint = $constraint; $this->description = $description; $this->prettyConstraint = $prettyConstraint; } public function getSource() { return $this->source; } public function getTarget() { return $this->target; } public function getConstraint() { return $this->constraint; } public function getPrettyConstraint() { 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() { return $this->source.' '.$this->description.' '.$this->target.' ('.$this->constraint.')'; } public function getPrettyString(PackageInterface $sourcePackage) { return $sourcePackage->getPrettyString().' '.$this->description.' '.$this->target.' '.$this->constraint->getPrettyString().''; } } prettyString = $prettyString; } public function getPrettyString() { if ($this->prettyString) { return $this->prettyString; } return $this->__toString(); } public function __toString() { return '[]'; } } constraints = $constraints; $this->conjunctive = $conjunctive; } public function matches(LinkConstraintInterface $provider) { if (false === $this->conjunctive) { foreach ($this->constraints as $constraint) { if ($constraint->matches($provider)) { return true; } } return false; } foreach ($this->constraints as $constraint) { if (!$constraint->matches($provider)) { return false; } } return true; } public function setPrettyString($prettyString) { $this->prettyString = $prettyString; } public function getPrettyString() { if ($this->prettyString) { return $this->prettyString; } return $this->__toString(); } public function __toString() { $constraints = array(); foreach ($this->constraints as $constraint) { $constraints[] = $constraint->__toString(); } return '['.implode($this->conjunctive ? ', ' : ' | ', $constraints).']'; } } ' === $operator) { $operator = '!='; } $this->operator = $operator; $this->version = $version; } public function versionCompare($a, $b, $operator) { if ('dev-' === substr($a, 0, 4) && 'dev-' === substr($b, 0, 4)) { return $operator == '==' && $a === $b; } return version_compare($a, $b, $operator); } public function matchSpecific(VersionConstraint $provider) { $noEqualOp = str_replace('=', '', $this->operator); $providerNoEqualOp = str_replace('=', '', $provider->operator); $isEqualOp = '==' === $this->operator; $isNonEqualOp = '!=' === $this->operator; $isProviderEqualOp = '==' === $provider->operator; $isProviderNonEqualOp = '!=' === $provider->operator; if ($isNonEqualOp || $isProviderNonEqualOp) { return !$isEqualOp && !$isProviderEqualOp || $this->versionCompare($provider->version, $this->version, '!='); } if ($this->operator != '==' && $noEqualOp == $providerNoEqualOp) { return true; } if ($this->versionCompare($provider->version, $this->version, $this->operator)) { if ($provider->version == $this->version && $provider->operator == $providerNoEqualOp && $this->operator != $noEqualOp) { return false; } return true; } return false; } public function __toString() { return $this->operator.' '.$this->version; } } matches($this); } elseif ($provider instanceof $this) { return $this->matchSpecific($provider); } return true; } public function setPrettyString($prettyString) { $this->prettyString = $prettyString; } public function getPrettyString() { if ($this->prettyString) { return $this->prettyString; } return $this->__toString(); } } io = $io; $this->root = rtrim($cacheDir, '/\\') . '/'; $this->whitelist = $whitelist; $this->filesystem = $filesystem ?: new Filesystem(); if (!is_dir($this->root)) { if (!@mkdir($this->root, 0777, true)) { $this->enabled = false; } } } public function isEnabled() { return $this->enabled; } public function getRoot() { return $this->root; } public function read($file) { $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); if ($this->enabled && file_exists($this->root . $file)) { return file_get_contents($this->root . $file); } return false; } public function write($file, $contents) { if ($this->enabled) { $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); return file_put_contents($this->root . $file, $contents); } return false; } public function copyFrom($file, $source) { if ($this->enabled) { $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); $this->filesystem->ensureDirectoryExists(dirname($this->root . $file)); return copy($source, $this->root . $file); } return false; } public function copyTo($file, $target) { $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); if ($this->enabled && file_exists($this->root . $file)) { touch($this->root . $file); return copy($this->root . $file, $target); } return false; } public function remove($file) { $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); if ($this->enabled && file_exists($this->root . $file)) { return unlink($this->root . $file); } return false; } public function gc($ttl, $maxSize) { $expire = new \DateTime(); $expire->modify('-'.$ttl.' seconds'); $finder = $this->getFinder()->date('until '.$expire->format('Y-m-d H:i:s')); foreach ($finder as $file) { unlink($file->getRealPath()); } $totalSize = $this->filesystem->size($this->root); if ($totalSize > $maxSize) { $iterator = $this->getFinder()->sortByAccessedTime()->getIterator(); while ($totalSize > $maxSize && $iterator->valid()) { $filepath = $iterator->current()->getRealPath(); $totalSize -= $this->filesystem->size($filepath); unlink($filepath); $iterator->next(); } } return true; } public function sha1($file) { $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); if ($this->enabled && file_exists($this->root . $file)) { return sha1_file($this->root . $file); } return false; } public function sha256($file) { $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); if ($this->enabled && file_exists($this->root . $file)) { return hash_file('sha256', $this->root . $file); } return false; } protected function getFinder() { return Finder::create()->in($this->root)->files(); } } 'UNKNOWN', self::TYPE_PACKAGE => 'PACKAGE', self::TYPE_JOB => 'JOB', self::TYPE_LEARNED => 'LEARNED', ); protected $rules; protected $ruleById; protected $nextRuleId; protected $rulesByHash; public function __construct() { $this->nextRuleId = 0; foreach ($this->getTypes() as $type) { $this->rules[$type] = array(); } $this->rulesByHash = array(); } public function add(Rule $rule, $type) { if (!isset(self::$types[$type])) { throw new \OutOfBoundsException('Unknown rule type: ' . $type); } if (!isset($this->rules[$type])) { $this->rules[$type] = array(); } $this->rules[$type][] = $rule; $this->ruleById[$this->nextRuleId] = $rule; $rule->setType($type); $rule->setId($this->nextRuleId); $this->nextRuleId++; $hash = $rule->getHash(); if (!isset($this->rulesByHash[$hash])) { $this->rulesByHash[$hash] = array($rule); } else { $this->rulesByHash[$hash][] = $rule; } } public function count() { return $this->nextRuleId; } public function ruleById($id) { return $this->ruleById[$id]; } public function getRules() { return $this->rules; } public function getIterator() { return new RuleSetIterator($this->getRules()); } public function getIteratorFor($types) { if (!is_array($types)) { $types = array($types); } $allRules = $this->getRules(); $rules = array(); foreach ($types as $type) { $rules[$type] = $allRules[$type]; } return new RuleSetIterator($rules); } public function getIteratorWithout($types) { if (!is_array($types)) { $types = array($types); } $rules = $this->getRules(); foreach ($types as $type) { unset($rules[$type]); } return new RuleSetIterator($rules); } public function getTypes() { $types = self::$types; unset($types[-1]); return array_keys($types); } public function containsEqual($rule) { if (isset($this->rulesByHash[$rule->getHash()])) { $potentialDuplicates = $this->rulesByHash[$rule->getHash()]; foreach ($potentialDuplicates as $potentialDuplicate) { if ($rule->equals($potentialDuplicate)) { return true; } } } return false; } public function __toString() { $string = "\n"; foreach ($this->rules as $type => $rules) { $string .= str_pad(self::$types[$type], 8, ' ') . ": "; foreach ($rules as $rule) { $string .= $rule."\n"; } $string .= "\n\n"; } return $string; } } preferStable = $preferStable; } public function versionCompare(PackageInterface $a, PackageInterface $b, $operator) { if ($this->preferStable && ($stabA = $a->getStability()) !== ($stabB = $b->getStability())) { return BasePackage::$stabilities[$stabA] < BasePackage::$stabilities[$stabB]; } $constraint = new VersionConstraint($operator, $b->getVersion()); $version = new VersionConstraint('==', $a->getVersion()); return $constraint->matchSpecific($version); } public function findUpdatePackages(Pool $pool, array $installedMap, PackageInterface $package) { $packages = array(); foreach ($pool->whatProvides($package->getName()) as $candidate) { if ($candidate !== $package) { $packages[] = $candidate; } } return $packages; } public function getPriority(Pool $pool, PackageInterface $package) { return $pool->getPriority($package->getRepository()); } public function selectPreferedPackages(Pool $pool, array $installedMap, array $literals, $requiredPackage = null) { $packages = $this->groupLiteralsByNamePreferInstalled($pool, $installedMap, $literals); foreach ($packages as &$literals) { $policy = $this; usort($literals, function ($a, $b) use ($policy, $pool, $installedMap, $requiredPackage) { return $policy->compareByPriorityPreferInstalled($pool, $installedMap, $pool->literalToPackage($a), $pool->literalToPackage($b), $requiredPackage, true); }); } foreach ($packages as &$literals) { $literals = $this->pruneToBestVersion($pool, $literals); $literals = $this->pruneToHighestPriorityOrInstalled($pool, $installedMap, $literals); $literals = $this->pruneRemoteAliases($pool, $literals); } $selected = call_user_func_array('array_merge', $packages); usort($selected, function ($a, $b) use ($policy, $pool, $installedMap, $requiredPackage) { return $policy->compareByPriorityPreferInstalled($pool, $installedMap, $pool->literalToPackage($a), $pool->literalToPackage($b), $requiredPackage); }); return $selected; } protected function groupLiteralsByNamePreferInstalled(Pool $pool, array $installedMap, $literals) { $packages = array(); foreach ($literals as $literal) { $packageName = $pool->literalToPackage($literal)->getName(); if (!isset($packages[$packageName])) { $packages[$packageName] = array(); } if (isset($installedMap[abs($literal)])) { array_unshift($packages[$packageName], $literal); } else { $packages[$packageName][] = $literal; } } return $packages; } public function compareByPriorityPreferInstalled(Pool $pool, array $installedMap, PackageInterface $a, PackageInterface $b, $requiredPackage = null, $ignoreReplace = false) { if ($a->getRepository() === $b->getRepository()) { 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 = substr($a->getName(), 0, $pos) === $requiredVendor; $bIsSameVendor = substr($b->getName(), 0, $pos) === $requiredVendor; if ($bIsSameVendor !== $aIsSameVendor) { return $aIsSameVendor ? -1 : 1; } } } if ($a->getId() === $b->getId()) { return 0; } return ($a->getId() < $b->getId()) ? -1 : 1; } if (isset($installedMap[$a->getId()])) { return -1; } if (isset($installedMap[$b->getId()])) { return 1; } return ($this->getPriority($pool, $a) > $this->getPriority($pool, $b)) ? -1 : 1; } protected function replaces(PackageInterface $source, PackageInterface $target) { foreach ($source->getReplaces() as $link) { if ($link->getTarget() === $target->getName() ) { return true; } } return false; } protected function pruneToBestVersion(Pool $pool, $literals) { $bestLiterals = array($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, '>')) { $bestPackage = $package; $bestLiterals = array($literal); } elseif ($this->versionCompare($package, $bestPackage, '==')) { $bestLiterals[] = $literal; } } return $bestLiterals; } protected function selectNewestPackages(array $installedMap, array $literals) { $maxLiterals = array($literals[0]); $maxPackage = $literals[0]->getPackage(); foreach ($literals as $i => $literal) { if (0 === $i) { continue; } if ($this->versionCompare($literal->getPackage(), $maxPackage, '>')) { $maxPackage = $literal->getPackage(); $maxLiterals = array($literal); } elseif ($this->versionCompare($literal->getPackage(), $maxPackage, '==')) { $maxLiterals[] = $literal; } } return $maxLiterals; } protected function pruneToHighestPriorityOrInstalled(Pool $pool, array $installedMap, array $literals) { $selected = array(); $priority = null; foreach ($literals as $literal) { $package = $pool->literalToPackage($literal); if (isset($installedMap[$package->getId()])) { $selected[] = $literal; continue; } if (null === $priority) { $priority = $this->getPriority($pool, $package); } if ($this->getPriority($pool, $package) != $priority) { break; } $selected[] = $literal; } return $selected; } protected function pruneRemoteAliases(Pool $pool, array $literals) { $hasLocalAlias = false; foreach ($literals as $literal) { $package = $pool->literalToPackage($literal); if ($package instanceof AliasPackage && $package->isRootPackageAlias()) { $hasLocalAlias = true; break; } } if (!$hasLocalAlias) { return $literals; } $selected = array(); foreach ($literals as $literal) { $package = $pool->literalToPackage($literal); if ($package instanceof AliasPackage && $package->isRootPackageAlias()) { $selected[] = $literal; } } return $selected; } } pool = $pool; $this->decisionMap = array(); } public function decide($literal, $level, $why) { $this->addDecision($literal, $level); $this->decisionQueue[] = array( self::DECISION_LITERAL => $literal, self::DECISION_REASON => $why, ); } public function satisfy($literal) { $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($literal) { $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($literalOrPackageId) { return !empty($this->decisionMap[abs($literalOrPackageId)]); } public function undecided($literalOrPackageId) { return empty($this->decisionMap[abs($literalOrPackageId)]); } public function decidedInstall($literalOrPackageId) { $packageId = abs($literalOrPackageId); return isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] > 0; } public function decisionLevel($literalOrPackageId) { $packageId = abs($literalOrPackageId); if (isset($this->decisionMap[$packageId])) { return abs($this->decisionMap[$packageId]); } return 0; } public function decisionRule($literalOrPackageId) { $packageId = abs($literalOrPackageId); foreach ($this->decisionQueue as $i => $decision) { if ($packageId === abs($decision[self::DECISION_LITERAL])) { return $decision[self::DECISION_REASON]; } } return null; } public function atOffset($queueOffset) { return $this->decisionQueue[$queueOffset]; } public function validOffset($queueOffset) { return $queueOffset >= 0 && $queueOffset < count($this->decisionQueue); } public function lastReason() { return $this->decisionQueue[count($this->decisionQueue) - 1][self::DECISION_REASON]; } public function lastLiteral() { return $this->decisionQueue[count($this->decisionQueue) - 1][self::DECISION_LITERAL]; } public function reset() { while ($decision = array_pop($this->decisionQueue)) { $this->decisionMap[abs($decision[self::DECISION_LITERAL])] = 0; } } public function resetToOffset($offset) { while (count($this->decisionQueue) > $offset + 1) { $decision = array_pop($this->decisionQueue); $this->decisionMap[abs($decision[self::DECISION_LITERAL])] = 0; } } public function revertLast() { $this->decisionMap[abs($this->lastLiteral())] = 0; array_pop($this->decisionQueue); } public function count() { return count($this->decisionQueue); } public function rewind() { end($this->decisionQueue); } public function current() { return current($this->decisionQueue); } public function key() { return key($this->decisionQueue); } public function next() { return prev($this->decisionQueue); } public function valid() { return false !== current($this->decisionQueue); } public function isEmpty() { return count($this->decisionQueue) === 0; } protected function addDecision($literal, $level) { $packageId = abs($literal); $previousDecision = isset($this->decisionMap[$packageId]) ? $this->decisionMap[$packageId] : null; if ($previousDecision != 0) { $literalString = $this->pool->literalToString($literal); $package = $this->pool->literalToPackage($literal); throw new SolverBugException( "Trying to decide $literalString on level $level, even though $package was previously decided as ".(int) $previousDecision."." ); } if ($literal > 0) { $this->decisionMap[$packageId] = $level; } else { $this->decisionMap[$packageId] = -$level; } } } rule = $rule; $literals = $rule->getLiterals(); $this->watch1 = count($literals) > 0 ? $literals[0] : 0; $this->watch2 = count($literals) > 1 ? $literals[1] : 0; } public function watch2OnHighest(Decisions $decisions) { $literals = $this->rule->getLiterals(); if ($literals < 3) { return; } $watchLevel = 0; foreach ($literals as $literal) { $level = $decisions->decisionLevel($literal); if ($level > $watchLevel) { $this->rule->watch2 = $literal; $watchLevel = $level; } } } public function getRule() { return $this->rule; } public function getOtherWatch($literal) { if ($this->watch1 == $literal) { return $this->watch2; } else { return $this->watch1; } } public function moveWatch($from, $to) { if ($this->watch1 == $from) { $this->watch1 = $to; } else { $this->watch2 = $to; } } } problems = $problems; $this->installedMap = $installedMap; parent::__construct($this->createMessage(), 2); } protected function createMessage() { $text = "\n"; foreach ($this->problems as $i => $problem) { $text .= " Problem ".($i+1).$problem->getPrettyString($this->installedMap)."\n"; } if (strpos($text, 'could not be found') || strpos($text, 'no matching package found')) { $text .= "\nPotential 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\nRead for further common problems."; } return $text; } public function getProblems() { return $this->problems; } } policy = $policy; $this->pool = $pool; $this->installedMap = $installedMap; $this->decisions = $decisions; $this->transaction = array(); } public function getOperations() { $installMeansUpdateMap = $this->findUpdates(); $updateMap = array(); $installMap = array(); $uninstallMap = array(); foreach ($this->decisions as $i => $decision) { $literal = $decision[Decisions::DECISION_LITERAL]; $reason = $decision[Decisions::DECISION_REASON]; $package = $this->pool->literalToPackage($literal); if (($literal > 0) == (isset($this->installedMap[$package->getId()]))) { continue; } if ($literal > 0) { if (isset($installMeansUpdateMap[abs($literal)]) && !$package instanceof AliasPackage) { $source = $installMeansUpdateMap[abs($literal)]; $updateMap[$package->getId()] = array( 'package' => $package, 'source' => $source, 'reason' => $reason, ); unset($installMeansUpdateMap[abs($literal)]); $ignoreRemove[$source->getId()] = true; } else { $installMap[$package->getId()] = array( 'package' => $package, 'reason' => $reason, ); } } } foreach ($this->decisions as $i => $decision) { $literal = $decision[Decisions::DECISION_LITERAL]; $package = $this->pool->literalToPackage($literal); if ($literal <= 0 && isset($this->installedMap[$package->getId()]) && !isset($ignoreRemove[$package->getId()])) { $uninstallMap[$package->getId()] = array( 'package' => $package, 'reason' => $reason, ); } } $this->transactionFromMaps($installMap, $updateMap, $uninstallMap); return $this->transaction; } protected function transactionFromMaps($installMap, $updateMap, $uninstallMap) { $queue = array_map(function ($operation) { return $operation['package']; }, $this->findRootPackages($installMap, $updateMap) ); $visited = array(); while (!empty($queue)) { $package = array_pop($queue); $packageId = $package->getId(); if (!isset($visited[$packageId])) { array_push($queue, $package); if ($package instanceof AliasPackage) { array_push($queue, $package->getAliasOf()); } else { foreach ($package->getRequires() as $link) { $possibleRequires = $this->pool->whatProvides($link->getTarget(), $link->getConstraint()); foreach ($possibleRequires as $require) { array_push($queue, $require); } } } $visited[$package->getId()] = true; } else { if (isset($installMap[$packageId])) { $this->install( $installMap[$packageId]['package'], $installMap[$packageId]['reason'] ); unset($installMap[$packageId]); } if (isset($updateMap[$packageId])) { $this->update( $updateMap[$packageId]['source'], $updateMap[$packageId]['package'], $updateMap[$packageId]['reason'] ); unset($updateMap[$packageId]); } } } foreach ($uninstallMap as $uninstall) { $this->uninstall($uninstall['package'], $uninstall['reason']); } } protected function findRootPackages($installMap, $updateMap) { $packages = $installMap + $updateMap; $roots = $packages; foreach ($packages as $packageId => $operation) { $package = $operation['package']; if (!isset($roots[$packageId])) { continue; } foreach ($package->getRequires() as $link) { $possibleRequires = $this->pool->whatProvides($link->getTarget(), $link->getConstraint()); foreach ($possibleRequires as $require) { unset($roots[$require->getId()]); } } } return $roots; } protected function findUpdates() { $installMeansUpdateMap = array(); foreach ($this->decisions as $i => $decision) { $literal = $decision[Decisions::DECISION_LITERAL]; $package = $this->pool->literalToPackage($literal); if ($package instanceof AliasPackage) { continue; } if ($literal <= 0 && isset($this->installedMap[$package->getId()])) { $updates = $this->policy->findUpdatePackages($this->pool, $this->installedMap, $package); $literals = array($package->getId()); foreach ($updates as $update) { $literals[] = $update->getId(); } foreach ($literals as $updateLiteral) { if ($updateLiteral !== $literal) { $installMeansUpdateMap[abs($updateLiteral)] = $package; } } } } return $installMeansUpdateMap; } protected function install($package, $reason) { if ($package instanceof AliasPackage) { return $this->markAliasInstalled($package, $reason); } $this->transaction[] = new Operation\InstallOperation($package, $reason); } protected function update($from, $to, $reason) { $this->transaction[] = new Operation\UpdateOperation($from, $to, $reason); } protected function uninstall($package, $reason) { if ($package instanceof AliasPackage) { return $this->markAliasUninstalled($package, $reason); } $this->transaction[] = new Operation\UninstallOperation($package, $reason); } protected function markAliasInstalled($package, $reason) { $this->transaction[] = new Operation\MarkAliasInstalledOperation($package, $reason); } protected function markAliasUninstalled($package, $reason) { $this->transaction[] = new Operation\MarkAliasUninstalledOperation($package, $reason); } } package = $package; } public function getPackage() { return $this->package; } public function getJobType() { return 'uninstall'; } public function __toString() { return 'Uninstalling '.$this->package->getPrettyName().' ('.$this->formatVersion($this->package).')'; } } initialPackage = $initial; $this->targetPackage = $target; } public function getInitialPackage() { return $this->initialPackage; } public function getTargetPackage() { return $this->targetPackage; } public function getJobType() { return 'update'; } public function __toString() { return 'Updating '.$this->initialPackage->getPrettyName().' ('.$this->formatVersion($this->initialPackage).') to '. $this->targetPackage->getPrettyName(). ' ('.$this->formatVersion($this->targetPackage).')'; } } package = $package; } public function getPackage() { return $this->package; } public function getJobType() { return 'markAliasInstalled'; } public function __toString() { return 'Marking '.$this->package->getPrettyName().' ('.$this->formatVersion($this->package).') as installed, alias of '.$this->package->getAliasOf()->getPrettyName().' ('.$this->formatVersion($this->package->getAliasOf()).')'; } } package = $package; } public function getPackage() { return $this->package; } public function getJobType() { return 'install'; } public function __toString() { return 'Installing '.$this->package->getPrettyName().' ('.$this->formatVersion($this->package).')'; } } reason = $reason; } public function getReason() { return $this->reason; } protected function formatVersion(PackageInterface $package) { return VersionParser::formatVersion($package); } } package = $package; } public function getPackage() { return $this->package; } public function getJobType() { return 'markAliasUninstalled'; } public function __toString() { return 'Marking '.$this->package->getPrettyName().' ('.$this->formatVersion($this->package).') as uninstalled, alias of '.$this->package->getAliasOf()->getPrettyName().' ('.$this->formatVersion($this->package->getAliasOf()).')'; } } versionParser = new VersionParser; $this->acceptableStabilities = array(); foreach (BasePackage::$stabilities as $stability => $value) { if ($value <= BasePackage::$stabilities[$minimumStability]) { $this->acceptableStabilities[$stability] = $value; } } $this->stabilityFlags = $stabilityFlags; } public function addRepository(RepositoryInterface $repo, $rootAliases = array()) { if ($repo instanceof CompositeRepository) { $repos = $repo->getRepositories(); } else { $repos = array($repo); } foreach ($repos as $repo) { $this->repositories[] = $repo; $exempt = $repo instanceof PlatformRepository || $repo instanceof InstalledRepositoryInterface; if ($repo instanceof ComposerRepository && $repo->hasProviders()) { $this->providerRepos[] = $repo; $repo->setRootAliases($rootAliases); $repo->resetPackageIds(); } elseif ($repo instanceof StreamableRepositoryInterface) { foreach ($repo->getMinimalPackages() as $package) { $name = $package['name']; $version = $package['version']; $stability = VersionParser::parseStability($version); $names = array( $name => true, ); if (isset($package['provide'])) { foreach ($package['provide'] as $target => $constraint) { $names[$target] = true; } } if (isset($package['replace'])) { foreach ($package['replace'] as $target => $constraint) { $names[$target] = true; } } $names = array_keys($names); if ($exempt || $this->isPackageAcceptable($names, $stability)) { $package['id'] = $this->id++; $this->packages[] = $package; foreach ($names as $provided) { $this->packageByName[$provided][$package['id']] = $this->packages[$this->id - 2]; } unset($rootAliasData); if (isset($rootAliases[$name][$version])) { $rootAliasData = $rootAliases[$name][$version]; } elseif (isset($package['alias_normalized']) && isset($rootAliases[$name][$package['alias_normalized']])) { $rootAliasData = $rootAliases[$name][$package['alias_normalized']]; } if (isset($rootAliasData)) { $alias = $package; unset($alias['raw']); $alias['version'] = $rootAliasData['alias_normalized']; $alias['alias'] = $rootAliasData['alias']; $alias['alias_of'] = $package['id']; $alias['id'] = $this->id++; $alias['root_alias'] = true; $this->packages[] = $alias; foreach ($names as $provided) { $this->packageByName[$provided][$alias['id']] = $this->packages[$this->id - 2]; } } if (isset($package['alias'])) { $alias = $package; unset($alias['raw']); $alias['version'] = $package['alias_normalized']; $alias['alias'] = $package['alias']; $alias['alias_of'] = $package['id']; $alias['id'] = $this->id++; $this->packages[] = $alias; foreach ($names as $provided) { $this->packageByName[$provided][$alias['id']] = $this->packages[$this->id - 2]; } } } } } else { foreach ($repo->getPackages() as $package) { $names = $package->getNames(); $stability = $package->getStability(); if ($exempt || $this->isPackageAcceptable($names, $stability)) { $package->setId($this->id++); $this->packages[] = $package; foreach ($names as $provided) { $this->packageByName[$provided][] = $package; } $name = $package->getName(); if (isset($rootAliases[$name][$package->getVersion()])) { $alias = $rootAliases[$name][$package->getVersion()]; if ($package instanceof AliasPackage) { $package = $package->getAliasOf(); } $aliasPackage = new AliasPackage($package, $alias['alias_normalized'], $alias['alias']); $aliasPackage->setRootPackageAlias(true); $aliasPackage->setId($this->id++); $package->getRepository()->addPackage($aliasPackage); $this->packages[] = $aliasPackage; foreach ($aliasPackage->getNames() as $name) { $this->packageByName[$name][] = $aliasPackage; } } } } } } } public function getPriority(RepositoryInterface $repo) { $priority = array_search($repo, $this->repositories, true); if (false === $priority) { throw new \RuntimeException("Could not determine repository priority. The repository was not registered in the pool."); } return -$priority; } public function packageById($id) { return $this->ensurePackageIsLoaded($this->packages[$id - 1]); } public function whatProvides($name, LinkConstraintInterface $constraint = null) { if (isset($this->providerCache[$name][(string) $constraint])) { return $this->providerCache[$name][(string) $constraint]; } return $this->providerCache[$name][(string) $constraint] = $this->computeWhatProvides($name, $constraint); } private function computeWhatProvides($name, $constraint) { $candidates = array(); foreach ($this->providerRepos as $repo) { foreach ($repo->whatProvides($this, $name) as $candidate) { $candidates[] = $candidate; if ($candidate->getId() < 1) { $candidate->setId($this->id++); $this->packages[$this->id - 2] = $candidate; } } } if (isset($this->packageByName[$name])) { $candidates = array_merge($candidates, $this->packageByName[$name]); } if (null === $constraint) { foreach ($candidates as $key => $candidate) { $candidates[$key] = $this->ensurePackageIsLoaded($candidate); } return $candidates; } $matches = $provideMatches = array(); $nameMatch = false; foreach ($candidates as $candidate) { switch ($this->match($candidate, $name, $constraint)) { case self::MATCH_NONE: break; case self::MATCH_NAME: $nameMatch = true; break; case self::MATCH: $nameMatch = true; $matches[] = $this->ensurePackageIsLoaded($candidate); break; case self::MATCH_PROVIDE: $provideMatches[] = $this->ensurePackageIsLoaded($candidate); break; case self::MATCH_REPLACE: $matches[] = $this->ensurePackageIsLoaded($candidate); break; default: throw new \UnexpectedValueException('Unexpected match type'); } } if ($nameMatch) { return $matches; } return array_merge($matches, $provideMatches); } public function literalToPackage($literal) { $packageId = abs($literal); return $this->packageById($packageId); } public function literalToString($literal) { return ($literal > 0 ? '+' : '-') . $this->literalToPackage($literal); } public function literalToPrettyString($literal, $installedMap) { $package = $this->literalToPackage($literal); if (isset($installedMap[$package->getId()])) { $prefix = ($literal > 0 ? 'keep' : 'remove'); } else { $prefix = ($literal > 0 ? 'install' : 'don\'t install'); } return $prefix.' '.$package->getPrettyString(); } public function isPackageAcceptable($name, $stability) { foreach ((array) $name as $n) { if (!isset($this->stabilityFlags[$n]) && isset($this->acceptableStabilities[$stability])) { return true; } if (isset($this->stabilityFlags[$n]) && BasePackage::$stabilities[$stability] <= $this->stabilityFlags[$n]) { return true; } } return false; } private function ensurePackageIsLoaded($data) { if (is_array($data)) { if (isset($data['alias_of'])) { $aliasOf = $this->packageById($data['alias_of']); $package = $this->packages[$data['id'] - 1] = $data['repo']->loadAliasPackage($data, $aliasOf); $package->setRootPackageAlias(!empty($data['root_alias'])); } else { $package = $this->packages[$data['id'] - 1] = $data['repo']->loadPackage($data); } foreach ($package->getNames() as $name) { $this->packageByName[$name][$data['id']] = $package; } $package->setId($data['id']); return $package; } return $data; } private function match($candidate, $name, LinkConstraintInterface $constraint) { if (is_array($candidate)) { $candidateName = $candidate['name']; $candidateVersion = $candidate['version']; } else { $candidateName = $candidate->getName(); $candidateVersion = $candidate->getVersion(); } if ($candidateName === $name) { return $constraint->matches(new VersionConstraint('==', $candidateVersion)) ? self::MATCH : self::MATCH_NAME; } if (is_array($candidate)) { $provides = isset($candidate['provide']) ? $this->versionParser->parseLinks($candidateName, $candidateVersion, 'provides', $candidate['provide']) : array(); $replaces = isset($candidate['replace']) ? $this->versionParser->parseLinks($candidateName, $candidateVersion, 'replaces', $candidate['replace']) : array(); } else { $provides = $candidate->getProvides(); $replaces = $candidate->getReplaces(); } if (isset($replaces[0]) || isset($provides[0])) { foreach ($provides as $link) { if ($link->getTarget() === $name && $constraint->matches($link->getConstraint())) { return self::MATCH_PROVIDE; } } foreach ($replaces as $link) { if ($link->getTarget() === $name && $constraint->matches($link->getConstraint())) { return self::MATCH_REPLACE; } } return self::MATCH_NONE; } if (isset($provides[$name]) && $constraint->matches($provides[$name]->getConstraint())) { return self::MATCH_PROVIDE; } if (isset($replaces[$name]) && $constraint->matches($replaces[$name]->getConstraint())) { return self::MATCH_REPLACE; } return self::MATCH_NONE; } } pool = $pool; sort($literals); $this->literals = $literals; $this->reason = $reason; $this->reasonData = $reasonData; $this->disabled = false; $this->job = $job; $this->type = -1; $this->ruleHash = substr(md5(implode(',', $this->literals)), 0, 5); } public function getHash() { return $this->ruleHash; } public function setId($id) { $this->id = $id; } public function getId() { return $this->id; } public function getJob() { return $this->job; } public function getReason() { return $this->reason; } public function getReasonData() { return $this->reasonData; } public function getRequiredPackage() { if ($this->reason === self::RULE_JOB_INSTALL) { return $this->reasonData; } if ($this->reason === self::RULE_PACKAGE_REQUIRES) { return $this->reasonData->getTarget(); } } public function equals(Rule $rule) { if ($this->ruleHash !== $rule->ruleHash) { return false; } if (count($this->literals) != count($rule->literals)) { return false; } for ($i = 0, $n = count($this->literals); $i < $n; $i++) { if ($this->literals[$i] !== $rule->literals[$i]) { return false; } } return true; } public function setType($type) { $this->type = $type; } public function getType() { return $this->type; } public function disable() { $this->disabled = true; } public function enable() { $this->disabled = false; } public function isDisabled() { return $this->disabled; } public function isEnabled() { return !$this->disabled; } public function getLiterals() { return $this->literals; } public function isAssertion() { return 1 === count($this->literals); } public function getPrettyString(array $installedMap = array()) { $ruleText = ''; foreach ($this->literals as $i => $literal) { if ($i != 0) { $ruleText .= '|'; } $ruleText .= $this->pool->literalToPrettyString($literal, $installedMap); } switch ($this->reason) { case self::RULE_INTERNAL_ALLOW_UPDATE: return $ruleText; case self::RULE_JOB_INSTALL: return "Install command rule ($ruleText)"; case self::RULE_JOB_REMOVE: return "Remove command rule ($ruleText)"; case self::RULE_PACKAGE_CONFLICT: $package1 = $this->pool->literalToPackage($this->literals[0]); $package2 = $this->pool->literalToPackage($this->literals[1]); return $package1->getPrettyString().' conflicts with '.$this->formatPackagesUnique(array($package2)).'.'; case self::RULE_PACKAGE_REQUIRES: $literals = $this->literals; $sourceLiteral = array_shift($literals); $sourcePackage = $this->pool->literalToPackage($sourceLiteral); $requires = array(); foreach ($literals as $literal) { $requires[] = $this->pool->literalToPackage($literal); } $text = $this->reasonData->getPrettyString($sourcePackage); if ($requires) { $text .= ' -> satisfiable by ' . $this->formatPackagesUnique($requires) . '.'; } else { $targetName = $this->reasonData->getTarget(); if (0 === strpos($targetName, 'ext-')) { $ext = substr($targetName, 4); $error = extension_loaded($ext) ? 'has the wrong version ('.phpversion($ext).') installed' : 'is missing from your system'; $text .= ' -> the requested PHP extension '.$ext.' '.$error.'.'; } elseif (0 === strpos($targetName, 'lib-')) { $lib = substr($targetName, 4); $text .= ' -> the requested linked library '.$lib.' has the wrong version installed or is missing from your system, make sure to have the extension providing it.'; } else { $text .= ' -> no matching package found.'; } } return $text; case self::RULE_PACKAGE_OBSOLETES: return $ruleText; case self::RULE_INSTALLED_PACKAGE_OBSOLETES: return $ruleText; case self::RULE_PACKAGE_SAME_NAME: return 'Can only install one of: ' . $this->formatPackagesUnique($this->literals) . '.'; case self::RULE_PACKAGE_IMPLICIT_OBSOLETES: return $ruleText; case self::RULE_LEARNED: return 'Conclusion: '.$ruleText; case self::RULE_PACKAGE_ALIAS: return $ruleText; } } protected function formatPackagesUnique(array $packages) { $prepared = array(); foreach ($packages as $package) { if (!is_object($package)) { $package = $this->pool->literalToPackage($package); } $prepared[$package->getName()]['name'] = $package->getPrettyName(); $prepared[$package->getName()]['versions'][$package->getVersion()] = $package->getPrettyVersion(); } foreach ($prepared as $name => $package) { $prepared[$name] = $package['name'].'['.implode(', ', $package['versions']).']'; } return implode(', ', $prepared); } public function __toString() { $result = ($this->isDisabled()) ? 'disabled(' : '('; foreach ($this->literals as $i => $literal) { if ($i != 0) { $result .= '|'; } $result .= $this->pool->literalToString($literal); } $result .= ')'; return $result; } } policy = $policy; $this->pool = $pool; } protected function createRequireRule(PackageInterface $package, array $providers, $reason, $reasonData = null) { $literals = array(-$package->getId()); foreach ($providers as $provider) { if ($provider === $package) { return null; } $literals[] = $provider->getId(); } return new Rule($this->pool, $literals, $reason, $reasonData); } protected function createInstallOneOfRule(array $packages, $reason, $job) { $literals = array(); foreach ($packages as $package) { $literals[] = $package->getId(); } return new Rule($this->pool, $literals, $reason, $job['packageName'], $job); } protected function createRemoveRule(PackageInterface $package, $reason, $job) { return new Rule($this->pool, array(-$package->getId()), $reason, $job['packageName'], $job); } protected function createConflictRule(PackageInterface $issuer, PackageInterface $provider, $reason, $reasonData = null) { if ($issuer === $provider) { return null; } return new Rule($this->pool, array(-$issuer->getId(), -$provider->getId()), $reason, $reasonData); } private function addRule($type, Rule $newRule = null) { if (!$newRule || $this->rules->containsEqual($newRule)) { return; } $this->rules->add($newRule, $type); } protected function addRulesForPackage(PackageInterface $package) { $workQueue = new \SplQueue; $workQueue->enqueue($package); while (!$workQueue->isEmpty()) { $package = $workQueue->dequeue(); if (isset($this->addedMap[$package->getId()])) { continue; } $this->addedMap[$package->getId()] = true; foreach ($package->getRequires() as $link) { $possibleRequires = $this->pool->whatProvides($link->getTarget(), $link->getConstraint()); $this->addRule(RuleSet::TYPE_PACKAGE, $rule = $this->createRequireRule($package, $possibleRequires, Rule::RULE_PACKAGE_REQUIRES, $link)); foreach ($possibleRequires as $require) { $workQueue->enqueue($require); } } foreach ($package->getConflicts() as $link) { $possibleConflicts = $this->pool->whatProvides($link->getTarget(), $link->getConstraint()); foreach ($possibleConflicts as $conflict) { $this->addRule(RuleSet::TYPE_PACKAGE, $this->createConflictRule($package, $conflict, Rule::RULE_PACKAGE_CONFLICT, $link)); } } $isInstalled = (isset($this->installedMap[$package->getId()])); foreach ($package->getReplaces() as $link) { $obsoleteProviders = $this->pool->whatProvides($link->getTarget(), $link->getConstraint()); foreach ($obsoleteProviders as $provider) { if ($provider === $package) { continue; } if (!$this->obsoleteImpossibleForAlias($package, $provider)) { $reason = ($isInstalled) ? Rule::RULE_INSTALLED_PACKAGE_OBSOLETES : Rule::RULE_PACKAGE_OBSOLETES; $this->addRule(RuleSet::TYPE_PACKAGE, $this->createConflictRule($package, $provider, $reason, $link)); } } } $obsoleteProviders = $this->pool->whatProvides($package->getName(), null); foreach ($obsoleteProviders as $provider) { if ($provider === $package) { continue; } if (($package instanceof AliasPackage) && $package->getAliasOf() === $provider) { $this->addRule(RuleSet::TYPE_PACKAGE, $rule = $this->createRequireRule($package, array($provider), Rule::RULE_PACKAGE_ALIAS, $package)); } elseif (!$this->obsoleteImpossibleForAlias($package, $provider)) { $reason = ($package->getName() == $provider->getName()) ? Rule::RULE_PACKAGE_SAME_NAME : Rule::RULE_PACKAGE_IMPLICIT_OBSOLETES; $this->addRule(RuleSet::TYPE_PACKAGE, $rule = $this->createConflictRule($package, $provider, $reason, $package)); } } } } protected function obsoleteImpossibleForAlias($package, $provider) { $packageIsAlias = $package instanceof AliasPackage; $providerIsAlias = $provider instanceof AliasPackage; $impossible = ( ($packageIsAlias && $package->getAliasOf() === $provider) || ($providerIsAlias && $provider->getAliasOf() === $package) || ($packageIsAlias && $providerIsAlias && $provider->getAliasOf() === $package->getAliasOf()) ); return $impossible; } private function addRulesForUpdatePackages(PackageInterface $package) { $updates = $this->policy->findUpdatePackages($this->pool, $this->installedMap, $package); foreach ($updates as $update) { $this->addRulesForPackage($update); } } protected function addRulesForJobs() { foreach ($this->jobs as $job) { switch ($job['cmd']) { case 'install': if ($job['packages']) { foreach ($job['packages'] as $package) { if (!isset($this->installedMap[$package->getId()])) { $this->addRulesForPackage($package); } } $rule = $this->createInstallOneOfRule($job['packages'], Rule::RULE_JOB_INSTALL, $job); $this->addRule(RuleSet::TYPE_JOB, $rule); } break; case 'remove': foreach ($job['packages'] as $package) { $rule = $this->createRemoveRule($package, Rule::RULE_JOB_REMOVE, $job); $this->addRule(RuleSet::TYPE_JOB, $rule); } break; } } } public function getRulesFor($jobs, $installedMap) { $this->jobs = $jobs; $this->rules = new RuleSet; $this->installedMap = $installedMap; foreach ($this->installedMap as $package) { $this->addRulesForPackage($package); $this->addRulesForUpdatePackages($package); } $this->addRulesForJobs(); return $this->rules; } } decisionMap as $packageId => $level) { if ($packageId === 0) { continue; } if ($level > 0) { echo ' +' . $this->pool->packageById($packageId)."\n"; } elseif ($level < 0) { echo ' -' . $this->pool->packageById($packageId)."\n"; } else { echo ' ?' . $this->pool->packageById($packageId)."\n"; } } echo "\n"; } protected function printDecisionQueue() { echo "DecisionQueue: \n"; foreach ($this->decisionQueue as $i => $literal) { echo ' ' . $this->pool->literalToString($literal) . ' ' . $this->decisionQueueWhy[$i]." level ".$this->decisionMap[abs($literal)]."\n"; } echo "\n"; } protected function printWatches() { echo "\nWatches:\n"; foreach ($this->watches as $literalId => $watch) { echo ' '.$this->literalFromId($literalId)."\n"; $queue = array(array(' ', $watch)); while (!empty($queue)) { list($indent, $watch) = array_pop($queue); echo $indent.$watch; if ($watch) { echo ' [id='.$watch->getId().',watch1='.$this->literalFromId($watch->watch1).',watch2='.$this->literalFromId($watch->watch2)."]"; } echo "\n"; if ($watch && ($watch->next1 == $watch || $watch->next2 == $watch)) { if ($watch->next1 == $watch) { echo $indent." 1 *RECURSION*"; } if ($watch->next2 == $watch) { echo $indent." 2 *RECURSION*"; } } elseif ($watch && ($watch->next1 || $watch->next2)) { $indent = str_replace(array('1', '2'), ' ', $indent); array_push($queue, array($indent.' 2 ', $watch->next2)); array_push($queue, array($indent.' 1 ', $watch->next1)); } } echo "\n"; } } } rules = $rules; $this->types = array_keys($rules); sort($this->types); $this->rewind(); } public function current() { return $this->rules[$this->currentType][$this->currentOffset]; } public function key() { return $this->currentType; } public function next() { $this->currentOffset++; if (!isset($this->rules[$this->currentType])) { return; } if ($this->currentOffset >= sizeof($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 (isset($this->types[$this->currentTypeOffset]) && !sizeof($this->rules[$this->currentType])); } } public function rewind() { $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 (isset($this->types[$this->currentTypeOffset]) && !sizeof($this->rules[$this->currentType])); } public function valid() { return isset($this->rules[$this->currentType]) && isset($this->rules[$this->currentType][$this->currentOffset]); } } rewind(); for ($i = 0; $i < $offset; $i++, $this->next()); } public function remove() { $offset = $this->key(); $this->offsetUnset($offset); $this->seek($offset); } } policy = $policy; $this->pool = $pool; $this->installed = $installed; $this->ruleSetGenerator = new RuleSetGenerator($policy, $pool); } private function makeAssertionRuleDecisions() { $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(abs($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($this->pool); $problem->addRule($rule); $problem->addRule($conflict); $this->disableProblem($rule); $this->problems[] = $problem; continue; } $problem = new Problem($this->pool); $problem->addRule($rule); $problem->addRule($conflict); foreach ($this->rules->getIteratorFor(RuleSet::TYPE_JOB) as $assertRule) { if ($assertRule->isDisabled() || !$assertRule->isAssertion()) { continue; } $assertRuleLiterals = $assertRule->getLiterals(); $assertRuleLiteral = $assertRuleLiterals[0]; if (abs($literal) !== abs($assertRuleLiteral)) { continue; } $problem->addRule($assertRule); $this->disableProblem($assertRule); } $this->problems[] = $problem; $this->decisions->resetToOffset($decisionStart); $ruleIndex = -1; } } protected function setupInstalledMap() { $this->installedMap = array(); foreach ($this->installed->getPackages() as $package) { $this->installedMap[$package->getId()] = $package; } foreach ($this->jobs as $job) { switch ($job['cmd']) { case 'update': foreach ($job['packages'] as $package) { if (isset($this->installedMap[$package->getId()])) { $this->updateMap[$package->getId()] = true; } } break; case 'update-all': foreach ($this->installedMap as $package) { $this->updateMap[$package->getId()] = true; } break; case 'install': if (!$job['packages']) { $problem = new Problem($this->pool); $problem->addRule(new Rule($this->pool, array(), null, null, $job)); $this->problems[] = $problem; } break; } } } public function solve(Request $request) { $this->jobs = $request->getJobs(); $this->setupInstalledMap(); $this->decisions = new Decisions($this->pool); $this->rules = $this->ruleSetGenerator->getRulesFor($this->jobs, $this->installedMap); $this->watchGraph = new RuleWatchGraph; foreach ($this->rules as $rule) { $this->watchGraph->insert(new RuleWatchNode($rule)); } $this->makeAssertionRuleDecisions(); $this->runSat(true); foreach ($this->installedMap as $packageId => $void) { if ($this->decisions->undecided($packageId)) { $this->decisions->decide(-$packageId, 1, null); } } if ($this->problems) { throw new SolverProblemsException($this->problems, $this->installedMap); } $transaction = new Transaction($this->policy, $this->pool, $this->installedMap, $this->decisions); return $transaction->getOperations(); } protected function literalFromId($id) { $package = $this->pool->packageById(abs($id)); return new Literal($package, $id > 0); } protected function propagate($level) { 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($level) { 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($level, $literal, $disableRules, Rule $rule) { $level++; $this->decisions->decide($literal, $level, $rule); while (true) { $rule = $this->propagate($level); if (!$rule) { break; } if ($level == 1) { return $this->analyzeUnsolvable($rule, $disableRules); } list($learnLiteral, $newLevel, $newRule, $why) = $this->analyze($level, $rule); if ($newLevel <= 0 || $newLevel >= $level) { throw new SolverBugException( "Trying to revert to invalid level ".(int) $newLevel." from level ".(int) $level."." ); } elseif (!$newRule) { throw new SolverBugException( "No rule was learned from analyzing $rule at level $level." ); } $level = $newLevel; $this->revert($level); $this->rules->add($newRule, RuleSet::TYPE_LEARNED); $this->learnedWhy[$newRule->getId()] = $why; $ruleNode = new RuleWatchNode($newRule); $ruleNode->watch2OnHighest($this->decisions); $this->watchGraph->insert($ruleNode); $this->decisions->decide($learnLiteral, $level, $newRule); } return $level; } private function selectAndInstall($level, array $decisionQueue, $disableRules, Rule $rule) { $literals = $this->policy->selectPreferedPackages($this->pool, $this->installedMap, $decisionQueue, $rule->getRequiredPackage()); $selectedLiteral = array_shift($literals); if (count($literals)) { $this->branches[] = array($literals, $level); } return $this->setPropagateLearn($level, $selectedLiteral, $disableRules, $rule); } protected function analyze($level, $rule) { $analyzedRule = $rule; $ruleLevel = 1; $num = 0; $l1num = 0; $seen = array(); $learnedLiterals = array(null); $decisionId = count($this->decisions); $this->learnedPool[] = array(); while (true) { $this->learnedPool[count($this->learnedPool) - 1][] = $rule; foreach ($rule->getLiterals() as $literal) { 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; } } } $l1retry = true; while ($l1retry) { $l1retry = false; if (!$num && !--$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 ($num && 0 === --$num) { $learnedLiterals[0] = -abs($literal); if (!$l1num) { break 2; } foreach ($learnedLiterals as $i => $learnedLiteral) { if ($i !== 0) { unset($seen[abs($learnedLiteral)]); } } $l1num++; $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 Rule($this->pool, $learnedLiterals, Rule::RULE_LEARNED, $why); return array($learnedLiterals[0], $ruleLevel, $newRule, $why); } private function analyzeUnsolvableRule($problem, $conflictRule) { $why = $conflictRule->getId(); if ($conflictRule->getType() == RuleSet::TYPE_LEARNED) { $learnedWhy = $this->learnedWhy[$why]; $problemRules = $this->learnedPool[$learnedWhy]; foreach ($problemRules as $problemRule) { $this->analyzeUnsolvableRule($problem, $problemRule); } return; } if ($conflictRule->getType() == RuleSet::TYPE_PACKAGE) { return; } $problem->nextSection(); $problem->addRule($conflictRule); } private function analyzeUnsolvable($conflictRule, $disableRules) { $problem = new Problem($this->pool); $problem->addRule($conflictRule); $this->analyzeUnsolvableRule($problem, $conflictRule); $this->problems[] = $problem; $seen = array(); $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); $literals = $why->getLiterals(); foreach ($literals as $literal) { if ($this->decisions->satisfy($literal)) { continue; } $seen[abs($literal)] = true; } } if ($disableRules) { foreach ($this->problems[count($this->problems) - 1] as $reason) { $this->disableProblem($reason['rule']); } $this->resetSolver(); return 1; } return 0; } private function disableProblem($why) { $job = $why->getJob(); if (!$job) { $why->disable(); return; } foreach ($this->rules as $rule) { if ($job === $rule->getJob()) { $rule->disable(); } } } private function resetSolver() { $this->decisions->reset(); $this->propagateIndex = 0; $this->branches = array(); $this->enableDisableLearnedRules(); $this->makeAssertionRuleDecisions(); } private function enableDisableLearnedRules() { foreach ($this->rules->getIteratorFor(RuleSet::TYPE_LEARNED) as $rule) { $why = $this->learnedWhy[$rule->getId()]; $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($disableRules = true) { $this->propagateIndex = 0; $decisionQueue = array(); $decisionSupplementQueue = array(); $disableRules = array(); $level = 1; $systemLevel = $level + 1; $installedPos = 0; while (true) { if (1 === $level) { $conflictRule = $this->propagate($level); if (null !== $conflictRule) { if ($this->analyzeUnsolvable($conflictRule, $disableRules)) { continue; } return; } } if ($level < $systemLevel) { $iterator = $this->rules->getIteratorFor(RuleSet::TYPE_JOB); foreach ($iterator as $rule) { if ($rule->isEnabled()) { $decisionQueue = array(); $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)) { if (count($this->installed) != count($this->updateMap)) { $prunedQueue = array(); foreach ($decisionQueue as $literal) { if (isset($this->installedMap[abs($literal)])) { $prunedQueue[] = $literal; if (isset($this->updateMap[abs($literal)])) { $prunedQueue = $decisionQueue; break; } } } $decisionQueue = $prunedQueue; } } if ($noneSatisfied && count($decisionQueue)) { $oLevel = $level; $level = $this->selectAndInstall($level, $decisionQueue, $disableRules, $rule); if (0 === $level) { return; } if ($level <= $oLevel) { break; } } } } $systemLevel = $level + 1; $iterator->next(); if ($iterator->valid()) { continue; } } if ($level < $systemLevel) { $systemLevel = $level; } for ($i = 0, $n = 0; $n < count($this->rules); $i++, $n++) { if ($i == count($this->rules)) { $i = 0; } $rule = $this->rules->ruleById($i); $literals = $rule->getLiterals(); if ($rule->isDisabled()) { continue; } $decisionQueue = array(); foreach ($literals as $literal) { if ($literal <= 0) { if (!$this->decisions->decidedInstall(abs($literal))) { continue 2; } } else { if ($this->decisions->decidedInstall(abs($literal))) { continue 2; } if ($this->decisions->undecided(abs($literal))) { $decisionQueue[] = $literal; } } } if (count($decisionQueue) < 2) { continue; } $oLevel = $level; $level = $this->selectAndInstall($level, $decisionQueue, $disableRules, $rule); if (0 === $level) { return; } $n = -1; } if ($level < $systemLevel) { continue; } if (count($this->branches)) { $lastLiteral = null; $lastLevel = null; $lastBranchIndex = 0; $lastBranchOffset = 0; $l = 0; for ($i = count($this->branches) - 1; $i >= 0; $i--) { list($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]); array_values($this->branches[$lastBranchIndex][self::BRANCH_LITERALS]); $level = $lastLevel; $this->revert($level); $why = $this->decisions->lastReason(); $oLevel = $level; $level = $this->setPropagateLearn($level, $lastLiteral, $disableRules, $why); if ($level == 0) { return; } continue; } } break; } } } pool = $pool; $this->jobs = array(); } public function install($packageName, LinkConstraintInterface $constraint = null) { $this->addJob($packageName, 'install', $constraint); } public function update($packageName, LinkConstraintInterface $constraint = null) { $this->addJob($packageName, 'update', $constraint); } public function remove($packageName, LinkConstraintInterface $constraint = null) { $this->addJob($packageName, 'remove', $constraint); } protected function addJob($packageName, $cmd, LinkConstraintInterface $constraint = null) { $packageName = strtolower($packageName); $packages = $this->pool->whatProvides($packageName, $constraint); $this->jobs[] = array( 'packages' => $packages, 'cmd' => $cmd, 'packageName' => $packageName, 'constraint' => $constraint, ); } public function updateAll() { $this->jobs[] = array('cmd' => 'update-all', 'packages' => array()); } public function getJobs() { return $this->jobs; } } pool = $pool; } public function addRule(Rule $rule) { $this->addReason($rule->getId(), array( 'rule' => $rule, 'job' => $rule->getJob(), )); } public function getReasons() { return $this->reasons; } public function getPrettyString(array $installedMap = array()) { $reasons = call_user_func_array('array_merge', array_reverse($this->reasons)); if (count($reasons) === 1) { reset($reasons); $reason = current($reasons); $rule = $reason['rule']; $job = $reason['job']; if ($job && $job['cmd'] === 'install' && empty($job['packages'])) { if (0 === stripos($job['packageName'], 'ext-')) { $ext = substr($job['packageName'], 4); $error = extension_loaded($ext) ? 'has the wrong version ('.phpversion($ext).') installed' : 'is missing from your system'; return "\n - The requested PHP extension ".$job['packageName'].$this->constraintToText($job['constraint']).' '.$error.'.'; } if (0 === stripos($job['packageName'], 'lib-')) { $lib = substr($job['packageName'], 4); return "\n - The requested linked library ".$job['packageName'].$this->constraintToText($job['constraint']).' has the wrong version installed or is missing from your system, make sure to have the extension providing it.'; } if (!preg_match('{^[A-Za-z0-9_./-]+$}', $job['packageName'])) { $illegalChars = preg_replace('{[A-Za-z0-9_./-]+}', '', $job['packageName']); return "\n - The requested package ".$job['packageName'].' could not be found, it looks like its name is invalid, "'.$illegalChars.'" is not allowed in package names.'; } if (!$this->pool->whatProvides($job['packageName'])) { return "\n - The requested package ".$job['packageName'].' could not be found in any version, there may be a typo in the package name.'; } return "\n - The requested package ".$job['packageName'].$this->constraintToText($job['constraint']).' could not be found.'; } } $messages = array(); foreach ($reasons as $reason) { $rule = $reason['rule']; $job = $reason['job']; if ($job) { $messages[] = $this->jobToText($job); } elseif ($rule) { if ($rule instanceof Rule) { $messages[] = $rule->getPrettyString($installedMap); } } } return "\n - ".implode("\n - ", $messages); } protected function addReason($id, $reason) { if (!isset($this->reasonSeen[$id])) { $this->reasonSeen[$id] = true; $this->reasons[$this->section][] = $reason; } } public function nextSection() { $this->section++; } protected function jobToText($job) { switch ($job['cmd']) { case 'install': if (!$job['packages']) { return 'No package found to satisfy install request for '.$job['packageName'].$this->constraintToText($job['constraint']); } return 'Installation request for '.$job['packageName'].$this->constraintToText($job['constraint']).' -> satisfiable by '.$this->getPackageList($job['packages']).'.'; case 'update': return 'Update request for '.$job['packageName'].$this->constraintToText($job['constraint']).'.'; case 'remove': return 'Removal request for '.$job['packageName'].$this->constraintToText($job['constraint']).''; } return 'Job(cmd='.$job['cmd'].', target='.$job['packageName'].', packages=['.$this->getPackageList($job['packages']).'])'; } protected function getPackageList($packages) { $prepared = array(); foreach ($packages as $package) { $prepared[$package->getName()]['name'] = $package->getPrettyName(); $prepared[$package->getName()]['versions'][$package->getVersion()] = $package->getPrettyVersion(); } foreach ($prepared as $name => $package) { $prepared[$name] = $package['name'].'['.implode(', ', $package['versions']).']'; } return implode(', ', $prepared); } protected function constraintToText($constraint) { return ($constraint) ? ' '.$constraint->getPrettyString() : ''; } } getRule()->isAssertion()) { return; } foreach (array($node->watch1, $node->watch2) as $literal) { if (!isset($this->watchChains[$literal])) { $this->watchChains[$literal] = new RuleWatchChain; } $this->watchChains[$literal]->unshift($node); } } public function propagateLiteral($decidedLiteral, $level, $decisions) { $literal = -$decidedLiteral; if (!isset($this->watchChains[$literal])) { return null; } $chain = $this->watchChains[$literal]; $chain->rewind(); while ($chain->valid()) { $node = $chain->current(); $otherWatch = $node->getOtherWatch($literal); if (!$node->getRule()->isDisabled() && !$decisions->satisfy($otherWatch)) { $ruleLiterals = $node->getRule()->getLiterals(); $alternativeLiterals = array_filter($ruleLiterals, function ($ruleLiteral) use ($literal, $otherWatch, $decisions) { return $literal !== $ruleLiteral && $otherWatch !== $ruleLiteral && !$decisions->conflict($ruleLiteral); }); if ($alternativeLiterals) { reset($alternativeLiterals); $this->moveWatch($literal, current($alternativeLiterals), $node); continue; } if ($decisions->conflict($otherWatch)) { return $node->getRule(); } $decisions->decide($otherWatch, $level, $node->getRule()); } $chain->next(); } return null; } protected function moveWatch($fromLiteral, $toLiteral, $node) { if (!isset($this->watchChains[$toLiteral])) { $this->watchChains[$toLiteral] = new RuleWatchChain; } $node->moveWatch($fromLiteral, $toLiteral); $this->watchChains[$fromLiteral]->remove(); $this->watchChains[$toLiteral]->unshift($node); } } file = $file; } public function addRepository($name, $config) { $this->manipulateJson('addRepository', $name, $config, function (&$config, $repo, $repoConfig) { $config['repositories'][$repo] = $repoConfig; }); } public function removeRepository($name) { $this->manipulateJson('removeRepository', $name, function (&$config, $repo) { unset($config['repositories'][$repo]); }); } public function addConfigSetting($name, $value) { $this->manipulateJson('addConfigSetting', $name, $value, function (&$config, $key, $val) { $config['config'][$key] = $val; }); } public function removeConfigSetting($name) { $this->manipulateJson('removeConfigSetting', $name, function (&$config, $key) { unset($config['config'][$key]); }); } public function addLink($type, $name, $value) { $this->manipulateJson('addLink', $type, $name, $value, function (&$config, $key) { $config[$type][$name] = $value; }); } public function removeLink($type, $name) { $this->manipulateJson('removeSubNode', $type, $name, function (&$config, $key) { unset($config[$type][$name]); }); } protected function manipulateJson($method, $args, $fallback) { $args = func_get_args(); array_shift($args); $fallback = array_pop($args); if ($this->file->exists()) { $contents = file_get_contents($this->file->getPath()); } else { $contents = "{\n \"config\": {\n }\n}\n"; } $manipulator = new JsonManipulator($contents); $newFile = !$this->file->exists(); if (call_user_func_array(array($manipulator, $method), $args)) { file_put_contents($this->file->getPath(), $manipulator->getContents()); } else { $config = $this->file->read(); array_unshift($args, $config); call_user_func_array($fallback, $args); $this->file->write($config); } if ($newFile) { chmod($this->file->getPath(), 0600); } } } merge(array('config' => array('home' => $home, 'cache-dir' => $cacheDir))); $file = new JsonFile($home.'/config.json'); if ($file->exists()) { $config->merge($file->read()); } $config->setConfigSource(new JsonConfigSource($file)); $legacyPaths = array( 'cache-repo-dir' => array('/cache' => '/http*', '/cache.svn' => '/*', '/cache.github' => '/*'), 'cache-vcs-dir' => array('/cache.git' => '/*', '/cache.hg' => '/*'), 'cache-files-dir' => array('/cache.files' => '/*'), ); foreach ($legacyPaths as $key => $oldPaths) { foreach ($oldPaths as $oldPath => $match) { $dir = $config->get($key); if ('/cache.github' === $oldPath) { $dir .= '/github.com'; } $oldPath = $config->get('home').$oldPath; $oldPathMatch = $oldPath . $match; if (is_dir($oldPath) && $dir !== $oldPath) { if (!is_dir($dir)) { if (!@mkdir($dir, 0777, true)) { continue; } } if (is_array($children = glob($oldPathMatch))) { foreach ($children as $child) { @rename($child, $dir.'/'.basename($child)); } } @rmdir($oldPath); } } } return $config; } public static function getComposerFile() { return trim(getenv('COMPOSER')) ?: 'composer.json'; } public static function createAdditionalStyles() { return array( 'highlight' => new OutputFormatterStyle('red'), 'warning' => new OutputFormatterStyle('black', 'yellow'), ); } public static function createDefaultRepositories(IOInterface $io = null, Config $config = null, RepositoryManager $rm = null) { $repos = array(); if (!$config) { $config = static::createConfig(); } if (!$rm) { if (!$io) { throw new \InvalidArgumentException('This function requires either an IOInterface or a RepositoryManager'); } $factory = new static; $rm = $factory->createRepositoryManager($io, $config); } foreach ($config->getRepositories() as $index => $repo) { if (!is_array($repo)) { throw new \UnexpectedValueException('Repository '.$index.' ('.json_encode($repo).') should be an array, '.gettype($repo).' given'); } if (!isset($repo['type'])) { throw new \UnexpectedValueException('Repository '.$index.' ('.json_encode($repo).') must have a type defined'); } $name = is_int($index) && isset($repo['url']) ? preg_replace('{^https?://}i', '', $repo['url']) : $index; while (isset($repos[$name])) { $name .= '2'; } $repos[$name] = $rm->createRepository($repo['type'], $repo); } return $repos; } public function createComposer(IOInterface $io, $localConfig = null) { if (null === $localConfig) { $localConfig = static::getComposerFile(); } if (is_string($localConfig)) { $composerFile = $localConfig; $file = new JsonFile($localConfig, new RemoteFilesystem($io)); if (!$file->exists()) { if ($localConfig === 'composer.json') { $message = 'Composer could not find a composer.json file in '.getcwd(); } else { $message = 'Composer could not find the config file: '.$localConfig; } $instructions = 'To initialize a project, please create a composer.json file as described in the http://getcomposer.org/ "Getting Started" section'; throw new \InvalidArgumentException($message.PHP_EOL.$instructions); } $file->validateSchema(JsonFile::LAX_SCHEMA); $localConfig = $file->read(); } $config = static::createConfig(); $config->merge($localConfig); if ($tokens = $config->get('github-oauth')) { foreach ($tokens as $domain => $token) { if (!preg_match('{^[a-z0-9]+$}', $token)) { throw new \UnexpectedValueException('Your github oauth token for '.$domain.' contains invalid characters: "'.$token.'"'); } $io->setAuthentication($domain, $token, 'x-oauth-basic'); } } $vendorDir = $config->get('vendor-dir'); $binDir = $config->get('bin-dir'); ProcessExecutor::setTimeout((int) $config->get('process-timeout')); $rm = $this->createRepositoryManager($io, $config); $this->addLocalRepository($rm, $vendorDir); $loader = new Package\Loader\RootPackageLoader($rm, $config); $package = $loader->load($localConfig); $dm = $this->createDownloadManager($io, $config); $im = $this->createInstallationManager(); $composer = new Composer(); $composer->setConfig($config); $composer->setPackage($package); $composer->setRepositoryManager($rm); $composer->setDownloadManager($dm); $composer->setInstallationManager($im); $dispatcher = new EventDispatcher($composer, $io); $composer->setEventDispatcher($dispatcher); $generator = new AutoloadGenerator($dispatcher); $composer->setAutoloadGenerator($generator); $this->createDefaultInstallers($im, $composer, $io); $this->purgePackages($rm, $im); if (isset($composerFile)) { $lockFile = "json" === pathinfo($composerFile, PATHINFO_EXTENSION) ? substr($composerFile, 0, -4).'lock' : $composerFile . '.lock'; $locker = new Package\Locker(new JsonFile($lockFile, new RemoteFilesystem($io)), $rm, $im, md5_file($composerFile)); $composer->setLocker($locker); } return $composer; } protected function createRepositoryManager(IOInterface $io, Config $config) { $rm = new RepositoryManager($io, $config); $rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository'); $rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository'); $rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository'); $rm->setRepositoryClass('pear', 'Composer\Repository\PearRepository'); $rm->setRepositoryClass('git', 'Composer\Repository\VcsRepository'); $rm->setRepositoryClass('svn', 'Composer\Repository\VcsRepository'); $rm->setRepositoryClass('hg', 'Composer\Repository\VcsRepository'); $rm->setRepositoryClass('artifact', 'Composer\Repository\ArtifactRepository'); return $rm; } protected function addLocalRepository(RepositoryManager $rm, $vendorDir) { $rm->setLocalRepository(new Repository\InstalledFilesystemRepository(new JsonFile($vendorDir.'/composer/installed.json'))); } public function createDownloadManager(IOInterface $io, Config $config) { $cache = null; if ($config->get('cache-files-ttl') > 0) { $cache = new Cache($io, $config->get('cache-files-dir'), 'a-z0-9_./'); } $dm = new Downloader\DownloadManager(); $dm->setDownloader('git', new Downloader\GitDownloader($io, $config)); $dm->setDownloader('svn', new Downloader\SvnDownloader($io, $config)); $dm->setDownloader('hg', new Downloader\HgDownloader($io, $config)); $dm->setDownloader('zip', new Downloader\ZipDownloader($io, $config, $cache)); $dm->setDownloader('tar', new Downloader\TarDownloader($io, $config, $cache)); $dm->setDownloader('phar', new Downloader\PharDownloader($io, $config, $cache)); $dm->setDownloader('file', new Downloader\FileDownloader($io, $config, $cache)); return $dm; } public function createArchiveManager(Config $config, Downloader\DownloadManager $dm = null) { if (null === $dm) { $dm = $this->createDownloadManager(new IO\NullIO(), $config); } $am = new Archiver\ArchiveManager($dm); $am->addArchiver(new Archiver\PharArchiver); return $am; } protected function createInstallationManager() { return new Installer\InstallationManager(); } protected function createDefaultInstallers(Installer\InstallationManager $im, Composer $composer, IOInterface $io) { $im->addInstaller(new Installer\LibraryInstaller($io, $composer, null)); $im->addInstaller(new Installer\PearInstaller($io, $composer, 'pear-library')); $im->addInstaller(new Installer\InstallerInstaller($io, $composer)); $im->addInstaller(new Installer\MetapackageInstaller($io)); } protected function purgePackages(Repository\RepositoryManager $rm, Installer\InstallationManager $im) { $repo = $rm->getLocalRepository(); foreach ($repo->getPackages() as $package) { if (!$im->isPackageInstalled($repo, $package)) { $repo->removePackage($package); } } } public static function create(IOInterface $io, $config = null) { $factory = new static(); return $factory->createComposer($io, $config); } } processExecutor = $executor ?: new ProcessExecutor(); } public function remove($file) { if (is_dir($file)) { return $this->removeDirectory($file); } if (file_exists($file)) { return unlink($file); } return false; } public function removeDirectory($directory) { if (!is_dir($directory)) { return true; } if (!function_exists('proc_open')) { return $this->removeDirectoryPhp($directory); } if (defined('PHP_WINDOWS_VERSION_BUILD')) { $cmd = sprintf('rmdir /S /Q %s', escapeshellarg(realpath($directory))); } else { $cmd = sprintf('rm -rf %s', escapeshellarg($directory)); } $result = $this->getProcess()->execute($cmd, $output) === 0; clearstatcache(); return $result && !is_dir($directory); } public function removeDirectoryPhp($directory) { $it = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS); $ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); foreach ($ri as $file) { if ($file->isDir()) { rmdir($file->getPathname()); } else { unlink($file->getPathname()); } } return rmdir($directory); } public function ensureDirectoryExists($directory) { if (!is_dir($directory)) { if (file_exists($directory)) { throw new \RuntimeException( $directory.' exists and is not a directory.' ); } if (!@mkdir($directory, 0777, true)) { throw new \RuntimeException( $directory.' does not exist and could not be created.' ); } } } public function copyThenRemove($source, $target) { $it = new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS); $ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST); if (!file_exists($target)) { mkdir($target, 0777, true); } foreach ($ri as $file) { $targetPath = $target . DIRECTORY_SEPARATOR . $ri->getSubPathName(); if ($file->isDir()) { mkdir($targetPath); } else { copy($file->getPathname(), $targetPath); } } $this->removeDirectoryPhp($source); } public function rename($source, $target) { if (true === @rename($source, $target)) { return; } if (!function_exists('proc_open')) { return $this->copyThenRemove($source, $target); } if (defined('PHP_WINDOWS_VERSION_BUILD')) { $command = sprintf('xcopy %s %s /E /I /Q', escapeshellarg($source), escapeshellarg($target)); $result = $this->processExecutor->execute($command, $output); clearstatcache(); if (0 === $result) { $this->remove($source); return; } return $this->copyThenRemove($source, $target); } else { $command = sprintf('mv %s %s', escapeshellarg($source), escapeshellarg($target)); $result = $this->processExecutor->execute($command, $output); clearstatcache(); if (0 === $result) { return; } } throw new \RuntimeException(sprintf('Could not rename "%s" to "%s".', $source, $target)); } public function findShortestPath($from, $to, $directories = false) { if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) { throw new \InvalidArgumentException(sprintf('$from (%s) and $to (%s) must be absolute paths.', $from, $to)); } $from = lcfirst($this->normalizePath($from)); $to = lcfirst($this->normalizePath($to)); if ($directories) { $from .= '/dummy_file'; } if (dirname($from) === dirname($to)) { return './'.basename($to); } $commonPath = $to; while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) { $commonPath = strtr(dirname($commonPath), '\\', '/'); } if (0 !== strpos($from, $commonPath) || '/' === $commonPath || '.' === $commonPath) { return $to; } $commonPath = rtrim($commonPath, '/') . '/'; $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/'); $commonPathCode = str_repeat('../', $sourcePathDepth); return ($commonPathCode . substr($to, strlen($commonPath))) ?: './'; } public function findShortestPathCode($from, $to, $directories = false) { if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) { throw new \InvalidArgumentException(sprintf('$from (%s) and $to (%s) must be absolute paths.', $from, $to)); } $from = lcfirst($this->normalizePath($from)); $to = lcfirst($this->normalizePath($to)); if ($from === $to) { return $directories ? '__DIR__' : '__FILE__'; } $commonPath = $to; while (strpos($from, $commonPath) !== 0 && '/' !== $commonPath && !preg_match('{^[a-z]:/?$}i', $commonPath) && '.' !== $commonPath) { $commonPath = strtr(dirname($commonPath), '\\', '/'); } if (0 !== strpos($from, $commonPath) || '/' === $commonPath || '.' === $commonPath) { return var_export($to, true); } $commonPath = rtrim($commonPath, '/') . '/'; if (strpos($to, $from.'/') === 0) { return '__DIR__ . '.var_export(substr($to, strlen($from)), true); } $sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/') + $directories; $commonPathCode = str_repeat('dirname(', $sourcePathDepth).'__DIR__'.str_repeat(')', $sourcePathDepth); $relTarget = substr($to, strlen($commonPath)); return $commonPathCode . (strlen($relTarget) ? '.' . var_export('/' . $relTarget, true) : ''); } public function isAbsolutePath($path) { return substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':'; } public function size($path) { if (!file_exists($path)) { throw new \RuntimeException("$path does not exist."); } if (is_dir($path)) { return $this->directorySize($path); } return filesize($path); } public function normalizePath($path) { $parts = array(); $path = strtr($path, '\\', '/'); $prefix = ''; $absolute = false; if (preg_match('{^([0-9a-z]+:(?://(?:[a-z]:)?)?)}i', $path, $match)) { $prefix = $match[1]; $path = substr($path, strlen($prefix)); } if (substr($path, 0, 1) === '/') { $absolute = true; $path = substr($path, 1); } $up = false; foreach (explode('/', $path) as $chunk) { if ('..' === $chunk && ($absolute || $up)) { array_pop($parts); $up = !(empty($parts) || '..' === end($parts)); } elseif ('.' !== $chunk && '' !== $chunk) { $parts[] = $chunk; $up = '..' !== $chunk; } } return $prefix.($absolute ? '/' : '').implode('/', $parts); } protected function directorySize($directory) { $it = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS); $ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); $size = 0; foreach ($ri as $file) { if ($file->isFile()) { $size += $file->getSize(); } } return $size; } protected function getProcess() { return new ProcessExecutor; } } io = $io; $this->config = $config; $this->process = $process ?: new ProcessExecutor; $this->remoteFilesystem = $remoteFilesystem ?: new RemoteFilesystem($io); } public function authorizeOAuth($originUrl) { if ('github.com' !== $originUrl) { return false; } if (0 === $this->process->execute('git config github.accesstoken', $output)) { $this->io->setAuthentication($originUrl, trim($output), 'x-oauth-basic'); return true; } return false; } public function authorizeOAuthInteractively($originUrl, $message = null) { $attemptCounter = 0; if ($message) { $this->io->write($message); } $this->io->write('The credentials will be swapped for an OAuth token stored in '.$this->config->get('home').'/config.json, your password will not be stored'); $this->io->write('To revoke access to this token you can visit https://github.com/settings/applications'); while ($attemptCounter++ < 5) { try { $username = $this->io->ask('Username: '); $password = $this->io->askAndHideAnswer('Password: '); $this->io->setAuthentication($originUrl, $username, $password); $appName = 'Composer'; if (0 === $this->process->execute('hostname', $output)) { $appName .= ' on ' . trim($output); } $contents = JsonFile::parseJson($this->remoteFilesystem->getContents($originUrl, 'https://api.github.com/authorizations', false, array( 'http' => array( 'method' => 'POST', 'follow_location' => false, 'header' => "Content-Type: application/json\r\n", 'content' => json_encode(array( 'scopes' => array('repo'), 'note' => $appName, 'note_url' => 'https://getcomposer.org/', )), ) ))); } catch (TransportException $e) { if (in_array($e->getCode(), array(403, 401))) { $this->io->write('Invalid credentials.'); continue; } throw $e; } $this->io->setAuthentication($originUrl, $contents['token'], 'x-oauth-basic'); $githubTokens = $this->config->get('github-oauth') ?: array(); $githubTokens[$originUrl] = $contents['token']; $this->config->getConfigSource()->addConfigSetting('github-oauth', $githubTokens); return true; } throw new \RuntimeException("Invalid GitHub credentials 5 times in a row, aborting."); } } io = $io; } public function execute($command, &$output = null, $cwd = null) { $this->captureOutput = count(func_get_args()) > 1; $this->errorOutput = null; $process = new Process($command, $cwd, null, null, static::getTimeout()); if ($this->io && $this->io->isDebug()) { $safeCommand = preg_replace('{(://[^:/\s]+:)[^@\s/]+}i', '$1****', $command); $this->io->write('Executing command ('.($cwd ?: 'CWD').'): '.$safeCommand); } $callback = is_callable($output) ? $output : array($this, 'outputHandler'); $process->run($callback); if ($this->captureOutput && !is_callable($output)) { $output = $process->getOutput(); } $this->errorOutput = $process->getErrorOutput(); return $process->getExitCode(); } public function splitLines($output) { return ((string) $output === '') ? array() : preg_split('{\r?\n}', $output); } public function getErrorOutput() { return $this->errorOutput; } public function outputHandler($type, $buffer) { if ($this->captureOutput) { return; } echo $buffer; } public static function getTimeout() { return static::$timeout; } public static function setTimeout($timeout) { static::$timeout = $timeout; } } io = $io; $this->options = $options; } public function copy($originUrl, $fileUrl, $fileName, $progress = true, $options = array()) { return $this->get($originUrl, $fileUrl, $options, $fileName, $progress); } public function getContents($originUrl, $fileUrl, $progress = true, $options = array()) { return $this->get($originUrl, $fileUrl, $options, null, $progress); } protected function get($originUrl, $fileUrl, $additionalOptions = array(), $fileName = null, $progress = true) { $this->bytesMax = 0; $this->originUrl = $originUrl; $this->fileUrl = $fileUrl; $this->fileName = $fileName; $this->progress = $progress; $this->lastProgress = null; $options = $this->getOptionsForUrl($originUrl, $additionalOptions); if ($this->io->isDebug()) { $this->io->write('Downloading '.$fileUrl); } if (isset($options['github-token'])) { $fileUrl .= (false === strpos($fileUrl, '?') ? '?' : '&') . 'access_token='.$options['github-token']; unset($options['github-token']); } $ctx = StreamContextFactory::getContext($options, array('notification' => array($this, 'callbackGet'))); if ($this->progress) { $this->io->write(" Downloading: connection...", false); } $errorMessage = ''; $errorCode = 0; $result = false; set_error_handler(function ($code, $msg) use (&$errorMessage) { if ($errorMessage) { $errorMessage .= "\n"; } $errorMessage .= preg_replace('{^file_get_contents\(.*?\): }', '', $msg); }); try { $result = file_get_contents($fileUrl, false, $ctx); } catch (\Exception $e) { if ($e instanceof TransportException && !empty($http_response_header[0])) { $e->setHeaders($http_response_header); } } if ($errorMessage && !ini_get('allow_url_fopen')) { $errorMessage = 'allow_url_fopen must be enabled in php.ini ('.$errorMessage.')'; } restore_error_handler(); if (isset($e) && !$this->retry) { throw $e; } if (!empty($http_response_header[0]) && preg_match('{^HTTP/\S+ ([45]\d\d)}i', $http_response_header[0], $match)) { $result = false; $errorCode = $match[1]; } if ($result && extension_loaded('zlib') && substr($fileUrl, 0, 4) === 'http') { $decode = false; foreach ($http_response_header as $header) { if (preg_match('{^content-encoding: *gzip *$}i', $header)) { $decode = true; continue; } elseif (preg_match('{^HTTP/}i', $header)) { $decode = false; } } if ($decode) { if (version_compare(PHP_VERSION, '5.4.0', '>=')) { $result = zlib_decode($result); } else { $result = file_get_contents('compress.zlib://data:application/octet-stream;base64,'.base64_encode($result)); } } } if ($this->progress) { $this->io->overwrite(" Downloading: 100%"); } if (false !== $result && null !== $fileName) { if ('' === $result) { throw new TransportException('"'.$this->fileUrl.'" appears broken, and returned an empty 200 response'); } $errorMessage = ''; set_error_handler(function ($code, $msg) use (&$errorMessage) { if ($errorMessage) { $errorMessage .= "\n"; } $errorMessage .= preg_replace('{^file_put_contents\(.*?\): }', '', $msg); }); $result = (bool) file_put_contents($fileName, $result); restore_error_handler(); if (false === $result) { throw new TransportException('The "'.$this->fileUrl.'" file could not be written to '.$fileName.': '.$errorMessage); } } if ($this->retry) { $this->retry = false; return $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress); } if (false === $result) { $e = new TransportException('The "'.$this->fileUrl.'" file could not be downloaded: '.$errorMessage, $errorCode); if (!empty($http_response_header[0])) { $e->setHeaders($http_response_header); } throw $e; } return $result; } protected function callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax) { switch ($notificationCode) { case STREAM_NOTIFY_FAILURE: case STREAM_NOTIFY_AUTH_REQUIRED: if (401 === $messageCode) { if (!$this->io->isInteractive()) { $message = "The '" . $this->fileUrl . "' URL required authentication.\nYou must be using the interactive console"; throw new TransportException($message, 401); } $this->io->overwrite(' Authentication required ('.parse_url($this->fileUrl, PHP_URL_HOST).'):'); $username = $this->io->ask(' Username: '); $password = $this->io->askAndHideAnswer(' Password: '); $this->io->setAuthentication($this->originUrl, $username, $password); $this->retry = true; throw new TransportException('RETRY'); break; } if ($notificationCode === STREAM_NOTIFY_AUTH_REQUIRED) { break; } throw new TransportException('The "'.$this->fileUrl.'" file could not be downloaded ('.trim($message).')', $messageCode); case STREAM_NOTIFY_AUTH_RESULT: if (403 === $messageCode) { $message = "The '" . $this->fileUrl . "' URL could not be accessed: " . $message; throw new TransportException($message, 403); } break; case STREAM_NOTIFY_FILE_SIZE_IS: if ($this->bytesMax < $bytesMax) { $this->bytesMax = $bytesMax; } break; case STREAM_NOTIFY_PROGRESS: if ($this->bytesMax > 0 && $this->progress) { $progression = 0; if ($this->bytesMax > 0) { $progression = round($bytesTransferred / $this->bytesMax * 100); } if ((0 === $progression % 5) && $progression !== $this->lastProgress) { $this->lastProgress = $progression; $this->io->overwrite(" Downloading: $progression%", false); } } break; default: break; } } protected function getOptionsForUrl($originUrl, $additionalOptions) { $headers = array( sprintf( 'User-Agent: Composer/%s (%s; %s; PHP %s.%s.%s)', Composer::VERSION === '1.0.0-alpha7' ? 'source' : Composer::VERSION, php_uname('s'), php_uname('r'), PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION ) ); if (extension_loaded('zlib')) { $headers[] = 'Accept-Encoding: gzip'; } $options = array_replace_recursive($this->options, $additionalOptions); if ($this->io->hasAuthentication($originUrl)) { $auth = $this->io->getAuthentication($originUrl); if ('github.com' === $originUrl && 'x-oauth-basic' === $auth['password']) { $options['github-token'] = $auth['username']; } else { $authStr = base64_encode($auth['username'] . ':' . $auth['password']); $headers[] = 'Authorization: Basic '.$authStr; } } if (isset($options['http']['header']) && !is_array($options['http']['header'])) { $options['http']['header'] = explode("\r\n", trim($options['http']['header'], "\r\n")); } foreach ($headers as $header) { $options['http']['header'][] = $header; } return $options; } } array( 'follow_location' => 1, 'max_redirects' => 20, )); if (!empty($_SERVER['HTTP_PROXY']) || !empty($_SERVER['http_proxy'])) { $proxy = parse_url(!empty($_SERVER['http_proxy']) ? $_SERVER['http_proxy'] : $_SERVER['HTTP_PROXY']); } if (!empty($proxy)) { $proxyURL = isset($proxy['scheme']) ? $proxy['scheme'] . '://' : ''; $proxyURL .= isset($proxy['host']) ? $proxy['host'] : ''; if (isset($proxy['port'])) { $proxyURL .= ":" . $proxy['port']; } elseif ('http://' == substr($proxyURL, 0, 7)) { $proxyURL .= ":80"; } elseif ('https://' == substr($proxyURL, 0, 8)) { $proxyURL .= ":443"; } $proxyURL = str_replace(array('http://', 'https://'), array('tcp://', 'ssl://'), $proxyURL); if (0 === strpos($proxyURL, 'ssl:') && !extension_loaded('openssl')) { throw new \RuntimeException('You must enable the openssl extension to use a proxy over https'); } $options['http']['proxy'] = $proxyURL; $reqFullUriEnv = getenv('HTTP_PROXY_REQUEST_FULLURI'); if ($reqFullUriEnv === false || $reqFullUriEnv === '' || (strtolower($reqFullUriEnv) !== 'false' && (bool) $reqFullUriEnv)) { $options['http']['request_fulluri'] = true; } if (isset($proxy['user'])) { $auth = $proxy['user']; if (isset($proxy['pass'])) { $auth .= ':' . $proxy['pass']; } $auth = base64_encode($auth); if (isset($defaultOptions['http']['header'])) { if (is_string($defaultOptions['http']['header'])) { $defaultOptions['http']['header'] = array($defaultOptions['http']['header']); } $defaultOptions['http']['header'][] = "Proxy-Authorization: Basic {$auth}"; } else { $options['http']['header'] = array("Proxy-Authorization: Basic {$auth}"); } } } $options = array_replace_recursive($options, $defaultOptions); if (isset($options['http']['header'])) { $options['http']['header'] = self::fixHttpHeaderField($options['http']['header']); } return stream_context_create($options, $defaultParams); } private static function fixHttpHeaderField($header) { if (!is_array($header)) { $header = explode("\r\n", $header); } uasort($header, function ($el) { return preg_match('{^content-type}i', $el) ? 1 : -1; }); return $header; } } io = $io; } public function validate($file) { $errors = array(); $publishErrors = array(); $warnings = array(); $laxValid = false; $valid = false; try { $json = new JsonFile($file, new RemoteFilesystem($this->io)); $manifest = $json->read(); $json->validateSchema(JsonFile::LAX_SCHEMA); $laxValid = true; $json->validateSchema(); $valid = true; } catch (JsonValidationException $e) { foreach ($e->getErrors() as $message) { if ($laxValid) { $publishErrors[] = $message; } else { $errors[] = $message; } } } catch (\Exception $e) { $errors[] = $e->getMessage(); return array($errors, $publishErrors, $warnings); } if (!empty($manifest['license'])) { if (is_array($manifest['license'])) { foreach ($manifest['license'] as $key => $license) { if ('proprietary' === $license) { unset($manifest['license'][$key]); } } } $licenseValidator = new SpdxLicenseIdentifier(); if ('proprietary' !== $manifest['license'] && array() !== $manifest['license'] && !$licenseValidator->validate($manifest['license'])) { $warnings[] = sprintf( 'License %s is not a valid SPDX license identifier, see http://www.spdx.org/licenses/ if you use an open license.' ."\nIf the software is closed-source, you may use \"proprietary\" as license.", json_encode($manifest['license']) ); } } else { $warnings[] = 'No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.'; } if (!empty($manifest['name']) && preg_match('{[A-Z]}', $manifest['name'])) { $suggestName = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $manifest['name']); $suggestName = strtolower($suggestName); $warnings[] = sprintf( 'Name "%s" does not match the best practice (e.g. lower-cased/with-dashes). We suggest using "%s" instead. As such you will not be able to submit it to Packagist.', $manifest['name'], $suggestName ); } try { $loader = new ValidatingArrayLoader(new ArrayLoader()); if (!isset($manifest['version'])) { $manifest['version'] = '1.0.0'; } if (!isset($manifest['name'])) { $manifest['name'] = 'dummy/dummy'; } $loader->load($manifest); } catch (InvalidPackageException $e) { $errors = array_merge($errors, $e->getErrors()); } $warnings = array_merge($warnings, $loader->getWarnings()); return array($errors, $publishErrors, $warnings); } } initIdentifiers(); } public function validate($license) { if (is_array($license)) { $count = count($license); if ($count !== count(array_filter($license, 'is_string'))) { throw new \InvalidArgumentException('Array of strings expected.'); } $license = $count > 1 ? '('.implode(' or ', $license).')' : (string) reset($license); } if (!is_string($license)) { throw new \InvalidArgumentException(sprintf( 'Array or String expected, %s given.', gettype($license) )); } return $this->isValidLicenseString($license); } private function initIdentifiers() { $jsonFile = new JsonFile(__DIR__ . '/../../../res/spdx-identifier.json'); $this->identifiers = $jsonFile->read(); } private function isValidLicenseIdentifier($identifier) { return in_array($identifier, $this->identifiers); } private function isValidLicenseString($license) { $tokens = array( 'po' => '\(', 'pc' => '\)', 'op' => '(?:or|and)', 'lix' => '(?:NONE|NOASSERTION)', 'lir' => 'LicenseRef-\d+', 'lic' => '[-+_.a-zA-Z0-9]{3,}', 'ws' => '\s+', '_' => '.', ); $next = function () use ($license, $tokens) { static $offset = 0; if ($offset >= strlen($license)) { return null; } foreach ($tokens as $name => $token) { if (false === $r = preg_match('{' . $token . '}', $license, $matches, PREG_OFFSET_CAPTURE, $offset)) { throw new \RuntimeException('Pattern for token %s failed (regex error).', $name); } if ($r === 0) { continue; } if ($matches[0][1] !== $offset) { continue; } $offset += strlen($matches[0][0]); return array($name, $matches[0][0]); } throw new \RuntimeException('At least the last pattern needs to match, but it did not (dot-match-all is missing?).'); }; $open = 0; $require = 1; $lastop = null; while (list($token, $string) = $next()) { switch ($token) { case 'po': if ($open || !$require) { return false; } $open = 1; break; case 'pc': if ($open !== 1 || $require || !$lastop) { return false; } $open = 2; break; case 'op': if ($require || !$open) { return false; } $lastop || $lastop = $string; if ($lastop !== $string) { return false; } $require = 1; break; case 'lix': if ($open) { return false; } goto lir; case 'lic': if (!$this->isValidLicenseIdentifier($string)) { return false; } case 'lir': lir: if (!$require) { return false; } $require = 0; break; case 'ws': break; case '_': return false; default: throw new \RuntimeException(sprintf('Unparsed token: %s.', print_r($token, true))); } } return !($open % 2 || $require); } } url = $url; $this->io = $io; $this->process = $process ?: new ProcessExecutor; } public function execute($command, $url, $cwd = null, $path = null, $verbose = false) { $svnCommand = $this->getCommand($command, $url, $path); $output = null; $io = $this->io; $handler = function ($type, $buffer) use (&$output, $io, $verbose) { if ($type !== 'out') { return; } $output .= $buffer; if ($verbose) { $io->write($buffer, false); } }; $status = $this->process->execute($svnCommand, $handler, $cwd); if (0 === $status) { return $output; } if (empty($output)) { $output = $this->process->getErrorOutput(); } if (false === stripos($output, 'Could not authenticate to server:')) { throw new \RuntimeException($output); } if (!$this->io->isInteractive()) { throw new \RuntimeException( 'can not ask for authentication in non interactive mode ('.$output.')' ); } if (!$this->hasAuth()) { $this->doAuthDance(); return $this->execute($command, $url, $cwd, $path, $verbose); } throw new \RuntimeException( 'wrong credentials provided ('.$output.')' ); } protected function doAuthDance() { $this->io->write("The Subversion server ({$this->url}) requested credentials:"); $this->hasAuth = true; $this->credentials['username'] = $this->io->ask("Username: "); $this->credentials['password'] = $this->io->askAndHideAnswer("Password: "); $this->cacheCredentials = $this->io->askConfirmation("Should Subversion cache these credentials? (yes/no) ", true); return $this; } protected function getCommand($cmd, $url, $path = null) { $cmd = sprintf('%s %s%s %s', $cmd, '--non-interactive ', $this->getCredentialString(), escapeshellarg($url) ); if ($path) { $cmd .= ' ' . escapeshellarg($path); } return $cmd; } protected function getCredentialString() { if (!$this->hasAuth()) { return ''; } return sprintf( ' %s--username %s --password %s ', $this->getAuthCache(), escapeshellarg($this->getUsername()), escapeshellarg($this->getPassword()) ); } protected function getPassword() { if ($this->credentials === null) { throw new \LogicException("No svn auth detected."); } return isset($this->credentials['password']) ? $this->credentials['password'] : ''; } protected function getUsername() { if ($this->credentials === null) { throw new \LogicException("No svn auth detected."); } return $this->credentials['username']; } protected function hasAuth() { if (null !== $this->hasAuth) { return $this->hasAuth; } $uri = parse_url($this->url); if (empty($uri['user'])) { return $this->hasAuth = false; } $this->credentials['username'] = $uri['user']; if (!empty($uri['pass'])) { $this->credentials['password'] = $uri['pass']; } return $this->hasAuth = true; } protected function getAuthCache() { return $this->cacheCredentials ? '' : '--no-auth-cache '; } } package = $package; } public function getPackage() { return $this->package; } public function setConfig(Config $config) { $this->config = $config; } public function getConfig() { return $this->config; } public function setLocker(Locker $locker) { $this->locker = $locker; } public function getLocker() { return $this->locker; } public function setRepositoryManager(RepositoryManager $manager) { $this->repositoryManager = $manager; } public function getRepositoryManager() { return $this->repositoryManager; } public function setDownloadManager(DownloadManager $manager) { $this->downloadManager = $manager; } public function getDownloadManager() { return $this->downloadManager; } public function setInstallationManager(InstallationManager $manager) { $this->installationManager = $manager; } public function getInstallationManager() { return $this->installationManager; } public function setEventDispatcher(EventDispatcher $eventDispatcher) { $this->eventDispatcher = $eventDispatcher; } public function getEventDispatcher() { return $this->eventDispatcher; } public function setAutoloadGenerator(AutoloadGenerator $autoloadGenerator) { $this->autoloadGenerator = $autoloadGenerator; } public function getAutoloadGenerator() { return $this->autoloadGenerator; } } newline = false !== strpos($contents, "\r\n") ? "\r\n": "\n"; $this->contents = $contents === '{}' ? '{' . $this->newline . '}' : $contents; $this->detectIndenting(); } public function getContents() { return $this->contents . $this->newline; } public function addLink($type, $package, $constraint) { if (!preg_match('#"'.$type.'":\s*\{#', $this->contents)) { $this->addMainKey($type, $this->format(array($package => $constraint))); return true; } $linksRegex = '#("'.$type.'":\s*\{)([^}]+)(\})#s'; if (!preg_match($linksRegex, $this->contents, $match)) { return false; } $links = $match[2]; $packageRegex = str_replace('/', '\\\\?/', preg_quote($package)); if (preg_match('{"'.$packageRegex.'"\s*:}i', $links)) { $links = preg_replace('{"'.$packageRegex.'"(\s*:\s*)"[^"]+"}i', addcslashes(JsonFile::encode($package).'${1}"'.$constraint.'"', '\\'), $links); } elseif (preg_match('#[^\s](\s*)$#', $links, $match)) { $links = preg_replace( '#'.$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) . $links; } $this->contents = preg_replace($linksRegex, addcslashes('${1}'.$links.'$3', '\\'), $this->contents); return true; } public function addRepository($name, $config) { return $this->addSubNode('repositories', $name, $config); } public function removeRepository($name) { return $this->removeSubNode('repositories', $name); } public function addConfigSetting($name, $value) { return $this->addSubNode('config', $name, $value); } public function removeConfigSetting($name) { return $this->removeSubNode('config', $name); } public function addSubNode($mainNode, $name, $value) { if (!preg_match('#"'.$mainNode.'":\s*\{#', $this->contents)) { $this->addMainKey(''.$mainNode.'', $this->format(array($name => $value))); return true; } $subName = null; if (false !== strpos($name, '.')) { list($name, $subName) = explode('.', $name, 2); } $nodeRegex = '#("'.$mainNode.'":\s*\{)('.self::$RECURSE_BLOCKS.')(\})#s'; if (!preg_match($nodeRegex, $this->contents, $match)) { return false; } $children = $match[2]; if (!json_decode('{'.$children.'}')) { return false; } $that = $this; if (preg_match('{("'.preg_quote($name).'"\s*:\s*)([0-9.]+|null|true|false|"[^"]+"|\[[^\]]*\]|\{'.self::$RECURSE_BLOCKS.'\})(,?)}', $children, $matches)) { $children = preg_replace_callback('{("'.preg_quote($name).'"\s*:\s*)([0-9.]+|null|true|false|"[^"]+"|\[[^\]]*\]|\{'.self::$RECURSE_BLOCKS.'\})(,?)}', function ($matches) use ($name, $subName, $value, $that) { if ($subName !== null) { $curVal = json_decode($matches[2], true); $curVal[$subName] = $value; $value = $curVal; } return $matches[1] . $that->format($value, 1) . $matches[3]; }, $children); } elseif (preg_match('#[^\s](\s*)$#', $children, $match)) { if ($subName !== null) { $value = array($subName => $value); } $children = preg_replace( '#'.$match[1].'$#', addcslashes(',' . $this->newline . $this->indent . $this->indent . JsonFile::encode($name).': '.$this->format($value, 1) . $match[1], '\\'), $children ); } else { if ($subName !== null) { $value = array($subName => $value); } $children = $this->newline . $this->indent . $this->indent . JsonFile::encode($name).': '.$this->format($value, 1) . $children; } $this->contents = preg_replace($nodeRegex, addcslashes('${1}'.$children.'$3', '\\'), $this->contents); return true; } public function removeSubNode($mainNode, $name) { if (!preg_match('#"'.$mainNode.'":\s*\{#', $this->contents)) { return true; } if (preg_match('#"'.$mainNode.'":\s*\{\s*\}#s', $this->contents)) { return true; } $nodeRegex = '#("'.$mainNode.'":\s*\{)('.self::$RECURSE_BLOCKS.')(\})#s'; if (!preg_match($nodeRegex, $this->contents, $match)) { return false; } $children = $match[2]; if (!json_decode('{'.$children.'}')) { return false; } $subName = null; if (false !== strpos($name, '.')) { list($name, $subName) = explode('.', $name, 2); } if (preg_match('{"'.preg_quote($name).'"\s*:}i', $children)) { if (preg_match_all('{"'.preg_quote($name).'"\s*:\s*(?:[0-9.]+|null|true|false|"[^"]+"|\[[^\]]*\]|\{'.self::$RECURSE_BLOCKS.'\})}', $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; } } } } if (!trim($childrenClean)) { $this->contents = preg_replace($nodeRegex, '$1'.$this->newline.$this->indent.'}', $this->contents); if ($subName !== null) { $curVal = json_decode('{'.$children.'}', true); unset($curVal[$name][$subName]); $this->addSubNode($mainNode, $name, $curVal[$name]); } return true; } $that = $this; $this->contents = preg_replace_callback($nodeRegex, function ($matches) use ($that, $name, $subName, $childrenClean) { if ($subName !== null) { $curVal = json_decode('{'.$matches[2].'}', true); unset($curVal[$name][$subName]); $childrenClean = substr($that->format($curVal, 0), 1, -1); } return $matches[1] . $childrenClean . $matches[3]; }, $this->contents); return true; } public function addMainKey($key, $content) { if (preg_match('#[^{\s](\s*)\}$#', $this->contents, $match)) { $this->contents = preg_replace( '#'.$match[1].'\}$#', addcslashes(',' . $this->newline . $this->indent . JsonFile::encode($key). ': '. $content . $this->newline . '}', '\\'), $this->contents ); } else { $this->contents = preg_replace( '#\}$#', addcslashes($this->indent . JsonFile::encode($key). ': '.$content . $this->newline . '}', '\\'), $this->contents ); } } public function format($data, $depth = 0) { 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 = array(); 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() { if (preg_match('{^(\s+)"}m', $this->contents, $match)) { $this->indent = $match[1]; } else { $this->indent = ' '; } } } path = $path; if (null === $rfs && preg_match('{^https?://}i', $path)) { throw new \InvalidArgumentException('http urls require a RemoteFilesystem instance to be passed'); } $this->rfs = $rfs; } public function getPath() { return $this->path; } public function exists() { return is_file($this->path); } public function read() { try { if ($this->rfs) { $json = $this->rfs->getContents($this->path, $this->path, false); } else { $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()); } return static::parseJson($json, $this->path); } public function write(array $hash, $options = 448) { $dir = dirname($this->path); if (!is_dir($dir)) { if (file_exists($dir)) { throw new \UnexpectedValueException( $dir.' exists and is not a directory.' ); } if (!@mkdir($dir, 0777, true)) { throw new \UnexpectedValueException( $dir.' does not exist and could not be created.' ); } } file_put_contents($this->path, static::encode($hash, $options). ($options & self::JSON_PRETTY_PRINT ? "\n" : '')); } public function validateSchema($schema = self::STRICT_SCHEMA) { $content = file_get_contents($this->path); $data = json_decode($content); if (null === $data && 'null' !== $content) { self::validateSyntax($content, $this->path); } $schemaFile = __DIR__ . '/../../../res/composer-schema.json'; $schemaData = json_decode(file_get_contents($schemaFile)); if ($schema === self::LAX_SCHEMA) { $schemaData->additionalProperties = true; $schemaData->properties->name->required = false; $schemaData->properties->description->required = false; } $validator = new Validator(); $validator->check($data, $schemaData); if (!$validator->isValid()) { $errors = array(); foreach ((array) $validator->getErrors() as $error) { $errors[] = ($error['property'] ? $error['property'].' : ' : '').$error['message']; } throw new JsonValidationException('"'.$this->path.'" does not match the expected JSON schema', $errors); } return true; } public static function encode($data, $options = 448) { if (version_compare(PHP_VERSION, '5.4', '>=')) { return json_encode($data, $options); } $json = json_encode($data); $prettyPrint = (bool) ($options & self::JSON_PRETTY_PRINT); $unescapeUnicode = (bool) ($options & self::JSON_UNESCAPED_UNICODE); $unescapeSlashes = (bool) ($options & self::JSON_UNESCAPED_SLASHES); if (!$prettyPrint && !$unescapeUnicode && !$unescapeSlashes) { return $json; } $result = ''; $pos = 0; $strLen = strlen($json); $indentStr = ' '; $newLine = "\n"; $outOfQuotes = true; $buffer = ''; $noescape = true; for ($i = 0; $i < $strLen; $i++) { $char = substr($json, $i, 1); if ('"' === $char && $noescape) { $outOfQuotes = !$outOfQuotes; } if (!$outOfQuotes) { $buffer .= $char; $noescape = '\\' === $char ? !$noescape : true; continue; } elseif ('' !== $buffer) { if ($unescapeSlashes) { $buffer = str_replace('\\/', '/', $buffer); } if ($unescapeUnicode && function_exists('mb_convert_encoding')) { $buffer = preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function($match) { return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE'); }, $buffer); } $result .= $buffer.$char; $buffer = ''; continue; } if (':' === $char) { $char .= ' '; } elseif (('}' === $char || ']' === $char)) { $pos--; $prevChar = substr($json, $i - 1, 1); if ('{' !== $prevChar && '[' !== $prevChar) { $result .= $newLine; for ($j = 0; $j < $pos; $j++) { $result .= $indentStr; } } else { $result = rtrim($result)."\n\n".$indentStr; } } $result .= $char; if (',' === $char || '{' === $char || '[' === $char) { $result .= $newLine; if ('{' === $char || '[' === $char) { $pos++; } for ($j = 0; $j < $pos; $j++) { $result .= $indentStr; } } } return $result; } public static function parseJson($json, $file = 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($json, $file = null) { $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()); } } errors = $errors; parent::__construct($message); } public function getErrors() { return $this->errors; } } 300, 'use-include-path' => false, 'preferred-install' => 'auto', 'notify-on-install' => true, 'github-protocols' => array('git', 'https', 'http'), 'vendor-dir' => 'vendor', 'bin-dir' => '{$vendor-dir}/bin', 'cache-dir' => '{$home}/cache', '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', 'discard-changes' => false, ); public static $defaultRepositories = array( 'packagist' => array( 'type' => 'composer', 'url' => 'https?://packagist.org', 'allow_ssl_downgrade' => true, ) ); private $config; private $repositories; private $configSource; public function __construct() { $this->config = static::$defaultConfig; $this->repositories = static::$defaultRepositories; } public function setConfigSource(ConfigSourceInterface $source) { $this->configSource = $source; } public function getConfigSource() { return $this->configSource; } public function merge(array $config) { if (!empty($config['config']) && is_array($config['config'])) { foreach ($config['config'] as $key => $val) { if (in_array($key, array('github-oauth')) && isset($this->config[$key])) { $this->config[$key] = array_merge($this->config[$key], $val); } else { $this->config[$key] = $val; } } } 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) { unset($this->repositories[$name]); continue; } if (1 === count($repository) && false === current($repository)) { unset($this->repositories[key($repository)]); continue; } if (is_int($name)) { $this->repositories[] = $repository; } else { $this->repositories[$name] = $repository; } } $this->repositories = array_reverse($this->repositories, true); } } public function getRepositories() { return $this->repositories; } public function get($key) { switch ($key) { case 'vendor-dir': case 'bin-dir': case 'process-timeout': case 'cache-dir': case 'cache-files-dir': case 'cache-repo-dir': case 'cache-vcs-dir': $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_')); return rtrim($this->process(getenv($env) ?: $this->config[$key]), '/\\'); case 'cache-ttl': return (int) $this->config[$key]; case 'cache-files-maxsize': if (!preg_match('/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i', $this->config[$key], $matches)) { throw new \RuntimeException( "Could not parse the value of 'cache-files-maxsize': {$this->config[$key]}" ); } $size = $matches[1]; if (isset($matches[2])) { switch (strtolower($matches[2])) { case 'g': $size *= 1024; case 'm': $size *= 1024; case 'k': $size *= 1024; break; } } return $size; case 'cache-files-ttl': if (isset($this->config[$key])) { return (int) $this->config[$key]; } return (int) $this->config['cache-ttl']; case 'home': return rtrim($this->process($this->config[$key]), '/\\'); case 'discard-changes': if ($env = getenv('COMPOSER_DISCARD_CHANGES')) { if (!in_array($env, array('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], array(true, false, 'stash'), true)) { throw new \RuntimeException( "Invalid value for 'discard-changes': {$this->config[$key]}. Expected true, false or stash" ); } return $this->config[$key]; default: if (!isset($this->config[$key])) { return null; } return $this->process($this->config[$key]); } } public function all() { $all = array( 'repositories' => $this->getRepositories(), ); foreach (array_keys($this->config) as $key) { $all['config'][$key] = $this->get($key); } return $all; } public function raw() { return array( 'repositories' => $this->getRepositories(), 'config' => $this->config, ); } public function has($key) { return array_key_exists($key, $this->config); } private function process($value) { $config = $this; if (!is_string($value)) { return $value; } return preg_replace_callback('#\{\$(.+)\}#', function ($match) use ($config) { return $config->get($match[1]); }, $value); } } name = $name; $this->composer = $composer; $this->io = $io; $this->devMode = $devMode; } public function getName() { return $this->name; } public function getComposer() { return $this->composer; } public function getIO() { return $this->io; } public function isDevMode() { return $this->devMode; } } operation = $operation; } public function getOperation() { return $this->operation; } } composer = $composer; $this->io = $io; $this->process = $process ?: new ProcessExecutor(); } public function dispatch($eventName, Event $event = null) { if (null == $event) { $event = new Event($eventName, $this->composer, $this->io); } $this->doDispatch($event); } public function dispatchPackageEvent($eventName, $devMode, OperationInterface $operation) { $this->doDispatch(new PackageEvent($eventName, $this->composer, $this->io, $devMode, $operation)); } public function dispatchCommandEvent($eventName, $devMode) { $this->doDispatch(new CommandEvent($eventName, $this->composer, $this->io, $devMode)); } protected function doDispatch(Event $event) { $listeners = $this->getListeners($event); foreach ($listeners as $callable) { if ($this->isPhpScript($callable)) { $className = substr($callable, 0, strpos($callable, '::')); $methodName = substr($callable, strpos($callable, '::') + 2); if (!class_exists($className)) { $this->io->write('Class '.$className.' is not autoloadable, can not call '.$event->getName().' script'); continue; } if (!is_callable($callable)) { $this->io->write('Method '.$callable.' is not callable, can not call '.$event->getName().' script'); continue; } try { $this->executeEventPhpScript($className, $methodName, $event); } catch (\Exception $e) { $message = "Script %s handling the %s event terminated with an exception"; $this->io->write(''.sprintf($message, $callable, $event->getName()).''); throw $e; } } else { if (0 !== $this->process->execute($callable)) { $event->getIO()->write(sprintf('Script %s handling the %s event returned with an error: %s', $callable, $event->getName(), $this->process->getErrorOutput())); } } } } protected function executeEventPhpScript($className, $methodName, Event $event) { $className::$methodName($event); } protected function getListeners(Event $event) { $package = $this->composer->getPackage(); $scripts = $package->getScripts(); if (empty($scripts[$event->getName()])) { return array(); } if ($this->loader) { $this->loader->unregister(); } $generator = $this->composer->getAutoloadGenerator(); $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->loader->register(); return $scripts[$event->getName()]; } protected function isPhpScript($callable) { return false === strpos($callable, ' ') && false !== strpos($callable, '::'); } } hasPackage($package); } public function install(InstalledRepositoryInterface $repo, PackageInterface $package) { 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); } $repo->removePackage($initial); if (!$repo->hasPackage($target)) { $repo->addPackage(clone $target); } } public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) { if (!$repo->hasPackage($package)) { return; throw new \InvalidArgumentException('Package is not installed: '.$package); } $repo->removePackage($package); } public function getInstallPath(PackageInterface $package) { $targetDir = $package->getTargetDir(); return $package->getPrettyName() . ($targetDir ? '/'.$targetDir : ''); } } hasPackage($package); } public function install(InstalledRepositoryInterface $repo, PackageInterface $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); } $repo->removePackage($initial); $repo->addPackage(clone $target); } public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) { if (!$repo->hasPackage($package)) { return; throw new \InvalidArgumentException('Package is not installed: '.$package); } $repo->removePackage($package); } public function getInstallPath(PackageInterface $package) { return ''; } } uninstall($repo, $initial); $this->install($repo, $target); } protected function installCode(PackageInterface $package) { parent::installCode($package); parent::initializeBinDir(); $isWindows = defined('PHP_WINDOWS_VERSION_BUILD'); $php_bin = $this->binDir . ($isWindows ? '/composer-php.bat' : '/composer-php'); if (!$isWindows) { $php_bin = '/usr/bin/env ' . $php_bin; } $installPath = $this->getInstallPath($package); $vars = array( 'os' => $isWindows ? 'windows' : 'linux', 'php_bin' => $php_bin, 'pear_php' => $installPath, 'php_dir' => $installPath, 'bin_dir' => $installPath . '/bin', 'data_dir' => $installPath . '/data', 'version' => $package->getPrettyVersion(), ); $packageArchive = $this->getInstallPath($package).'/'.pathinfo($package->getDistUrl(), PATHINFO_BASENAME); $pearExtractor = new PearPackageExtractor($packageArchive); $pearExtractor->extractTo($this->getInstallPath($package), array('php' => '/', 'script' => '/bin', 'data' => '/data'), $vars); if ($this->io->isVerbose()) { $this->io->write(' Cleaning up'); } unlink($packageArchive); } protected function getBinaries(PackageInterface $package) { $binariesPath = $this->getInstallPath($package) . '/bin/'; $binaries = array(); if (file_exists($binariesPath)) { foreach (new \FilesystemIterator($binariesPath, \FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::CURRENT_AS_FILEINFO) as $fileName => $value) { if (!$value->isDir()) { $binaries[] = 'bin/'.$fileName; } } } return $binaries; } protected function initializeBinDir() { parent::initializeBinDir(); file_put_contents($this->binDir.'/composer-php', $this->generateUnixyPhpProxyCode()); chmod($this->binDir.'/composer-php', 0777); file_put_contents($this->binDir.'/composer-php.bat', $this->generateWindowsPhpProxyCode()); chmod($this->binDir.'/composer-php.bat', 0777); } protected function generateWindowsProxyCode($bin, $link) { $binPath = $this->filesystem->findShortestPath($link, $bin); if ('.bat' === substr($bin, -4)) { $caller = 'call'; } else { $handle = fopen($bin, 'r'); $line = fgets($handle); fclose($handle); if (preg_match('{^#!/(?:usr/bin/env )?(?:[^/]+/)*(.+)$}m', $line, $match)) { $caller = trim($match[1]); } else { $caller = 'php'; } if ($caller === 'php') { return "@echo off\r\n". "pushd .\r\n". "cd %~dp0\r\n". "set PHP_PROXY=%CD%\\composer-php.bat\r\n". "cd ".escapeshellarg(dirname($binPath))."\r\n". "set BIN_TARGET=%CD%\\".basename($binPath)."\r\n". "popd\r\n". "%PHP_PROXY% \"%BIN_TARGET%\" %*\r\n"; } } return "@echo off\r\n". "pushd .\r\n". "cd %~dp0\r\n". "cd ".escapeshellarg(dirname($binPath))."\r\n". "set BIN_TARGET=%CD%\\".basename($binPath)."\r\n". "popd\r\n". $caller." \"%BIN_TARGET%\" %*\r\n"; } private function generateWindowsPhpProxyCode() { $binToVendor = $this->filesystem->findShortestPath($this->binDir, $this->vendorDir, true); return "@echo off\r\n" . "setlocal enabledelayedexpansion\r\n" . "set BIN_DIR=%~dp0\r\n" . "set VENDOR_DIR=%BIN_DIR%\\".$binToVendor."\r\n" . "set DIRS=.\r\n" . "FOR /D %%V IN (%VENDOR_DIR%\\*) DO (\r\n" . " FOR /D %%P IN (%%V\\*) DO (\r\n" . " set DIRS=!DIRS!;%%~fP\r\n" . " )\r\n" . ")\r\n" . "php.exe -d include_path=!DIRS! %*\r\n"; } private function generateUnixyPhpProxyCode() { $binToVendor = $this->filesystem->findShortestPath($this->binDir, $this->vendorDir, true); return "#!/usr/bin/env sh\n". "SRC_DIR=`pwd`\n". "BIN_DIR=`dirname $0`\n". "VENDOR_DIR=\$BIN_DIR/".escapeshellarg($binToVendor)."\n". "DIRS=\"\"\n". "for vendor in \$VENDOR_DIR/*; do\n". " if [ -d \"\$vendor\" ]; then\n". " for package in \$vendor/*; do\n". " if [ -d \"\$package\" ]; then\n". " DIRS=\"\${DIRS}:\${package}\"\n". " fi\n". " done\n". " fi\n". "done\n". "php -d include_path=\".\$DIRS\" $@\n"; } } installPath = rtrim(strtr($installPath, '\\', '/'), '/').'/'; $this->downloadManager = $dm; } public function supports($packageType) { return true; } public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package) { return false; } public function install(InstalledRepositoryInterface $repo, PackageInterface $package) { $installPath = $this->installPath; if (file_exists($installPath) && (count(glob($installPath.'*')) || (count(glob($installPath.'.*')) > 2))) { throw new \InvalidArgumentException("Project directory $installPath is not empty."); } if (!is_dir($installPath)) { mkdir($installPath, 0777, true); } $this->downloadManager->download($package, $installPath); } public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) { throw new \InvalidArgumentException("not supported"); } public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) { throw new \InvalidArgumentException("not supported"); } public function getInstallPath(PackageInterface $package) { return $this->installPath; } } composer = $composer; $this->downloadManager = $composer->getDownloadManager(); $this->io = $io; $this->type = $type; $this->filesystem = new Filesystem(); $this->vendorDir = rtrim($composer->getConfig()->get('vendor-dir'), '/'); $this->binDir = rtrim($composer->getConfig()->get('bin-dir'), '/'); } public function supports($packageType) { return $packageType === $this->type || null === $this->type; } public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package) { return $repo->hasPackage($package) && is_readable($this->getInstallPath($package)); } public function install(InstalledRepositoryInterface $repo, PackageInterface $package) { $this->initializeVendorDir(); $downloadPath = $this->getInstallPath($package); if (!is_readable($downloadPath) && $repo->hasPackage($package)) { $this->removeBinaries($package); } $this->installCode($package); $this->installBinaries($package); 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->removeBinaries($initial); $this->updateCode($initial, $target); $this->installBinaries($target); $repo->removePackage($initial); if (!$repo->hasPackage($target)) { $repo->addPackage(clone $target); } } public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $package) { if (!$repo->hasPackage($package)) { return; throw new \InvalidArgumentException('Package is not installed: '.$package); } $this->removeCode($package); $this->removeBinaries($package); $repo->removePackage($package); $downloadPath = $this->getPackageBasePath($package); if (strpos($package->getName(), '/')) { $packageVendorDir = dirname($downloadPath); if (is_dir($packageVendorDir) && !glob($packageVendorDir.'/*')) { @rmdir($packageVendorDir); } } } public function getInstallPath(PackageInterface $package) { $targetDir = $package->getTargetDir(); return $this->getPackageBasePath($package) . ($targetDir ? '/'.$targetDir : ''); } protected function getPackageBasePath(PackageInterface $package) { $this->initializeVendorDir(); return ($this->vendorDir ? $this->vendorDir.'/' : '') . $package->getPrettyName(); } protected function installCode(PackageInterface $package) { $downloadPath = $this->getInstallPath($package); $this->downloadManager->download($package, $downloadPath); } protected function updateCode(PackageInterface $initial, PackageInterface $target) { $downloadPath = $this->getInstallPath($initial); $this->downloadManager->update($initial, $target, $downloadPath); } protected function removeCode(PackageInterface $package) { $downloadPath = $this->getPackageBasePath($package); $this->downloadManager->remove($package, $downloadPath); } protected function getBinaries(PackageInterface $package) { return $package->getBinaries(); } protected function installBinaries(PackageInterface $package) { $binaries = $this->getBinaries($package); if (!$binaries) { return; } foreach ($binaries as $bin) { $binPath = $this->getInstallPath($package).'/'.$bin; if (!file_exists($binPath)) { $this->io->write(' Skipped installation of '.$bin.' for package '.$package->getName().': file not found in package'); continue; } $this->initializeBinDir(); $link = $this->binDir.'/'.basename($bin); if (file_exists($link)) { if (is_link($link)) { chmod($link, 0777 & ~umask()); } $this->io->write(' Skipped installation of '.$bin.' for package '.$package->getName().': name conflicts with an existing file'); continue; } if (defined('PHP_WINDOWS_VERSION_BUILD')) { if ('.bat' !== substr($binPath, -4)) { file_put_contents($link, $this->generateUnixyProxyCode($binPath, $link)); chmod($link, 0777 & ~umask()); $link .= '.bat'; if (file_exists($link)) { $this->io->write(' Skipped installation of '.$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)); } } else { $cwd = getcwd(); try { $relativeBin = $this->filesystem->findShortestPath($link, $binPath); chdir(dirname($link)); if (false === symlink($relativeBin, $link)) { throw new \ErrorException(); } } catch (\ErrorException $e) { file_put_contents($link, $this->generateUnixyProxyCode($binPath, $link)); } chdir($cwd); } chmod($link, 0777 & ~umask()); } } protected function removeBinaries(PackageInterface $package) { $binaries = $this->getBinaries($package); if (!$binaries) { return; } foreach ($binaries as $bin) { $link = $this->binDir.'/'.basename($bin); if (is_link($link) || file_exists($link)) { unlink($link); } if (file_exists($link.'.bat')) { unlink($link.'.bat'); } } } protected function initializeVendorDir() { $this->filesystem->ensureDirectoryExists($this->vendorDir); $this->vendorDir = realpath($this->vendorDir); } protected function initializeBinDir() { $this->filesystem->ensureDirectoryExists($this->binDir); $this->binDir = realpath($this->binDir); } protected function generateWindowsProxyCode($bin, $link) { $binPath = $this->filesystem->findShortestPath($link, $bin); if ('.bat' === substr($bin, -4) || '.exe' === substr($bin, -4)) { $caller = 'call'; } else { $handle = fopen($bin, 'r'); $line = fgets($handle); fclose($handle); if (preg_match('{^#!/(?:usr/bin/env )?(?:[^/]+/)*(.+)$}m', $line, $match)) { $caller = trim($match[1]); } else { $caller = 'php'; } } return "@ECHO OFF\r\n". "SET BIN_TARGET=%~dp0\\".escapeshellarg(dirname($binPath)).'\\'.basename($binPath)."\r\n". "{$caller} \"%BIN_TARGET%\" %*\r\n"; } protected function generateUnixyProxyCode($bin, $link) { $binPath = $this->filesystem->findShortestPath($link, $bin); return "#!/usr/bin/env sh\n". 'SRC_DIR="`pwd`"'."\n". 'cd "`dirname "$0"`"'."\n". 'cd '.escapeshellarg(dirname($binPath))."\n". 'BIN_TARGET="`pwd`/'.basename($binPath)."\"\n". 'cd "$SRC_DIR"'."\n". '"$BIN_TARGET" "$@"'."\n"; } } notifiablePackages = array(); } public function addInstaller(InstallerInterface $installer) { array_unshift($this->installers, $installer); $this->cache = array(); } public function removeInstaller(InstallerInterface $installer) { if (false !== ($key = array_search($installer, $this->installers, true))) { array_splice($this->installers, $key, 1); $this->cache = array(); } } public function disableCustomInstallers() { foreach ($this->installers as $i => $installer) { if (!$installer instanceof InstallerInstaller) { continue; } unset($this->installers[$i]); } } public function getInstaller($type) { $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) { if ($package instanceof AliasPackage) { return $repo->hasPackage($package) && $this->isPackageInstalled($repo, $package->getAliasOf()); } return $this->getInstaller($package->getType())->isInstalled($repo, $package); } public function execute(RepositoryInterface $repo, OperationInterface $operation) { $method = $operation->getJobType(); $this->$method($repo, $operation); } public function install(RepositoryInterface $repo, InstallOperation $operation) { $package = $operation->getPackage(); $installer = $this->getInstaller($package->getType()); $installer->install($repo, $package); $this->markForNotification($package); } public function update(RepositoryInterface $repo, UpdateOperation $operation) { $initial = $operation->getInitialPackage(); $target = $operation->getTargetPackage(); $initialType = $initial->getType(); $targetType = $target->getType(); if ($initialType === $targetType) { $installer = $this->getInstaller($initialType); $installer->update($repo, $initial, $target); $this->markForNotification($target); } else { $this->getInstaller($initialType)->uninstall($repo, $initial); $this->getInstaller($targetType)->install($repo, $target); } } public function uninstall(RepositoryInterface $repo, UninstallOperation $operation) { $package = $operation->getPackage(); $installer = $this->getInstaller($package->getType()); $installer->uninstall($repo, $package); } public function markAliasInstalled(RepositoryInterface $repo, MarkAliasInstalledOperation $operation) { $package = $operation->getPackage(); if (!$repo->hasPackage($package)) { $repo->addPackage(clone $package); } } public function markAliasUninstalled(RepositoryInterface $repo, MarkAliasUninstalledOperation $operation) { $package = $operation->getPackage(); $repo->removePackage($package); } public function getInstallPath(PackageInterface $package) { $installer = $this->getInstaller($package->getType()); return $installer->getInstallPath($package); } public function notifyInstalls() { foreach ($this->notifiablePackages as $repoUrl => $packages) { if (strpos($repoUrl, '%package%')) { foreach ($packages as $package) { $url = str_replace('%package%', $package->getPrettyName(), $repoUrl); $params = array( 'version' => $package->getPrettyVersion(), 'version_normalized' => $package->getVersion(), ); $opts = array('http' => array( 'method' => 'POST', 'header' => array('Content-type: application/x-www-form-urlencoded'), 'content' => http_build_query($params, '', '&'), 'timeout' => 3, ) ); $context = StreamContextFactory::getContext($opts); @file_get_contents($url, false, $context); } continue; } $postData = array('downloads' => array()); foreach ($packages as $package) { $postData['downloads'][] = array( 'name' => $package->getPrettyName(), 'version' => $package->getVersion(), ); } $opts = array('http' => array( 'method' => 'POST', 'header' => array('Content-Type: application/json'), 'content' => json_encode($postData), 'timeout' => 6, ) ); $context = StreamContextFactory::getContext($opts); @file_get_contents($repoUrl, false, $context); } $this->reset(); } private function markForNotification(PackageInterface $package) { if ($package->getNotificationUrl()) { $this->notifiablePackages[$package->getNotificationUrl()][$package->getName()] = $package; } } } installationManager = $composer->getInstallationManager(); $repo = $composer->getRepositoryManager()->getLocalRepository(); foreach ($repo->getPackages() as $package) { if ('composer-installer' === $package->getType()) { $this->registerInstaller($package); } } } public function install(InstalledRepositoryInterface $repo, PackageInterface $package) { $extra = $package->getExtra(); if (empty($extra['class'])) { throw new \UnexpectedValueException('Error while installing '.$package->getPrettyName().', composer-installer packages should have a class defined in their extra key to be usable.'); } parent::install($repo, $package); $this->registerInstaller($package); } public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) { $extra = $target->getExtra(); if (empty($extra['class'])) { throw new \UnexpectedValueException('Error while installing '.$target->getPrettyName().', composer-installer packages should have a class defined in their extra key to be usable.'); } parent::update($repo, $initial, $target); $this->registerInstaller($target); } private function registerInstaller(PackageInterface $package) { $downloadPath = $this->getInstallPath($package); $extra = $package->getExtra(); $classes = is_array($extra['class']) ? $extra['class'] : array($extra['class']); $generator = $this->composer->getAutoloadGenerator(); $map = $generator->parseAutoloads(array(array($package, $downloadPath)), new Package('dummy', '1.0.0.0', '1.0.0')); $classLoader = $generator->createLoader($map); $classLoader->register(); foreach ($classes as $class) { if (class_exists($class, false)) { $code = file_get_contents($classLoader->findFile($class)); $code = preg_replace('{^class\s+(\S+)}mi', 'class $1_composer_tmp'.self::$classCounter, $code); eval('?>'.$code); $class .= '_composer_tmp'.self::$classCounter; self::$classCounter++; } $installer = new $class($this->io, $this->composer); $this->installationManager->addInstaller($installer); } } } io = new ConsoleIO($input, $output, $this->getHelperSet()); if (version_compare(PHP_VERSION, '5.3.2', '<')) { $output->writeln('Composer only officially supports PHP 5.3.2 and above, you will most likely encounter problems with your PHP '.PHP_VERSION.', upgrading is strongly recommended.'); } if (defined('COMPOSER_DEV_WARNING_TIME') && $this->getCommandName($input) !== 'self-update') { if (time() > COMPOSER_DEV_WARNING_TIME) { $output->writeln(sprintf('Warning: This development build of composer is over 30 days old. It is recommended to update it by running "%s self-update" to get the latest version.', $_SERVER['PHP_SELF'])); } } if (getenv('COMPOSER_NO_INTERACTION')) { $input->setInteractive(false); } if ($input->hasParameterOption('--profile')) { $startTime = microtime(true); $this->io->enableDebugging($startTime); } if ($newWorkDir = $this->getNewWorkingDir($input)) { $oldWorkingDir = getcwd(); chdir($newWorkDir); } $result = parent::doRun($input, $output); if (isset($oldWorkingDir)) { chdir($oldWorkingDir); } if (isset($startTime)) { $output->writeln('Memory usage: '.round(memory_get_usage() / 1024 / 1024, 2).'MB (peak: '.round(memory_get_peak_usage() / 1024 / 1024, 2).'MB), time: '.round(microtime(true) - $startTime, 2).'s'); } return $result; } private function getNewWorkingDir(InputInterface $input) { $workingDir = $input->getParameterOption(array('--working-dir', '-d')); if (false !== $workingDir && !is_dir($workingDir)) { throw new \RuntimeException('Invalid working directory specified.'); } return $workingDir; } public function getComposer($required = true) { if (null === $this->composer) { try { $this->composer = Factory::create($this->io); } catch (\InvalidArgumentException $e) { if ($required) { $this->io->write($e->getMessage()); exit(1); } } catch (JsonValidationException $e) { $errors = ' - ' . implode(PHP_EOL . ' - ', $e->getErrors()); $message = $e->getMessage() . ':' . PHP_EOL . $errors; throw new JsonValidationException($message); } } return $this->composer; } public function getIO() { return $this->io; } public function getHelp() { return self::$logo . parent::getHelp(); } protected function getDefaultCommands() { $commands = parent::getDefaultCommands(); $commands[] = new Command\AboutCommand(); $commands[] = new Command\ConfigCommand(); $commands[] = new Command\DependsCommand(); $commands[] = new Command\InitCommand(); $commands[] = new Command\InstallCommand(); $commands[] = new Command\CreateProjectCommand(); $commands[] = new Command\UpdateCommand(); $commands[] = new Command\SearchCommand(); $commands[] = new Command\ValidateCommand(); $commands[] = new Command\ShowCommand(); $commands[] = new Command\RequireCommand(); $commands[] = new Command\DumpAutoloadCommand(); $commands[] = new Command\StatusCommand(); $commands[] = new Command\ArchiveCommand(); $commands[] = new Command\DiagnoseCommand(); $commands[] = new Command\RunScriptCommand(); if ('phar:' === substr(__FILE__, 0, 5)) { $commands[] = new Command\SelfUpdateCommand(); } return $commands; } protected function getDefaultInputDefinition() { $definition = parent::getDefaultInputDefinition(); $definition->addOption(new InputOption('--profile', null, InputOption::VALUE_NONE, 'Display timing and memory usage information')); $definition->addOption(new InputOption('--working-dir', '-d', InputOption::VALUE_REQUIRED, 'If specified, use the given directory as working directory.')); return $definition; } protected function getDefaultHelperSet() { $helperSet = parent::getDefaultHelperSet(); $helperSet->set(new DialogHelper()); return $helperSet; } } 'black', 31 => 'red', 32 => 'green', 33 => 'yellow', 34 => 'blue', 35 => 'magenta', 36 => 'cyan', 37 => 'white' ); private static $availableBackgroundColors = array( 40 => 'black', 41 => 'red', 42 => 'green', 43 => 'yellow', 44 => 'blue', 45 => 'magenta', 46 => 'cyan', 47 => 'white' ); private static $availableOptions = array( 1 => 'bold', 4 => 'underscore', ); public function __construct(array $styles = array()) { parent::__construct(true, $styles); } public function format($message) { $formatted = parent::format($message); return preg_replace_callback("{\033\[([0-9;]+)m(.*?)\033\[0m}s", array($this, 'formatHtml'), $formatted); } private function formatHtml($matches) { $out = ''.$matches[2].''; } } eventDispatcher = $eventDispatcher; } public function dump(Config $config, InstalledRepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $scanPsr0Packages = false, $suffix = '') { $filesystem = new Filesystem(); $filesystem->ensureDirectoryExists($config->get('vendor-dir')); $basePath = $filesystem->normalizePath(getcwd()); $vendorPath = $filesystem->normalizePath(realpath($config->get('vendor-dir'))); $useGlobalIncludePath = (bool) $config->get('use-include-path'); $targetDir = $vendorPath.'/'.$targetDir; $filesystem->ensureDirectoryExists($targetDir); $vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true); $vendorPathCode52 = str_replace('__DIR__', 'dirname(__FILE__)', $vendorPathCode); $vendorPathToTargetDirCode = $filesystem->findShortestPathCode($vendorPath, realpath($targetDir), true); $appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, $basePath, true); $appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode); $namespacesFile = <<buildPackageMap($installationManager, $mainPackage, $localRepo->getCanonicalPackages()); $autoloads = $this->parseAutoloads($packageMap, $mainPackage); foreach ($autoloads['psr-0'] as $namespace => $paths) { $exportedPaths = array(); foreach ($paths as $path) { $exportedPaths[] = $this->getPathCode($filesystem, $basePath, $vendorPath, $path); } $exportedPrefix = var_export($namespace, true); $namespacesFile .= " $exportedPrefix => "; if (count($exportedPaths) > 1) { $namespacesFile .= "array(".implode(', ', $exportedPaths)."),\n"; } else { $namespacesFile .= $exportedPaths[0].",\n"; } } $namespacesFile .= ");\n"; $classmapFile = <<getAutoload(); if ($mainPackage->getTargetDir() && !empty($mainAutoload['psr-0'])) { $levels = count(explode('/', $filesystem->normalizePath($mainPackage->getTargetDir()))); $prefixes = implode(', ', array_map(function ($prefix) { return var_export($prefix, true); }, array_keys($mainAutoload['psr-0']))); $baseDirFromTargetDirCode = $filesystem->findShortestPathCode($targetDir, $basePath, true); $targetDirLoader = << $paths) { foreach ($paths as $dir) { $dir = $filesystem->normalizePath($filesystem->isAbsolutePath($dir) ? $dir : $basePath.'/'.$dir); if (!is_dir($dir)) { continue; } $whitelist = sprintf( '{%s/%s.+(? $path) { if ('' === $namespace || 0 === strpos($class, $namespace)) { if (!isset($classMap[$class])) { $path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path); $classMap[$class] = $path.",\n"; } } } } } } $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap'])); foreach ($autoloads['classmap'] as $dir) { foreach (ClassMapGenerator::createMap($dir) as $class => $path) { $path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path); $classMap[$class] = $path.",\n"; } } ksort($classMap); foreach ($classMap as $class => $code) { $classmapFile .= ' '.var_export($class, true).' => '.$code; } $classmapFile .= ");\n"; $filesCode = ""; $autoloads['files'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['files'])); foreach ($autoloads['files'] as $functionFile) { $filesCode .= ' require '.$this->getPathCode($filesystem, $basePath, $vendorPath, $functionFile).";\n"; } if (!$suffix) { $suffix = md5(uniqid('', true)); } file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile); file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile); if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) { file_put_contents($targetDir.'/include_paths.php', $includePathFile); } file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix)); file_put_contents($targetDir.'/autoload_real.php', $this->getAutoloadRealFile(true, true, (bool) $includePathFile, $targetDirLoader, $filesCode, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath)); $sourceLoader = fopen(__DIR__.'/ClassLoader.php', 'r'); $targetLoader = fopen($targetDir.'/ClassLoader.php', 'w+'); stream_copy_to_stream($sourceLoader, $targetLoader); fclose($sourceLoader); fclose($targetLoader); unset($sourceLoader, $targetLoader); $this->eventDispatcher->dispatch(ScriptEvents::POST_AUTOLOAD_DUMP); } public function buildPackageMap(InstallationManager $installationManager, PackageInterface $mainPackage, array $packages) { $packageMap = array(array($mainPackage, '')); foreach ($packages as $package) { if ($package instanceof AliasPackage) { continue; } $packageMap[] = array( $package, $installationManager->getInstallPath($package) ); } return $packageMap; } public function parseAutoloads(array $packageMap, PackageInterface $mainPackage) { $mainPackageMap = array_shift($packageMap); $sortedPackageMap = $this->sortPackageMap($packageMap); $sortedPackageMap[] = $mainPackageMap; array_unshift($packageMap, $mainPackageMap); $psr0 = $this->parseAutoloadsType($packageMap, 'psr-0', $mainPackage); $classmap = $this->parseAutoloadsType($sortedPackageMap, 'classmap', $mainPackage); $files = $this->parseAutoloadsType($sortedPackageMap, 'files', $mainPackage); krsort($psr0); return array('psr-0' => $psr0, 'classmap' => $classmap, 'files' => $files); } public function createLoader(array $autoloads) { $loader = new ClassLoader(); if (isset($autoloads['psr-0'])) { foreach ($autoloads['psr-0'] as $namespace => $path) { $loader->add($namespace, $path); } } return $loader; } protected function getIncludePathsFile(array $packageMap, Filesystem $filesystem, $basePath, $vendorPath, $vendorPathCode, $appBaseDirCode) { $includePaths = array(); foreach ($packageMap as $item) { list($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; } $includePathsFile = <<getPathCode($filesystem, $basePath, $vendorPath, $path) . ",\n"; } return $includePathsFile . ");\n"; } protected function getPathCode(Filesystem $filesystem, $basePath, $vendorPath, $path) { if (!$filesystem->isAbsolutePath($path)) { $path = $basePath . '/' . $path; } $path = $filesystem->normalizePath($path); $baseDir = ''; if (strpos($path, $vendorPath) === 0) { $path = substr($path, strlen($vendorPath)); $baseDir = '$vendorDir . '; } else { $path = $filesystem->normalizePath($filesystem->findShortestPath($basePath, $path, true)); if (!$filesystem->isAbsolutePath($path)) { $baseDir = '$baseDir . '; $path = '/' . $path; } } if (preg_match('/\.phar$/', $path)){ $baseDir = "'phar://' . " . $baseDir; } return $baseDir.var_export($path, true); } protected function getAutoloadFile($vendorPathToTargetDirCode, $suffix) { return << $path) { $loader->add($namespace, $path); } PSR0; } if ($useClassMap) { $file .= <<<'CLASSMAP' $classMap = require __DIR__ . '/autoload_classmap.php'; if ($classMap) { $loader->addClassMap($classMap); } CLASSMAP; } if ($useGlobalIncludePath) { $file .= <<<'INCLUDEPATH' $loader->setUseIncludePath(true); INCLUDEPATH; } if ($targetDirLoader) { $file .= <<register(true);{$filesCode} return \$loader; } METHOD_FOOTER; $file .= $targetDirLoader; return $file . <<