#!/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'); if (time() > 1340224205) { echo 'This dev build of composer is outdated, please run "'.$argv[0].' self-update" to get the latest.'.PHP_EOL; } require 'phar://composer.phar/bin/composer'; __HALT_COMPILER(); ?> = composer.pharsrc/bootstrap.phpͥO`W٠ src/Composer/Util/Filesystem.phpQ ͥOQ "c"src/Composer/Util/ErrorHandler.phpͥO@*src/Composer/Util/StreamContextFactory.php@ͥO@܈gsrc/Composer/Util/Svn.phpN ͥON h&src/Composer/Util/RemoteFilesystem.phpXͥOX"ȶ%src/Composer/Util/ProcessExecutor.phpqͥOq+'$+src/Composer/Util/SpdxLicenseIdentifier.php5 ͥO5 Y4$src/Composer/Console/Application.phpY ͥOY ;src/Composer/DependencyResolver/SolverProblemsException.phpͥObǣa(src/Composer/DependencyResolver/Rule.phpͥO/3src/Composer/DependencyResolver/RuleSetIterator.phpͥO}+src/Composer/DependencyResolver/Request.phpͥO1I1src/Composer/DependencyResolver/DefaultPolicy.php|ͥO|Isrc/Composer/DependencyResolver/Operation/MarkAliasInstalledOperation.phpͥOxUZaKsrc/Composer/DependencyResolver/Operation/MarkAliasUninstalledOperation.phpͥO_iǫ@src/Composer/DependencyResolver/Operation/UninstallOperation.phpIͥOIFɶ=src/Composer/DependencyResolver/Operation/UpdateOperation.phphͥOhS]>src/Composer/DependencyResolver/Operation/InstallOperation.phpCͥOC\*@src/Composer/DependencyResolver/Operation/OperationInterface.phpͥO` Z=src/Composer/DependencyResolver/Operation/SolverOperation.phpdͥOd9$<6src/Composer/DependencyResolver/SolverBugException.phpͥO"qN+src/Composer/DependencyResolver/Problem.php: ͥO: e(src/Composer/DependencyResolver/Pool.php; ͥO; ̟3src/Composer/DependencyResolver/PolicyInterface.phpͥOζ+src/Composer/DependencyResolver/Literal.phpGͥOG+src/Composer/DependencyResolver/RuleSet.php ͥO D.2*src/Composer/DependencyResolver/Solver.phpzͥOzSma,src/Composer/Command/Helper/DialogHelper.phpͥOw-src/Composer/Command/CreateProjectCommand.phpͥOX$src/Composer/Command/InitCommand.php#ͥO#gn&src/Composer/Command/UpdateCommand.phpͥO4fY$src/Composer/Command/ShowCommand.phpfͥOfgvU'src/Composer/Command/DependsCommand.php ͥO %~ %src/Composer/Command/AboutCommand.phpͥO϶ src/Composer/Command/Command.php^ͥO^-g(l&src/Composer/Command/SearchCommand.php ͥO X>'src/Composer/Command/InstallCommand.phpͥO_0iP(src/Composer/Command/ValidateCommand.php| ͥO| x͕*src/Composer/Command/SelfUpdateCommand.phpͥOua-src/Composer/Installer/InstallerInterface.phpͥO 9+src/Composer/Installer/ProjectInstaller.php ͥO  qm.src/Composer/Installer/InstallationManager.php-ͥO-4-src/Composer/Installer/InstallerInstaller.php ͥO =+src/Composer/Installer/LibraryInstaller.php(ͥO( /src/Composer/Installer/MetapackageInstaller.phpͥOfζsrc/Composer/Composer.phpKͥOKlTsrc/Composer/IO/IOInterface.phpͥO{8src/Composer/IO/NullIO.phpͥOsrc/Composer/IO/ConsoleIO.phpf ͥOf /N$src/Composer/Script/ScriptEvents.phpͥO'src/Composer/Script/EventDispatcher.php)ͥO)^ƶ$src/Composer/Script/PackageEvent.phpͥO[src/Composer/Script/Event.phpͥO$src/Composer/Script/CommandEvent.phpͥOqsrc/Composer/Factory.phpͥOE*src/Composer/Package/Loader/JsonLoader.phpͥOͥʶ1src/Composer/Package/Loader/RootPackageLoader.phpͥOnM+src/Composer/Package/Loader/ArrayLoader.phpͥOƶ$src/Composer/Package/BasePackage.php ͥO -2ö.src/Composer/Package/Version/VersionParser.phpzͥOzFB)src/Composer/Package/PackageInterface.phpͥOj 9src/Composer/Package/LinkConstraint/VersionConstraint.phpͥO,7src/Composer/Package/LinkConstraint/MultiConstraint.phplͥOl2,:src/Composer/Package/LinkConstraint/SpecificConstraint.phpͥOH?src/Composer/Package/LinkConstraint/LinkConstraintInterface.phpͥOm%src/Composer/Package/AliasPackage.phpͥOBsrc/Composer/Package/Link.phpGͥOGƲ磶+src/Composer/Package/Dumper/ArrayDumper.php ͥO *g&src/Composer/Package/MemoryPackage.phpXͥOX4src/Composer/Package/Locker.phpͥO src/Composer/Cache.php%ͥO%C}src/Composer/Config.php,ͥO,Jc_-src/Composer/Json/JsonValidationException.php2ͥO2v7isrc/Composer/Json/JsonFile.php~ͥO~ˏ/src/Composer/Repository/CompositeRepository.phpͥODž>8src/Composer/Repository/InstalledRepositoryInterface.phpͥOpM`/+src/Composer/Repository/ArrayRepository.php- ͥO- ].src/Composer/Repository/PlatformRepository.phpͥO?+ 9src/Composer/Repository/InstalledFilesystemRepository.php9ͥO9X*src/Composer/Repository/PearRepository.php$ͥO$')src/Composer/Repository/Vcs/SvnDriver.phpqͥOqŲ)src/Composer/Repository/Vcs/GitDriver.phpcͥOc\2src/Composer/Repository/Vcs/VcsDriverInterface.phpͥOtљ2src/Composer/Repository/Vcs/GitBitbucketDriver.php ͥO _{)src/Composer/Repository/Vcs/VcsDriver.phpͥO2(src/Composer/Repository/Vcs/HgDriver.php[ ͥO[ lQt,src/Composer/Repository/Vcs/GitHubDriver.phpͥO)i1src/Composer/Repository/Vcs/HgBitbucketDriver.phpM ͥOM L5.src/Composer/Repository/ComposerRepository.phpZ ͥOZ `Z-src/Composer/Repository/RepositoryManager.phpoͥOo</src/Composer/Repository/RepositoryInterface.phpQͥOQ|S)src/Composer/Repository/VcsRepository.php&ͥO&v7src/Composer/Repository/WritableRepositoryInterface.php?ͥO?0src/Composer/Repository/FilesystemRepository.phpͥO\P9src/Composer/Repository/NotifiableRepositoryInterface.phpͥO-src/Composer/Repository/PackageRepository.phpͥO(4src/Composer/Repository/InstalledArrayRepository.phpqͥOqJbŶsrc/Composer/Installer.php0ͥO0[=Ҷ+src/Composer/Autoload/AutoloadGenerator.php_$ͥO_$\+src/Composer/Autoload/ClassMapGenerator.php,ͥO,m*+src/Composer/Downloader/DownloadManager.php ͥO 3n*src/Composer/Downloader/FileDownloader.php ͥO JB2ʶ(src/Composer/Downloader/HgDownloader.phpͥO1"^*src/Composer/Downloader/PearDownloader.phpwͥOw%-src/Composer/Downloader/ArchiveDownloader.phpͥOit/src/Composer/Downloader/DownloaderInterface.phpsͥOs$X*src/Composer/Downloader/PharDownloader.php ͥO .src/Composer/Downloader/TransportException.phpdͥOdGɇ)src/Composer/Downloader/ZipDownloader.phpͥObE)src/Composer/Downloader/SvnDownloader.php;ͥO;t)src/Composer/Downloader/VcsDownloader.php[ͥO[1L)src/Composer/Downloader/TarDownloader.php ͥO ?6)src/Composer/Downloader/GitDownloader.phpͥOw!%src/Composer/Autoload/ClassLoader.phpͥOs!res/composer-schema.json%ͥO%,Sres/spdx-identifier.jsonX ͥOX f̶src/Composer/IO/hiddeninput.exe$ͥO$v<vendor/symfony/process/Symfony/Component/Process/Process.php~-ͥO~-ZHvendor/symfony/process/Symfony/Component/Process/PhpExecutableFinder.phpDͥOD1Evendor/symfony/process/Symfony/Component/Process/ExecutableFinder.phpͥOymOvendor/symfony/process/Symfony/Component/Process/Exception/RuntimeException.phpͥO:Uvendor/symfony/process/Symfony/Component/Process/Exception/ProcessFailedException.phpͥO:/Qvendor/symfony/process/Symfony/Component/Process/Exception/ExceptionInterface.phpfͥOf]>T?vendor/symfony/process/Symfony/Component/Process/PhpProcess.phpͥOZ⮶Cvendor/symfony/process/Symfony/Component/Process/ProcessBuilder.phpͥO{QKvendor/symfony/console/Symfony/Component/Console/Helper/HelperInterface.phpͥOCHvendor/symfony/console/Symfony/Component/Console/Helper/DialogHelper.phpͥOlʔCKvendor/symfony/console/Symfony/Component/Console/Helper/FormatterHelper.phpͥO Evendor/symfony/console/Symfony/Component/Console/Helper/HelperSet.phpͥO^9tBvendor/symfony/console/Symfony/Component/Console/Helper/Helper.php9ͥO9Wvendor/symfony/console/Symfony/Component/Console/Formatter/OutputFormatterInterface.phpnͥOnSvendor/symfony/console/Symfony/Component/Console/Formatter/OutputFormatterStyle.php) ͥO) 5]nҶNvendor/symfony/console/Symfony/Component/Console/Formatter/OutputFormatter.php ͥO  Xvendor/symfony/console/Symfony/Component/Console/Formatter/OutputFormatterStyleStack.phpͥO+R\vendor/symfony/console/Symfony/Component/Console/Formatter/OutputFormatterStyleInterface.phpdͥOdB#Hvendor/symfony/console/Symfony/Component/Console/Input/InputArgument.phpͥOwOm@vendor/symfony/console/Symfony/Component/Console/Input/Input.php ͥO |>\Jvendor/symfony/console/Symfony/Component/Console/Input/InputDefinition.php%ͥO%dFvendor/symfony/console/Symfony/Component/Console/Input/StringInput.phpͥO"Fvendor/symfony/console/Symfony/Component/Console/Input/InputOption.phpT ͥOT 䊫?Ivendor/symfony/console/Symfony/Component/Console/Input/InputInterface.phpͥO~%sEvendor/symfony/console/Symfony/Component/Console/Input/ArrayInput.phpͥO_öDvendor/symfony/console/Symfony/Component/Console/Input/ArgvInput.phpͥO4̶Dvendor/symfony/console/Symfony/Component/Console/Command/Command.php#ͥO#Hvendor/symfony/console/Symfony/Component/Console/Command/ListCommand.php=ͥO=\Hvendor/symfony/console/Symfony/Component/Console/Command/HelpCommand.phpͥOIvendor/symfony/console/Symfony/Component/Console/Tester/CommandTester.phpxͥOxC☶Mvendor/symfony/console/Symfony/Component/Console/Tester/ApplicationTester.phpͥO4:vendor/symfony/console/Symfony/Component/Console/Shell.php ͥO SQض@vendor/symfony/console/Symfony/Component/Console/Application.phpGͥOG*Rvendor/symfony/console/Symfony/Component/Console/Output/ConsoleOutputInterface.phpͥOBvendor/symfony/console/Symfony/Component/Console/Output/Output.phpuͥOukFvendor/symfony/console/Symfony/Component/Console/Output/NullOutput.phpͥO-Kvendor/symfony/console/Symfony/Component/Console/Output/OutputInterface.phpͥO-Hvendor/symfony/console/Symfony/Component/Console/Output/StreamOutput.phpͥOx_Ivendor/symfony/console/Symfony/Component/Console/Output/ConsoleOutput.phptͥOt(>TRvendor/symfony/finder/Symfony/Component/Finder/Iterator/FilenameFilterIterator.phpoͥOoLvendor/symfony/finder/Symfony/Component/Finder/Iterator/SortableIterator.phpͥO H:Svendor/symfony/finder/Symfony/Component/Finder/Iterator/DateRangeFilterIterator.php/ͥO/شVvendor/symfony/finder/Symfony/Component/Finder/Iterator/RecursiveDirectoryIterator.php7ͥO7P7Pvendor/symfony/finder/Symfony/Component/Finder/Iterator/CustomFilterIterator.php\ͥO\I3Tvendor/symfony/finder/Symfony/Component/Finder/Iterator/DepthRangeFilterIterator.phphͥOhUoRvendor/symfony/finder/Symfony/Component/Finder/Iterator/FileTypeFilterIterator.php7ͥO7h_Svendor/symfony/finder/Symfony/Component/Finder/Iterator/SizeRangeFilterIterator.phpͥOݍVvendor/symfony/finder/Symfony/Component/Finder/Iterator/MultiplePcreFilterIterator.phpͥO2.*Zvendor/symfony/finder/Symfony/Component/Finder/Iterator/ExcludeDirectoryFilterIterator.phpͥOLyIUvendor/symfony/finder/Symfony/Component/Finder/Iterator/FilecontentFilterIterator.phpkͥOk{q9Nvendor/symfony/finder/Symfony/Component/Finder/Comparator/NumberComparator.phpyͥOy"`۶Hvendor/symfony/finder/Symfony/Component/Finder/Comparator/Comparator.phpͥO>]XLvendor/symfony/finder/Symfony/Component/Finder/Comparator/DateComparator.php&ͥO&hd7vendor/symfony/finder/Symfony/Component/Finder/Glob.php ͥO b>vendor/symfony/finder/Symfony/Component/Finder/SplFileInfo.phpͥO.-9vendor/symfony/finder/Symfony/Component/Finder/Finder.php6ͥO6G2qZ4vendor/seld/jsonlint/src/Seld/JsonLint/Undefined.php>ͥO>q5vendor/seld/jsonlint/src/Seld/JsonLint/JsonParser.php&ͥO&~0vendor/seld/jsonlint/src/Seld/JsonLint/Lexer.phpͥO8I D;vendor/seld/jsonlint/src/Seld/JsonLint/ParsingException.phpͥO:˶=vendor/justinrainbow/json-schema/src/JsonSchema/Validator.php[ͥO[8Ivendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Undefined.phpxͥOxhqJvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php ͥO >Fvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Schema.phpͥO:fMDvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Type.phpͥO+FJvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Collection.phpVͥOV<Fvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/String.phpͥOkFvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Object.phpͥOcZSvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/ConstraintInterface.php$ͥO$gDvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Enum.phpͥO$(mFvendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Number.phpWͥOW.1vendor/autoload.phpͥOa['vendor/composer/autoload_namespaces.phpͥO}1%vendor/composer/autoload_classmap.phpZͥOZᖶvendor/composer/ClassLoader.phpA ͥOA s_ bin/composerͥO>LICENSE3ͥO3 2 getProcess()->execute($cmd) === 0; clearstatcache(); return $result && !is_dir($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 findShortestPath($from, $to, $directories = false) { if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) { throw new \InvalidArgumentException('from and to must be absolute paths'); } $from = lcfirst(rtrim(strtr($from, '\\', '/'), '/')); $to = lcfirst(rtrim(strtr($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('from and to must be absolute paths'); } $from = lcfirst(strtr($from, '\\', '/')); $to = lcfirst(strtr($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) === ':'; } protected function getProcess() { return new ProcessExecutor; } } array()); if (isset($_SERVER['HTTP_PROXY']) || isset($_SERVER['http_proxy'])) { $proxy = parse_url(isset($_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'] = array( 'proxy' => $proxyURL, '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'])) { $defaultOptions['http']['header'] .= "Proxy-Authorization: Basic {$auth}\r\n"; } else { $options['http']['header'] = "Proxy-Authorization: Basic {$auth}\r\n"; } } } $options = array_replace_recursive($options, $defaultOptions); return stream_context_create($options, $defaultParams); } } 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, 'authorization failed:')) { 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 '; } }io = $io; } public function copy($originUrl, $fileUrl, $fileName, $progress = true) { $this->get($originUrl, $fileUrl, $fileName, $progress); return $this->result; } public function getContents($originUrl, $fileUrl, $progress = true) { $this->get($originUrl, $fileUrl, null, $progress); return $this->result; } protected function get($originUrl, $fileUrl, $fileName = null, $progress = true) { $this->bytesMax = 0; $this->result = null; $this->originUrl = $originUrl; $this->fileUrl = $fileUrl; $this->fileName = $fileName; $this->progress = $progress; $this->lastProgress = null; $options = $this->getOptionsForUrl($originUrl); $ctx = StreamContextFactory::getContext($options, array('notification' => array($this, 'callbackGet'))); if ($this->progress) { $this->io->write(" Downloading: connection...", false); } $result = @file_get_contents($fileUrl, false, $ctx); if (!empty($http_response_header[0]) && preg_match('{^HTTP/\S+ 404}i', $http_response_header[0])) { $result = false; } if (false !== $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) { $result = (Boolean) @file_put_contents($fileName, $result); if (false === $result) { throw new TransportException('The "'.$fileUrl.'" file could not be written to '.$fileName); } } if (null === $this->result) { $this->result = $result; } if (false === $this->result) { throw new TransportException('The "'.$fileUrl.'" file could not be downloaded'); } } protected function callbackGet($notificationCode, $severity, $message, $messageCode, $bytesTransferred, $bytesMax) { switch ($notificationCode) { case STREAM_NOTIFY_FAILURE: throw new TransportException('The "'.$this->fileUrl.'" file could not be downloaded ('.trim($message).')', $messageCode); break; 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->setAuthorization($this->originUrl, $username, $password); $this->get($this->originUrl, $this->fileUrl, $this->fileName, $this->progress); } 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) { $options['http']['header'] = 'User-Agent: Composer/'.Composer::VERSION."\r\n"; if (extension_loaded('zlib')) { $options['http']['header'] .= 'Accept-Encoding: gzip'."\r\n"; } if ($this->io->hasAuthorization($originUrl)) { $auth = $this->io->getAuthorization($originUrl); $authStr = base64_encode($auth['username'] . ':' . $auth['password']); $options['http']['header'] .= "Authorization: Basic $authStr\r\n"; } return $options; } } captureOutput = count(func_get_args()) > 1; $this->errorOutput = null; $process = new Process($command, $cwd, null, null, static::getTimeout()); $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; } static public function getTimeout() { return static::$timeout; } static public function setTimeout($timeout) { static::$timeout = $timeout; } } 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); } } registerCommands(); $this->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.'); } return parent::doRun($input, $output); } 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); } return; } } return $this->composer; } public function getIO() { return $this->io; } protected function registerCommands() { $this->add(new Command\AboutCommand()); $this->add(new Command\DependsCommand()); $this->add(new Command\InitCommand()); $this->add(new Command\InstallCommand()); $this->add(new Command\CreateProjectCommand()); $this->add(new Command\UpdateCommand()); $this->add(new Command\SearchCommand()); $this->add(new Command\ValidateCommand()); $this->add(new Command\ShowCommand()); if ('phar:' === substr(__FILE__, 0, 5)) { $this->add(new Command\SelfUpdateCommand()); } } protected function getDefaultHelperSet() { $helperSet = parent::getDefaultHelperSet(); $helperSet->set(new DialogHelper()); return $helperSet; } } problems = $problems; parent::__construct($this->createMessage()); } protected function createMessage() { $messages = array(); foreach ($this->problems as $problem) { $messages[] = (string) $problem; } return "\n\tProblems:\n\t\t- ".implode("\n\t\t- ", $messages); } public function getProblems() { return $this->problems; } } literals = $literals; $this->reason = $reason; $this->reasonData = $reasonData; $this->disabled = false; $this->weak = false; $this->watch1 = (count($this->literals) > 0) ? $literals[0]->getId() : 0; $this->watch2 = (count($this->literals) > 1) ? $literals[1]->getId() : 0; $this->type = -1; $this->ruleHash = substr(md5(implode(',', array_map(function ($l) { return $l->getId(); }, $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 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]->getId() !== $rule->literals[$i]->getId()) { 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 isWeak() { return $this->weak; } public function setWeak($weak) { $this->weak = $weak; } public function getLiterals() { return $this->literals; } public function isAssertion() { return 1 === count($this->literals); } public function getNext(Literal $literal) { if ($this->watch1 == $literal->getId()) { return $this->next1; } else { return $this->next2; } } public function getOtherWatch(Literal $literal) { if ($this->watch1 == $literal->getId()) { return $this->watch2; } else { return $this->watch1; } } public function toHumanReadableString() { $ruleText = ''; foreach ($this->literals as $i => $literal) { if ($i != 0) { $ruleText .= '|'; } $ruleText .= $literal; } 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_JOB_LOCK: return "Lock command rule ($ruleText)"; case self::RULE_NOT_INSTALLABLE: return $ruleText; case self::RULE_PACKAGE_CONFLICT: $package1 = $this->literals[0]->getPackage(); $package2 = $this->literals[1]->getPackage(); return 'Package "'.$package1.'" conflicts with "'.$package2.'"'; case self::RULE_PACKAGE_REQUIRES: $literals = $this->literals; $sourceLiteral = array_shift($literals); $sourcePackage = $sourceLiteral->getPackage(); $requires = array(); foreach ($literals as $literal) { $requires[] = $literal->getPackage(); } $text = 'Package "'.$sourcePackage.'" contains the rule '.$this->reasonData.'. '; if ($requires) { $text .= 'Any of these packages satisfy the dependency: '.implode(', ', $requires).'.'; } else { $text .= 'No package satisfies this dependency.'; } return $text; case self::RULE_PACKAGE_OBSOLETES: return $ruleText; case self::RULE_INSTALLED_PACKAGE_OBSOLETES: return $ruleText; case self::RULE_PACKAGE_SAME_NAME: return $ruleText; case self::RULE_PACKAGE_IMPLICIT_OBSOLETES: return $ruleText; case self::RULE_LEARNED: return 'learned: '.$ruleText; case self::RULE_PACKAGE_ALIAS: return $ruleText; } } public function __toString() { $result = ($this->isDisabled()) ? 'disabled(' : '('; foreach ($this->literals as $i => $literal) { if ($i != 0) { $result .= '|'; } $result .= $literal; } $result .= ')'; return $result; } private function compareLiteralsById(Literal $a, Literal $b) { if ($a->getId() === $b->getId()) { return 0; } return $a->getId() < $b->getId() ? -1 : 1; } } 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]); } } 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; } } getVersion()); $version = new VersionConstraint('==', $a->getVersion()); return $constraint->matchSpecific($version); } public function findUpdatePackages(Solver $solver, Pool $pool, array $installedMap, PackageInterface $package) { $packages = array(); foreach ($pool->whatProvides($package->getName()) as $candidate) { if ($candidate !== $package) { $packages[] = $candidate; } } return $packages; } public function installable(Solver $solver, Pool $pool, array $installedMap, PackageInterface $package) { return true; } public function getPriority(Pool $pool, PackageInterface $package) { return $pool->getPriority($package->getRepository()); } public function selectPreferedPackages(Pool $pool, array $installedMap, array $literals) { $packages = $this->groupLiteralsByNamePreferInstalled($installedMap, $literals); foreach ($packages as &$literals) { $policy = $this; usort($literals, function ($a, $b) use ($policy, $pool, $installedMap) { return $policy->compareByPriorityPreferInstalled($pool, $installedMap, $a->getPackage(), $b->getPackage(), true); }); } foreach ($packages as &$literals) { $literals = $this->pruneToBestVersion($literals); $literals = $this->pruneToHighestPriorityOrInstalled($pool, $installedMap, $literals); $literals = $this->pruneRemoteAliases($literals); } $selected = call_user_func_array('array_merge', $packages); usort($selected, function ($a, $b) use ($policy, $pool, $installedMap) { return $policy->compareByPriorityPreferInstalled($pool, $installedMap, $a->getPackage(), $b->getPackage()); }); return $selected; } protected function groupLiteralsByNamePreferInstalled(array $installedMap, $literals) { $packages = array(); foreach ($literals as $literal) { $packageName = $literal->getPackage()->getName(); if (!isset($packages[$packageName])) { $packages[$packageName] = array(); } if (isset($installedMap[$literal->getPackageId()])) { array_unshift($packages[$packageName], $literal); } else { $packages[$packageName][] = $literal; } } return $packages; } public function compareByPriorityPreferInstalled(Pool $pool, array $installedMap, PackageInterface $a, PackageInterface $b, $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 ($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($literals) { $bestLiterals = array($literals[0]); $bestPackage = $literals[0]->getPackage(); foreach ($literals as $i => $literal) { if (0 === $i) { continue; } if ($this->versionCompare($literal->getPackage(), $bestPackage, '>')) { $bestPackage = $literal->getPackage(); $bestLiterals = array($literal); } else if ($this->versionCompare($literal->getPackage(), $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); } else if ($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 = $literal->getPackage(); 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(array $literals) { $hasLocalAlias = false; foreach ($literals as $literal) { $package = $literal->getPackage(); if ($package instanceof AliasPackage && $package->isRootPackageAlias()) { $hasLocalAlias = true; break; } } if (!$hasLocalAlias) { return $literals; } $selected = array(); foreach ($literals as $literal) { $package = $literal->getPackage(); if ($package instanceof AliasPackage && $package->isRootPackageAlias()) { $selected[] = $literal; } } return $selected; } } 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 '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()).')'; } } 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 '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) { if (!$package->isDev() || !in_array($package->getSourceType(), array('hg', 'git'))) { return $package->getPrettyVersion(); } return $package->getPrettyVersion().' '.substr($package->getSourceReference(), 0, 6); } } addReason(serialize($job), array( 'rule' => $rule, 'job' => $job, )); } public function addRule(Rule $rule) { $this->addReason($rule->getId(), array( 'rule' => $rule, 'job' => null, )); } public function getReasons() { return $this->reasons; } public function __toString() { if (count($this->reasons) === 1) { reset($this->reasons); $reason = current($this->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 'The requested PHP extension "'.$job['packageName'].'" '.$this->constraintToText($job['constraint']).$error.'.'; } return 'The requested package "'.$job['packageName'].'" '.$this->constraintToText($job['constraint']).'could not be found.'; } } $messages = array("Problem caused by:"); foreach ($this->reasons as $reason) { $rule = $reason['rule']; $job = $reason['job']; if ($job) { $messages[] = $this->jobToText($job); } elseif ($rule) { if ($rule instanceof Rule) { $messages[] = $rule->toHumanReadableString(); } } } return implode("\n\t\t\t- ", $messages); } protected function addReason($id, $reason) { if (!isset($this->reasons[$id])) { $this->reasons[$id] = $reason; } } protected function jobToText($job) { switch ($job['cmd']) { case 'install': return 'Installation of package "'.$job['packageName'].'" '.$this->constraintToText($job['constraint']).'was requested. Satisfiable by packages ['.implode(', ', $job['packages']).'].'; case 'update': return 'Update of package "'.$job['packageName'].'" '.$this->constraintToText($job['constraint']).'was requested.'; case 'remove': return 'Removal of package "'.$job['packageName'].'" '.$this->constraintToText($job['constraint']).'was requested.'; } return 'Job(cmd='.$job['cmd'].', target='.$job['packageName'].', packages=['.implode(', ', $job['packages']).'])'; } protected function constraintToText($constraint) { return ($constraint) ? 'with constraint '.$constraint.' ' : ''; } } 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) { if ($repo instanceof CompositeRepository) { $repos = $repo->getRepositories(); } else { $repos = array($repo); } $id = count($this->packages) + 1; foreach ($repos as $repo) { $this->repositories[] = $repo; $exempt = $repo instanceof PlatformRepository || $repo instanceof InstalledRepositoryInterface; foreach ($repo->getPackages() as $package) { $name = $package->getName(); $stability = $package->getStability(); if ( $exempt || (!isset($this->stabilityFlags[$name]) && isset($this->acceptableStabilities[$stability])) || (isset($this->stabilityFlags[$name]) && BasePackage::$stabilities[$stability] <= $this->stabilityFlags[$name] ) ) { $package->setId($id++); $this->packages[] = $package; foreach ($package->getNames() as $name) { $this->packageByName[$name][] = $package; } } } } } 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->packages[$id - 1]; } public function getMaxId() { return count($this->packages); } public function whatProvides($name, LinkConstraintInterface $constraint = null) { if (!isset($this->packageByName[$name])) { return array(); } $candidates = $this->packageByName[$name]; if (null === $constraint) { return $candidates; } $result = array(); foreach ($candidates as $candidate) { if ($candidate->matches($name, $constraint)) { $result[] = $candidate; } } return $result; } } package = $package; $this->wanted = $wanted; $this->id = ($this->wanted ? '' : '-') . $this->package->getId(); } public function isWanted() { return $this->wanted; } public function getPackage() { return $this->package; } public function getPackageId() { return $this->package->getId(); } public function getId() { return $this->id; } public function __toString() { return ($this->wanted ? '+' : '-') . $this->getPackage(); } public function inverted() { return new Literal($this->getPackage(), !$this->isWanted()); } public function equals(Literal $b) { return $this->id === $b->id; } } '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; } } policy = $policy; $this->pool = $pool; $this->installed = $installed; $this->rules = new RuleSet; } protected function createRequireRule(PackageInterface $package, array $providers, $reason, $reasonData = null) { $literals = array(new Literal($package, false)); foreach ($providers as $provider) { if ($provider === $package) { return null; } $literals[] = new Literal($provider, true); } return new Rule($literals, $reason, $reasonData); } protected function createUpdateRule(PackageInterface $package, array $updates, $reason, $reasonData = null) { $literals = array(new Literal($package, true)); foreach ($updates as $update) { $literals[] = new Literal($update, true); } return new Rule($literals, $reason, $reasonData); } protected function createInstallRule(PackageInterface $package, $reason, $reasonData = null) { return new Rule(new Literal($package, true)); } protected function createInstallOneOfRule(array $packages, $reason, $reasonData = null) { $literals = array(); foreach ($packages as $package) { $literals[] = new Literal($package, true); } return new Rule($literals, $reason, $reasonData); } protected function createRemoveRule(PackageInterface $package, $reason, $reasonData = null) { return new Rule(array(new Literal($package, false)), $reason, $reasonData); } protected function createConflictRule(PackageInterface $issuer, PackageInterface $provider, $reason, $reasonData = null) { if ($issuer === $provider) { return null; } return new Rule(array(new Literal($issuer, false), new Literal($provider, false)), $reason, $reasonData); } private function addRule($type, Rule $newRule = null) { if ($newRule) { if ($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; if (!$this->policy->installable($this, $this->pool, $this->installedMap, $package)) { $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRemoveRule($package, Rule::RULE_NOT_INSTALLABLE, (string) $package)); continue; } 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, (string) $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, (string) $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, (string) $link)); } } } if (!$isInstalled) { $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, (string) $package)); } else if (!$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, (string) $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, $this->pool, $this->installedMap, $package); $this->addRulesForPackage($package); foreach ($updates as $update) { $this->addRulesForPackage($update); } } private function addWatchesToRule(Rule $rule) { if ($rule->isAssertion()) { return; } if (!isset($this->watches[$rule->watch1])) { $this->watches[$rule->watch1] = null; } $rule->next1 = $this->watches[$rule->watch1]; $this->watches[$rule->watch1] = $rule; if (!isset($this->watches[$rule->watch2])) { $this->watches[$rule->watch2] = null; } $rule->next2 = $this->watches[$rule->watch2]; $this->watches[$rule->watch2] = $rule; } private function watch2OnHighest(Rule $rule) { $literals = $rule->getLiterals(); if ($literals < 3) { return; } $watchLevel = 0; foreach ($literals as $literal) { $level = abs($this->decisionMap[$literal->getPackageId()]); if ($level > $watchLevel) { $rule->watch2 = $literal->getId(); $watchLevel = $level; } } } private function findDecisionRule(PackageInterface $package) { foreach ($this->decisionQueue as $i => $literal) { if ($package === $literal->getPackage()) { return $this->decisionQueueWhy[$i]; } } return null; } private function makeAssertionRuleDecisions() { $decisionStart = count($this->decisionQueue); for ($ruleIndex = 0; $ruleIndex < count($this->rules); $ruleIndex++) { $rule = $this->rules->ruleById($ruleIndex); if ($rule->isWeak() || !$rule->isAssertion() || $rule->isDisabled()) { continue; } $literals = $rule->getLiterals(); $literal = $literals[0]; if (!$this->decided($literal->getPackage())) { $this->decisionQueue[] = $literal; $this->decisionQueueWhy[] = $rule; $this->addDecision($literal, 1); continue; } if ($this->decisionsSatisfy($literal)) { continue; } if (RuleSet::TYPE_LEARNED === $rule->getType()) { $rule->disable(); continue; } $conflict = $this->findDecisionRule($literal->getPackage()); if ($conflict && RuleSet::TYPE_PACKAGE === $conflict->getType()) { $problem = new Problem; if ($rule->getType() == RuleSet::TYPE_JOB) { $job = $this->ruleToJob[$rule->getId()]; $problem->addJobRule($job, $rule); $problem->addRule($conflict); $this->disableProblem($job); } else { $problem->addRule($rule); $problem->addRule($conflict); $this->disableProblem($rule); } $this->problems[] = $problem; continue; } $problem = new Problem; $problem->addRule($rule); $problem->addRule($conflict); foreach ($this->rules->getIteratorFor(RuleSet::TYPE_JOB) as $assertRule) { if ($assertRule->isDisabled() || !$assertRule->isAssertion() || $assertRule->isWeak()) { continue; } $assertRuleLiterals = $assertRule->getLiterals(); $assertRuleLiteral = $assertRuleLiterals[0]; if ($literal->getPackageId() !== $assertRuleLiteral->getPackageId()) { continue; } if ($assertRule->getType() === RuleSet::TYPE_JOB) { $job = $this->ruleToJob[$assertRule->getId()]; $problem->addJobRule($job, $assertRule); $this->disableProblem($job); } else { $problem->addRule($assertRule); $this->disableProblem($assertRule); } } $this->problems[] = $problem; while (count($this->decisionQueue) > $decisionStart) { $decisionLiteral = array_pop($this->decisionQueue); array_pop($this->decisionQueueWhy); unset($this->decisionQueueFree[count($this->decisionQueue)]); $this->decisionMap[$decisionLiteral->getPackageId()] = 0; } $ruleIndex = -1; } foreach ($this->rules as $rule) { if (!$rule->isWeak() || !$rule->isAssertion() || $rule->isDisabled()) { continue; } $literals = $rule->getLiterals(); $literal = $literals[0]; if ($this->decisionMap[$literal->getPackageId()] == 0) { $this->decisionQueue[] = $literal; $this->decisionQueueWhy[] = $rule; $this->addDecision($literal, 1); continue; } if ($this->decisionsSatisfy($literals[0])) { continue; } if ($rule->getType() == RuleSet::TYPE_JOB) { $why = $this->ruleToJob[$rule->getId()]; } else { $why = $rule; } $this->disableProblem($why); } } protected function setupInstalledMap() { $this->installedMap = array(); foreach ($this->installed->getPackages() as $package) { $this->installedMap[$package->getId()] = $package; } } public function solve(Request $request) { $this->jobs = $request->getJobs(); $this->setupInstalledMap(); if (version_compare(PHP_VERSION, '5.3.4', '>=')) { $this->decisionMap = new \SplFixedArray($this->pool->getMaxId() + 1); } else { $this->decisionMap = array_fill(0, $this->pool->getMaxId() + 1, 0); } foreach ($this->jobs as $job) { foreach ($job['packages'] as $package) { switch ($job['cmd']) { case 'update': if (isset($this->installedMap[$package->getId()])) { $this->updateMap[$package->getId()] = true; } break; } } switch ($job['cmd']) { case 'update-all': foreach ($this->installedMap as $package) { $this->updateMap[$package->getId()] = true; } break; } } foreach ($this->installedMap as $package) { $this->addRulesForPackage($package); } foreach ($this->installedMap as $package) { $this->addRulesForUpdatePackages($package); } foreach ($this->jobs as $job) { foreach ($job['packages'] as $package) { switch ($job['cmd']) { case 'install': $this->installCandidateMap[$package->getId()] = true; $this->addRulesForPackage($package); break; } } } foreach ($this->installedMap as $package) { $updates = $this->policy->findUpdatePackages($this, $this->pool, $this->installedMap, $package); $rule = $this->createUpdateRule($package, $updates, Rule::RULE_INTERNAL_ALLOW_UPDATE, (string) $package); $this->packageToUpdateRule[$package->getId()] = $rule; } foreach ($this->jobs as $job) { switch ($job['cmd']) { case 'install': if (empty($job['packages'])) { $problem = new Problem(); $problem->addJobRule($job); $this->problems[] = $problem; } else { $rule = $this->createInstallOneOfRule($job['packages'], Rule::RULE_JOB_INSTALL, $job['packageName']); $this->addRule(RuleSet::TYPE_JOB, $rule); $this->ruleToJob[$rule->getId()] = $job; } break; case 'remove': foreach ($job['packages'] as $package) { $rule = $this->createRemoveRule($package, Rule::RULE_JOB_REMOVE); $this->addRule(RuleSet::TYPE_JOB, $rule); $this->ruleToJob[$rule->getId()] = $job; } break; case 'lock': foreach ($job['packages'] as $package) { if (isset($this->installedMap[$package->getId()])) { $rule = $this->createInstallRule($package, Rule::RULE_JOB_LOCK); } else { $rule = $this->createRemoveRule($package, Rule::RULE_JOB_LOCK); } $this->addRule(RuleSet::TYPE_JOB, $rule); $this->ruleToJob[$rule->getId()] = $job; } break; } } foreach ($this->rules as $rule) { $this->addWatchesToRule($rule); } $this->makeAssertionRuleDecisions(); $installRecommended = 0; $this->runSat(true, $installRecommended); if ($this->problems) { throw new SolverProblemsException($this->problems); } return $this->createTransaction(); } protected function createTransaction() { $transaction = array(); $installMeansUpdateMap = array(); foreach ($this->decisionQueue as $i => $literal) { $package = $literal->getPackage(); if (!$literal->isWanted() && isset($this->installedMap[$package->getId()])) { $literals = array(); if (isset($this->packageToUpdateRule[$package->getId()])) { $literals = array_merge($literals, $this->packageToUpdateRule[$package->getId()]->getLiterals()); } foreach ($literals as $updateLiteral) { if (!$updateLiteral->equals($literal)) { $installMeansUpdateMap[$updateLiteral->getPackageId()] = $package; } } } } foreach ($this->decisionQueue as $i => $literal) { $package = $literal->getPackage(); if ($literal->isWanted() == (isset($this->installedMap[$package->getId()]))) { continue; } if ($literal->isWanted()) { if ($package instanceof AliasPackage) { $transaction[] = new Operation\MarkAliasInstalledOperation( $package, $this->decisionQueueWhy[$i] ); continue; } if (isset($installMeansUpdateMap[$literal->getPackageId()])) { $source = $installMeansUpdateMap[$literal->getPackageId()]; $transaction[] = new Operation\UpdateOperation( $source, $package, $this->decisionQueueWhy[$i] ); unset($installMeansUpdateMap[$literal->getPackageId()]); $ignoreRemove[$source->getId()] = true; } else { $transaction[] = new Operation\InstallOperation( $package, $this->decisionQueueWhy[$i] ); } } else if (!isset($ignoreRemove[$package->getId()])) { if ($package instanceof AliasPackage) { $transaction[] = new Operation\MarkAliasInstalledOperation( $package, $this->decisionQueueWhy[$i] ); } else { $transaction[] = new Operation\UninstallOperation( $package, $this->decisionQueueWhy[$i] ); } } } $allDecidedMap = $this->decisionMap; foreach ($this->decisionMap as $packageId => $decision) { if ($decision != 0) { $package = $this->pool->packageById($packageId); if ($package instanceof AliasPackage) { $allDecidedMap[$package->getAliasOf()->getId()] = $decision; } } } foreach ($allDecidedMap as $packageId => $decision) { if ($packageId === 0) { continue; } if (0 == $decision && isset($this->installedMap[$packageId])) { $package = $this->pool->packageById($packageId); if ($package instanceof AliasPackage) { $transaction[] = new Operation\MarkAliasInstalledOperation( $package, null ); } else { $transaction[] = new Operation\UninstallOperation( $package, null ); } $this->decisionMap[$packageId] = -1; } } foreach ($allDecidedMap as $packageId => $decision) { if ($packageId === 0) { continue; } if (0 == $decision && isset($this->installedMap[$packageId])) { $package = $this->pool->packageById($packageId); if ($package instanceof AliasPackage) { $transaction[] = new Operation\MarkAliasInstalledOperation( $package, null ); } else { $transaction[] = new Operation\UninstallOperation( $package, null ); } $this->decisionMap[$packageId] = -1; } } return array_reverse($transaction); } protected function literalFromId($id) { $package = $this->pool->packageById(abs($id)); return new Literal($package, $id > 0); } protected function addDecision(Literal $l, $level) { $this->addDecisionId($l->getId(), $level); } protected function addDecisionId($literalId, $level) { $packageId = abs($literalId); $previousDecision = $this->decisionMap[$packageId]; if ($previousDecision != 0) { $literal = $this->literalFromId($literalId); throw new SolverBugException( "Trying to decide $literal on level $level, even though ".$literal->getPackage()." was previously decided as ".(int) $previousDecision."." ); } if ($literalId > 0) { $this->decisionMap[$packageId] = $level; } else { $this->decisionMap[$packageId] = -$level; } } protected function decisionsContain(Literal $l) { return ( $this->decisionMap[$l->getPackageId()] > 0 && $l->isWanted() || $this->decisionMap[$l->getPackageId()] < 0 && !$l->isWanted() ); } protected function decisionsContainId($literalId) { $packageId = abs($literalId); return ( $this->decisionMap[$packageId] > 0 && $literalId > 0 || $this->decisionMap[$packageId] < 0 && $literalId < 0 ); } protected function decisionsSatisfy(Literal $l) { return ($l->isWanted() && $this->decisionMap[$l->getPackageId()] > 0) || (!$l->isWanted() && $this->decisionMap[$l->getPackageId()] < 0); } protected function decisionsConflict(Literal $l) { return ( $this->decisionMap[$l->getPackageId()] > 0 && !$l->isWanted() || $this->decisionMap[$l->getPackageId()] < 0 && $l->isWanted() ); } protected function decisionsConflictId($literalId) { $packageId = abs($literalId); return ( ($this->decisionMap[$packageId] > 0 && $literalId < 0) || ($this->decisionMap[$packageId] < 0 && $literalId > 0) ); } protected function decided(PackageInterface $p) { return $this->decisionMap[$p->getId()] != 0; } protected function undecided(PackageInterface $p) { return $this->decisionMap[$p->getId()] == 0; } protected function decidedInstall(PackageInterface $p) { return $this->decisionMap[$p->getId()] > 0; } protected function decidedRemove(PackageInterface $p) { return $this->decisionMap[$p->getId()] < 0; } protected function propagate($level) { while ($this->propagateIndex < count($this->decisionQueue)) { $literal = $this->decisionQueue[$this->propagateIndex]->inverted(); $this->propagateIndex++; if (!isset($this->watches[$literal->getId()])) { continue; } $prevRule = null; for ($rule = $this->watches[$literal->getId()]; $rule !== null; $prevRule = $rule, $rule = $nextRule) { $nextRule = $rule->getNext($literal); if ($rule->isDisabled()) { continue; } $otherWatch = $rule->getOtherWatch($literal); if ($this->decisionsContainId($otherWatch)) { continue; } $ruleLiterals = $rule->getLiterals(); if (sizeof($ruleLiterals) > 2) { foreach ($ruleLiterals as $ruleLiteral) { if ($otherWatch !== $ruleLiteral->getId() && !$this->decisionsConflict($ruleLiteral)) { if ($literal->getId() === $rule->watch1) { $rule->watch1 = $ruleLiteral->getId(); $rule->next1 = (isset($this->watches[$ruleLiteral->getId()])) ? $this->watches[$ruleLiteral->getId()] : null; } else { $rule->watch2 = $ruleLiteral->getId(); $rule->next2 = (isset($this->watches[$ruleLiteral->getId()])) ? $this->watches[$ruleLiteral->getId()] : null; } if ($prevRule) { if ($prevRule->next1 == $rule) { $prevRule->next1 = $nextRule; } else { $prevRule->next2 = $nextRule; } } else { $this->watches[$literal->getId()] = $nextRule; } $this->watches[$ruleLiteral->getId()] = $rule; $rule = $prevRule; continue 2; } } } if ($this->decisionsConflictId($otherWatch)) { return $rule; } $this->addDecisionId($otherWatch, $level); $this->decisionQueue[] = $this->literalFromId($otherWatch); $this->decisionQueueWhy[] = $rule; } } return null; } private function revert($level) { while (!empty($this->decisionQueue)) { $literal = $this->decisionQueue[count($this->decisionQueue) - 1]; if (!$this->decisionMap[$literal->getPackageId()]) { break; } $decisionLevel = abs($this->decisionMap[$literal->getPackageId()]); if ($decisionLevel <= $level) { break; } $this->decisionMap[$literal->getPackageId()] = 0; array_pop($this->decisionQueue); array_pop($this->decisionQueueWhy); $this->propagateIndex = count($this->decisionQueue); } while (!empty($this->branches)) { list($literals, $branchLevel) = $this->branches[count($this->branches) - 1]; if ($branchLevel >= $level) { break; } array_pop($this->branches); } $this->recommendsIndex = -1; } private function setPropagateLearn($level, Literal $literal, $disableRules, Rule $rule) { $level++; $this->addDecision($literal, $level); $this->decisionQueue[] = $literal; $this->decisionQueueWhy[] = $rule; $this->decisionQueueFree[count($this->decisionQueueWhy) - 1] = true; 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."." ); } else if (!$newRule) { throw new SolverBugException( "No rule was learned from analyzing $rule at level $level." ); } $level = $newLevel; $this->revert($level); $this->addRule(RuleSet::TYPE_LEARNED, $newRule); $this->learnedWhy[$newRule->getId()] = $why; $this->watch2OnHighest($newRule); $this->addWatchesToRule($newRule); $this->addDecision($learnLiteral, $level); $this->decisionQueue[] = $learnLiteral; $this->decisionQueueWhy[] = $newRule; } return $level; } private function selectAndInstall($level, array $decisionQueue, $disableRules, Rule $rule) { $literals = $this->policy->selectPreferedPackages($this->pool, $this->installedMap, $decisionQueue); $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->decisionQueue); $this->learnedPool[] = array(); while(true) { $this->learnedPool[count($this->learnedPool) - 1][] = $rule; foreach ($rule->getLiterals() as $literal) { if ($this->decisionsSatisfy($literal)) { continue; } if (isset($seen[$literal->getPackageId()])) { continue; } $seen[$literal->getPackageId()] = true; $l = abs($this->decisionMap[$literal->getPackageId()]); if (1 === $l) { $l1num++; } else if ($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--; $literal = $this->decisionQueue[$decisionId]; if (isset($seen[$literal->getPackageId()])) { break; } } unset($seen[$literal->getPackageId()]); if ($num && 0 === --$num) { $learnedLiterals[0] = $this->literalFromId(-$literal->getPackageId()); if (!$l1num) { break 2; } foreach ($learnedLiterals as $i => $learnedLiteral) { if ($i !== 0) { unset($seen[$literal->getPackageId()]); } } $l1num++; $l1retry = true; } } $rule = $this->decisionQueueWhy[$decisionId]; } $why = count($this->learnedPool) - 1; if (!$learnedLiterals[0]) { throw new SolverBugException( "Did not find a learnable literal in analyzed rule $analyzedRule." ); } $newRule = new Rule($learnedLiterals, Rule::RULE_LEARNED, $why); return array($learnedLiterals[0], $ruleLevel, $newRule, $why); } private function analyzeUnsolvableRule($problem, $conflictRule, &$lastWeakWhy) { $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, $lastWeakWhy); } return; } if ($conflictRule->getType() == RuleSet::TYPE_PACKAGE) { return; } if ($conflictRule->isWeak()) { if (!$lastWeakWhy || $why > $lastWeakWhy->getId()) { $lastWeakWhy = $conflictRule; } } if ($conflictRule->getType() == RuleSet::TYPE_JOB) { $job = $this->ruleToJob[$conflictRule->getId()]; $problem->addJobRule($job, $conflictRule); } else { $problem->addRule($conflictRule); } } private function analyzeUnsolvable($conflictRule, $disableRules) { $lastWeakWhy = null; $problem = new Problem; $problem->addRule($conflictRule); $this->analyzeUnsolvableRule($problem, $conflictRule, $lastWeakWhy); $this->problems[] = $problem; $seen = array(); $literals = $conflictRule->getLiterals(); foreach ($literals as $literal) { if ($this->decisionsSatisfy($literal)) { continue; } $seen[$literal->getPackageId()] = true; } $decisionId = count($this->decisionQueue); while ($decisionId > 0) { $decisionId--; $literal = $this->decisionQueue[$decisionId]; if (!isset($seen[$literal->getPackageId()])) { continue; } $why = $this->decisionQueueWhy[$decisionId]; $problem->addRule($why); $this->analyzeUnsolvableRule($problem, $why, $lastWeakWhy); $literals = $why->getLiterals(); foreach ($literals as $literal) { if ($this->decisionsSatisfy($literal)) { continue; } $seen[$literal->getPackageId()] = true; } } if ($lastWeakWhy) { array_pop($this->problems); if ($lastWeakWhy->getType() === RuleSet::TYPE_JOB) { $why = $this->ruleToJob[$lastWeakWhy]; } else { $why = $lastWeakWhy; } $this->disableProblem($why); $this->resetSolver(); return 1; } if ($disableRules) { foreach ($this->problems[count($this->problems) - 1] as $reason) { if ($reason['job']) { $this->disableProblem($reason['job']); } else { $this->disableProblem($reason['rule']); } } $this->resetSolver(); return 1; } return 0; } private function disableProblem($why) { if ($why instanceof Rule) { $why->disable(); } else if (is_array($why)) { foreach ($this->ruleToJob as $ruleId => $job) { if ($why === $job) { $this->rules->ruleById($ruleId)->disable(); } } } } private function resetSolver() { while ($literal = array_pop($this->decisionQueue)) { $this->decisionMap[$literal->getPackageId()] = 0; } $this->decisionQueueWhy = array(); $this->decisionQueueFree = array(); $this->recommendsIndex = -1; $this->propagateIndex = 0; $this->recommendations = array(); $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(); } else if (!$foundDisabled && $rule->isDisabled()) { $rule->enable(); } } } private function runSat($disableRules = true, $installRecommended = false) { $this->propagateIndex = 0; $decisionQueue = array(); $decisionSupplementQueue = array(); $disableRules = array(); $level = 1; $systemLevel = $level + 1; $minimizationSteps = 0; $installedPos = 0; while (true) { if (1 === $level) { $conflictRule = $this->propagate($level); if ($conflictRule !== null) { if ($this->analyzeUnsolvable($conflictRule, $disableRules)) { continue; } else { 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->decisionsSatisfy($literal)) { $noneSatisfied = false; break; } $decisionQueue[] = $literal; } if ($noneSatisfied && count($decisionQueue)) { if (count($this->installed) != count($this->updateMap)) { $prunedQueue = array(); foreach ($decisionQueue as $literal) { if (isset($this->installedMap[$literal->getPackageId()])) { $prunedQueue[] = $literal; if (isset($this->updateMap[$literal->getPackageId()])) { $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->isWanted()) { if (!$this->decidedInstall($literal->getPackage())) { continue 2; } } else { if ($this->decidedInstall($literal->getPackage())) { continue 2; } if ($this->undecided($literal->getPackage())) { $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; for ($i = count($this->branches) - 1; $i >= 0; $i--) { list($literals, $level) = $this->branches[$i]; foreach ($literals as $offset => $literal) { if ($literal && $literal->isWanted() && $this->decisionMap[$literal->getPackageId()] > $level + 1) { $lastLiteral = $literal; $lastBranchIndex = $i; $lastBranchOffset = $offset; $lastLevel = $level; } } } if ($lastLiteral) { $this->branches[$lastBranchIndex][$lastBranchOffset] = null; $minimizationSteps++; $level = $lastLevel; $this->revert($level); $why = $this->decisionQueueWhy[count($this->decisionQueueWhy) - 1]; $oLevel = $level; $level = $this->setPropagateLearn($level, $lastLiteral, $disableRules, $why); if ($level == 0) { return; } continue; } } break; } } private function printDecisionMap() { echo "\nDecisionMap: \n"; foreach ($this->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"; } private function printDecisionQueue() { echo "DecisionQueue: \n"; foreach ($this->decisionQueue as $i => $literal) { echo ' ' . $literal . ' ' . $this->decisionQueueWhy[$i]." level ".$this->decisionMap[$literal->getPackageId()]."\n"; } echo "\n"; } private 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"; } } } %s [%s]%s ', $question, $default, $sep) : sprintf('%s%s ', $question, $sep); } }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('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), new InputOption('repository-url', null, InputOption::VALUE_REQUIRED, 'Pick a different repository url to look for the package.'), )) ->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] To setup a developer workable version you should create the project using the source controlled code by appending the '--prefer-source' flag. To install a package from another repository repository than the default one you can pass the '--repository-url=http://myrepository.org' flag. EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { return $this->installProject( $this->getIO(), $input->getArgument('package'), $input->getArgument('directory'), $input->getArgument('version'), (Boolean) $input->getOption('prefer-source'), $input->getOption('repository-url') ); } public function installProject(IOInterface $io, $packageName, $directory = null, $version = null, $preferSource = false, $repositoryUrl = null) { $dm = $this->createDownloadManager($io); if ($preferSource) { $dm->setPreferSource(true); } $config = Factory::createConfig(); if (null === $repositoryUrl) { $sourceRepo = new ComposerRepository(array('url' => 'http://packagist.org'), $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."); } $candidates = $sourceRepo->findPackages($packageName, $version); if (!$candidates) { throw new \InvalidArgumentException("Could not find package $packageName" . ($version ? " with version $version." : '')); } if (null === $directory) { $parts = explode("/", $packageName, 2); $directory = getcwd() . DIRECTORY_SEPARATOR . array_pop($parts); } $package = $candidates[0]; foreach ($candidates as $candidate) { if (version_compare($package->getVersion(), $candidate->getVersion(), '<')) { $package = $candidate; } } $io->write('Installing ' . $package->getName() . ' as new project.', true); $projectInstaller = new ProjectInstaller($directory, $dm); $projectInstaller->install(new InstalledFilesystemRepository(new JsonFile('php://memory')), $package); $io->write('Created project into directory ' . $directory . '', true); chdir($directory); $composer = Factory::create($io); $installer = Installer::create($io, $composer); $installer ->setPreferSource($preferSource) ->run(); } protected function createDownloadManager(IOInterface $io) { $factory = new Factory(); return $factory->createDownloadManager($io); } } [- \.,\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_NONE, 'Name of the package'), new InputOption('description', null, InputOption::VALUE_NONE, 'Description of package'), new InputOption('author', null, InputOption::VALUE_NONE, 'Author name of package'), new InputOption('homepage', null, InputOption::VALUE_NONE, 'Homepage of package'), new InputOption('require', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'An array required packages'), )) ->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', 'require'); $options = array_filter(array_intersect_key($input->getOptions(), array_flip($whitelist))); if (isset($options['author'])) { $options['authors'] = $this->formatAuthors($options['author']); unset($options['author']); } $options['require'] = isset($options['require']) ? $this->formatRequirements($options['require']) : 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()) { $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 (false === $name = $input->getOption('name')) { $name = basename($cwd); 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; } } $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_.-]+$}i', $value)) { throw new \InvalidArgumentException( 'The package name '.$value.' is invalid, it should 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 (false === $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); $output->writeln(array( '', 'Define your dependencies.', '' )); $requirements = array(); if ($dialog->askConfirmation($output, $dialog->getQuestion('Would you like to define your dependencies interactively', 'yes', '?'), true)) { $requirements = $this->determineRequirements($input, $output); } $input->setOption('require', $requirements); } protected function findPackages($name) { $packages = array(); if (!$this->repos) { $this->repos = new CompositeRepository(array( new PlatformRepository, new ComposerRepository(array('url' => 'http://packagist.org'), $this->getIO(), Factory::createConfig()) )); } $token = strtolower($name); foreach ($this->repos->getPackages() as $package) { if (false === ($pos = strpos($package->getName(), $token))) { continue; } $packages[] = $package; } return $packages; } protected function determineRequirements(InputInterface $input, OutputInterface $output) { $dialog = $this->getHelperSet()->get('dialog'); $prompt = $dialog->getQuestion('Search for a package', false, ':'); $requires = $input->getOption('require') ?: array(); 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), '' )); foreach ($matches as $position => $package) { $output->writeln(sprintf(' %5s %s %s', "[$position]", $package->getPrettyName(), $package->getPrettyVersion())); } $output->writeln(''); $validator = function ($selection) use ($matches) { if ('' === $selection) { return false; } if (!is_numeric($selection) && preg_match('{^\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 sprintf('%s %s', $package->getName(), $package->getPrettyVersion()); }; $package = $dialog->askAndValidate($output, $dialog->getQuestion('Enter package # to add, or a couple if it is not listed', false, ':'), $validator, 3); if (false !== $package) { $requires[] = $package; } } } return $requires; } protected function formatAuthors($author) { return array($this->parseAuthorString($author)); } protected function formatRequirements(array $requirements) { $requires = array(); foreach ($requirements as $requirement) { list($packageName, $packageVersion) = explode(" ", $requirement, 2); $requires[$packageName] = $packageVersion; } return empty($requires) ? new \stdClass : $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 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"); } }setName('update') ->setDescription('Updates your dependencies to the latest version, and updates the composer.lock file.') ->setDefinition(array( new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), 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 dev-require packages.'), )) ->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 EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $composer = $this->getComposer(); $io = $this->getIO(); $install = Installer::create($io, $composer); $install ->setDryRun($input->getOption('dry-run')) ->setVerbose($input->getOption('verbose')) ->setPreferSource($input->getOption('prefer-source')) ->setDevMode($input->getOption('dev')) ->setUpdate(true) ; return $install->run() ? 0 : 1; } } setName('show') ->setDescription('Show information about packages') ->setDefinition(array( new InputArgument('package', InputArgument::OPTIONAL, 'Package to inspect'), new InputArgument('version', InputArgument::OPTIONAL, 'Version to inspect'), new InputOption('installed', null, InputOption::VALUE_NONE, 'List installed packages only'), new InputOption('platform', null, InputOption::VALUE_NONE, 'List platform packages only'), )) ->setHelp(<<getOption('platform')) { $repos = $installedRepo = $platformRepo; } elseif ($input->getOption('installed')) { $composer = $this->getComposer(); $repos = $installedRepo = $composer->getRepositoryManager()->getLocalRepository(); } elseif ($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 { $output->writeln('No composer.json found in the current directory, showing packages from packagist.org'); $installedRepo = $platformRepo; $packagist = new ComposerRepository(array('url' => 'http://packagist.org'), $this->getIO(), Factory::createConfig()); $repos = new CompositeRepository(array($installedRepo, $packagist)); } if ($input->getArgument('package')) { $package = $this->getPackage($input, $output, $installedRepo, $repos); if (!$package) { throw new \InvalidArgumentException('Package '.$input->getArgument('package').' not found'); } $this->printMeta($input, $output, $package, $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(); foreach ($repos->getPackages() as $package) { if ($platformRepo->hasPackage($package)) { $type = 'platform:'; } elseif ($installedRepo->hasPackage($package)) { $type = 'installed:'; } else { $type = 'available:'; } if (isset($packages[$type][$package->getName()]) && version_compare($packages[$type][$package->getName()]->getVersion(), $package->getVersion(), '>=') ) { continue; } $packages[$type][$package->getName()] = $package; } foreach (array('platform:', 'available:', 'installed:') as $type) { if (isset($packages[$type])) { $output->writeln($type); ksort($packages[$type]); foreach ($packages[$type] as $package) { $output->writeln(' '.$package->getPrettyName() .' : '. strtok($package->getDescription(), "\r\n")); } $output->writeln(''); } } } protected function getPackage(InputInterface $input, OutputInterface $output, RepositoryInterface $installedRepo, RepositoryInterface $repos) { if ($input->getArgument('version')) { return $repos->findPackage($input->getArgument('package'), $input->getArgument('version')); } foreach ($installedRepo->getPackages() as $package) { if ($package->getName() === $input->getArgument('package')) { return $package; } } $highestVersion = null; foreach ($repos->findPackages($input->getArgument('package')) as $package) { if (null === $highestVersion || version_compare($package->getVersion(), $highestVersion->getVersion(), '>=')) { $highestVersion = $package; } } return $highestVersion; } protected function printMeta(InputInterface $input, OutputInterface $output, PackageInterface $package, 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, $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->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)); } } } } protected function printVersions(InputInterface $input, OutputInterface $output, PackageInterface $package, RepositoryInterface $installedRepo, RepositoryInterface $repos) { if ($input->getArgument('version')) { $output->writeln('version : ' . $package->getPrettyVersion()); return; } $versions = array(); foreach ($repos->findPackages($package->getName()) as $version) { $versions[$version->getPrettyVersion()] = $version->getVersion(); } uasort($versions, 'version_compare'); $versions = implode(', ', array_keys(array_reverse($versions))); if ($installedRepo->hasPackage($package)) { $versions = str_replace($package->getPrettyVersion(), '* ' . $package->getPrettyVersion() . '', $versions); } $output->writeln('versions : ' . $versions); } protected function printLinks(InputInterface $input, OutputInterface $output, PackageInterface $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() . ''); } } } } 'requires', 'require-dev' => 'devRequires', ); 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_OPTIONAL | 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) { $composer = $this->getComposer(); $references = $this->getReferences($input, $output, $composer); if ($input->getOption('verbose')) { $this->printReferences($input, $output, $references); } else { $this->printPackages($input, $output, $references); } } private function getReferences(InputInterface $input, OutputInterface $output, Composer $composer) { $needle = $input->getArgument('package'); $references = array(); $verbose = (Boolean) $input->getOption('verbose'); $repos = $composer->getRepositoryManager()->getRepositories(); $types = $input->getOption('link-type'); foreach ($repos as $repository) { foreach ($repository->getPackages() as $package) { foreach ($types as $type) { $type = rtrim($type, 's'); if (!isset($this->linkTypes[$type])) { throw new \InvalidArgumentException('Unexpected link type: '.$type.', valid types: '.implode(', ', array_keys($this->linkTypes))); } foreach ($package->{'get'.$this->linkTypes[$type]}() as $link) { if ($link->getTarget() === $needle) { if ($verbose) { $references[] = array($type, $package, $link); } else { $references[$package->getName()] = $package->getPrettyName(); } } } } } } return $references; } private function printReferences(InputInterface $input, OutputInterface $output, array $references) { foreach ($references as $ref) { $output->writeln($ref[1]->getPrettyName() . ' ' . $ref[1]->getPrettyVersion() . ' ' . $ref[0] . ' ' . $ref[2]->getPrettyConstraint()); } } private function printPackages(InputInterface $input, OutputInterface $output, array $packages) { ksort($packages); foreach ($packages as $package) { $output->writeln($package); } } }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 package manager tracking local dependencies of your projects and libraries. See http://getcomposer.org/ for more information. EOT ); } } getApplication()->getComposer($required); } protected function getIO() { return $this->getApplication()->getIO(); } } setName('search') ->setDescription('Search for packages') ->setDefinition(array( 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 { $output->writeln('No composer.json found in the current directory, showing packages from packagist.org'); $installedRepo = $platformRepo; $packagist = new ComposerRepository(array('url' => 'http://packagist.org'), $this->getIO(), Factory::createConfig()); $repos = new CompositeRepository(array($installedRepo, $packagist)); } $tokens = $input->getArgument('tokens'); $packages = array(); $maxPackageLength = 0; foreach ($repos->getPackages() as $package) { if ($package instanceof AliasPackage || isset($packages[$package->getName()])) { continue; } foreach ($tokens as $token) { if (!$score = $this->matchPackage($package, $token)) { continue; } if (false !== ($pos = stripos($package->getName(), $token))) { $name = substr($package->getPrettyName(), 0, $pos) . '' . substr($package->getPrettyName(), $pos, strlen($token)) . '' . substr($package->getPrettyName(), $pos + strlen($token)); } else { $name = $package->getPrettyName(); } $description = strtok($package->getDescription(), "\r\n"); if (false !== ($pos = stripos($description, $token))) { $description = substr($description, 0, $pos) . '' . substr($description, $pos, strlen($token)) . '' . substr($description, $pos + strlen($token)); } $packages[$package->getName()] = array( 'name' => $name, 'description' => $description, 'length' => $length = strlen($package->getPrettyName()), 'score' => $score, ); $maxPackageLength = max($maxPackageLength, $length); continue 2; } } usort($packages, function ($a, $b) { if ($a['score'] === $b['score']) { return 0; } return $a['score'] > $b['score'] ? -1 : 1; }); foreach ($packages as $details) { $extraSpaces = $maxPackageLength - $details['length']; $output->writeln($details['name'] . str_repeat(' ', $extraSpaces) .' : '. $details['description']); } } private function matchPackage(PackageInterface $package, $token) { $score = 0; if (false !== stripos($package->getName(), $token)) { $score += 5; } if (false !== stripos(join(',', $package->getKeywords() ?: array()), $token)) { $score += 3; } if (false !== stripos($package->getDescription(), $token)) { $score += 1; } return $score; } } setName('install') ->setDescription('Parses the composer.json file and downloads the needed dependencies.') ->setDefinition(array( new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'), 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 dev-require packages.'), )) ->setHelp(<<install command reads the composer.json file from the current directory, processes it, and downloads and installs all the libraries and dependencies outlined in that file. php composer.phar install EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $composer = $this->getComposer(); $io = $this->getIO(); $install = Installer::create($io, $composer); $install ->setDryRun($input->getOption('dry-run')) ->setVerbose($input->getOption('verbose')) ->setPreferSource($input->getOption('prefer-source')) ->setDevMode($input->getOption('dev')) ; 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; } $errors = array(); $publishErrors = array(); $warnings = array(); $laxValid = false; $valid = false; try { $json = new JsonFile($file, new RemoteFilesystem($this->getIO())); $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[] = 'Publish Error: ' . $message . ''; } else { $errors[] = '' . $message . ''; } } } catch (\Exception $e) { $output->writeln('' . $file . ' contains a JSON Syntax Error:'); $output->writeln('' . $e->getMessage() . ''); return 1; } if (!empty($manifest['license'])) { $licenseValidator = new SpdxLicenseIdentifier(); if (!$licenseValidator->validate($manifest['license'])) { $warnings[] = sprintf( 'License %s is not a valid SPDX license identifier, see http://www.spdx.org/licenses/', json_encode($manifest['license']) ); } } else { $warnings[] = 'No license specified, it is recommended to do so'; } 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; } } setName('self-update') ->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) { $rfs = new RemoteFilesystem($this->getIO()); $latest = trim($rfs->getContents('getcomposer.org', 'http://getcomposer.org/version', false)); if (Composer::VERSION !== $latest) { $output->writeln(sprintf("Updating to version %s.", $latest)); $remoteFilename = 'http://getcomposer.org/composer.phar'; $localFilename = $_SERVER['argv'][0]; $tempFilename = basename($localFilename, '.phar').'-temp.phar'; $rfs->copy('getcomposer.org', $remoteFilename, $tempFilename); try { chmod($tempFilename, 0777 & ~umask()); $phar = new \Phar($tempFilename); unset($phar); rename($tempFilename, $localFilename); } catch (\Exception $e) { if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) { throw $e; } unlink($tempFilename); $output->writeln('The download is corrupt ('.$e->getMessage().').'); $output->writeln('Please re-run the self-update command to try again.'); } } else { $output->writeln("You are using the latest composer version."); } } } installPath = $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)) { throw new \InvalidArgumentException("Project directory $installPath already exists."); } if (!file_exists(dirname($installPath))) { throw new \InvalidArgumentException("Project root " . dirname($installPath) . " does not exist."); } mkdir($installPath, 0777); $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; } } isAbsolutePath($vendorDir)) { $basePath = getcwd(); $relativePath = $fs->findShortestPath($basePath.'/file', $vendorDir); if ($fs->isAbsolutePath($relativePath)) { throw new \InvalidArgumentException("Vendor dir ($vendorDir) must be accessible from the directory ($basePath)."); } $this->vendorPath = $relativePath; } else { $this->vendorPath = rtrim($vendorDir, '/'); } } public function addInstaller(InstallerInterface $installer) { array_unshift($this->installers, $installer); $this->cache = array(); } 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); } 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->notifyInstall($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->notifyInstall($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 getVendorPath($absolute = false) { if (!$absolute) { return $this->vendorPath; } return getcwd().DIRECTORY_SEPARATOR.$this->vendorPath; } private function notifyInstall(PackageInterface $package) { if ($package->getRepository() instanceof NotifiableRepositoryInterface) { $package->getRepository()->notifyInstall($package); } } } installationManager = $im; foreach ($localRepositories as $repo) { 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(); $class = $extra['class']; $generator = new AutoloadGenerator; $map = $generator->parseAutoloads(array(array($package, $downloadPath))); $classLoader = $generator->createLoader($map); $classLoader->register(); 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++; } $extra = $package->getExtra(); $installer = new $class($this->vendorDir, $this->binDir, $this->downloadManager, $this->io); $this->installationManager->addInstaller($installer); } } downloadManager = $dm; $this->io = $io; $this->type = $type; $this->filesystem = new Filesystem(); $this->vendorDir = rtrim($vendorDir, '/'); $this->binDir = rtrim($binDir, '/'); } 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->downloadManager->download($package, $downloadPath); $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(); $downloadPath = $this->getInstallPath($initial); $this->removeBinaries($initial); $this->downloadManager->update($initial, $target, $downloadPath); $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); } $downloadPath = $this->getInstallPath($package); $this->downloadManager->remove($package, $downloadPath); $this->removeBinaries($package); $repo->removePackage($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->vendorDir ? $this->vendorDir.'/' : '') . $package->getPrettyName() . ($targetDir ? '/'.$targetDir : ''); } protected function installBinaries(PackageInterface $package) { if (!$package->getBinaries()) { return; } foreach ($package->getBinaries() as $bin) { $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; } $bin = $this->getInstallPath($package).'/'.$bin; if (defined('PHP_WINDOWS_VERSION_BUILD')) { if ('.bat' !== substr($bin, -4)) { file_put_contents($link, $this->generateUnixyProxyCode($bin, $link)); chmod($link, 0777 & ~umask()); $link .= '.bat'; } file_put_contents($link, $this->generateWindowsProxyCode($bin, $link)); } else { $cwd = getcwd(); try { $relativeBin = $this->filesystem->findShortestPath($link, $bin); chdir(dirname($link)); symlink($relativeBin, $link); } catch (\ErrorException $e) { file_put_contents($link, $this->generateUnixyProxyCode($bin, $link)); } chdir($cwd); } chmod($link, 0777 & ~umask()); } } protected function removeBinaries(PackageInterface $package) { if (!$package->getBinaries()) { return; } foreach ($package->getBinaries() as $bin) { $link = $this->binDir.'/'.basename($bin); if (!file_exists($link)) { continue; } unlink($link); } } 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); } private 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 = $match[1]; } else { $caller = 'php'; } } 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 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"; } } 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 ''; } } 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; } } null, 'password' => null); } public function setAuthorization($repositoryName, $username, $password = null) { } } input = $input; $this->output = $output; $this->helperSet = $helperSet; } public function isInteractive() { return $this->input->isInteractive(); } public function isDecorated() { return $this->output->isDecorated(); } public function isVerbose() { return (Boolean) $this->input->getOption('verbose'); } public function write($messages, $newline = true) { $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'; copy($exe, $tmpExe); $exe = $tmpExe; } $this->write($question, false); $value = rtrim(shell_exec($exe)); $this->write(''); if (isset($tmpExe)) { unlink($tmpExe); } return $value; } if ('OK' === rtrim(shell_exec("/usr/bin/env bash -c 'echo OK'"))) { $this->write($question, false); $command = "/usr/bin/env bash -c 'read -s mypassword && echo \$mypassword'"; $value = rtrim(shell_exec($command)); $this->write(''); return $value; } return $this->ask($question); } public function getAuthorizations() { return $this->authorizations; } public function hasAuthorization($repositoryName) { $auths = $this->getAuthorizations(); return isset($auths[$repositoryName]); } public function getAuthorization($repositoryName) { $auths = $this->getAuthorizations(); return isset($auths[$repositoryName]) ? $auths[$repositoryName] : array('username' => null, 'password' => null); } public function setAuthorization($repositoryName, $username, $password = null) { $this->authorizations[$repositoryName] = array('username' => $username, 'password' => $password); } } composer = $composer; $this->io = $io; } public function dispatchPackageEvent($eventName, OperationInterface $operation) { $this->doDispatch(new PackageEvent($eventName, $this->composer, $this->io, $operation)); } public function dispatchCommandEvent($eventName) { $this->doDispatch(new CommandEvent($eventName, $this->composer, $this->io)); } protected function doDispatch(Event $event) { $listeners = $this->getListeners($event); foreach ($listeners as $callable) { $className = substr($callable, 0, strpos($callable, '::')); $methodName = substr($callable, strpos($callable, '::') + 2); if (!class_exists($className)) { throw new \UnexpectedValueException('Class '.$className.' is not autoloadable, can not call '.$event->getName().' script'); } if (!is_callable($callable)) { throw new \UnexpectedValueException('Method '.$callable.' is not callable, can not call '.$event->getName().' script'); } $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 = new AutoloadGenerator; $packages = $this->composer->getRepositoryManager()->getLocalRepository()->getPackages(); $packageMap = $generator->buildPackageMap($this->composer->getInstallationManager(), $package, $packages); $map = $generator->parseAutoloads($packageMap); $this->loader = $generator->createLoader($map); $this->loader->register(); return $scripts[$event->getName()]; } } operation = $operation; } public function getOperation() { return $this->operation; } } name = $name; $this->composer = $composer; $this->io = $io; } public function getName() { return $this->name; } public function getComposer() { return $this->composer; } public function getIO() { return $this->io; } } exists()) { $config->merge($file->read()); } $config->merge(array('config' => array('home' => $home))); return $config; } public function createComposer(IOInterface $io, $localConfig = null) { if (null === $localConfig) { $localConfig = getenv('COMPOSER') ?: 'composer.json'; } 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); $vendorDir = $config->get('vendor-dir'); $binDir = $config->get('bin-dir'); ProcessExecutor::setTimeout((int) $config->get('process-timeout')); $rm = $this->createRepositoryManager($io, $config); $localConfig = $this->addPackagistRepository($localConfig); $this->addLocalRepository($rm, $vendorDir); $loader = new Package\Loader\RootPackageLoader($rm); $package = $loader->load($localConfig); $dm = $this->createDownloadManager($io); $im = $this->createInstallationManager($rm, $dm, $vendorDir, $binDir, $io); $this->purgePackages($rm, $im); $composer = new Composer(); $composer->setConfig($config); $composer->setPackage($package); $composer->setRepositoryManager($rm); $composer->setDownloadManager($dm); $composer->setInstallationManager($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, 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'); return $rm; } protected function addLocalRepository(RepositoryManager $rm, $vendorDir) { if (file_exists($vendorDir.'/.composer/installed.json')) { if (!is_dir($vendorDir.'/composer')) { mkdir($vendorDir.'/composer/', 0777, true); } rename($vendorDir.'/.composer/installed.json', $vendorDir.'/composer/installed.json'); } if (file_exists($vendorDir.'/.composer/installed_dev.json')) { if (!is_dir($vendorDir.'/composer')) { mkdir($vendorDir.'/composer/', 0777, true); } rename($vendorDir.'/.composer/installed_dev.json', $vendorDir.'/composer/installed_dev.json'); } if (file_exists($vendorDir.'/installed.json')) { if (!is_dir($vendorDir.'/composer')) { mkdir($vendorDir.'/composer/', 0777, true); } rename($vendorDir.'/installed.json', $vendorDir.'/composer/installed.json'); } if (file_exists($vendorDir.'/installed_dev.json')) { if (!is_dir($vendorDir.'/composer')) { mkdir($vendorDir.'/composer/', 0777, true); } rename($vendorDir.'/installed_dev.json', $vendorDir.'/composer/installed_dev.json'); } $rm->setLocalRepository(new Repository\InstalledFilesystemRepository(new JsonFile($vendorDir.'/composer/installed.json'))); $rm->setLocalDevRepository(new Repository\InstalledFilesystemRepository(new JsonFile($vendorDir.'/composer/installed_dev.json'))); } protected function addPackagistRepository(array $localConfig) { $loadPackagist = true; $packagistConfig = array( 'type' => 'composer', 'url' => 'http://packagist.org' ); if (isset($localConfig['repositories'])) { foreach ($localConfig['repositories'] as $key => $repo) { if (isset($repo['packagist'])) { if (true === $repo['packagist']) { $localConfig['repositories'][$key] = $packagistConfig; } $loadPackagist = false; break; } } } else { $localConfig['repositories'] = array(); } if ($loadPackagist) { $localConfig['repositories'][] = $packagistConfig; } return $localConfig; } public function createDownloadManager(IOInterface $io) { $dm = new Downloader\DownloadManager(); $dm->setDownloader('git', new Downloader\GitDownloader($io)); $dm->setDownloader('svn', new Downloader\SvnDownloader($io)); $dm->setDownloader('hg', new Downloader\HgDownloader($io)); $dm->setDownloader('pear', new Downloader\PearDownloader($io)); $dm->setDownloader('zip', new Downloader\ZipDownloader($io)); $dm->setDownloader('tar', new Downloader\TarDownloader($io)); $dm->setDownloader('phar', new Downloader\PharDownloader($io)); $dm->setDownloader('file', new Downloader\FileDownloader($io)); return $dm; } protected function createInstallationManager(Repository\RepositoryManager $rm, Downloader\DownloadManager $dm, $vendorDir, $binDir, IOInterface $io) { $im = new Installer\InstallationManager($vendorDir); $im->addInstaller(new Installer\LibraryInstaller($vendorDir, $binDir, $dm, $io, null)); $im->addInstaller(new Installer\InstallerInstaller($vendorDir, $binDir, $dm, $io, $im, $rm->getLocalRepositories())); $im->addInstaller(new Installer\MetapackageInstaller($io)); return $im; } protected function purgePackages(Repository\RepositoryManager $rm, Installer\InstallationManager $im) { foreach ($rm->getLocalRepositories() as $repo) { foreach ($repo->getPackages() as $package) { if (!$im->isPackageInstalled($repo, $package)) { $repo->removePackage($package); } } } } static public function create(IOInterface $io, $config = null) { $factory = new static(); return $factory->createComposer($io, $config); } } read(); } elseif (file_exists($json)) { $config = JsonFile::parseJson(file_get_contents($json)); } elseif (is_string($json)) { $config = JsonFile::parseJson($json); } return parent::load($config); } } manager = $manager; $this->process = $process ?: new ProcessExecutor(); parent::__construct($parser); } public function load($config) { if (!isset($config['name'])) { $config['name'] = '__root__'; } if (!isset($config['version'])) { $version = '1.0.0'; if (0 === $this->process->execute('git branch --no-color --no-abbrev -v', $output)) { foreach ($this->process->splitLines($output) as $branch) { if ($branch && preg_match('{^(?:\* ) *(?:[^/ ]+?/)?(\S+) *[a-f0-9]+ .*$}', $branch, $match)) { $version = 'dev-'.$match[1]; } } } $config['version'] = $version; } else { $version = $config['version']; } $package = parent::load($config); $aliases = array(); $stabilityFlags = array(); $references = array(); foreach (array('require', 'require-dev') as $linkType) { if (isset($config[$linkType])) { $aliases = $this->extractAliases($config[$linkType], $aliases); $stabilityFlags = $this->extractStabilityFlags($config[$linkType], $stabilityFlags); $references = $this->extractReferences($config[$linkType], $references); } } $package->setAliases($aliases); $package->setStabilityFlags($stabilityFlags); $package->setReferences($references); if (isset($config['minimum-stability'])) { $package->setMinimumStability(VersionParser::normalizeStability($config['minimum-stability'])); } if (isset($config['repositories'])) { foreach ($config['repositories'] as $index => $repo) { if (isset($repo['packagist']) && $repo['packagist'] === false) { continue; } if (!is_array($repo)) { throw new \UnexpectedValueException('Repository '.$index.' should be an array, '.gettype($repo).' given'); } if (!isset($repo['type'])) { throw new \UnexpectedValueException('Repository '.$index.' ('.json_encode($repo).') must have a type defined'); } $repository = $this->manager->createRepository($repo['type'], $repo); $this->manager->addRepository($repository); } $package->setRepositories($config['repositories']); } 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]), 'alias' => $match[2], 'alias_normalized' => $this->versionParser->normalize($match[2]), ); } } 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; } 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) { if (preg_match('{^[^,\s@]+?#([a-f0-9]+)$}', $reqVersion, $match) && 'dev' === ($stabilityName = VersionParser::parseStability($reqVersion))) { $name = strtolower($reqName); $references[$name] = $match[1]; } } return $references; } } versionParser = $parser; } public function load($config) { 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 Package\MemoryPackage($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['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'])) { $package->setKeywords(is_array($config['keywords']) ? $config['keywords'] : array($config['keywords'])); } if (!empty($config['license'])) { $package->setLicense(is_array($config['license']) ? $config['license'] : array($config['license'])); } if (!empty($config['time'])) { try { $date = new \DateTime($config['time']); $date->setTimezone(new \DateTimeZone('UTC')); $package->setReleaseDate($date); } catch (\Exception $e) { } } if (!empty($config['authors']) && is_array($config['authors'])) { $package->setAuthors($config['authors']); } if (isset($config['installation-source'])) { $package->setInstallationSource($config['installation-source']); } if (isset($config['source'])) { if (!isset($config['source']['type']) || !isset($config['source']['url'])) { throw new \UnexpectedValueException(sprintf( "package source should be specified as {\"type\": ..., \"url\": ...},\n%s given", 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 dist should be specified as ". "{\"type\": ..., \"url\": ..., \"reference\": ..., \"shasum\": ...},\n%s given", 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); } if ('dev-' === substr($package->getPrettyVersion(), 0, 4) && isset($config['extra']['branch-alias']) && is_array($config['extra']['branch-alias'])) { 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($package->getPrettyVersion()) !== strtolower($sourceBranch)) { continue; } $package->setAlias($validatedTargetBranch); $package->setPrettyAlias(preg_replace('{(\.9{7})+}', '.x', $validatedTargetBranch)); break; } } foreach (Package\BasePackage::$supportedLinkTypes as $type => $opts) { if (isset($config[$type])) { $method = 'set'.ucfirst($opts['method']); $package->{$method}( $this->loadLinksFromConfig($package, $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']); } return $package; } private function loadLinksFromConfig($package, $description, array $linksSpecs) { $links = array(); foreach ($linksSpecs as $packageName => $constraint) { if ('self.version' === $constraint) { $parsedConstraint = $this->versionParser->parseConstraints($package->getPrettyVersion()); } else { $parsedConstraint = $this->versionParser->parseConstraints($constraint); } $links[] = new Package\Link($package->getName(), $packageName, $parsedConstraint, $description, $constraint); } return $links; } } 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 matches($name, LinkConstraintInterface $constraint) { if ($this->name === $name) { return $constraint->matches(new VersionConstraint('==', $this->getVersion())); } foreach ($this->getProvides() as $link) { if ($link->getTarget() === $name && $constraint->matches($link->getConstraint())) { return true; } } foreach ($this->getReplaces() as $link) { if ($link->getTarget() === $name && $constraint->matches($link->getConstraint())) { return true; } } return false; } public function getRepository() { return $this->repository; } public function setRepository(RepositoryInterface $repository) { if ($this->repository) { throw new \LogicException('A package can only be added to one repository'); } $this->repository = $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 __clone() { $this->repository = null; } } normalizeBranch($match[1]); } catch (\Exception $e) {} } throw new \UnexpectedValueException('Invalid version string '.$version); } 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 parseConstraints($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)#[a-f0-9]+$}i', $constraints, $match)) { $constraints = $match[1]; } $constraints = preg_split('{\s*,\s*}', trim($constraints)); if (count($constraints) > 1) { $constraintObjects = array(); foreach ($constraints as $constraint) { $constraintObjects = array_merge($constraintObjects, $this->parseConstraint($constraint)); } } else { $constraintObjects = $this->parseConstraint($constraints[0]); } if (1 === count($constraintObjects)) { return $constraintObjects[0]; } return new MultiConstraint($constraintObjects); } private function parseConstraint($constraint) { if (preg_match('{^[x*](\.[x*])*$}i', $constraint)) { return array(); } 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]); return array(new VersionConstraint($matches[1] ?: '=', $version)); } catch (\Exception $e) {} } throw new \UnexpectedValueException('Could not parse version constraint '.$constraint); } } operator = $operator; $this->version = $version; } public function matchSpecific(VersionConstraint $provider) { $noEqualOp = str_replace('=', '', $this->operator); $providerNoEqualOp = str_replace('=', '', $provider->operator); if ($this->operator != '==' && $noEqualOp == $providerNoEqualOp) { return true; } if (version_compare($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; } } constraints = $constraints; } public function matches(LinkConstraintInterface $provider) { foreach ($this->constraints as $constraint) { if (!$constraint->matches($provider)) { return false; } } return true; } public function __toString() { $constraints = array(); foreach ($this->constraints as $constraint) { $constraints[] = $constraint->__toString(); } return '['.implode(', ', $constraints).']'; } } matches($this); } elseif ($provider instanceof $this) { return $this->matchSpecific($provider); } return true; } } 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, $this->version); } } $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, $this->version); } } $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 getAlias() { return ''; } public function getPrettyAlias() { return ''; } 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 setAliases(array $aliases) { return $this->aliasOf->setAliases($aliases); } public function getAliases() { return $this->aliasOf->getAliases(); } 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 __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.')'; } } 'bin', 'scripts', 'type', 'extra', 'installationSource' => 'installation-source', 'license', 'authors', 'description', 'homepage', 'keywords', 'autoload', 'repositories', '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->getReleaseDate()) { $data['time'] = $package->getReleaseDate()->format('Y-m-d H:i:s'); } 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(); } foreach (BasePackage::$supportedLinkTypes as $type => $opts) { if ($links = $package->{'get'.ucfirst($opts['method'])}()) { foreach ($links as $link) { $data[$type][$link->getTarget()] = $link->getPrettyConstraint(); } } } if ($packages = $package->getSuggests()) { $data['suggest'] = $packages; } 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; } } 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() { return $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 setScripts(array $scripts) { $this->scripts = $scripts; } public function getScripts() { return $this->scripts; } public function setAliases(array $aliases) { $this->aliases = $aliases; } public function getAliases() { return $this->aliases; } public function setAlias($alias) { $this->alias = $alias; } public function getAlias() { return $this->alias; } public function setPrettyAlias($prettyAlias) { $this->prettyAlias = $prettyAlias; } public function getPrettyAlias() { return $this->prettyAlias; } 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 setRepositories($repositories) { $this->repositories = $repositories; } public function getRepositories() { return $this->repositories; } public function getVersion() { return $this->version; } public function getPrettyVersion() { return $this->prettyVersion; } public function setLicense(array $license) { $this->license = $license; } public function getLicense() { return $this->license; } 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 setReleaseDate(\DateTime $releaseDate) { $this->releaseDate = $releaseDate; } public function getReleaseDate() { return $this->releaseDate; } 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 setMinimumStability($minimumStability) { $this->minimumStability = $minimumStability; } public function getMinimumStability() { return $this->minimumStability; } public function setStabilityFlags(array $stabilityFlags) { $this->stabilityFlags = $stabilityFlags; } public function getStabilityFlags() { return $this->stabilityFlags; } public function setReferences(array $references) { $this->references = $references; } public function getReferences() { return $this->references; } 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; } } lockFile = $lockFile; $this->repositoryManager = $repositoryManager; $this->hash = $hash; } public function isLocked($dev = false) { if (!$this->lockFile->exists()) { return false; } $data = $this->getLockData(); if ($dev) { return isset($data['packages-dev']); } return isset($data['packages']); } public function isFresh() { $lock = $this->lockFile->read(); return $this->hash === $lock['hash']; } public function getLockedPackages($dev = false) { $lockData = $this->getLockData(); $packages = array(); $lockedPackages = $dev ? $lockData['packages-dev'] : $lockData['packages']; $repo = $dev ? $this->repositoryManager->getLocalDevRepository() : $this->repositoryManager->getLocalRepository(); foreach ($lockedPackages as $info) { if (isset($info['alias']) && empty($warned)) { $warned = true; echo 'BC warning: your lock file appears to be of an older format than this composer version, it is recommended to run composer update'.PHP_EOL; } $resolvedVersion = !empty($info['alias-version']) ? $info['alias-version'] : $info['version']; $package = $repo->findPackage($info['package'], $resolvedVersion); if (!$package) { $package = $this->repositoryManager->findPackage($info['package'], $resolvedVersion); } if (!$package && !empty($info['alias-version'])) { $package = $this->repositoryManager->findPackage($info['package'], $info['version']); if ($package) { $alias = new AliasPackage($package, $info['alias-version'], $info['alias-pretty-version']); $package->getRepository()->addPackage($alias); $package = $alias; } } if (!$package) { throw new \LogicException(sprintf( 'Can not find "%s-%s" package in registered repositories', $info['package'], $info['version'] )); } $packages[] = $package; } return $packages; } public function getMinimumStability() { $lockData = $this->getLockData(); return isset($lockData['minimum-stability']) ? $lockData['minimum-stability'] : 'dev'; } 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 $aliases, $minimumStability, array $stabilityFlags) { $lock = array( 'hash' => $this->hash, 'packages' => null, 'packages-dev' => null, 'aliases' => $aliases, 'minimum-stability' => $minimumStability, 'stability-flags' => $stabilityFlags, ); $lock['packages'] = $this->lockPackages($packages); if (null !== $devPackages) { $lock['packages-dev'] = $this->lockPackages($devPackages); } 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) { $alias = null; if ($package instanceof AliasPackage) { $alias = $package; $package = $package->getAliasOf(); } $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 = array('package' => $name, 'version' => $version); if ($package->isDev()) { $spec['source-reference'] = $package->getSourceReference(); } if ($alias) { $spec['alias-pretty-version'] = $alias->getPrettyVersion(); $spec['alias-version'] = $alias->getVersion(); } $locked[] = $spec; } usort($locked, function ($a, $b) { return strcmp($a['package'], $b['package']); }); return $locked; } } io = $io; $this->root = rtrim($cacheDir, '/\\') . '/'; if (!is_dir($this->root)) { if (!@mkdir($this->root, 0777, true)) { $this->enabled = false; } } } public function getRoot() { return $this->root; } public function read($file) { if ($this->enabled && file_exists($this->root . $file)) { return file_get_contents($this->root . $file); } } public function write($file, $contents) { if ($this->enabled) { file_put_contents($this->root . $file, $contents); } } public function sha1($file) { if ($this->enabled && file_exists($this->root . $file)) { return sha1_file($this->root . $file); } } } config = array( 'process-timeout' => 300, 'vendor-dir' => 'vendor', 'bin-dir' => '{$vendor-dir}/bin', 'notify-on-install' => true, ); } public function merge(array $config) { if (!empty($config['config']) && is_array($config['config'])) { $this->config = array_replace_recursive($this->config, $config['config']); } } public function get($key) { switch ($key) { case 'vendor-dir': case 'bin-dir': case 'process-timeout': $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_')); return $this->process(getenv($env) ?: $this->config[$key]); case 'home': return rtrim($this->process($this->config[$key]), '/\\'); default: return $this->process($this->config[$key]); } } public function has($key) { return array_key_exists($key, $this->config); } private function process($value) { $config = $this; return preg_replace_callback('#\{\$(.+)\}#', function ($match) use ($config) { return $config->get($match[1]); }, $value); } } errors = $errors; } public function getErrors() { return $this->errors; } } 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('Could not read '.$this->path.', either you or the remote host is probably offline'."\n\n".$e->getMessage()); } catch (\Exception $e) { throw new \RuntimeException('Could not read '.$this->path."\n\n".$e->getMessage()); } return static::parseJson($json); } 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); } $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($errors); } return true; } static public function encode($data, $options = 448) { if (version_compare(PHP_VERSION, '5.4', '>=')) { return json_encode($data, $options); } $json = json_encode($data); $prettyPrint = (Boolean) ($options & self::JSON_PRETTY_PRINT); $unescapeUnicode = (Boolean) ($options & self::JSON_UNESCAPED_UNICODE); $unescapeSlashes = (Boolean) ($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) { $data = json_decode($json, true); if (null === $data && JSON_ERROR_NONE !== json_last_error()) { self::validateSyntax($json); } return $data; } protected static function validateSyntax($json) { $parser = new JsonParser(); $result = $parser->lint($json); if (null === $result) { if (defined('JSON_ERROR_UTF8') && JSON_ERROR_UTF8 === json_last_error()) { throw new \UnexpectedValueException('JSON file is not UTF-8 encoded'); } return true; } throw $result; } } repositories = $repositories; } 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 getPackages() { $packages = array(); foreach ($this->repositories as $repository) { $packages[] = $repository->getPackages(); } return call_user_func_array('array_merge', $packages); } public function count() { $total = 0; foreach ($this->repositories as $repository) { $total += $repository->count(); } return $total; } public function addRepository(RepositoryInterface $repository) { $this->repositories[] = $repository; } }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 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->getAlias()) { $this->addPackage($this->createAliasPackage($package)); } } protected function createAliasPackage(PackageInterface $package, $alias = null, $prettyAlias = null) { return new AliasPackage($package, $alias ?: $package->getAlias(), $prettyAlias ?: $package->getPrettyAlias()); } 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(); } } normalize($prettyVersion); } catch (\UnexpectedValueException $e) { $prettyVersion = preg_replace('#^(.+?)(-.+)?$#', '$1', PHP_VERSION); $version = $versionParser->normalize($prettyVersion); } $php = new MemoryPackage('php', $version, $prettyVersion); $php->setDescription('The PHP interpreter'); parent::addPackage($php); foreach (get_loaded_extensions() 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 MemoryPackage('ext-'.$name, $version, $prettyVersion); $ext->setDescription('The '.$name.' PHP extension'); parent::addPackage($ext); } } } =') && !filter_var($repoConfig['url'], FILTER_VALIDATE_URL)) { throw new \UnexpectedValueException('Invalid url given for PEAR repository: '.$repoConfig['url']); } $this->url = rtrim($repoConfig['url'], '/'); $this->channel = !empty($repoConfig['channel']) ? $repoConfig['channel'] : null; $this->io = $io; $this->rfs = $rfs ?: new RemoteFilesystem($this->io); } protected function initialize() { parent::initialize(); $this->io->write('Initializing PEAR repository '.$this->url); $this->initializeChannel(); $this->io->write('Packages names will be prefixed with: pear-'.$this->channel.'/'); try { $json = new JsonFile($this->url.'/packages.json', new RemoteFilesystem($this->io)); $packages = $json->read(); if ($this->io->isVerbose()) { $this->io->write('Repository is Composer-compatible, loading via packages.json instead of PEAR protocol'); } $loader = new ArrayLoader(); foreach ($packages as $data) { foreach ($data['versions'] as $rev) { if (strpos($rev['name'], 'pear-'.$this->channel) !== 0) { $rev['name'] = 'pear-'.$this->channel.'/'.$rev['name']; } $this->addPackage($loader->load($rev)); if ($this->io->isVerbose()) { $this->io->write('Loaded '.$rev['name'].' '.$rev['version']); } } } return; } catch (\Exception $e) { } $this->fetchFromServer(); } protected function initializeChannel() { $channelXML = $this->requestXml($this->url . "/channel.xml"); if (!$this->channel) { $this->channel = $channelXML->getElementsByTagName("suggestedalias")->item(0)->nodeValue ?: $channelXML->getElementsByTagName("name")->item(0)->nodeValue; } if (!$this->baseUrl) { $this->baseUrl = $channelXML->getElementsByTagName("baseurl")->item(0)->nodeValue ? trim($channelXML->getElementsByTagName("baseurl")->item(0)->nodeValue, '/') : $this->url . '/rest'; } self::$channelNames[$channelXML->getElementsByTagName("name")->item(0)->nodeValue] = $this->channel; } protected function fetchFromServer() { $categoryXML = $this->requestXml($this->baseUrl . "/c/categories.xml"); $categories = $categoryXML->getElementsByTagName("c"); foreach ($categories as $category) { $link = $this->baseUrl . '/c/' . str_replace(' ', '+', $category->nodeValue); try { $packagesLink = $link . "/packagesinfo.xml"; $this->fetchPear2Packages($packagesLink); } catch (TransportException $e) { if (false === strpos($e->getMessage(), '404')) { throw $e; } $categoryLink = $link . "/packages.xml"; $this->fetchPearPackages($categoryLink); } } } private function fetchPearPackages($categoryLink) { $packagesXML = $this->requestXml($categoryLink); $packages = $packagesXML->getElementsByTagName('p'); $loader = new ArrayLoader(); foreach ($packages as $package) { $packageName = $package->nodeValue; $fullName = 'pear-'.$this->channel.'/'.$packageName; $releaseLink = $this->baseUrl . "/r/" . $packageName; $allReleasesLink = $releaseLink . "/allreleases2.xml"; try { $releasesXML = $this->requestXml($allReleasesLink); } catch (TransportException $e) { if (strpos($e->getMessage(), '404')) { continue; } throw $e; } $releases = $releasesXML->getElementsByTagName('r'); foreach ($releases as $release) { $pearVersion = $release->getElementsByTagName('v')->item(0)->nodeValue; $packageData = array( 'name' => $fullName, 'type' => 'library', 'dist' => array('type' => 'pear', 'url' => $this->url.'/get/'.$packageName.'-'.$pearVersion.".tgz"), 'version' => $pearVersion, 'autoload' => array( 'classmap' => array(''), ), ); try { $deps = $this->rfs->getContents($this->url, $releaseLink . "/deps.".$pearVersion.".txt", false); } catch (TransportException $e) { if (strpos($e->getMessage(), '404')) { continue; } throw $e; } $packageData += $this->parseDependencies($deps); try { $this->addPackage($loader->load($packageData)); if ($this->io->isVerbose()) { $this->io->write('Loaded '.$packageData['name'].' '.$packageData['version']); } } catch (\UnexpectedValueException $e) { if ($this->io->isVerbose()) { $this->io->write('Could not load '.$packageData['name'].' '.$packageData['version'].': '.$e->getMessage()); } continue; } } } } private function parseVersion(array $data) { if (!isset($data['min']) && !isset($data['max'])) { return '*'; } $versions = array(); if (isset($data['min'])) { $versions[] = '>=' . $data['min']; } if (isset($data['max'])) { $versions[] = '<=' . $data['max']; } return implode(',', $versions); } private function parseDependenciesOptions(array $depsOptions) { $data = array(); foreach ($depsOptions as $name => $options) { if (isset($options['name'])) { $options = array($options); } if ('php' == $name) { $data[$name] = $this->parseVersion($options); } elseif ('package' == $name) { foreach ($options as $key => $value) { if (isset($value['providesextension'])) { continue; } if (isset($value['uri'])) { continue; } if (is_array($value)) { $dataKey = $value['name']; if (false === strpos($dataKey, '/')) { $dataKey = $this->getChannelShorthand($value['channel']).'/'.$dataKey; } $data['pear-'.$dataKey] = $this->parseVersion($value); } } } elseif ('extension' == $name) { foreach ($options as $key => $value) { $dataKey = 'ext-' . $value['name']; $data[$dataKey] = $this->parseVersion($value); } } } return $data; } private function parseDependencies($deps) { if (preg_match('((O:([0-9])+:"([^"]+)"))', $deps, $matches)) { if (strlen($matches[3]) == $matches[2]) { throw new \InvalidArgumentException("Invalid dependency data, it contains serialized objects."); } } $deps = (array) @unserialize($deps); unset($deps['required']['pearinstaller']); $depsData = array(); if (!empty($deps['required'])) { $depsData['require'] = $this->parseDependenciesOptions($deps['required']); } if (!empty($deps['optional'])) { $depsData['suggest'] = $this->parseDependenciesOptions($deps['optional']); } return $depsData; } private function fetchPear2Packages($packagesLink) { $loader = new ArrayLoader(); $packagesXml = $this->requestXml($packagesLink); $informations = $packagesXml->getElementsByTagName('pi'); foreach ($informations as $information) { $package = $information->getElementsByTagName('p')->item(0); $packageName = $package->getElementsByTagName('n')->item(0)->nodeValue; $fullName = 'pear-'.$this->channel.'/'.$packageName; $packageData = array( 'name' => $fullName, 'type' => 'library', 'autoload' => array( 'classmap' => array(''), ), ); $packageKeys = array('l' => 'license', 'd' => 'description'); foreach ($packageKeys as $pear => $composer) { if ($package->getElementsByTagName($pear)->length > 0 && ($pear = $package->getElementsByTagName($pear)->item(0)->nodeValue)) { $packageData[$composer] = $pear; } } $depsData = array(); foreach ($information->getElementsByTagName('deps') as $depElement) { $depsVersion = $depElement->getElementsByTagName('v')->item(0)->nodeValue; $depsData[$depsVersion] = $this->parseDependencies( $depElement->getElementsByTagName('d')->item(0)->nodeValue ); } $releases = $information->getElementsByTagName('a')->item(0); if (!$releases) { continue; } $releases = $releases->getElementsByTagName('r'); $packageUrl = $this->url . '/get/' . $packageName; foreach ($releases as $release) { $version = $release->getElementsByTagName('v')->item(0)->nodeValue; $releaseData = array( 'dist' => array( 'type' => 'pear', 'url' => $packageUrl . '-' . $version . '.tgz' ), 'version' => $version ); if (isset($depsData[$version])) { $releaseData += $depsData[$version]; } $package = $packageData + $releaseData; try { $this->addPackage($loader->load($package)); if ($this->io->isVerbose()) { $this->io->write('Loaded '.$package['name'].' '.$package['version']); } } catch (\UnexpectedValueException $e) { if ($this->io->isVerbose()) { $this->io->write('Could not load '.$package['name'].' '.$package['version'].': '.$e->getMessage()); } continue; } } } } private function requestXml($url) { $content = $this->rfs->getContents($this->url, $url, false); if (!$content) { throw new \UnexpectedValueException('The PEAR channel at '.$url.' did not respond.'); } $dom = new \DOMDocument('1.0', 'UTF-8'); $dom->loadXML($content); return $dom; } private function getChannelShorthand($url) { if (!isset(self::$channelNames[$url])) { try { $channelXML = $this->requestXml('http://'.$url."/channel.xml"); $shorthand = $channelXML->getElementsByTagName("suggestedalias")->item(0)->nodeValue ?: $channelXML->getElementsByTagName("name")->item(0)->nodeValue; self::$channelNames[$url] = $shorthand; } catch (\Exception $e) { self::$channelNames[$url] = substr($url, 0, strpos($url, '.')); } } return self::$channelNames[$url]; } } util) { $this->util = new SvnUtil($this->baseUrl, $this->io, $this->process); } try { return $this->util->execute($command, $url); } catch (\RuntimeException $e) { throw new \RuntimeException( 'Repository '.$this->url.' could not be processed, '.$e->getMessage() ); } } public function initialize() { $this->url = $this->baseUrl = rtrim(self::normalizeUrl($this->url), '/'); if (false !== ($pos = strrpos($this->url, '/trunk'))) { $this->baseUrl = substr($this->url, 0, $pos); } $this->getBranches(); $this->getTags(); } public function getRootIdentifier() { return 'trunk'; } 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 (!isset($this->infoCache[$identifier])) { preg_match('{^(.+?)(@\d+)?/$}', $identifier, $match); if (!empty($match[2])) { $identifier = $match[1]; $rev = $match[2]; } else { $rev = ''; } try { $output = $this->execute('svn cat', $this->baseUrl . $identifier . 'composer.json' . $rev); if (!trim($output)) { return; } } catch (\RuntimeException $e) { throw new TransportException($e->getMessage()); } $composer = JsonFile::parseJson($output); if (!isset($composer['time'])) { $output = $this->execute('svn info', $this->baseUrl . $identifier . $rev); foreach ($this->process->splitLines($output) as $line) { if ($line && preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) { $date = new \DateTime($match[1]); $composer['time'] = $date->format('Y-m-d H:i:s'); break; } } } $this->infoCache[$identifier] = $composer; } return $this->infoCache[$identifier]; } public function getTags() { if (null === $this->tags) { $this->tags = array(); $output = $this->execute('svn ls', $this->baseUrl . '/tags'); if ($output) { foreach ($this->process->splitLines($output) as $tag) { if ($tag) { $this->tags[rtrim($tag, '/')] = '/tags/'.$tag; } } } } 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] === 'trunk/') { $this->branches['trunk'] = '/trunk/@'.$match[1]; break; } } } } unset($output); $output = $this->execute('svn ls --verbose', $this->baseUrl . '/branches'); 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], '/')] = '/branches/'.$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; } } url)) { $this->repoDir = str_replace('file://', '', $this->url); } else { $this->repoDir = $this->config->get('home') . '/cache.git/' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/'; 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 = new Filesystem(); $fs->removeDirectory($this->repoDir); $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])) { $this->process->execute(sprintf('git show %s:composer.json', escapeshellarg($identifier)), $composer, $this->repoDir); if (!trim($composer)) { return; } $composer = JsonFile::parseJson($composer); if (!isset($composer['time'])) { $this->process->execute(sprintf('git log -1 --format=%%at %s', escapeshellarg($identifier)), $output, $this->repoDir); $date = new \DateTime('@'.trim($output)); $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)) { 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@|//git\.|//github.com/)#i', $url)) { return true; } if (static::isLocalUrl($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, $match); $this->owner = $match[1]; $this->repository = $match[2]; $this->originUrl = 'bitbucket.org'; } public function getRootIdentifier() { if (null === $this->rootIdentifier) { $repoData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository)); $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])) { $composer = $this->getContents($this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/raw/'.$identifier.'/composer.json'); if (!$composer) { return; } $composer = JsonFile::parseJson($composer); if (!isset($composer['time'])) { $changeset = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier)); $composer['time'] = $changeset['timestamp']; } $this->infoCache[$identifier] = $composer; } return $this->infoCache[$identifier]; } public function getTags() { if (null === $this->tags) { $tagsData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags')); $this->tags = array(); foreach ($tagsData as $tag => $data) { $this->tags[$tag] = $data['raw_node']; } } return $this->tags; } public function getBranches() { if (null === $this->branches) { $branchData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches')); $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 = $url; $this->originUrl = $url; $this->io = $io; $this->config = $config; $this->process = $process ?: new ProcessExecutor; $this->remoteFilesystem = $remoteFilesystem ?: new RemoteFilesystem($io); } public function hasComposerFile($identifier) { try { return (Boolean) $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 (Boolean) preg_match('{^(file://|/|[a-z]:[\\\\/])}i', $url); } } tmpDir = $this->config->get('home') . '/cache.hg/' . preg_replace('{[^a-z0-9]}i', '-', $this->url) . '/'; if (is_dir($this->tmpDir)) { $this->process->execute(sprintf('cd %s && hg pull -u', escapeshellarg($this->tmpDir)), $output); } else { $dir = dirname($this->tmpDir); if (!is_dir($dir)) { mkdir($dir, 0777, true); } $this->process->execute(sprintf('cd %s && hg clone %s %s', escapeshellarg($dir), escapeshellarg($this->url), escapeshellarg($this->tmpDir)), $output); } $this->getTags(); $this->getBranches(); } public function getRootIdentifier() { $tmpDir = escapeshellarg($this->tmpDir); if (null === $this->rootIdentifier) { $this->process->execute(sprintf('cd %s && hg tip --template "{node}"', $tmpDir), $output); $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('cd %s && hg cat -r %s composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $composer); if (!trim($composer)) { return; } $composer = JsonFile::parseJson($composer); if (!isset($composer['time'])) { $this->process->execute(sprintf('cd %s && hg log --template "{date|rfc822date}" -r %s', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output); $date = new \DateTime(trim($output)); $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(sprintf('cd %s && hg tags', escapeshellarg($this->tmpDir)), $output); 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(); $this->process->execute(sprintf('cd %s && hg branches', escapeshellarg($this->tmpDir)), $output); foreach ($this->process->splitLines($output) as $branch) { if ($branch && preg_match('(^([^\s]+)\s+\d+:(.*)$)', $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('#(^(?:https?|ssh)://(?:[^@]@)?bitbucket.org|https://(?:.*?)\.kilnhg.com)#i', $url)) { return true; } if (!$deep) { return false; } $processExecutor = new ProcessExecutor(); $exit = $processExecutor->execute(sprintf('cd %s && hg identify %s', escapeshellarg(sys_get_temp_dir()), escapeshellarg($url)), $ignored); return $exit === 0; } } url, $match); $this->owner = $match[1]; $this->repository = $match[2]; $this->originUrl = 'github.com'; $this->cache = new Cache($this->io, $this->config->get('home').'/cache.github/'.$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 $this->url; } 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 = $this->getScheme() . '://github.com/'.$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 { $composer = $this->getContents($this->getScheme() . '://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json'); } catch (TransportException $e) { if (404 !== $e->getCode()) { throw $e; } $composer = false; } if ($composer) { $composer = JsonFile::parseJson($composer); if (!isset($composer['time'])) { $commit = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/commits/'.$identifier)); $composer['time'] = $commit['commit']['committer']['date']; } } 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) { $tagsData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/tags')); $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) { $branchData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository.'/git/refs/heads')); $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 fetchRootIdentifier() { $repoDataUrl = $this->getScheme() . '://api.github.com/repos/'.$this->owner.'/'.$this->repository; $attemptCounter = 0; while (null === $this->rootIdentifier) { if (5 == $attemptCounter++) { throw new \RuntimeException("Either you have entered invalid credentials or this GitHub repository does not exists (404)"); } try { $repoData = JsonFile::parseJson($this->getContents($repoDataUrl)); if (isset($repoData['default_branch'])) { $this->rootIdentifier = $repoData['default_branch']; } elseif (isset($repoData['master_branch'])) { $this->rootIdentifier = $repoData['master_branch']; } else { $this->rootIdentifier = 'master'; } } catch (TransportException $e) { switch($e->getCode()) { case 401: case 404: $this->isPrivate = true; if (!$this->io->isInteractive()) { $this->gitDriver = new GitDriver( $this->generateSshUrl(), $this->io, $this->config, $this->process, $this->remoteFilesystem ); $this->gitDriver->initialize(); return; } $this->io->write('Authentication required ('.$this->url.'):'); $username = $this->io->ask('Username: '); $password = $this->io->askAndHideAnswer('Password: '); $this->io->setAuthorization($this->originUrl, $username, $password); break; default: throw $e; break; } } } } } url, $match); $this->owner = $match[1]; $this->repository = $match[2]; $this->originUrl = 'bitbucket.org'; } public function getRootIdentifier() { if (null === $this->rootIdentifier) { $repoData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags')); $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])) { $composer = $this->getContents($this->getScheme() . '://bitbucket.org/'.$this->owner.'/'.$this->repository.'/raw/'.$identifier.'/composer.json'); if (!$composer) { return; } $composer = JsonFile::parseJson($composer); if (!isset($composer['time'])) { $changeset = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/changesets/'.$identifier)); $composer['time'] = $changeset['timestamp']; } $this->infoCache[$identifier] = $composer; } return $this->infoCache[$identifier]; } public function getTags() { if (null === $this->tags) { $tagsData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/tags')); $this->tags = array(); foreach ($tagsData as $tag => $data) { $this->tags[$tag] = $data['raw_node']; } } return $this->tags; } public function getBranches() { if (null === $this->branches) { $branchData = JsonFile::parseJson($this->getContents($this->getScheme() . '://api.bitbucket.org/1.0/repositories/'.$this->owner.'/'.$this->repository.'/branches')); $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; } } =') && !filter_var($repoConfig['url'], FILTER_VALIDATE_URL)) { throw new \UnexpectedValueException('Invalid url given for Composer repository: '.$repoConfig['url']); } $this->config = $config; $this->url = $repoConfig['url']; $this->io = $io; $this->cache = new Cache($io, $config->get('home').'/cache/'.preg_replace('{[^a-z0-9.]}', '-', $this->url)); } public function notifyInstall(PackageInterface $package) { if (!$this->notifyUrl || !$this->config->get('notify-on-install')) { return; } $url = str_replace('%package%', $package->getPrettyName(), $this->notifyUrl); $params = array( 'version' => $package->getPrettyVersion(), 'version_normalized' => $package->getVersion(), ); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($params, '', '&'), 'timeout' => 3, ) ); $context = stream_context_create($opts); @file_get_contents($url, false, $context); } protected function initialize() { parent::initialize(); try { $json = new JsonFile($this->url.'/packages.json', new RemoteFilesystem($this->io)); $data = $json->read(); if (!empty($data['notify'])) { $this->notifyUrl = preg_replace('{(https?://[^/]+).*}i', '$1' . $data['notify'], $this->url); } $this->cache->write('packages.json', json_encode($data)); } catch (\Exception $e) { if ($contents = $this->cache->read('packages.json')) { $this->io->write(''.$this->url.' could not be loaded, package information was loaded from the local cache and may be out of date'); $data = json_decode($contents, true); } else { throw $e; } } $loader = new ArrayLoader(); $this->loadRepository($loader, $data); } protected function loadRepository(ArrayLoader $loader, $data) { if (!isset($data['packages']) && !isset($data['includes'])) { foreach ($data as $pkg) { foreach ($pkg['versions'] as $metadata) { $this->addPackage($loader->load($metadata)); } } return; } if (isset($data['packages'])) { foreach ($data['packages'] as $package => $versions) { foreach ($versions as $version => $metadata) { $this->addPackage($loader->load($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 { $json = new JsonFile($this->url.'/'.$include, new RemoteFilesystem($this->io)); $includedData = $json->read(); $this->cache->write($include, json_encode($includedData)); } $this->loadRepository($loader, $includedData); } } } } 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(RepositoryInterface $repository) { $this->localRepository = $repository; } public function getLocalRepository() { return $this->localRepository; } public function setLocalDevRepository(RepositoryInterface $repository) { $this->localDevRepository = $repository; } public function getLocalDevRepository() { return $this->localDevRepository; } public function getLocalRepositories() { return array($this->localRepository, $this->localDevRepository); } } drivers = $drivers ?: array( 'github' => 'Composer\Repository\Vcs\GitHubDriver', 'git-bitbucket' => 'Composer\Repository\Vcs\GitBitbucketDriver', 'git' => 'Composer\Repository\Vcs\GitDriver', 'svn' => 'Composer\Repository\Vcs\SvnDriver', 'hg-bitbucket' => 'Composer\Repository\Vcs\HgBitbucketDriver', 'hg' => 'Composer\Repository\Vcs\HgDriver', ); $this->url = $repoConfig['url']; $this->io = $io; $this->type = isset($repoConfig['type']) ? $repoConfig['type'] : 'vcs'; $this->verbose = $io->isVerbose(); $this->config = $config; } public function getDriver() { if (isset($this->drivers[$this->type])) { $class = $this->drivers[$this->type]; $driver = new $class($this->url, $this->io, $this->config); $driver->initialize(); return $driver; } foreach ($this->drivers as $driver) { if ($driver::supports($this->io, $this->url)) { $driver = new $driver($this->url, $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->url, $this->io, $this->config); $driver->initialize(); return $driver; } } } 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; $loader = new ArrayLoader(); 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); } 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($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; } } $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'].')'); } $this->addPackage($loader->load($this->preProcess($driver, $data, $identifier))); } catch (TransportException $e) { if ($verbose) { $this->io->write('Skipped branch '.$branch.', no composer file was found'); } continue; } catch (\Exception $e) { $this->io->write('Skipped branch '.$branch.', '.$e->getMessage()); continue; } } $this->io->overwrite('', false); } 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; } } file = $repositoryFile; } protected function initialize() { parent::initialize(); if (!$this->file->exists()) { return; } $packages = $this->file->read(); if (!is_array($packages)) { throw new \UnexpectedValueException('Could not parse package list from the '.$this->file->getPath().' repository'); } $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() { $packages = array(); $dumper = new ArrayDumper(); foreach ($this->getPackages() as $package) { if (!$package instanceof AliasPackage) { $data = $dumper->dump($package); $packages[] = $data; } } $this->file->write($packages); } } config = $config['package']; if (!is_numeric(key($this->config))) { $this->config = array($this->config); } } protected function initialize() { parent::initialize(); $loader = new ArrayLoader(); foreach ($this->config as $package) { $package = $loader->load($package); $this->addPackage($package); } } } io = $io; $this->package = $package; $this->downloadManager = $downloadManager; $this->repositoryManager = $repositoryManager; $this->locker = $locker; $this->installationManager = $installationManager; $this->eventDispatcher = $eventDispatcher; $this->autoloadGenerator = $autoloadGenerator; } public function run() { if ($this->dryRun) { $this->verbose = true; } if ($this->preferSource) { $this->downloadManager->setPreferSource(true); } $repos = array_merge( $this->repositoryManager->getLocalRepositories(), array( new InstalledArrayRepository(array($this->package)), new PlatformRepository(), ) ); $installedRepo = new CompositeRepository($repos); if ($this->additionalInstalledRepository) { $installedRepo->addRepository($this->additionalInstalledRepository); } $aliases = $this->aliasPackages(); if (!$this->dryRun) { $eventName = $this->update ? ScriptEvents::PRE_UPDATE_CMD : ScriptEvents::PRE_INSTALL_CMD; $this->eventDispatcher->dispatchCommandEvent($eventName); } $this->suggestedPackages = array(); if (!$this->doInstall($this->repositoryManager->getLocalRepository(), $installedRepo, $aliases)) { return false; } if ($this->devMode) { if (!$this->doInstall($this->repositoryManager->getLocalDevRepository(), $installedRepo, $aliases, true)) { return false; } } foreach ($this->suggestedPackages as $suggestion) { if (!$installedRepo->findPackages($suggestion['target'])) { $this->io->write($suggestion['source'].' suggests installing '.$suggestion['target'].' ('.$suggestion['reason'].')'); } } if (!$this->dryRun) { if ($this->update || !$this->locker->isLocked()) { $updatedLock = $this->locker->setLockData( $this->repositoryManager->getLocalRepository()->getPackages(), $this->devMode ? $this->repositoryManager->getLocalDevRepository()->getPackages() : null, $aliases, $this->package->getMinimumStability(), $this->package->getStabilityFlags() ); if ($updatedLock) { $this->io->write('Writing lock file'); } } $this->io->write('Generating autoload files'); $localRepos = new CompositeRepository($this->repositoryManager->getLocalRepositories()); $this->autoloadGenerator->dump($localRepos, $this->package, $this->installationManager, $this->installationManager->getVendorPath() . '/composer', true); $eventName = $this->update ? ScriptEvents::POST_UPDATE_CMD : ScriptEvents::POST_INSTALL_CMD; $this->eventDispatcher->dispatchCommandEvent($eventName); } return true; } protected function doInstall($localRepo, $installedRepo, $aliases, $devMode = false) { $minimumStability = $this->package->getMinimumStability(); $stabilityFlags = $this->package->getStabilityFlags(); if (!$this->update && $this->locker->isLocked($devMode)) { $lockedPackages = $this->locker->getLockedPackages($devMode); $minimumStability = $this->locker->getMinimumStability(); $stabilityFlags = $this->locker->getStabilityFlags(); } $pool = new Pool($minimumStability, $stabilityFlags); $pool->addRepository($installedRepo); foreach ($this->repositoryManager->getRepositories() as $repository) { $pool->addRepository($repository); } $installFromLock = false; $request = new Request($pool); $constraint = new VersionConstraint('=', $this->package->getVersion()); $request->install($this->package->getName(), $constraint); if ($this->update) { $this->io->write('Updating '.($devMode ? 'dev ': '').'dependencies'); $request->updateAll(); $links = $devMode ? $this->package->getDevRequires() : $this->package->getRequires(); foreach ($links as $link) { $request->install($link->getTarget(), $link->getConstraint()); } } elseif ($this->locker->isLocked($devMode)) { $installFromLock = true; $this->io->write('Installing '.($devMode ? 'dev ': '').'dependencies from lock file'); if (!$this->locker->isFresh() && !$devMode) { $this->io->write('Your lock file is out of sync with your composer.json, run "composer.phar update" to update dependencies'); } foreach ($lockedPackages as $package) { $version = $package->getVersion(); foreach ($aliases as $alias) { if ($alias['package'] === $package->getName() && $alias['version'] === $package->getVersion()) { $version = $alias['alias_normalized']; break; } } $constraint = new VersionConstraint('=', $version); $request->install($package->getName(), $constraint); } } else { $this->io->write('Installing '.($devMode ? 'dev ': '').'dependencies'); $links = $devMode ? $this->package->getDevRequires() : $this->package->getRequires(); foreach ($links as $link) { $request->install($link->getTarget(), $link->getConstraint()); } } foreach ($installedRepo->getPackages() as $package) { if ($package->getRepository() === $localRepo) { continue; } $constraint = new VersionConstraint('=', $package->getVersion()); $request->install($package->getName(), $constraint); } $policy = new DefaultPolicy(); $solver = new Solver($policy, $pool, $installedRepo); try { $operations = $solver->solve($request); } catch (SolverProblemsException $e) { $this->io->write('Your requirements could not be solved to an installable set of packages.'); $this->io->write($e->getMessage()); return false; } foreach ($localRepo->getPackages() as $package) { if (!$package->isDev()) { continue; } foreach ($operations as $operation) { if (('update' === $operation->getJobType() && $operation->getInitialPackage()->equals($package)) || ('uninstall' === $operation->getJobType() && $operation->getPackage()->equals($package)) ) { continue 2; } } if ($installFromLock) { $lockData = $this->locker->getLockData(); unset($lockedReference); foreach ($lockData['packages'] as $lockedPackage) { if (!empty($lockedPackage['source-reference']) && strtolower($lockedPackage['package']) === $package->getName()) { $lockedReference = $lockedPackage['source-reference']; break; } } if (isset($lockedReference) && $lockedReference !== $package->getSourceReference()) { $operations[] = new UpdateOperation($package, clone $package); } } else { if ($this->update) { $newPackage = $this->repositoryManager->findPackage($package->getName(), $package->getVersion()); if ($newPackage && $newPackage->getSourceReference() !== $package->getSourceReference()) { $operations[] = new UpdateOperation($package, $newPackage); } } $references = $this->package->getReferences(); if (isset($references[$package->getName()]) && $references[$package->getName()] !== $package->getSourceReference()) { $operations[] = new UpdateOperation($package, clone $package); } } } if (!$operations) { $this->io->write('Nothing to install or update'); } foreach ($operations as $operation) { if ($this->verbose) { $this->io->write((string) $operation); } if ('install' === $operation->getJobType()) { foreach ($operation->getPackage()->getSuggests() as $target => $reason) { $this->suggestedPackages[] = array( 'source' => $operation->getPackage()->getPrettyName(), 'target' => $target, 'reason' => $reason, ); } } if (!$this->dryRun) { $event = 'Composer\Script\ScriptEvents::PRE_PACKAGE_'.strtoupper($operation->getJobType()); if (defined($event)) { $this->eventDispatcher->dispatchPackageEvent(constant($event), $operation); } if ($installFromLock) { $package = null; if ('update' === $operation->getJobType()) { $package = $operation->getTargetPackage(); } elseif ('install' === $operation->getJobType()) { $package = $operation->getPackage(); } if ($package && $package->isDev()) { $lockData = $this->locker->getLockData(); foreach ($lockData['packages'] as $lockedPackage) { if (!empty($lockedPackage['source-reference']) && strtolower($lockedPackage['package']) === $package->getName()) { $package->setSourceReference($lockedPackage['source-reference']); break; } } } } else { $package = null; if ('update' === $operation->getJobType()) { $package = $operation->getTargetPackage(); } elseif ('install' === $operation->getJobType()) { $package = $operation->getPackage(); } if ($package && $package->isDev()) { $references = $this->package->getReferences(); if (isset($references[$package->getName()])) { $package->setSourceReference($references[$package->getName()]); } } } $this->installationManager->execute($localRepo, $operation); $event = 'Composer\Script\ScriptEvents::POST_PACKAGE_'.strtoupper($operation->getJobType()); if (defined($event)) { $this->eventDispatcher->dispatchPackageEvent(constant($event), $operation); } $localRepo->write(); } } return true; } private function aliasPackages() { if (!$this->update && $this->locker->isLocked()) { $aliases = $this->locker->getAliases(); } else { $aliases = $this->package->getAliases(); } foreach ($aliases as $alias) { foreach ($this->repositoryManager->findPackages($alias['package'], $alias['version']) as $package) { $package->setAlias($alias['alias_normalized']); $package->setPrettyAlias($alias['alias']); $package->getRepository()->addPackage($aliasPackage = new AliasPackage($package, $alias['alias_normalized'], $alias['alias'])); $aliasPackage->setRootPackageAlias(true); } foreach ($this->repositoryManager->getLocalRepositories() as $repo) { foreach ($repo->findPackages($alias['package'], $alias['version']) as $package) { $package->setAlias($alias['alias_normalized']); $package->setPrettyAlias($alias['alias']); $package->getRepository()->addPackage($aliasPackage = new AliasPackage($package, $alias['alias_normalized'], $alias['alias'])); $aliasPackage->setRootPackageAlias(true); } } } return $aliases; } static public function create(IOInterface $io, Composer $composer, EventDispatcher $eventDispatcher = null, AutoloadGenerator $autoloadGenerator = null) { $eventDispatcher = $eventDispatcher ?: new EventDispatcher($composer, $io); $autoloadGenerator = $autoloadGenerator ?: new AutoloadGenerator; return new static( $io, $composer->getPackage(), $composer->getDownloadManager(), $composer->getRepositoryManager(), $composer->getLocker(), $composer->getInstallationManager(), $eventDispatcher, $autoloadGenerator ); } public function setAdditionalInstalledRepository(RepositoryInterface $additionalInstalledRepository) { $this->additionalInstalledRepository = $additionalInstalledRepository; return $this; } public function setDryRun($dryRun = true) { $this->dryRun = (boolean) $dryRun; return $this; } public function setPreferSource($preferSource = true) { $this->preferSource = (boolean) $preferSource; return $this; } public function setUpdate($update = true) { $this->update = (boolean) $update; return $this; } public function setDevMode($devMode = true) { $this->devMode = (boolean) $devMode; return $this; } public function setVerbose($verbose = true) { $this->verbose = (boolean) $verbose; return $this; } } ensureDirectoryExists($installationManager->getVendorPath()); $filesystem->ensureDirectoryExists($targetDir); $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/'); $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true); $vendorPathCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true); $vendorPathToTargetDirCode = $filesystem->findShortestPathCode($vendorPath, realpath($targetDir), true); $appBaseDirCode = $filesystem->findShortestPathCode($vendorPath, getcwd(), true); $appBaseDirCode = str_replace('__DIR__', '$vendorDir', $appBaseDirCode); $namespacesFile = <<buildPackageMap($installationManager, $mainPackage, $localRepo->getPackages()); $autoloads = $this->parseAutoloads($packageMap); foreach ($autoloads['psr-0'] as $namespace => $paths) { $exportedPaths = array(); foreach ($paths as $path) { $exportedPaths[] = $this->getPathCode($filesystem, $relVendorPath, $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() && $mainAutoload['psr-0']) { $levels = count(explode('/', trim(strtr($mainPackage->getTargetDir(), '\\', '/'), '/'))); $prefixes = implode(', ', array_map(function ($prefix) { return var_export($prefix, true); }, array_keys($mainAutoload['psr-0']))); $baseDirFromVendorDirCode = $filesystem->findShortestPathCode($vendorPath, getcwd(), true); $targetDirLoader = << $path) { $path = '/'.$filesystem->findShortestPath(getcwd(), $path, true); $classmapFile .= ' '.var_export($class, true).' => $baseDir . '.var_export($path, true).",\n"; } } $classmapFile .= ");\n"; file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile); file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile); if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $relVendorPath, $vendorPath, $vendorPathCode, $appBaseDirCode)) { file_put_contents($targetDir.'/include_paths.php', $includePathFile); } file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, true, true, (Boolean) $includePathFile, $targetDirLoader)); copy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php'); if ($bcLinks) { $filesystem->ensureDirectoryExists($vendorPath.'/.composer'); $deprecated = "// Deprecated file, use the one in root of vendor dir\n". "trigger_error(__FILE__.' is deprecated, please use vendor/autoload.php or vendor/composer/autoload_* instead'.PHP_EOL.'See https://groups.google.com/forum/#!msg/composer-dev/fWIs3KocwoA/nU3aLko9LhQJ for details', E_USER_DEPRECATED);\n"; file_put_contents($vendorPath.'/.composer/autoload_namespaces.php', "getInstallPath($package) ); } return $packageMap; } public function parseAutoloads(array $packageMap) { $autoloads = array('classmap' => array(), 'psr-0' => array()); foreach ($packageMap as $item) { list($package, $installPath) = $item; if (null !== $package->getTargetDir()) { $installPath = substr($installPath, 0, -strlen('/'.$package->getTargetDir())); } foreach ($package->getAutoload() as $type => $mapping) { if (!is_array($mapping)) { continue; } foreach ($mapping as $namespace => $paths) { foreach ((array) $paths as $path) { $autoloads[$type][$namespace][] = empty($installPath) ? $path : $installPath.'/'.$path; } } } } foreach ($autoloads as $type => $maps) { krsort($autoloads[$type]); } return $autoloads; } 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, $relVendorPath, $vendorPath, $vendorPathCode, $appBaseDirCode) { $includePaths = array(); foreach ($packageMap as $item) { list($package, $installPath) = $item; if (null !== $package->getTargetDir()) { $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, $relVendorPath, $vendorPath, $path) . ",\n"; } return $includePathsFile . ");\n"; } protected function getPathCode(Filesystem $filesystem, $relVendorPath, $vendorPath, $path) { $path = strtr($path, '\\', '/'); $baseDir = ''; if (!$filesystem->isAbsolutePath($path)) { if (strpos($path, $relVendorPath) === 0) { $path = substr($path, strlen($relVendorPath)); $baseDir = '$vendorDir . '; } else { $path = '/'.$path; $baseDir = '$baseDir . '; } } elseif (strpos($path, $vendorPath) === 0) { $path = substr($path, strlen($vendorPath)); $baseDir = '$vendorDir . '; } return $baseDir.var_export($path, true); } protected function getAutoloadFile($vendorPathToTargetDirCode, $usePSR0, $useClassMap, $useIncludePath, $targetDirLoader) { $file = <<
$path) { $loader->add($namespace, $path); } PSR0; } if ($useClassMap) { $file .= <<<'CLASSMAP' $classMap = require $composerDir . '/autoload_classmap.php'; if ($classMap) { $loader->addClassMap($classMap); } CLASSMAP; } $file .= $targetDirLoader; return $file . <<<'FOOTER' $loader->register(); return $loader; }); FOOTER; } } isFile()) { continue; } $path = $file->getRealPath(); if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') { continue; } $classes = self::findClasses($path); foreach ($classes as $class) { $map[$class] = $path; } } return $map; } static private function findClasses($path) { $contents = file_get_contents($path); try { $tokens = token_get_all($contents); } catch (\Exception $e) { throw new RuntimeException('Could not scan for classes inside '.$path.": \n".$e->getMessage(), 0, $e); } $T_TRAIT = version_compare(PHP_VERSION, '5.4', '<') ? -1 : T_TRAIT; $classes = array(); $namespace = ''; for ($i = 0, $max = count($tokens); $i < $max; $i++) { $token = $tokens[$i]; if (is_string($token)) { continue; } $class = ''; switch ($token[0]) { case T_NAMESPACE: $namespace = ''; while (($t = $tokens[++$i]) && is_array($t)) { if (in_array($t[0], array(T_STRING, T_NS_SEPARATOR))) { $namespace .= $t[1]; } } $namespace .= '\\'; break; case T_CLASS: case T_INTERFACE: case $T_TRAIT: while (($t = $tokens[++$i]) && is_array($t)) { if (T_STRING === $t[0]) { $class .= $t[1]; } elseif ($class !== '' && T_WHITESPACE == $t[0]) { break; } } $classes[] = ltrim($namespace . $class, '\\'); break; default: break; } } return $classes; } } preferSource = $preferSource; $this->filesystem = $filesystem ?: new Filesystem(); } public function setPreferSource($preferSource) { $this->preferSource = $preferSource; 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() && !($preferSource && $sourceType) && $distType) { $package->setInstallationSource('dist'); } elseif ($sourceType) { $package->setInstallationSource('source'); } elseif ($package->isDev()) { throw new \InvalidArgumentException('Dev package '.$package.' must have a source specified'); } 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); } } io = $io; $this->rfs = $rfs ?: new RemoteFilesystem($io); $this->filesystem = $filesystem ?: new Filesystem(); } 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() . " (" . $package->getPrettyVersion() . ")"); $processUrl = $this->processUrl($url); try { $this->rfs->copy($package->getSourceUrl(), $processUrl, $fileName); 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); throw $e; } } 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() . " (" . $package->getPrettyVersion() . ")"); if (!$this->filesystem->removeDirectory($path)) { throw new \RuntimeException('Could not completely delete '.$path.', aborting.'); } } protected function getFileName(PackageInterface $package, $path) { return $path.'/'.pathinfo($package->getDistUrl(), PATHINFO_BASENAME); } protected function processUrl($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; } } 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()); } } protected function enforceCleanDirectory($path) { $this->process->execute(sprintf('cd %s && hg st', escapeshellarg($path)), $output); if (trim($output)) { throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes'); } } } getFileName($package, $path); if ($this->io->isVerbose()) { $this->io->write(' Unpacking archive'); } try { $this->extract($fileName, $path); if ($this->io->isVerbose()) { $this->io->write(' Cleaning up'); } unlink($fileName); $contentDir = glob($path . '/*'); if (1 === count($contentDir)) { $contentDir = $contentDir[0]; $temporaryName = md5(time().rand()); rename($contentDir, $temporaryName); $contentDir = $temporaryName; foreach (array_merge(glob($contentDir . '/.*'), glob($contentDir . '/*')) as $file) { if (trim(basename($file), '.')) { rename($file, $path . '/' . basename($file)); } } rmdir($contentDir); } } catch (\Exception $e) { $this->filesystem->removeDirectory($path); throw $e; } $this->io->write(''); } protected function getFileName(PackageInterface $package, $path) { return rtrim($path.'/'.md5($path.spl_object_hash($package)).'.'.pathinfo($package->getDistUrl(), PATHINFO_EXTENSION), '.'); } protected function processUrl($url) { if (!extension_loaded('openssl') && (0 === strpos($url, 'https:') || 0 === strpos($url, 'http://github.com'))) { if (preg_match('{^https?://(github.com/[^/]+/[^/]+/(zip|tar)ball/[^/]+)$}i', $url, $match)) { $url = 'http://nodeload.'.$match[1]; } else { throw new \RuntimeException('You must enable the openssl extension to download files via https'); } } return $url; } abstract protected function extract($file, $path); } extractTo($path, null, true); } } process = $process ?: new ProcessExecutor; parent::__construct($io); } protected function extract($file, $path) { if (!class_exists('ZipArchive')) { $error = 'You need the zip extension enabled to use the ZipDownloader'; if (!defined('PHP_WINDOWS_VERSION_BUILD')) { $command = 'unzip '.escapeshellarg($file).' -d '.escapeshellarg($path); if (0 === $this->process->execute($command, $ignoredOutput)) { return; } $error = "Could not decompress the archive, enable the PHP zip extension or install unzip.\n". 'Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput(); } throw new \RuntimeException($error); } $zipArchive = new ZipArchive(); if (true !== ($retval = $zipArchive->open($file))) { throw new \UnexpectedValueException($this->getErrorMessage($retval, $file)); } $zipArchive->extractTo($path); $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); } } } 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); } protected function enforceCleanDirectory($path) { $this->process->execute('svn status', $output, $path); if (trim($output)) { throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes'); } } 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() ); } } } io = $io; $this->process = $process ?: new ProcessExecutor; $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() . " (" . $package->getPrettyVersion() . ")"); $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'); } $this->io->write(" - Updating " . $target->getName() . " (" . $target->getPrettyVersion() . ")"); $this->enforceCleanDirectory($path); $this->doUpdate($initial, $target, $path); $this->io->write(''); } public function remove(PackageInterface $package, $path) { $this->enforceCleanDirectory($path); $this->io->write(" - Removing " . $package->getName() . " (" . $package->getPrettyVersion() . ")"); if (!$this->filesystem->removeDirectory($path)) { throw new \RuntimeException('Could not completely delete '.$path.', aborting.'); } } abstract protected function doDownload(PackageInterface $package, $path); abstract protected function doUpdate(PackageInterface $initial, PackageInterface $target, $path); abstract protected function enforceCleanDirectory($path); } extractTo($path, null, true); } } getSourceReference(); $command = 'git clone %s %s && cd %2$s && git checkout %3$s && git reset --hard %3$s && git remote add composer %1$s'; $this->io->write(" Cloning ".$package->getSourceReference()); $commandCallable = function($url) use ($ref, $path, $command) { return sprintf($command, escapeshellarg($url), escapeshellarg($path), escapeshellarg($ref)); }; $this->runCommand($commandCallable, $package->getSourceUrl(), $path); $this->setPushUrl($package, $path); } public function doUpdate(PackageInterface $initial, PackageInterface $target, $path) { $ref = $target->getSourceReference(); $this->io->write(" Checking out ".$target->getSourceReference()); $command = 'cd %s && git remote set-url composer %s && git fetch composer && git fetch --tags composer && git checkout %3$s && git reset --hard %3$s'; $this->process->execute(sprintf('cd %s && git remote -v', escapeshellarg($path)), $output); if (preg_match('{^composer\s+https://(.+):(.+)@github.com/}im', $output, $match)) { $this->io->setAuthorization('github.com', $match[1], $match[2]); } $this->process->execute(sprintf('cd %s && git remote add composer %s', escapeshellarg($path), escapeshellarg($initial->getSourceUrl())), $ignoredOutput); $commandCallable = function($url) use ($ref, $path, $command) { return sprintf($command, escapeshellarg($path), escapeshellarg($url), escapeshellarg($ref)); }; $this->runCommand($commandCallable, $target->getSourceUrl()); } protected function enforceCleanDirectory($path) { $command = sprintf('cd %s && git status --porcelain --untracked-files=no', escapeshellarg($path)); if (0 !== $this->process->execute($command, $output)) { throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); } if (trim($output)) { throw new \RuntimeException('Source directory ' . $path . ' has uncommitted changes'); } } protected function runCommand($commandCallable, $url, $path = null) { $handler = array($this, 'outputHandler'); if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match)) { $protocols = array('git', 'https', 'http'); $messages = array(); foreach ($protocols as $protocol) { $url = $protocol . $match[1]; if (0 === $this->process->execute(call_user_func($commandCallable, $url), $handler)) { return; } $messages[] = '- ' . $url . "\n" . preg_replace('#^#m', ' ', $this->process->getErrorOutput()); if (null !== $path) { $this->filesystem->removeDirectory($path); } } $this->throwException('Failed to clone ' . $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, $handler)) { if (preg_match('{^git@github.com:(.+?)\.git$}i', $url, $match) && $this->io->isInteractive()) { $retries = 3; $retrying = false; do { if ($retrying) { $this->io->write('Invalid credentials'); } if (!$this->io->hasAuthorization('github.com') || $retrying) { $username = $this->io->ask('Username: '); $password = $this->io->askAndHideAnswer('Password: '); $this->io->setAuthorization('github.com', $username, $password); } $auth = $this->io->getAuthorization('github.com'); $url = 'https://'.$auth['username'] . ':' . $auth['password'] . '@github.com/'.$match[1].'.git'; $command = call_user_func($commandCallable, $url); if (0 === $this->process->execute($command, $handler)) { return; } if (null !== $path) { $this->filesystem->removeDirectory($path); } $retrying = true; } while (--$retries); } if (null !== $path) { $this->filesystem->removeDirectory($path); } $this->throwException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput(), $url); } } public function outputHandler($type, $buffer) { if ($type !== 'out') { return; } if ($this->io->isVerbose()) { $this->io->write($buffer, false); } } protected function throwException($message, $url) { if (0 !== $this->process->execute('git --version', $ignoredOutput)) { throw new \RuntimeException('Failed to clone '.$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 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); } } } * Jordi Boggiano * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Composer\Autoload; /** * ClassLoader implements a PSR-0 class loader * * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md * * $loader = new \Composer\Autoload\ClassLoader(); * * // register classes with namespaces * $loader->add('Symfony\Component', __DIR__.'/component'); * $loader->add('Symfony', __DIR__.'/framework'); * * // activate the autoloader * $loader->register(); * * // to enable searching the include path (eg. for PEAR packages) * $loader->setUseIncludePath(true); * * In this example, if you try to use a class in the Symfony\Component * namespace or one of its children (Symfony\Component\Console for instance), * the autoloader will first look for the class under the component/ * directory, and it will then fallback to the framework/ directory if not * found before giving up. * * This class is loosely based on the Symfony UniversalClassLoader. * * @author Fabien Potencier * @author Jordi Boggiano */ class ClassLoader { private $prefixes = array(); private $fallbackDirs = array(); private $useIncludePath = false; private $classMap = array(); public function getPrefixes() { return $this->prefixes; } public function getFallbackDirs() { return $this->fallbackDirs; } public function getClassMap() { return $this->classMap; } /** * @param array $classMap Class to filename map */ public function addClassMap(array $classMap) { if ($this->classMap) { $this->classMap = array_merge($this->classMap, $classMap); } else { $this->classMap = $classMap; } } /** * Registers a set of classes * * @param string $prefix The classes prefix * @param array|string $paths The location(s) of the classes */ public function add($prefix, $paths) { if (!$prefix) { foreach ((array) $paths as $path) { $this->fallbackDirs[] = $path; } return; } if (isset($this->prefixes[$prefix])) { $this->prefixes[$prefix] = array_merge( $this->prefixes[$prefix], (array) $paths ); } else { $this->prefixes[$prefix] = (array) $paths; } } /** * Turns on searching the include path for class files. * * @param Boolean $useIncludePath */ public function setUseIncludePath($useIncludePath) { $this->useIncludePath = $useIncludePath; } /** * Can be used to check if the autoloader uses the include path to check * for classes. * * @return Boolean */ public function getUseIncludePath() { return $this->useIncludePath; } /** * Registers this instance as an autoloader. * * @param Boolean $prepend Whether to prepend the autoloader or not */ public function register($prepend = false) { spl_autoload_register(array($this, 'loadClass'), true, $prepend); } /** * Unregisters this instance as an autoloader. */ public function unregister() { spl_autoload_unregister(array($this, 'loadClass')); } /** * Loads the given class or interface. * * @param string $class The name of the class * @return Boolean|null True, if loaded */ public function loadClass($class) { if ($file = $this->findFile($class)) { require $file; return true; } } /** * Finds the path to the file where the class is defined. * * @param string $class The name of the class * * @return string|null The path, if found */ public function findFile($class) { if (isset($this->classMap[$class])) { return $this->classMap[$class]; } if ('\\' == $class[0]) { $class = substr($class, 1); } if (false !== $pos = strrpos($class, '\\')) { // namespaced class name $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR; $className = substr($class, $pos + 1); } else { // PEAR-like class name $classPath = null; $className = $class; } $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; foreach ($this->prefixes as $prefix => $dirs) { if (0 === strpos($class, $prefix)) { foreach ($dirs as $dir) { if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { return $dir . DIRECTORY_SEPARATOR . $classPath; } } } } foreach ($this->fallbackDirs as $dir) { if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { return $dir . DIRECTORY_SEPARATOR . $classPath; } } if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) { return $file; } } } { "name": "Package", "type": "object", "additionalProperties": false, "properties": { "name": { "type": "string", "description": "Package name, including 'vendor-name/' prefix.", "required": true }, "type": { "description": "Package type, either 'library' for common packages, 'composer-installer' for custom installers, 'metapackage' for empty packages, or a custom type defined by whatever project this package applies to.", "type": "string" }, "target-dir": { "description": "Forces the package to be installed into the given subdirectory path. This is used for autoloading PSR-0 packages that do not contain their full path. Use forward slashes for cross-platform compatibility.", "type": "string" }, "description": { "type": "string", "description": "Short package description.", "required": true }, "keywords": { "type": "array", "items": { "type": "string", "description": "A tag/keyword that this package relates to." } }, "homepage": { "type": "string", "description": "Homepage URL for the project.", "format": "uri" }, "version": { "type": "string", "description": "Package version, see http://getcomposer.org/doc/04-schema.md#version for more info on valid schemes." }, "time": { "type": "string", "description": "Package release date, in 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS' format." }, "license": { "type": ["string", "array"], "description": "License name. Or an array of license names." }, "authors": { "type": "array", "description": "List of authors that contributed to the package. This is typically the main maintainers, not the full list.", "items": { "type": "object", "additionalProperties": false, "properties": { "name": { "type": "string", "description": "Full name of the author.", "required": true }, "email": { "type": "string", "description": "Email address of the author.", "format": "email" }, "homepage": { "type": "string", "description": "Homepage URL for the author.", "format": "uri" }, "role": { "type": "string", "description": "Author's role in the project." } } } }, "require": { "type": "object", "description": "This is a hash of package name (keys) and version constraints (values) that are required to run this package.", "additionalProperties": true }, "replace": { "type": "object", "description": "This is a hash of package name (keys) and version constraints (values) that can be replaced by this package.", "additionalProperties": true }, "conflict": { "type": "object", "description": "This is a hash of package name (keys) and version constraints (values) that conflict with this package.", "additionalProperties": true }, "provide": { "type": "object", "description": "This is a hash of package name (keys) and version constraints (values) that this package provides in addition to this package's name.", "additionalProperties": true }, "require-dev": { "type": "object", "description": "This is a hash of package name (keys) and version constraints (values) that this package requires for developing it (testing tools and such).", "additionalProperties": true }, "suggest": { "type": "object", "description": "This is a hash of package name (keys) and descriptions (values) that this package suggests work well with it (this will be suggested to the user during installation).", "additionalProperties": true }, "config": { "type": ["object"], "description": "Composer options.", "properties": { "vendor-dir": { "type": "string", "description": "The location where all packages are installed, defaults to \"vendor\"." }, "bin-dir": { "type": "string", "description": "The location where all binaries are linked, defaults to \"vendor/bin\"." } } }, "extra": { "type": ["object", "array"], "description": "Arbitrary extra data that can be used by custom installers, for example, package of type composer-installer must have a 'class' key defining the installer class name.", "additionalProperties": true }, "autoload": { "type": "object", "description": "Description of how the package can be autoloaded.", "properties": { "psr-0": { "type": "object", "description": "This is a hash of namespaces (keys) and the directories they can be found into (values, can be arrays of paths) by the autoloader.", "additionalProperties": true }, "classmap": { "type": "array", "description": "This is an array of directories that contain classes to be included in the class-map generation process." } } }, "repositories": { "type": ["object", "array"], "description": "A set of additional repositories where packages can be found.", "additionalProperties": true }, "minimum-stability": { "type": ["string"], "description": "The minimum stability the packages must have to be install-able. Possible values are: dev, alpha, beta, RC, stable." }, "bin": { "type": ["array"], "description": "A set of files that should be treated as binaries and symlinked into bin-dir (from config).", "items": { "type": "string" } }, "include-path": { "type": ["array"], "description": "DEPRECATED: A list of directories which should get added to PHP's include path. This is only present to support legacy projects, and all new code should preferably use autoloading.", "items": { "type": "string" } }, "scripts": { "type": ["object"], "description": "Scripts listeners that will be executed before/after some events.", "properties": { "pre-install-cmd": { "type": ["array", "string"], "description": "Occurs before the install command is executed, contains one or more Class::method callables." }, "post-install-cmd": { "type": ["array", "string"], "description": "Occurs after the install command is executed, contains one or more Class::method callables." }, "pre-update-cmd": { "type": ["array", "string"], "description": "Occurs before the update command is executed, contains one or more Class::method callables." }, "post-update-cmd": { "type": ["array", "string"], "description": "Occurs after the update command is executed, contains one or more Class::method callables." }, "pre-package-install": { "type": ["array", "string"], "description": "Occurs before a package is installed, contains one or more Class::method callables." }, "post-package-install": { "type": ["array", "string"], "description": "Occurs after a package is installed, contains one or more Class::method callables." }, "pre-package-update": { "type": ["array", "string"], "description": "Occurs before a package is updated, contains one or more Class::method callables." }, "post-package-update": { "type": ["array", "string"], "description": "Occurs after a package is updated, contains one or more Class::method callables." }, "pre-package-uninstall": { "type": ["array", "string"], "description": "Occurs before a package has been uninstalled, contains one or more Class::method callables." }, "post-package-uninstall": { "type": ["array", "string"], "description": "Occurs after a package has been uninstalled, contains one or more Class::method callables." } } } } } [ "AFL-1.1", "AFL-1.2", "AFL-2.0", "AFL-2.1", "AFL-3.0", "APL-1.0", "ANTLR-PD", "Apache-1.0", "Apache-1.1", "Apache-2.0", "APSL-1.0", "APSL-1.1", "APSL-1.2", "APSL-2.0", "Artistic-1.0", "Artistic-2.0", "AAL", "BSL-1.0", "BSD-2-Clause", "BSD-2-Clause-NetBSD", "BSD-2-Clause-FreeBSD", "BSD-3-Clause", "BSD-4-Clause", "BSD-4-Clause-UC", "CECILL-1.0", "CECILL-1.1", "CECILL-2.0", "CECILL-B", "CECILL-C", "ClArtistic", "CNRI-Python-GPL-Compatible", "CNRI-Python", "CDDL-1.0", "CDDL-1.1", "CPAL-1.0", "CPL-1.0", "CATOSL-1.1", "CC-BY-1.0", "CC-BY-2.0", "CC-BY-2.5", "CC-BY-3.0", "CC-BY-ND-1.0", "CC-BY-ND-2.0", "CC-BY-ND-2.5", "CC-BY-ND-3.0", "CC-BY-NC-1.0", "CC-BY-NC-2.0", "CC-BY-NC-2.5", "CC-BY-NC-3.0", "CC-BY-NC-ND-1.0", "CC-BY-NC-ND-2.0", "CC-BY-NC-ND-2.5", "CC-BY-NC-ND-3.0", "CC-BY-NC-SA-1.0", "CC-BY-NC-SA-2.0", "CC-BY-NC-SA-2.5", "CC-BY-NC-SA-3.0", "CC-BY-SA-1.0", "CC-BY-SA-2.0", "CC-BY-SA-2.5", "CC-BY-SA-3.0", "CC0-1.0", "CUA-OPL-1.0", "EPL-1.0", "eCos-2.0", "ECL-1.0", "ECL-2.0", "EFL-1.0", "EFL-2.0", "Entessa", "ErlPL-1.1", "EUDatagrid", "EUPL-1.0", "EUPL-1.1", "Fair", "Frameworx-1.0", "AGPL-3.0", "GFDL-1.1", "GFDL-1.2", "GFDL-1.3", "GPL-1.0", "GPL-1.0+", "GPL-2.0", "GPL-2.0+", "GPL-2.0-with-autoconf-exception", "GPL-2.0-with-bison-exception", "GPL-2.0-with-classpath-exception", "GPL-2.0-with-font-exception", "GPL-2.0-with-GCC-exception", "GPL-3.0", "GPL-3.0+", "GPL-3.0-with-autoconf-exception", "GPL-3.0-with-GCC-exception", "LGPL-2.1", "LGPL-2.1+", "LGPL-3.0", "LGPL-3.0+", "LGPL-2.0", "LGPL-2.0+", "gSOAP-1.3b", "HPND", "IPL-1.0", "IPA", "ISC", "LPPL-1.0", "LPPL-1.1", "LPPL-1.2", "LPPL-1.3c", "Libpng", "LPL-1.0", "LPL-1.02", "MS-PL", "MS-RL", "MirOS", "MIT", "Motosoto", "MPL-1.0", "MPL-1.1", "MPL-2.0", "Multics", "NASA-1.3", "Naumen", "NGPL", "Nokia", "NPOSL-3.0", "NTP", "OCLC-2.0", "ODbL-1.0", "PDDL-1.0", "OGTSL", "OSL-1.0", "OSL-2.0", "OSL-2.1", "OSL-3.0", "OLDAP-2.8", "OpenSSL", "PHP-3.0", "PHP-3.01", "PostgreSQL", "Python-2.0", "QPL-1.0", "RPSL-1.0", "RPL-1.5", "RHeCos-1.1", "RSCPL", "Ruby", "SAX-PD", "OFL-1.0", "OFL-1.1", "SimPL-2.0", "Sleepycat", "SugarCRM-1.1.3", "SPL-1.0", "Watcom-1.0", "NCSA", "VSL-1.0", "W3C", "WXwindows", "Xnet", "XFree86-1.1", "YPL-1.0", "YPL-1.1", "Zimbra-1.3", "Zlib", "ZPL-1.1", "ZPL-2.0", "ZPL-2.1" ]MZ@ !L!This program cannot be run in DOS mode. $,;B;B;B2מ:B2-B2ƞ9B2ў?Ba98B;CB2Ȟ:B2֞:B2Ӟ:BRich;BPELMoO  8 @`?@"P@ Pp!8!@ .text   `.rdata @@.data0@.rsrc @@@.relocP"@Bj$@xj @eEPV @EЃPV @MX @eEP5H @L @YY5\ @EP5` @D @YYP @MMT @3H; 0@uh@l3@$40@5h3@40@h$0@h(0@h 0@ @00@}jYjh"@3ۉ]dp]俀3@SVW0 @;t;u3Fuh4 @3F|3@;u j\Y;|3@u,5|3@h @h @YYtE5<0@|3@;uh @h @lYY|3@9]uSW8 @93@th3@Yt SjS3@$0@ @5$0@5(0@5 0@ 80@9,0@u7P @E MPQYYËeE80@39,0@uPh @9<0@u @E80@øMZf9@t3M<@@8PEuH t uՃv39xtv39j,0@p @jl @YY3@3@ @ t3@ @ p3@ @x3@V=0@u h@ @Yg=0@u j @Y3{U(H1@ D1@@1@<1@581@=41@f`1@f T1@f01@f,1@f%(1@f-$1@X1@EL1@EP1@E\1@0@P1@L0@@0@ D0@0@0@ @0@j?Yj @h!@$ @=0@ujYh ( @P, @ËUE8csmu*xu$@= t=!t="t=@u3]hH@ @3% @jh("@b53@5 @YEu u @YgjYe53@։E53@YYEEPEPu5l @YPUEu֣3@uփ3@E EjYËUuNYH]ËV!@!@W;stЃ;r_^ËV"@"@W;stЃ;r_^% @̋UMMZf9t3]ËA<8PEu3ҹ f9H‹]̋UEH<ASVq3WDv} H ;r X;r B(;r3_^[]̋UjhH"@he@dPSVW0@1E3PEdeEh@*tUE-@Ph@Pt;@$ЃEMd Y_^[]ËE3=‹ËeE3Md Y_^[]% @% @he@d5D$l$l$+SVW0@1E3PeuEEEEdËMd Y__^[]QËUuuu uh@h0@]ËVhh3V t VVVVV^3ËU0@eeSWN@;t t У0@`VEP< @u3u @3 @3 @3EP @E3E3;uO@ u 50@։50@^_[%t @%x @%| @% @% @% @% @% @% @Pd5D$ +d$ SVW(0@3PEuEEdËMd Y__^[]QËM3M%T @T$B J3J3l"@s###)r)b)H)4))(((((()#$%%&d&&$('''''(((6('H(Z(t(('''''l'^'R'F'>'>(0'')@W@@MoOl!@0@0@bad allocationH0@!@RSDSьJ!LZc:\users\seld\documents\visual studio 2010\Projects\hiddeninp\Release\hiddeninp.pdbe@@:@@@@"d"@"# $#&D H#(h ###)r)b)H)4))(((((()#$%%&d&&$('''''(((6('H(Z(t(('''''l'^'R'F'>'>(0'')GetConsoleModeSetConsoleMode;GetStdHandleKERNEL32.dll??$?6DU?$char_traits@D@std@@V?$allocator@D@1@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@0@@Z?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@AJ?cin@std@@3V?$basic_istream@DU?$char_traits@D@std@@@1@A??$getline@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@0@AAV10@AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@0@@Z??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@P6AAAV01@AAV01@@Z@Z_??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ{??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ?endl@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@ZMSVCP90.dll_amsg_exit__getmainargs,_cexit|_exitf_XcptFilterexit__initenv_initterm_initterm_e<_configthreadlocale__setusermatherr _adjust_fdiv__p__commode__p__fmodej_encode_pointer__set_app_typeK_crt_debugger_hookC?terminate@@YAXXZMSVCR90.dll_unlock__dllonexitv_lock_onexit`_decode_pointers_except_handler4_common _invoke_watson?_controlfp_sInterlockedExchange!SleepInterlockedCompareExchange-TerminateProcessGetCurrentProcess>UnhandledExceptionFilterSetUnhandledExceptionFilterIsDebuggerPresentTQueryPerformanceCounterfGetTickCountGetCurrentThreadIdGetCurrentProcessIdOGetSystemTimeAsFileTimes__CxxFrameHandler3N@D$!@ 8Ph  @(CV(4VS_VERSION_INFOStringFileInfob040904b0QFileDescriptionReads from stdin without leaking info to the terminal and outputs back to stdout6 FileVersion1, 0, 0, 08 InternalNamehiddeninputPLegalCopyrightJordi Boggiano - 2012HOriginalFilenamehiddeninput.exe: ProductNameHidden Input: ProductVersion1, 0, 0, 0DVarFileInfo$Translation  PAPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDINGPADDINGXXPADDING@00!0/080F0L0T0^0d0n0{000000000000001#1-1@1J1O1T1v1{1111111111111112"2*23292A2M2_2j2p222222222222 333%303N3T3Z3`3f3l3s3z333333333333333334444%4;4B444444444445!5^5c5555H6M6_6}66677 7*7w7|777778 88=8E8P8V8\8b8h8n8t8z88889 $0001 1t1x12 2@2\2`2h2t20 0 'OK', 1 => 'General error', 2 => 'Misuse of shell builtins', 126 => 'Invoked command cannot execute', 127 => 'Command not found', 128 => 'Invalid exit argument', 129 => 'Hangup', 130 => 'Interrupt', 131 => 'Quit and dump core', 132 => 'Illegal instruction', 133 => 'Trace/breakpoint trap', 134 => 'Process aborted', 135 => 'Bus error: "access to undefined portion of memory object"', 136 => 'Floating point exception: "erroneous arithmetic operation"', 137 => 'Kill (terminate immediately)', 138 => 'User-defined 1', 139 => 'Segmentation violation', 140 => 'User-defined 2', 141 => 'Write to pipe with no one reading', 142 => 'Signal raised by alarm', 143 => 'Termination (request to terminate)', 145 => 'Child process terminated, stopped (or continued*)', 146 => 'Continue if stopped', 147 => 'Stop executing temporarily', 148 => 'Terminal stop signal', 149 => 'Background process attempting to read from tty ("in")', 150 => 'Background process attempting to write to tty ("out")', 151 => 'Urgent data available on socket', 152 => 'CPU time limit exceeded', 153 => 'File size limit exceeded', 154 => 'Signal raised by timer counting virtual time: "virtual timer expired"', 155 => 'Profiling timer expired', 157 => 'Pollable event', 159 => 'Bad syscall', ); public function __construct($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array()) { if (!function_exists('proc_open')) { throw new \RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.'); } $this->commandline = $commandline; $this->cwd = null === $cwd ? getcwd() : $cwd; if (null !== $env) { $this->env = array(); foreach ($env as $key => $value) { $this->env[(binary) $key] = (binary) $value; } } else { $this->env = null; } $this->stdin = $stdin; $this->timeout = $timeout; $this->enhanceWindowsCompatibility = true; $this->options = array_replace(array('suppress_errors' => true, 'binary_pipes' => true), $options); } public function __destruct() { $this->stop(); } public function run($callback = null) { $this->start($callback); return $this->wait($callback); } public function start($callback = null) { if ($this->isRunning()) { throw new \RuntimeException('Process is already running'); } $this->stdout = ''; $this->stderr = ''; $callback = $this->buildCallback($callback); if (defined('PHP_WINDOWS_VERSION_BUILD')) { $this->fileHandles = array( self::STDOUT => tmpfile(), ); $descriptors = array(array('pipe', 'r'), $this->fileHandles[self::STDOUT], array('pipe', 'w')); } else { $descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')); } $commandline = $this->commandline; if (defined('PHP_WINDOWS_VERSION_BUILD') && $this->enhanceWindowsCompatibility) { $commandline = 'cmd /V:ON /E:ON /C "'.$commandline.'"'; if (!isset($this->options['bypass_shell'])) { $this->options['bypass_shell'] = true; } } $this->process = proc_open($commandline, $descriptors, $this->pipes, $this->cwd, $this->env, $this->options); if (!is_resource($this->process)) { throw new \RuntimeException('Unable to launch a new process.'); } $this->status = self::STATUS_STARTED; foreach ($this->pipes as $pipe) { stream_set_blocking($pipe, false); } if (null === $this->stdin) { fclose($this->pipes[0]); unset($this->pipes[0]); return; } else { $writePipes = array($this->pipes[0]); unset($this->pipes[0]); $stdinLen = strlen($this->stdin); $stdinOffset = 0; } while ($writePipes) { $r = $this->pipes; $w = $writePipes; $e = null; $n = @stream_select($r, $w, $e, $this->timeout); if (false === $n) { break; } elseif ($n === 0) { proc_terminate($this->process); throw new \RuntimeException('The process timed out.'); } if ($w) { $written = fwrite($writePipes[0], (binary) substr($this->stdin, $stdinOffset), 8192); if (false !== $written) { $stdinOffset += $written; } if ($stdinOffset >= $stdinLen) { fclose($writePipes[0]); $writePipes = null; } } if (defined('PHP_WINDOWS_VERSION_BUILD')) { $fh = $this->fileHandles; foreach ($fh as $type => $fileHandle) { fseek($fileHandle, 0); $data = fread($fileHandle, 8192); $this->readBytes[$type] = strlen($data); if (strlen($data) > 0) { call_user_func($callback, $type == 1 ? self::OUT : self::ERR, $data); } if (false === $data) { fclose($fileHandle); unset($this->fileHandles[$type]); } } } foreach ($r as $pipe) { $type = array_search($pipe, $this->pipes); $data = fread($pipe, 8192); if (strlen($data) > 0) { call_user_func($callback, $type == 1 ? self::OUT : self::ERR, $data); } if (false === $data || feof($pipe)) { fclose($pipe); unset($this->pipes[$type]); } } } $this->processInformation = proc_get_status($this->process); } public function wait($callback = null) { $this->processInformation = proc_get_status($this->process); $callback = $this->buildCallback($callback); while ($this->pipes || (defined('PHP_WINDOWS_VERSION_BUILD') && $this->fileHandles)) { $r = $this->pipes; $w = null; $e = null; $n = @stream_select($r, $w, $e, $this->timeout); if (false === $n) { break; } if (0 === $n) { proc_terminate($this->process); throw new \RuntimeException('The process timed out.'); } if (defined('PHP_WINDOWS_VERSION_BUILD')) { $fh = $this->fileHandles; foreach ($fh as $type => $fileHandle) { fseek($fileHandle, $this->readBytes[$type]); $data = fread($fileHandle, 8192); if(isset($this->readBytes)) { $this->readBytes[$type] += strlen($data); } else { $this->readBytes[$type] = strlen($data); } if (strlen($data) > 0) { call_user_func($callback, $type == 1 ? self::OUT : self::ERR, $data); } if (false === $data) { fclose($fileHandle); unset($this->fileHandles[$type]); } } } foreach ($r as $pipe) { $type = array_search($pipe, $this->pipes); $data = fread($pipe, 8192); if (strlen($data) > 0) { call_user_func($callback, $type == 1 ? self::OUT : self::ERR, $data); } if (false === $data || feof($pipe)) { fclose($pipe); unset($this->pipes[$type]); } } } $this->updateStatus(); if ($this->processInformation['signaled']) { throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->processInformation['stopsig'])); } $time = 0; while ($this->isRunning() && $time < 1000000) { $time += 1000; usleep(1000); } $exitcode = proc_close($this->process); if ($this->processInformation['signaled']) { throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->processInformation['stopsig'])); } return $this->exitcode = $this->processInformation['running'] ? $exitcode : $this->processInformation['exitcode']; } public function getOutput() { $this->updateOutput(); return $this->stdout; } public function getErrorOutput() { $this->updateErrorOutput(); return $this->stderr; } public function getExitCode() { $this->updateStatus(); return $this->exitcode; } public function getExitCodeText() { $this->updateStatus(); return isset(self::$exitCodes[$this->exitcode]) ? self::$exitCodes[$this->exitcode] : 'Unknown error'; } public function isSuccessful() { $this->updateStatus(); return 0 == $this->exitcode; } public function hasBeenSignaled() { $this->updateStatus(); return $this->processInformation['signaled']; } public function getTermSignal() { $this->updateStatus(); return $this->processInformation['termsig']; } public function hasBeenStopped() { $this->updateStatus(); return $this->processInformation['stopped']; } public function getStopSignal() { $this->updateStatus(); return $this->processInformation['stopsig']; } public function isRunning() { if (self::STATUS_STARTED !== $this->status) { return false; } $this->updateStatus(); return $this->processInformation['running']; } public function stop($timeout=10) { $timeoutMicro = (int) $timeout*10E6; if ($this->isRunning()) { proc_terminate($this->process); $time = 0; while (1 == $this->isRunning() && $time < $timeoutMicro) { $time += 1000; usleep(1000); } foreach ($this->pipes as $pipe) { fclose($pipe); } $this->pipes = array(); $exitcode = proc_close($this->process); $this->exitcode = -1 === $this->processInformation['exitcode'] ? $exitcode : $this->processInformation['exitcode']; if (defined('PHP_WINDOWS_VERSION_BUILD')) { foreach ($this->fileHandles as $fileHandle) { fclose($fileHandle); } $this->fileHandles = array(); } } $this->status = self::STATUS_TERMINATED; return $this->exitcode; } public function addOutput($line) { $this->stdout .= $line; } public function addErrorOutput($line) { $this->stderr .= $line; } public function getCommandLine() { return $this->commandline; } public function setCommandLine($commandline) { $this->commandline = $commandline; } public function getTimeout() { return $this->timeout; } public function setTimeout($timeout) { $this->timeout = $timeout; } public function getWorkingDirectory() { return $this->cwd; } public function setWorkingDirectory($cwd) { $this->cwd = $cwd; } public function getEnv() { return $this->env; } public function setEnv(array $env) { $this->env = $env; } public function getStdin() { return $this->stdin; } public function setStdin($stdin) { $this->stdin = $stdin; } public function getOptions() { return $this->options; } public function setOptions(array $options) { $this->options = $options; } public function getEnhanceWindowsCompatibility() { return $this->enhanceWindowsCompatibility; } public function setEnhanceWindowsCompatibility($enhance) { $this->enhanceWindowsCompatibility = (Boolean) $enhance; } protected function buildCallback($callback) { $that = $this; $out = self::OUT; $err = self::ERR; $callback = function ($type, $data) use ($that, $callback, $out, $err) { if ($out == $type) { $that->addOutput($data); } else { $that->addErrorOutput($data); } if (null !== $callback) { call_user_func($callback, $type, $data); } }; return $callback; } protected function updateStatus() { if (self::STATUS_STARTED !== $this->status) { return; } $this->processInformation = proc_get_status($this->process); if (!$this->processInformation['running']) { $this->status = self::STATUS_TERMINATED; if (-1 !== $this->processInformation['exitcode']) { $this->exitcode = $this->processInformation['exitcode']; } } } protected function updateErrorOutput() { if (isset($this->pipes[self::STDERR]) && is_resource($this->pipes[self::STDERR])) { $this->addErrorOutput(stream_get_contents($this->pipes[self::STDERR])); } } protected function updateOutput() { if (defined('PHP_WINDOWS_VERSION_BUILD') && isset($this->fileHandles[self::STDOUT]) && is_resource($this->fileHandles[self::STDOUT])) { fseek($this->fileHandles[self::STDOUT], $this->readBytes[self::STDOUT]); $this->addOutput(stream_get_contents($this->fileHandles[self::STDOUT])); } elseif (isset($this->pipes[self::STDOUT]) && is_resource($this->pipes[self::STDOUT])) { $this->addOutput(stream_get_contents($this->pipes[self::STDOUT])); } } } executableFinder = new ExecutableFinder(); } public function find() { if (defined('PHP_BINARY') && PHP_BINARY && ('cli' === PHP_SAPI)) { return PHP_BINARY; } if ($php = getenv('PHP_PATH')) { if (!is_executable($php)) { return false; } return $php; } $suffixes = DIRECTORY_SEPARATOR == '\\' ? (getenv('PATHEXT') ? explode(PATH_SEPARATOR, getenv('PATHEXT')) : array('.exe', '.bat', '.cmd', '.com')) : array(''); foreach ($suffixes as $suffix) { if (is_executable($php = PHP_BINDIR.DIRECTORY_SEPARATOR.'php'.$suffix)) { return $php; } } if ($php = getenv('PHP_PEAR_PHP_BIN')) { if (is_executable($php)) { return $php; } } return $this->executableFinder->find('php'); } } suffixes = $suffixes; } public function addSuffix($suffix) { $this->suffixes[] = $suffix; } public function find($name, $default = null) { if (ini_get('open_basedir')) { $searchPath = explode(PATH_SEPARATOR, getenv('open_basedir')); $dirs = array(); foreach ($searchPath as $path) { if (is_dir($path)) { $dirs[] = $path; } else { $file = str_replace(dirname($path), '', $path); if ($file == $name && is_executable($path)) { return $path; } } } } else { $dirs = explode(PATH_SEPARATOR, getenv('PATH') ? getenv('PATH') : getenv('Path')); } $suffixes = DIRECTORY_SEPARATOR == '\\' ? (getenv('PATHEXT') ? explode(PATH_SEPARATOR, getenv('PATHEXT')) : $this->suffixes) : array(''); foreach ($suffixes as $suffix) { foreach ($dirs as $dir) { if (is_file($file = $dir.DIRECTORY_SEPARATOR.$name.$suffix) && (self::$isWindows || is_executable($file))) { return $file; } } } return $default; } } isSuccessful()) { throw new \InvalidArgumentException('Expected a failed process, but the given process was successful.'); } parent::__construct( sprintf( 'The command "%s" failed.'."\n\nOutput:\n================\n".$process->getOutput()."\n\nError Output:\n================\n".$process->getErrorOutput(), $process->getCommandLine() ) ); $this->process = $process; } public function getProcess() { return $this->process; } } executableFinder = new PhpExecutableFinder(); } public function setPhpBinary($php) { $this->setCommandLine($php); } public function run($callback = null) { if (null === $this->getCommandLine()) { if (false === $php = $this->executableFinder->find()) { throw new \RuntimeException('Unable to find the PHP executable.'); } $this->setCommandLine($php); } return parent::run($callback); } } arguments = $arguments; $this->timeout = 60; $this->options = array(); $this->env = array(); $this->inheritEnv = true; } public static function create(array $arguments = array()) { return new static($arguments); } public function add($argument) { $this->arguments[] = $argument; return $this; } public function setWorkingDirectory($cwd) { $this->cwd = $cwd; return $this; } public function inheritEnvironmentVariables($inheritEnv = true) { $this->inheritEnv = $inheritEnv; return $this; } public function setEnv($name, $value) { $this->env[$name] = $value; return $this; } public function setInput($stdin) { $this->stdin = $stdin; return $this; } public function setTimeout($timeout) { $this->timeout = $timeout; return $this; } public function setOption($name, $value) { $this->options[$name] = $value; return $this; } public function getProcess() { if (!count($this->arguments)) { throw new \LogicException('You must add() command arguments before calling getProcess().'); } $options = $this->options; $script = implode(' ', array_map('escapeshellarg', $this->arguments)); if ($this->inheritEnv) { $env = $this->env ? $this->env + $_ENV : null; } else { $env = $this->env; } return new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options); } } write($question); $ret = fgets($this->inputStream ?: STDIN, 4096); if (false === $ret) { throw new \RuntimeException('Aborted'); } $ret = trim($ret); return strlen($ret) > 0 ? $ret : $default; } public function askConfirmation(OutputInterface $output, $question, $default = true) { $answer = 'z'; while ($answer && !in_array(strtolower($answer[0]), array('y', 'n'))) { $answer = $this->ask($output, $question); } if (false === $default) { return $answer && 'y' == strtolower($answer[0]); } return !$answer || 'y' == strtolower($answer[0]); } public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $default = null) { $error = null; while (false === $attempts || $attempts--) { if (null !== $error) { $output->writeln($this->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error')); } $value = $this->ask($output, $question, $default); try { return call_user_func($validator, $value); } catch (\Exception $error) { } } throw $error; } public function setInputStream($stream) { $this->inputStream = $stream; } public function getInputStream() { return $this->inputStream; } public function getName() { return 'dialog'; } } [%s] %s', $style, $section, $style, $message); } public function formatBlock($messages, $style, $large = false) { $messages = (array) $messages; $len = 0; $lines = array(); foreach ($messages as $message) { $lines[] = sprintf($large ? ' %s ' : ' %s ', $message); $len = max($this->strlen($message) + ($large ? 4 : 2), $len); } $messages = $large ? array(str_repeat(' ', $len)) : array(); foreach ($lines as $line) { $messages[] = $line.str_repeat(' ', $len - $this->strlen($line)); } if ($large) { $messages[] = str_repeat(' ', $len); } foreach ($messages as &$message) { $message = sprintf('<%s>%s', $style, $message, $style); } return implode("\n", $messages); } private function strlen($string) { if (!function_exists('mb_strlen')) { return strlen($string); } if (false === $encoding = mb_detect_encoding($string)) { return strlen($string); } return mb_strlen($string, $encoding); } public function getName() { return 'formatter'; } } helpers = array(); foreach ($helpers as $alias => $helper) { $this->set($helper, is_int($alias) ? null : $alias); } } public function set(HelperInterface $helper, $alias = null) { $this->helpers[$helper->getName()] = $helper; if (null !== $alias) { $this->helpers[$alias] = $helper; } $helper->setHelperSet($this); } public function has($name) { return isset($this->helpers[$name]); } public function get($name) { if (!$this->has($name)) { throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name)); } return $this->helpers[$name]; } public function setCommand(Command $command = null) { $this->command = $command; } public function getCommand() { return $this->command; } } helperSet = $helperSet; } public function getHelperSet() { return $this->helperSet; } } 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37 ); static private $availableBackgroundColors = array( 'black' => 40, 'red' => 41, 'green' => 42, 'yellow' => 43, 'blue' => 44, 'magenta' => 45, 'cyan' => 46, 'white' => 47 ); static private $availableOptions = array( 'bold' => 1, 'underscore' => 4, 'blink' => 5, 'reverse' => 7, 'conceal' => 8 ); private $foreground; private $background; private $options = array(); public function __construct($foreground = null, $background = null, array $options = array()) { if (null !== $foreground) { $this->setForeground($foreground); } if (null !== $background) { $this->setBackground($background); } if (count($options)) { $this->setOptions($options); } } public function setForeground($color = null) { if (null === $color) { $this->foreground = null; return; } if (!isset(static::$availableForegroundColors[$color])) { throw new \InvalidArgumentException(sprintf( 'Invalid foreground color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableForegroundColors)) )); } $this->foreground = static::$availableForegroundColors[$color]; } public function setBackground($color = null) { if (null === $color) { $this->background = null; return; } if (!isset(static::$availableBackgroundColors[$color])) { throw new \InvalidArgumentException(sprintf( 'Invalid background color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableBackgroundColors)) )); } $this->background = static::$availableBackgroundColors[$color]; } public function setOption($option) { if (!isset(static::$availableOptions[$option])) { throw new \InvalidArgumentException(sprintf( 'Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions)) )); } if (false === array_search(static::$availableOptions[$option], $this->options)) { $this->options[] = static::$availableOptions[$option]; } } public function unsetOption($option) { if (!isset(static::$availableOptions[$option])) { throw new \InvalidArgumentException(sprintf( 'Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions)) )); } $pos = array_search(static::$availableOptions[$option], $this->options); if (false !== $pos) { unset($this->options[$pos]); } } public function setOptions(array $options) { $this->options = array(); foreach ($options as $option) { $this->setOption($option); } } public function apply($text) { $codes = array(); if (null !== $this->foreground) { $codes[] = $this->foreground; } if (null !== $this->background) { $codes[] = $this->background; } if (count($this->options)) { $codes = array_merge($codes, $this->options); } return sprintf("\033[%sm%s\033[0m", implode(';', $codes), $text); } } ([^<]*)#is'; private $decorated; private $styles = array(); private $styleStack; public function __construct($decorated = null, array $styles = array()) { $this->decorated = (Boolean) $decorated; $this->setStyle('error', new OutputFormatterStyle('white', 'red')); $this->setStyle('info', new OutputFormatterStyle('green')); $this->setStyle('comment', new OutputFormatterStyle('yellow')); $this->setStyle('question', new OutputFormatterStyle('black', 'cyan')); foreach ($styles as $name => $style) { $this->setStyle($name, $style); } $this->styleStack = new OutputFormatterStyleStack(); } public function setDecorated($decorated) { $this->decorated = (Boolean) $decorated; } public function isDecorated() { return $this->decorated; } public function setStyle($name, OutputFormatterStyleInterface $style) { $this->styles[strtolower($name)] = $style; } public function hasStyle($name) { return isset($this->styles[strtolower($name)]); } public function getStyle($name) { if (!$this->hasStyle($name)) { throw new \InvalidArgumentException('Undefined style: '.$name); } return $this->styles[strtolower($name)]; } public function format($message) { return preg_replace_callback(self::FORMAT_PATTERN, array($this, 'replaceStyle'), $message); } private function replaceStyle($match) { if ('' === $match[2]) { if ('/' === $match[1]) { $this->styleStack->pop(); return $this->applyStyle($this->styleStack->getCurrent(), $match[3]); } return '<>'.$match[3]; } if (isset($this->styles[strtolower($match[2])])) { $style = $this->styles[strtolower($match[2])]; } else { $style = $this->createStyleFromString($match[2]); if (false === $style) { return $match[0]; } } if ('/' === $match[1]) { $this->styleStack->pop($style); } else { $this->styleStack->push($style); } return $this->applyStyle($this->styleStack->getCurrent(), $match[3]); } private function createStyleFromString($string) { if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) { return false; } $style = new OutputFormatterStyle(); foreach ($matches as $match) { array_shift($match); if ('fg' == $match[0]) { $style->setForeground($match[1]); } elseif ('bg' == $match[0]) { $style->setBackground($match[1]); } else { $style->setOption($match[1]); } } return $style; } private function applyStyle(OutputFormatterStyleInterface $style, $text) { return $this->isDecorated() && strlen($text) > 0 ? $style->apply($text) : $text; } } reset(); } public function reset() { $this->styles = array(); } public function push(OutputFormatterStyleInterface $style) { $this->styles[] = $style; } public function pop(OutputFormatterStyleInterface $style = null) { if (empty($this->styles)) { return new OutputFormatterStyle(); } if (null === $style) { return array_pop($this->styles); } foreach (array_reverse($this->styles, true) as $index => $stackedStyle) { if ($style->apply('') === $stackedStyle->apply('')) { $this->styles = array_slice($this->styles, 0, $index); return $stackedStyle; } } throw new \InvalidArgumentException('Incorrectly nested style tag found.'); } public function getCurrent() { if (empty($this->styles)) { return new OutputFormatterStyle(); } return $this->styles[count($this->styles)-1]; } } 7 || $mode < 1) { throw new \InvalidArgumentException(sprintf('Argument mode "%s" is not valid.', $mode)); } $this->name = $name; $this->mode = $mode; $this->description = $description; $this->setDefault($default); } public function getName() { return $this->name; } public function isRequired() { return self::REQUIRED === (self::REQUIRED & $this->mode); } public function isArray() { return self::IS_ARRAY === (self::IS_ARRAY & $this->mode); } public function setDefault($default = null) { if (self::REQUIRED === $this->mode && null !== $default) { throw new \LogicException('Cannot set a default value except for Parameter::OPTIONAL mode.'); } if ($this->isArray()) { if (null === $default) { $default = array(); } elseif (!is_array($default)) { throw new \LogicException('A default value for an array argument must be an array.'); } } $this->default = $default; } public function getDefault() { return $this->default; } public function getDescription() { return $this->description; } } definition = new InputDefinition(); } else { $this->bind($definition); $this->validate(); } } public function bind(InputDefinition $definition) { $this->arguments = array(); $this->options = array(); $this->definition = $definition; $this->parse(); } abstract protected function parse(); public function validate() { if (count($this->arguments) < $this->definition->getArgumentRequiredCount()) { throw new \RuntimeException('Not enough arguments.'); } } public function isInteractive() { return $this->interactive; } public function setInteractive($interactive) { $this->interactive = (Boolean) $interactive; } public function getArguments() { return array_merge($this->definition->getArgumentDefaults(), $this->arguments); } public function getArgument($name) { if (!$this->definition->hasArgument($name)) { throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); } return isset($this->arguments[$name]) ? $this->arguments[$name] : $this->definition->getArgument($name)->getDefault(); } public function setArgument($name, $value) { if (!$this->definition->hasArgument($name)) { throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); } $this->arguments[$name] = $value; } public function hasArgument($name) { return $this->definition->hasArgument($name); } public function getOptions() { return array_merge($this->definition->getOptionDefaults(), $this->options); } public function getOption($name) { if (!$this->definition->hasOption($name)) { throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); } return isset($this->options[$name]) ? $this->options[$name] : $this->definition->getOption($name)->getDefault(); } public function setOption($name, $value) { if (!$this->definition->hasOption($name)) { throw new \InvalidArgumentException(sprintf('The "%s" option does not exist.', $name)); } $this->options[$name] = $value; } public function hasOption($name) { return $this->definition->hasOption($name); } } setDefinition($definition); } public function setDefinition(array $definition) { $arguments = array(); $options = array(); foreach ($definition as $item) { if ($item instanceof InputOption) { $options[] = $item; } else { $arguments[] = $item; } } $this->setArguments($arguments); $this->setOptions($options); } public function setArguments($arguments = array()) { $this->arguments = array(); $this->requiredCount = 0; $this->hasOptional = false; $this->hasAnArrayArgument = false; $this->addArguments($arguments); } public function addArguments($arguments = array()) { if (null !== $arguments) { foreach ($arguments as $argument) { $this->addArgument($argument); } } } public function addArgument(InputArgument $argument) { if (isset($this->arguments[$argument->getName()])) { throw new \LogicException(sprintf('An argument with name "%s" already exist.', $argument->getName())); } if ($this->hasAnArrayArgument) { throw new \LogicException('Cannot add an argument after an array argument.'); } if ($argument->isRequired() && $this->hasOptional) { throw new \LogicException('Cannot add a required argument after an optional one.'); } if ($argument->isArray()) { $this->hasAnArrayArgument = true; } if ($argument->isRequired()) { ++$this->requiredCount; } else { $this->hasOptional = true; } $this->arguments[$argument->getName()] = $argument; } public function getArgument($name) { $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments; if (!$this->hasArgument($name)) { throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); } return $arguments[$name]; } public function hasArgument($name) { $arguments = is_int($name) ? array_values($this->arguments) : $this->arguments; return isset($arguments[$name]); } public function getArguments() { return $this->arguments; } public function getArgumentCount() { return $this->hasAnArrayArgument ? PHP_INT_MAX : count($this->arguments); } public function getArgumentRequiredCount() { return $this->requiredCount; } public function getArgumentDefaults() { $values = array(); foreach ($this->arguments as $argument) { $values[$argument->getName()] = $argument->getDefault(); } return $values; } public function setOptions($options = array()) { $this->options = array(); $this->shortcuts = array(); $this->addOptions($options); } public function addOptions($options = array()) { foreach ($options as $option) { $this->addOption($option); } } public function addOption(InputOption $option) { if (isset($this->options[$option->getName()]) && !$option->equals($this->options[$option->getName()])) { throw new \LogicException(sprintf('An option named "%s" already exist.', $option->getName())); } elseif (isset($this->shortcuts[$option->getShortcut()]) && !$option->equals($this->options[$this->shortcuts[$option->getShortcut()]])) { throw new \LogicException(sprintf('An option with shortcut "%s" already exist.', $option->getShortcut())); } $this->options[$option->getName()] = $option; if ($option->getShortcut()) { $this->shortcuts[$option->getShortcut()] = $option->getName(); } } public function getOption($name) { if (!$this->hasOption($name)) { throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name)); } return $this->options[$name]; } public function hasOption($name) { return isset($this->options[$name]); } public function getOptions() { return $this->options; } public function hasShortcut($name) { return isset($this->shortcuts[$name]); } public function getOptionForShortcut($shortcut) { return $this->getOption($this->shortcutToName($shortcut)); } public function getOptionDefaults() { $values = array(); foreach ($this->options as $option) { $values[$option->getName()] = $option->getDefault(); } return $values; } private function shortcutToName($shortcut) { if (!isset($this->shortcuts[$shortcut])) { throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut)); } return $this->shortcuts[$shortcut]; } public function getSynopsis() { $elements = array(); foreach ($this->getOptions() as $option) { $shortcut = $option->getShortcut() ? sprintf('-%s|', $option->getShortcut()) : ''; $elements[] = sprintf('['.($option->isValueRequired() ? '%s--%s="..."' : ($option->isValueOptional() ? '%s--%s[="..."]' : '%s--%s')).']', $shortcut, $option->getName()); } foreach ($this->getArguments() as $argument) { $elements[] = sprintf($argument->isRequired() ? '%s' : '[%s]', $argument->getName().($argument->isArray() ? '1' : '')); if ($argument->isArray()) { $elements[] = sprintf('... [%sN]', $argument->getName()); } } return implode(' ', $elements); } public function asText() { $max = 0; foreach ($this->getOptions() as $option) { $nameLength = strlen($option->getName()) + 2; if ($option->getShortcut()) { $nameLength += strlen($option->getShortcut()) + 3; } $max = max($max, $nameLength); } foreach ($this->getArguments() as $argument) { $max = max($max, strlen($argument->getName())); } ++$max; $text = array(); if ($this->getArguments()) { $text[] = 'Arguments:'; foreach ($this->getArguments() as $argument) { if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) { $default = sprintf(' (default: %s)', $this->formatDefaultValue($argument->getDefault())); } else { $default = ''; } $description = str_replace("\n", "\n".str_pad('', $max + 2, ' '), $argument->getDescription()); $text[] = sprintf(" %-${max}s %s%s", $argument->getName(), $description, $default); } $text[] = ''; } if ($this->getOptions()) { $text[] = 'Options:'; foreach ($this->getOptions() as $option) { if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) { $default = sprintf(' (default: %s)', $this->formatDefaultValue($option->getDefault())); } else { $default = ''; } $multiple = $option->isArray() ? ' (multiple values allowed)' : ''; $description = str_replace("\n", "\n".str_pad('', $max + 2, ' '), $option->getDescription()); $optionMax = $max - strlen($option->getName()) - 2; $text[] = sprintf(" %s %-${optionMax}s%s%s%s", '--'.$option->getName(), $option->getShortcut() ? sprintf('(-%s) ', $option->getShortcut()) : '', $description, $default, $multiple ); } $text[] = ''; } return implode("\n", $text); } public function asXml($asDom = false) { $dom = new \DOMDocument('1.0', 'UTF-8'); $dom->formatOutput = true; $dom->appendChild($definitionXML = $dom->createElement('definition')); $definitionXML->appendChild($argumentsXML = $dom->createElement('arguments')); foreach ($this->getArguments() as $argument) { $argumentsXML->appendChild($argumentXML = $dom->createElement('argument')); $argumentXML->setAttribute('name', $argument->getName()); $argumentXML->setAttribute('is_required', $argument->isRequired() ? 1 : 0); $argumentXML->setAttribute('is_array', $argument->isArray() ? 1 : 0); $argumentXML->appendChild($descriptionXML = $dom->createElement('description')); $descriptionXML->appendChild($dom->createTextNode($argument->getDescription())); $argumentXML->appendChild($defaultsXML = $dom->createElement('defaults')); $defaults = is_array($argument->getDefault()) ? $argument->getDefault() : (is_bool($argument->getDefault()) ? array(var_export($argument->getDefault(), true)) : ($argument->getDefault() ? array($argument->getDefault()) : array())); foreach ($defaults as $default) { $defaultsXML->appendChild($defaultXML = $dom->createElement('default')); $defaultXML->appendChild($dom->createTextNode($default)); } } $definitionXML->appendChild($optionsXML = $dom->createElement('options')); foreach ($this->getOptions() as $option) { $optionsXML->appendChild($optionXML = $dom->createElement('option')); $optionXML->setAttribute('name', '--'.$option->getName()); $optionXML->setAttribute('shortcut', $option->getShortcut() ? '-'.$option->getShortcut() : ''); $optionXML->setAttribute('accept_value', $option->acceptValue() ? 1 : 0); $optionXML->setAttribute('is_value_required', $option->isValueRequired() ? 1 : 0); $optionXML->setAttribute('is_multiple', $option->isArray() ? 1 : 0); $optionXML->appendChild($descriptionXML = $dom->createElement('description')); $descriptionXML->appendChild($dom->createTextNode($option->getDescription())); if ($option->acceptValue()) { $optionXML->appendChild($defaultsXML = $dom->createElement('defaults')); $defaults = is_array($option->getDefault()) ? $option->getDefault() : (is_bool($option->getDefault()) ? array(var_export($option->getDefault(), true)) : ($option->getDefault() ? array($option->getDefault()) : array())); foreach ($defaults as $default) { $defaultsXML->appendChild($defaultXML = $dom->createElement('default')); $defaultXML->appendChild($dom->createTextNode($default)); } } } return $asDom ? $dom : $dom->saveXml(); } private function formatDefaultValue($default) { if (is_array($default) && $default === array_values($default)) { return sprintf("array('%s')", implode("', '", $default)); } return str_replace("\n", '', var_export($default, true)); } } setTokens($this->tokenize($input)); } private function tokenize($input) { $input = preg_replace('/(\r\n|\r|\n|\t)/', ' ', $input); $tokens = array(); $length = strlen($input); $cursor = 0; while ($cursor < $length) { if (preg_match('/\s+/A', $input, $match, null, $cursor)) { } elseif (preg_match('/([^="\' ]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor)) { $tokens[] = $match[1].$match[2].stripcslashes(str_replace(array('"\'', '\'"', '\'\'', '""'), '', substr($match[3], 1, strlen($match[3]) - 2))); } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, null, $cursor)) { $tokens[] = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2)); } elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, null, $cursor)) { $tokens[] = stripcslashes($match[1]); } else { throw new \InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10))); } $cursor += strlen($match[0]); } return $tokens; } } 15 || $mode < 1) { throw new \InvalidArgumentException(sprintf('Option mode "%s" is not valid.', $mode)); } $this->name = $name; $this->shortcut = $shortcut; $this->mode = $mode; $this->description = $description; if ($this->isArray() && !$this->acceptValue()) { throw new \InvalidArgumentException('Impossible to have an option mode VALUE_IS_ARRAY if the option does not accept a value.'); } $this->setDefault($default); } public function getShortcut() { return $this->shortcut; } public function getName() { return $this->name; } public function acceptValue() { return $this->isValueRequired() || $this->isValueOptional(); } public function isValueRequired() { return self::VALUE_REQUIRED === (self::VALUE_REQUIRED & $this->mode); } public function isValueOptional() { return self::VALUE_OPTIONAL === (self::VALUE_OPTIONAL & $this->mode); } public function isArray() { return self::VALUE_IS_ARRAY === (self::VALUE_IS_ARRAY & $this->mode); } public function setDefault($default = null) { if (self::VALUE_NONE === (self::VALUE_NONE & $this->mode) && null !== $default) { throw new \LogicException('Cannot set a default value when using Option::VALUE_NONE mode.'); } if ($this->isArray()) { if (null === $default) { $default = array(); } elseif (!is_array($default)) { throw new \LogicException('A default value for an array option must be an array.'); } } $this->default = $this->acceptValue() ? $default : false; } public function getDefault() { return $this->default; } public function getDescription() { return $this->description; } public function equals(InputOption $option) { return $option->getName() === $this->getName() && $option->getShortcut() === $this->getShortcut() && $option->getDefault() === $this->getDefault() && $option->isArray() === $this->isArray() && $option->isValueRequired() === $this->isValueRequired() && $option->isValueOptional() === $this->isValueOptional() ; } } parameters = $parameters; parent::__construct($definition); } public function getFirstArgument() { foreach ($this->parameters as $key => $value) { if ($key && '-' === $key[0]) { continue; } return $value; } } public function hasParameterOption($values) { $values = (array) $values; foreach ($this->parameters as $k => $v) { if (!is_int($k)) { $v = $k; } if (in_array($v, $values)) { return true; } } return false; } public function getParameterOption($values, $default = false) { $values = (array) $values; foreach ($this->parameters as $k => $v) { if (is_int($k) && in_array($v, $values)) { return true; } elseif (in_array($k, $values)) { return $v; } } return $default; } protected function parse() { foreach ($this->parameters as $key => $value) { if (0 === strpos($key, '--')) { $this->addLongOption(substr($key, 2), $value); } elseif ('-' === $key[0]) { $this->addShortOption(substr($key, 1), $value); } else { $this->addArgument($key, $value); } } } private function addShortOption($shortcut, $value) { if (!$this->definition->hasShortcut($shortcut)) { throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut)); } $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); } private function addLongOption($name, $value) { if (!$this->definition->hasOption($name)) { throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name)); } $option = $this->definition->getOption($name); if (null === $value) { if ($option->isValueRequired()) { throw new \InvalidArgumentException(sprintf('The "--%s" option requires a value.', $name)); } $value = $option->isValueOptional() ? $option->getDefault() : true; } $this->options[$name] = $value; } private function addArgument($name, $value) { if (!$this->definition->hasArgument($name)) { throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name)); } $this->arguments[$name] = $value; } } tokens = $argv; parent::__construct($definition); } protected function setTokens(array $tokens) { $this->tokens = $tokens; } protected function parse() { $parseOptions = true; $this->parsed = $this->tokens; while (null !== $token = array_shift($this->parsed)) { if ($parseOptions && '' == $token) { $this->parseArgument($token); } elseif ($parseOptions && '--' == $token) { $parseOptions = false; } elseif ($parseOptions && 0 === strpos($token, '--')) { $this->parseLongOption($token); } elseif ($parseOptions && '-' === $token[0]) { $this->parseShortOption($token); } else { $this->parseArgument($token); } } } private function parseShortOption($token) { $name = substr($token, 1); if (strlen($name) > 1) { if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) { $this->addShortOption($name[0], substr($name, 1)); } else { $this->parseShortOptionSet($name); } } else { $this->addShortOption($name, null); } } private function parseShortOptionSet($name) { $len = strlen($name); for ($i = 0; $i < $len; $i++) { if (!$this->definition->hasShortcut($name[$i])) { throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $name[$i])); } $option = $this->definition->getOptionForShortcut($name[$i]); if ($option->acceptValue()) { $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1)); break; } else { $this->addLongOption($option->getName(), true); } } } private function parseLongOption($token) { $name = substr($token, 2); if (false !== $pos = strpos($name, '=')) { $this->addLongOption(substr($name, 0, $pos), substr($name, $pos + 1)); } else { $this->addLongOption($name, null); } } private function parseArgument($token) { $c = count($this->arguments); if ($this->definition->hasArgument($c)) { $arg = $this->definition->getArgument($c); $this->arguments[$arg->getName()] = $arg->isArray()? array($token) : $token; } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) { $arg = $this->definition->getArgument($c - 1); $this->arguments[$arg->getName()][] = $token; } else { throw new \RuntimeException('Too many arguments.'); } } private function addShortOption($shortcut, $value) { if (!$this->definition->hasShortcut($shortcut)) { throw new \RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut)); } $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); } private function addLongOption($name, $value) { if (!$this->definition->hasOption($name)) { throw new \RuntimeException(sprintf('The "--%s" option does not exist.', $name)); } $option = $this->definition->getOption($name); if (null === $value && $option->acceptValue()) { $next = array_shift($this->parsed); if ('-' !== $next[0]) { $value = $next; } else { array_unshift($this->parsed, $next); } } if (null === $value) { if ($option->isValueRequired()) { throw new \RuntimeException(sprintf('The "--%s" option requires a value.', $name)); } $value = $option->isValueOptional() ? $option->getDefault() : true; } if ($option->isArray()) { $this->options[$name][] = $value; } else { $this->options[$name] = $value; } } public function getFirstArgument() { foreach ($this->tokens as $token) { if ($token && '-' === $token[0]) { continue; } return $token; } } public function hasParameterOption($values) { $values = (array) $values; foreach ($this->tokens as $v) { if (in_array($v, $values)) { return true; } } return false; } public function getParameterOption($values, $default = false) { $values = (array) $values; $tokens = $this->tokens; while ($token = array_shift($tokens)) { foreach ($values as $value) { if (0 === strpos($token, $value)) { if (false !== $pos = strpos($token, '=')) { return substr($token, $pos + 1); } return array_shift($tokens); } } } return $default; } } definition = new InputDefinition(); $this->ignoreValidationErrors = false; $this->applicationDefinitionMerged = false; $this->aliases = array(); if (null !== $name) { $this->setName($name); } $this->configure(); if (!$this->name) { throw new \LogicException('The command name cannot be empty.'); } } public function ignoreValidationErrors() { $this->ignoreValidationErrors = true; } public function setApplication(Application $application = null) { $this->application = $application; if ($application) { $this->setHelperSet($application->getHelperSet()); } else { $this->helperSet = null; } } public function setHelperSet(HelperSet $helperSet) { $this->helperSet = $helperSet; } public function getHelperSet() { return $this->helperSet; } public function getApplication() { return $this->application; } public function isEnabled() { return true; } protected function configure() { } protected function execute(InputInterface $input, OutputInterface $output) { throw new \LogicException('You must override the execute() method in the concrete command class.'); } protected function interact(InputInterface $input, OutputInterface $output) { } protected function initialize(InputInterface $input, OutputInterface $output) { } public function run(InputInterface $input, OutputInterface $output) { $this->getSynopsis(); $this->mergeApplicationDefinition(); try { $input->bind($this->definition); } catch (\Exception $e) { if (!$this->ignoreValidationErrors) { throw $e; } } $this->initialize($input, $output); if ($input->isInteractive()) { $this->interact($input, $output); } $input->validate(); if ($this->code) { return call_user_func($this->code, $input, $output); } return $this->execute($input, $output); } public function setCode(\Closure $code) { $this->code = $code; return $this; } private function mergeApplicationDefinition() { if (null === $this->application || true === $this->applicationDefinitionMerged) { return; } $currentArguments = $this->definition->getArguments(); $this->definition->setArguments($this->application->getDefinition()->getArguments()); $this->definition->addArguments($currentArguments); $this->definition->addOptions($this->application->getDefinition()->getOptions()); $this->applicationDefinitionMerged = true; } public function setDefinition($definition) { if ($definition instanceof InputDefinition) { $this->definition = $definition; } else { $this->definition->setDefinition($definition); } $this->applicationDefinitionMerged = false; return $this; } public function getDefinition() { return $this->definition; } protected function getNativeDefinition() { return $this->getDefinition(); } public function addArgument($name, $mode = null, $description = '', $default = null) { $this->definition->addArgument(new InputArgument($name, $mode, $description, $default)); return $this; } public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null) { $this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default)); return $this; } public function setName($name) { $this->validateName($name); $this->name = $name; return $this; } public function getName() { return $this->name; } public function setDescription($description) { $this->description = $description; return $this; } public function getDescription() { return $this->description; } public function setHelp($help) { $this->help = $help; return $this; } public function getHelp() { return $this->help; } public function getProcessedHelp() { $name = $this->name; $placeholders = array( '%command.name%', '%command.full_name%' ); $replacements = array( $name, $_SERVER['PHP_SELF'].' '.$name ); return str_replace($placeholders, $replacements, $this->getHelp()); } public function setAliases($aliases) { foreach ($aliases as $alias) { $this->validateName($alias); } $this->aliases = $aliases; return $this; } public function getAliases() { return $this->aliases; } public function getSynopsis() { if (null === $this->synopsis) { $this->synopsis = trim(sprintf('%s %s', $this->name, $this->definition->getSynopsis())); } return $this->synopsis; } public function getHelper($name) { return $this->helperSet->get($name); } public function asText() { $messages = array( 'Usage:', ' '.$this->getSynopsis(), '', ); if ($this->getAliases()) { $messages[] = 'Aliases: '.implode(', ', $this->getAliases()).''; } $messages[] = $this->getNativeDefinition()->asText(); if ($help = $this->getProcessedHelp()) { $messages[] = 'Help:'; $messages[] = ' '.str_replace("\n", "\n ", $help)."\n"; } return implode("\n", $messages); } public function asXml($asDom = false) { $dom = new \DOMDocument('1.0', 'UTF-8'); $dom->formatOutput = true; $dom->appendChild($commandXML = $dom->createElement('command')); $commandXML->setAttribute('id', $this->name); $commandXML->setAttribute('name', $this->name); $commandXML->appendChild($usageXML = $dom->createElement('usage')); $usageXML->appendChild($dom->createTextNode(sprintf($this->getSynopsis(), ''))); $commandXML->appendChild($descriptionXML = $dom->createElement('description')); $descriptionXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $this->getDescription()))); $commandXML->appendChild($helpXML = $dom->createElement('help')); $helpXML->appendChild($dom->createTextNode(str_replace("\n", "\n ", $this->getProcessedHelp()))); $commandXML->appendChild($aliasesXML = $dom->createElement('aliases')); foreach ($this->getAliases() as $alias) { $aliasesXML->appendChild($aliasXML = $dom->createElement('alias')); $aliasXML->appendChild($dom->createTextNode($alias)); } $definition = $this->getNativeDefinition()->asXml(true); $commandXML->appendChild($dom->importNode($definition->getElementsByTagName('arguments')->item(0), true)); $commandXML->appendChild($dom->importNode($definition->getElementsByTagName('options')->item(0), true)); return $asDom ? $dom : $dom->saveXml(); } private function validateName($name) { if (!preg_match('/^[^\:]+(\:[^\:]+)*$/', $name)) { throw new \InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name)); } } } setName('list') ->setDefinition($this->createDefinition()) ->setDescription('Lists commands') ->setHelp(<<%command.name% command lists all commands: php %command.full_name% You can also display the commands for a specific namespace: php %command.full_name% test You can also output the information as XML by using the --xml option: php %command.full_name% --xml It's also possible to get raw list of commands (useful for embedding command runner): php %command.full_name% --raw EOF ) ; } protected function getNativeDefinition() { return $this->createDefinition(); } protected function execute(InputInterface $input, OutputInterface $output) { if ($input->getOption('xml')) { $output->writeln($this->getApplication()->asXml($input->getArgument('namespace')), OutputInterface::OUTPUT_RAW); } else { $output->writeln($this->getApplication()->asText($input->getArgument('namespace'), $input->getOption('raw'))); } } private function createDefinition() { return new InputDefinition(array( new InputArgument('namespace', InputArgument::OPTIONAL, 'The namespace name'), new InputOption('xml', null, InputOption::VALUE_NONE, 'To output help as XML'), new InputOption('raw', null, InputOption::VALUE_NONE, 'To output raw command list'), )); } } ignoreValidationErrors(); $this ->setName('help') ->setDefinition(array( new InputArgument('command_name', InputArgument::OPTIONAL, 'The command name', 'help'), new InputOption('xml', null, InputOption::VALUE_NONE, 'To output help as XML'), )) ->setDescription('Displays help for a command') ->setHelp(<<%command.name% command displays help for a given command: php %command.full_name% list You can also output the help as XML by using the --xml option: php %command.full_name% --xml list EOF ) ; } public function setCommand(Command $command) { $this->command = $command; } protected function execute(InputInterface $input, OutputInterface $output) { if (null === $this->command) { $this->command = $this->getApplication()->get($input->getArgument('command_name')); } if ($input->getOption('xml')) { $output->writeln($this->command->asXml(), OutputInterface::OUTPUT_RAW); } else { $output->writeln($this->command->asText()); } $this->command = null; } } command = $command; } public function execute(array $input, array $options = array()) { $this->input = new ArrayInput($input); if (isset($options['interactive'])) { $this->input->setInteractive($options['interactive']); } $this->output = new StreamOutput(fopen('php://memory', 'w', false)); if (isset($options['decorated'])) { $this->output->setDecorated($options['decorated']); } if (isset($options['verbosity'])) { $this->output->setVerbosity($options['verbosity']); } return $this->command->run($this->input, $this->output); } public function getDisplay() { rewind($this->output->getStream()); return stream_get_contents($this->output->getStream()); } public function getInput() { return $this->input; } public function getOutput() { return $this->output; } } application = $application; } public function run(array $input, $options = array()) { $this->input = new ArrayInput($input); if (isset($options['interactive'])) { $this->input->setInteractive($options['interactive']); } $this->output = new StreamOutput(fopen('php://memory', 'w', false)); if (isset($options['decorated'])) { $this->output->setDecorated($options['decorated']); } if (isset($options['verbosity'])) { $this->output->setVerbosity($options['verbosity']); } return $this->application->run($this->input, $this->output); } public function getDisplay() { rewind($this->output->getStream()); return stream_get_contents($this->output->getStream()); } public function getInput() { return $this->input; } public function getOutput() { return $this->output; } } hasReadline = function_exists('readline'); $this->application = $application; $this->history = getenv('HOME').'/.history_'.$application->getName(); $this->output = new ConsoleOutput(); $this->prompt = $application->getName().' > '; $this->processIsolation = false; } public function run() { $this->application->setAutoExit(false); $this->application->setCatchExceptions(true); if ($this->hasReadline) { readline_read_history($this->history); readline_completion_function(array($this, 'autocompleter')); } $this->output->writeln($this->getHeader()); $php = null; if ($this->processIsolation) { $finder = new PhpExecutableFinder(); $php = $finder->find(); $this->output->writeln(<<Running with process isolation, you should consider this: * each command is executed as separate process, * commands don't support interactivity, all params must be passed explicitly, * commands output is not colorized. EOF ); } while (true) { $command = $this->readline(); if (false === $command) { $this->output->writeln("\n"); break; } if ($this->hasReadline) { readline_add_history($command); readline_write_history($this->history); } if ($this->processIsolation) { $pb = new ProcessBuilder(); $process = $pb ->add($php) ->add($_SERVER['argv'][0]) ->add($command) ->inheritEnvironmentVariables(true) ->getProcess() ; $output = $this->output; $process->run(function($type, $data) use ($output) { $output->writeln($data); }); $ret = $process->getExitCode(); } else { $ret = $this->application->run(new StringInput($command), $this->output); } if (0 !== $ret) { $this->output->writeln(sprintf('The command terminated with an error status (%s)', $ret)); } } } protected function getHeader() { return <<{$this->application->getName()} shell ({$this->application->getVersion()}). At the prompt, type help for some help, or list to get a list of available commands. To exit the shell, type ^D. EOF; } private function autocompleter($text) { $info = readline_info(); $text = substr($info['line_buffer'], 0, $info['end']); if ($info['point'] !== $info['end']) { return true; } if (false === strpos($text, ' ') || !$text) { return array_keys($this->application->all()); } try { $command = $this->application->find(substr($text, 0, strpos($text, ' '))); } catch (\Exception $e) { return true; } $list = array('--help'); foreach ($command->getDefinition()->getOptions() as $option) { $list[] = '--'.$option->getName(); } return $list; } private function readline() { if ($this->hasReadline) { $line = readline($this->prompt); } else { $this->output->write($this->prompt); $line = fgets(STDIN, 1024); $line = (!$line && strlen($line) == 0) ? false : rtrim($line); } return $line; } public function getProcessIsolation() { return $this->processIsolation; } public function setProcessIsolation($processIsolation) { $this->processIsolation = (Boolean) $processIsolation; } } name = $name; $this->version = $version; $this->catchExceptions = true; $this->autoExit = true; $this->commands = array(); $this->helperSet = $this->getDefaultHelperSet(); $this->definition = $this->getDefaultInputDefinition(); foreach ($this->getDefaultCommands() as $command) { $this->add($command); } } public function run(InputInterface $input = null, OutputInterface $output = null) { if (null === $input) { $input = new ArgvInput(); } if (null === $output) { $output = new ConsoleOutput(); } try { $statusCode = $this->doRun($input, $output); } catch (\Exception $e) { if (!$this->catchExceptions) { throw $e; } if ($output instanceof ConsoleOutputInterface) { $this->renderException($e, $output->getErrorOutput()); } else { $this->renderException($e, $output); } $statusCode = $e->getCode(); $statusCode = is_numeric($statusCode) && $statusCode ? $statusCode : 1; } if ($this->autoExit) { if ($statusCode > 255) { $statusCode = 255; } exit($statusCode); } return $statusCode; } public function doRun(InputInterface $input, OutputInterface $output) { $name = $this->getCommandName($input); if (true === $input->hasParameterOption(array('--ansi'))) { $output->setDecorated(true); } elseif (true === $input->hasParameterOption(array('--no-ansi'))) { $output->setDecorated(false); } if (true === $input->hasParameterOption(array('--help', '-h'))) { if (!$name) { $name = 'help'; $input = new ArrayInput(array('command' => 'help')); } else { $this->wantHelps = true; } } if (true === $input->hasParameterOption(array('--no-interaction', '-n'))) { $input->setInteractive(false); } if (function_exists('posix_isatty') && $this->getHelperSet()->has('dialog')) { $inputStream = $this->getHelperSet()->get('dialog')->getInputStream(); if (!posix_isatty($inputStream)) { $input->setInteractive(false); } } if (true === $input->hasParameterOption(array('--quiet', '-q'))) { $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); } elseif (true === $input->hasParameterOption(array('--verbose', '-v'))) { $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); } if (true === $input->hasParameterOption(array('--version', '-V'))) { $output->writeln($this->getLongVersion()); return 0; } if (!$name) { $name = 'list'; $input = new ArrayInput(array('command' => 'list')); } $command = $this->find($name); $this->runningCommand = $command; $statusCode = $command->run($input, $output); $this->runningCommand = null; return is_numeric($statusCode) ? $statusCode : 0; } public function setHelperSet(HelperSet $helperSet) { $this->helperSet = $helperSet; } public function getHelperSet() { return $this->helperSet; } public function getDefinition() { return $this->definition; } public function getHelp() { $messages = array( $this->getLongVersion(), '', 'Usage:', sprintf(" [options] command [arguments]\n"), 'Options:', ); foreach ($this->getDefinition()->getOptions() as $option) { $messages[] = sprintf(' %-29s %s %s', '--'.$option->getName().'', $option->getShortcut() ? '-'.$option->getShortcut().'' : ' ', $option->getDescription() ); } return implode(PHP_EOL, $messages); } public function setCatchExceptions($boolean) { $this->catchExceptions = (Boolean) $boolean; } public function setAutoExit($boolean) { $this->autoExit = (Boolean) $boolean; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function getVersion() { return $this->version; } public function setVersion($version) { $this->version = $version; } public function getLongVersion() { if ('UNKNOWN' !== $this->getName() && 'UNKNOWN' !== $this->getVersion()) { return sprintf('%s version %s', $this->getName(), $this->getVersion()); } return 'Console Tool'; } public function register($name) { return $this->add(new Command($name)); } public function addCommands(array $commands) { foreach ($commands as $command) { $this->add($command); } } public function add(Command $command) { $command->setApplication($this); if (!$command->isEnabled()) { $command->setApplication(null); return; } $this->commands[$command->getName()] = $command; foreach ($command->getAliases() as $alias) { $this->commands[$alias] = $command; } return $command; } public function get($name) { if (!isset($this->commands[$name])) { throw new \InvalidArgumentException(sprintf('The command "%s" does not exist.', $name)); } $command = $this->commands[$name]; if ($this->wantHelps) { $this->wantHelps = false; $helpCommand = $this->get('help'); $helpCommand->setCommand($command); return $helpCommand; } return $command; } public function has($name) { return isset($this->commands[$name]); } public function getNamespaces() { $namespaces = array(); foreach ($this->commands as $command) { $namespaces[] = $this->extractNamespace($command->getName()); foreach ($command->getAliases() as $alias) { $namespaces[] = $this->extractNamespace($alias); } } return array_values(array_unique(array_filter($namespaces))); } public function findNamespace($namespace) { $allNamespaces = array(); foreach ($this->getNamespaces() as $n) { $allNamespaces[$n] = explode(':', $n); } $found = array(); foreach (explode(':', $namespace) as $i => $part) { $abbrevs = static::getAbbreviations(array_unique(array_values(array_filter(array_map(function ($p) use ($i) { return isset($p[$i]) ? $p[$i] : ''; }, $allNamespaces))))); if (!isset($abbrevs[$part])) { $message = sprintf('There are no commands defined in the "%s" namespace.', $namespace); if (1 <= $i) { $part = implode(':', $found).':'.$part; } if ($alternatives = $this->findAlternativeNamespace($part, $abbrevs)) { $message .= "\n\nDid you mean one of these?\n "; $message .= implode("\n ", $alternatives); } throw new \InvalidArgumentException($message); } if (count($abbrevs[$part]) > 1) { throw new \InvalidArgumentException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions($abbrevs[$part]))); } $found[] = $abbrevs[$part][0]; } return implode(':', $found); } public function find($name) { $namespace = ''; $searchName = $name; if (false !== $pos = strrpos($name, ':')) { $namespace = $this->findNamespace(substr($name, 0, $pos)); $searchName = $namespace.substr($name, $pos); } $commands = array(); foreach ($this->commands as $command) { if ($this->extractNamespace($command->getName()) == $namespace) { $commands[] = $command->getName(); } } $abbrevs = static::getAbbreviations(array_unique($commands)); if (isset($abbrevs[$searchName]) && 1 == count($abbrevs[$searchName])) { return $this->get($abbrevs[$searchName][0]); } if (isset($abbrevs[$searchName]) && count($abbrevs[$searchName]) > 1) { $suggestions = $this->getAbbreviationSuggestions($abbrevs[$searchName]); throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $name, $suggestions)); } $aliases = array(); foreach ($this->commands as $command) { foreach ($command->getAliases() as $alias) { if ($this->extractNamespace($alias) == $namespace) { $aliases[] = $alias; } } } $aliases = static::getAbbreviations(array_unique($aliases)); if (!isset($aliases[$searchName])) { $message = sprintf('Command "%s" is not defined.', $name); if ($alternatives = $this->findAlternativeCommands($searchName, $abbrevs)) { $message .= "\n\nDid you mean one of these?\n "; $message .= implode("\n ", $alternatives); } throw new \InvalidArgumentException($message); } if (count($aliases[$searchName]) > 1) { throw new \InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $name, $this->getAbbreviationSuggestions($aliases[$searchName]))); } return $this->get($aliases[$searchName][0]); } public function all($namespace = null) { if (null === $namespace) { return $this->commands; } $commands = array(); foreach ($this->commands as $name => $command) { if ($namespace === $this->extractNamespace($name, substr_count($namespace, ':') + 1)) { $commands[$name] = $command; } } return $commands; } static public function getAbbreviations($names) { $abbrevs = array(); foreach ($names as $name) { for ($len = strlen($name) - 1; $len > 0; --$len) { $abbrev = substr($name, 0, $len); if (!isset($abbrevs[$abbrev])) { $abbrevs[$abbrev] = array($name); } else { $abbrevs[$abbrev][] = $name; } } } foreach ($names as $name) { $abbrevs[$name] = array($name); } return $abbrevs; } public function asText($namespace = null, $raw = false) { $commands = $namespace ? $this->all($this->findNamespace($namespace)) : $this->commands; $width = 0; foreach ($commands as $command) { $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width; } $width += 2; if ($raw) { $messages = array(); foreach ($this->sortCommands($commands) as $space => $commands) { foreach ($commands as $name => $command) { $messages[] = sprintf("%-${width}s %s", $name, $command->getDescription()); } } return implode(PHP_EOL, $messages); } $messages = array($this->getHelp(), ''); if ($namespace) { $messages[] = sprintf("Available commands for the \"%s\" namespace:", $namespace); } else { $messages[] = 'Available commands:'; } foreach ($this->sortCommands($commands) as $space => $commands) { if (!$namespace && '_global' !== $space) { $messages[] = ''.$space.''; } foreach ($commands as $name => $command) { $messages[] = sprintf(" %-${width}s %s", $name, $command->getDescription()); } } return implode(PHP_EOL, $messages); } public function asXml($namespace = null, $asDom = false) { $commands = $namespace ? $this->all($this->findNamespace($namespace)) : $this->commands; $dom = new \DOMDocument('1.0', 'UTF-8'); $dom->formatOutput = true; $dom->appendChild($xml = $dom->createElement('symfony')); $xml->appendChild($commandsXML = $dom->createElement('commands')); if ($namespace) { $commandsXML->setAttribute('namespace', $namespace); } else { $namespacesXML = $dom->createElement('namespaces'); $xml->appendChild($namespacesXML); } foreach ($this->sortCommands($commands) as $space => $commands) { if (!$namespace) { $namespaceArrayXML = $dom->createElement('namespace'); $namespacesXML->appendChild($namespaceArrayXML); $namespaceArrayXML->setAttribute('id', $space); } foreach ($commands as $name => $command) { if ($name !== $command->getName()) { continue; } if (!$namespace) { $commandXML = $dom->createElement('command'); $namespaceArrayXML->appendChild($commandXML); $commandXML->appendChild($dom->createTextNode($name)); } $node = $command->asXml(true)->getElementsByTagName('command')->item(0); $node = $dom->importNode($node, true); $commandsXML->appendChild($node); } } return $asDom ? $dom : $dom->saveXml(); } public function renderException($e, $output) { $strlen = function ($string) { if (!function_exists('mb_strlen')) { return strlen($string); } if (false === $encoding = mb_detect_encoding($string)) { return strlen($string); } return mb_strlen($string, $encoding); }; do { $title = sprintf(' [%s] ', get_class($e)); $len = $strlen($title); $width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX; $lines = array(); foreach (preg_split("{\r?\n}", $e->getMessage()) as $line) { foreach (str_split($line, $width - 4) as $line) { $lines[] = sprintf(' %s ', $line); $len = max($strlen($line) + 4, $len); } } $messages = array(str_repeat(' ', $len), $title.str_repeat(' ', max(0, $len - $strlen($title)))); foreach ($lines as $line) { $messages[] = $line.str_repeat(' ', $len - $strlen($line)); } $messages[] = str_repeat(' ', $len); $output->writeln(""); $output->writeln(""); foreach ($messages as $message) { $output->writeln(''.$message.''); } $output->writeln(""); $output->writeln(""); if (OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity()) { $output->writeln('Exception trace:'); $trace = $e->getTrace(); array_unshift($trace, array( 'function' => '', 'file' => $e->getFile() != null ? $e->getFile() : 'n/a', 'line' => $e->getLine() != null ? $e->getLine() : 'n/a', 'args' => array(), )); for ($i = 0, $count = count($trace); $i < $count; $i++) { $class = isset($trace[$i]['class']) ? $trace[$i]['class'] : ''; $type = isset($trace[$i]['type']) ? $trace[$i]['type'] : ''; $function = $trace[$i]['function']; $file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a'; $line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a'; $output->writeln(sprintf(' %s%s%s() at %s:%s', $class, $type, $function, $file, $line)); } $output->writeln(""); $output->writeln(""); } } while ($e = $e->getPrevious()); if (null !== $this->runningCommand) { $output->writeln(sprintf('%s', sprintf($this->runningCommand->getSynopsis(), $this->getName()))); $output->writeln(""); $output->writeln(""); } } protected function getTerminalWidth() { if (defined('PHP_WINDOWS_VERSION_BUILD') && $ansicon = getenv('ANSICON')) { return preg_replace('{^(\d+)x.*$}', '$1', $ansicon); } if (preg_match("{rows.(\d+);.columns.(\d+);}i", $this->getSttyColumns(), $match)) { return $match[1]; } } protected function getTerminalHeight() { if (defined('PHP_WINDOWS_VERSION_BUILD') && $ansicon = getenv('ANSICON')) { return preg_replace('{^\d+x\d+ \(\d+x(\d+)\)$}', '$1', trim($ansicon)); } if (preg_match("{rows.(\d+);.columns.(\d+);}i", $this->getSttyColumns(), $match)) { return $match[2]; } } protected function getCommandName(InputInterface $input) { return $input->getFirstArgument('command'); } protected function getDefaultInputDefinition() { return new InputDefinition(array( new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'), new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message.'), new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message.'), new InputOption('--verbose', '-v', InputOption::VALUE_NONE, 'Increase verbosity of messages.'), new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version.'), new InputOption('--ansi', '', InputOption::VALUE_NONE, 'Force ANSI output.'), new InputOption('--no-ansi', '', InputOption::VALUE_NONE, 'Disable ANSI output.'), new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question.'), )); } protected function getDefaultCommands() { return array(new HelpCommand(), new ListCommand()); } protected function getDefaultHelperSet() { return new HelperSet(array( new FormatterHelper(), new DialogHelper(), )); } private function getSttyColumns() { $descriptorspec = array(1 => array('pipe', 'w'), 2 => array('pipe', 'w')); $process = proc_open('stty -a | grep columns', $descriptorspec, $pipes, null, null, array('suppress_errors' => true)); if (is_resource($process)) { $info = stream_get_contents($pipes[1]); fclose($pipes[1]); fclose($pipes[2]); proc_close($process); return $info; } } private function sortCommands($commands) { $namespacedCommands = array(); foreach ($commands as $name => $command) { $key = $this->extractNamespace($name, 1); if (!$key) { $key = '_global'; } $namespacedCommands[$key][$name] = $command; } ksort($namespacedCommands); foreach ($namespacedCommands as &$commands) { ksort($commands); } return $namespacedCommands; } private function getAbbreviationSuggestions($abbrevs) { return sprintf('%s, %s%s', $abbrevs[0], $abbrevs[1], count($abbrevs) > 2 ? sprintf(' and %d more', count($abbrevs) - 2) : ''); } private function extractNamespace($name, $limit = null) { $parts = explode(':', $name); array_pop($parts); return implode(':', null === $limit ? $parts : array_slice($parts, 0, $limit)); } private function findAlternativeCommands($name, $abbrevs) { $callback = function($item) { return $item->getName(); }; return $this->findAlternatives($name, $this->commands, $abbrevs, $callback); } private function findAlternativeNamespace($name, $abbrevs) { return $this->findAlternatives($name, $this->getNamespaces(), $abbrevs); } private function findAlternatives($name, $collection, $abbrevs, $callback = null) { $alternatives = array(); foreach ($collection as $item) { if (null !== $callback) { $item = call_user_func($callback, $item); } $lev = levenshtein($name, $item); if ($lev <= strlen($name) / 3 || false !== strpos($item, $name)) { $alternatives[$item] = $lev; } } if (!$alternatives) { foreach ($abbrevs as $key => $values) { $lev = levenshtein($name, $key); if ($lev <= strlen($name) / 3 || false !== strpos($key, $name)) { foreach ($values as $value) { $alternatives[$value] = $lev; } } } } asort($alternatives); return array_keys($alternatives); } } verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity; $this->formatter = null === $formatter ? new OutputFormatter() : $formatter; $this->formatter->setDecorated((Boolean) $decorated); } public function setFormatter(OutputFormatterInterface $formatter) { $this->formatter = $formatter; } public function getFormatter() { return $this->formatter; } public function setDecorated($decorated) { $this->formatter->setDecorated((Boolean) $decorated); } public function isDecorated() { return $this->formatter->isDecorated(); } public function setVerbosity($level) { $this->verbosity = (int) $level; } public function getVerbosity() { return $this->verbosity; } public function writeln($messages, $type = 0) { $this->write($messages, true, $type); } public function write($messages, $newline = false, $type = 0) { if (self::VERBOSITY_QUIET === $this->verbosity) { return; } $messages = (array) $messages; foreach ($messages as $message) { switch ($type) { case OutputInterface::OUTPUT_NORMAL: $message = $this->formatter->format($message); break; case OutputInterface::OUTPUT_RAW: break; case OutputInterface::OUTPUT_PLAIN: $message = strip_tags($this->formatter->format($message)); break; default: throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type)); } $this->doWrite($message, $newline); } } abstract public function doWrite($message, $newline); } stream = $stream; if (null === $decorated) { $decorated = $this->hasColorSupport($decorated); } parent::__construct($verbosity, $decorated, $formatter); } public function getStream() { return $this->stream; } public function doWrite($message, $newline) { if (false === @fwrite($this->stream, $message.($newline ? PHP_EOL : ''))) { throw new \RuntimeException('Unable to write output.'); } fflush($this->stream); } protected function hasColorSupport() { if (DIRECTORY_SEPARATOR == '\\') { return false !== getenv('ANSICON'); } return function_exists('posix_isatty') && @posix_isatty($this->stream); } } hasStdoutSupport()) { $outputStream = 'php://output'; } parent::__construct(fopen($outputStream, 'w'), $verbosity, $decorated, $formatter); $this->stderr = new StreamOutput(fopen('php://stderr', 'w'), $verbosity, $decorated, $formatter); } public function setDecorated($decorated) { parent::setDecorated($decorated); $this->stderr->setDecorated($decorated); } public function setFormatter(OutputFormatterInterface $formatter) { parent::setFormatter($formatter); $this->stderr->setFormatter($formatter); } public function setVerbosity($level) { parent::setVerbosity($level); $this->stderr->setVerbosity($level); } public function getErrorOutput() { return $this->stderr; } public function setErrorOutput(OutputInterface $error) { $this->stderr = $error; } protected function hasStdoutSupport() { return ('OS400' != php_uname('s')); } } getFilename(); foreach ($this->noMatchRegexps as $regex) { if (preg_match($regex, $filename)) { return false; } } $match = true; if ($this->matchRegexps) { $match = false; foreach ($this->matchRegexps as $regex) { if (preg_match($regex, $filename)) { return true; } } } return $match; } protected function toRegex($str) { return $this->isRegex($str) ? $str : Glob::toRegex($str); } } iterator = $iterator; if (self::SORT_BY_NAME === $sort) { $this->sort = function ($a, $b) { return strcmp($a->getRealpath(), $b->getRealpath()); }; } elseif (self::SORT_BY_TYPE === $sort) { $this->sort = function ($a, $b) { if ($a->isDir() && $b->isFile()) { return -1; } elseif ($a->isFile() && $b->isDir()) { return 1; } return strcmp($a->getRealpath(), $b->getRealpath()); }; } elseif (self::SORT_BY_ACCESSED_TIME === $sort) { $this->sort = function ($a, $b) { return ($a->getATime() > $b->getATime()); }; } elseif (self::SORT_BY_CHANGED_TIME === $sort) { $this->sort = function ($a, $b) { return ($a->getCTime() > $b->getCTime()); }; } elseif (self::SORT_BY_MODIFIED_TIME === $sort) { $this->sort = function ($a, $b) { return ($a->getMTime() > $b->getMTime()); }; } elseif (is_callable($sort)) { $this->sort = $sort; } else { throw new \InvalidArgumentException('The SortableIterator takes a PHP callback or a valid built-in sort algorithm as an argument.'); } } public function getIterator() { $array = iterator_to_array($this->iterator, true); uasort($array, $this->sort); return new \ArrayIterator($array); } } comparators = $comparators; parent::__construct($iterator); } public function accept() { $fileinfo = $this->current(); if (!$fileinfo->isFile()) { return true; } $filedate = $fileinfo->getMTime(); foreach ($this->comparators as $compare) { if (!$compare->test($filedate)) { return false; } } return true; } } getPathname(), $this->getSubPath(), $this->getSubPathname()); } } filters = $filters; parent::__construct($iterator); } public function accept() { $fileinfo = $this->current(); foreach ($this->filters as $filter) { if (false === call_user_func($filter, $fileinfo)) { return false; } } return true; } } getOperator()) { case '>': $minDepth = $comparator->getTarget() + 1; break; case '>=': $minDepth = $comparator->getTarget(); break; case '<': $maxDepth = $comparator->getTarget() - 1; break; case '<=': $maxDepth = $comparator->getTarget(); break; default: $minDepth = $maxDepth = $comparator->getTarget(); } } $this->minDepth = $minDepth; $iterator->setMaxDepth(INF === $maxDepth ? -1 : $maxDepth); parent::__construct($iterator); } public function accept() { return $this->getInnerIterator()->getDepth() >= $this->minDepth; } } mode = $mode; parent::__construct($iterator); } public function accept() { if (self::ONLY_DIRECTORIES === (self::ONLY_DIRECTORIES & $this->mode) && $this->isFile()) { return false; } elseif (self::ONLY_FILES === (self::ONLY_FILES & $this->mode) && $this->isDir()) { return false; } return true; } } comparators = $comparators; parent::__construct($iterator); } public function accept() { if (!$this->isFile()) { return true; } $filesize = $this->getSize(); foreach ($this->comparators as $compare) { if (!$compare->test($filesize)) { return false; } } return true; } } matchRegexps = array(); foreach ($matchPatterns as $pattern) { $this->matchRegexps[] = $this->toRegex($pattern); } $this->noMatchRegexps = array(); foreach ($noMatchPatterns as $pattern) { $this->noMatchRegexps[] = $this->toRegex($pattern); } parent::__construct($iterator); } protected function isRegex($str) { if (preg_match('/^(.{3,}?)[imsxuADU]*$/', $str, $m)) { $start = substr($m[1], 0, 1); $end = substr($m[1], -1); if ($start === $end) { return !preg_match('/[[:alnum:] \\\\]/', $start); } if ($start === '{' && $end === '}') { return true; } } return false; } abstract protected function toRegex($str); } patterns = array(); foreach ($directories as $directory) { $this->patterns[] = '#(^|/)'.preg_quote($directory, '#').'(/|$)#'; } parent::__construct($iterator); } public function accept() { $path = $this->isDir() ? $this->getSubPathname() : $this->getSubPath(); $path = strtr($path, '\\', '/'); foreach ($this->patterns as $pattern) { if (preg_match($pattern, $path)) { return false; } } return true; } } matchRegexps && !$this->noMatchRegexps) { return true; } if ($this->isDir() || !$this->isReadable()) { return false; } $content = @file_get_contents($filename = $this->getRealpath()); if (false === $content) { throw new \RuntimeException(sprintf('Error reading file "%s".', $this->getRealpath())); } foreach ($this->noMatchRegexps as $regex) { if (preg_match($regex, $content)) { return false; } } $match = true; if ($this->matchRegexps) { $match = false; foreach ($this->matchRegexps as $regex) { if (preg_match($regex, $content)) { return true; } } } return $match; } protected function toRegex($str) { return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/'; } } ]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) { throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test)); } $target = $matches[2]; if (!is_numeric($target)) { throw new \InvalidArgumentException(sprintf('Invalid number "%s".', $target)); } if (isset($matches[3])) { switch (strtolower($matches[3])) { case 'k': $target *= 1000; break; case 'ki': $target *= 1024; break; case 'm': $target *= 1000000; break; case 'mi': $target *= 1024*1024; break; case 'g': $target *= 1000000000; break; case 'gi': $target *= 1024*1024*1024; break; } } $this->setTarget($target); $this->setOperator(isset($matches[1]) ? $matches[1] : '=='); } } target; } public function setTarget($target) { $this->target = $target; } public function getOperator() { return $this->operator; } public function setOperator($operator) { if (!$operator) { $operator = '=='; } if (!in_array($operator, array('>', '<', '>=', '<=', '==', '!='))) { throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator)); } $this->operator = $operator; } public function test($test) { switch ($this->operator) { case '>': return $test > $this->target; case '>=': return $test >= $this->target; case '<': return $test < $this->target; case '<=': return $test <= $this->target; case '!=': return $test != $this->target; } return $test == $this->target; } } ]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) { throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a date test.', $test)); } try { $date = new \DateTime($matches[2]); $target = $date->format('U'); } catch (\Exception $e) { throw new \InvalidArgumentException(sprintf('"%s" is not a valid date.', $matches[2])); } $operator = isset($matches[1]) ? $matches[1] : '=='; if ('since' === $operator || 'after' === $operator) { $operator = '>'; } if ('until' === $operator || 'before' === $operator) { $operator = '<'; } $this->setOperator($operator); $this->setTarget($target); } } relativePath = $relativePath; $this->relativePathname = $relativePathname; } public function getRelativePath() { return $this->relativePath; } public function getRelativePathname() { return $this->relativePathname; } } ignore = static::IGNORE_VCS_FILES | static::IGNORE_DOT_FILES; } static public function create() { return new self(); } public function directories() { $this->mode = Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES; return $this; } public function files() { $this->mode = Iterator\FileTypeFilterIterator::ONLY_FILES; return $this; } public function depth($level) { $this->depths[] = new Comparator\NumberComparator($level); return $this; } public function date($date) { $this->dates[] = new Comparator\DateComparator($date); return $this; } public function name($pattern) { $this->names[] = $pattern; return $this; } public function notName($pattern) { $this->notNames[] = $pattern; return $this; } public function contains($pattern) { $this->contains[] = $pattern; return $this; } public function notContains($pattern) { $this->notContains[] = $pattern; return $this; } public function size($size) { $this->sizes[] = new Comparator\NumberComparator($size); return $this; } public function exclude($dirs) { $this->exclude = array_merge($this->exclude, (array) $dirs); return $this; } public function ignoreDotFiles($ignoreDotFiles) { if ($ignoreDotFiles) { $this->ignore = $this->ignore | static::IGNORE_DOT_FILES; } else { $this->ignore = $this->ignore ^ static::IGNORE_DOT_FILES; } return $this; } public function ignoreVCS($ignoreVCS) { if ($ignoreVCS) { $this->ignore = $this->ignore | static::IGNORE_VCS_FILES; } else { $this->ignore = $this->ignore ^ static::IGNORE_VCS_FILES; } return $this; } static public function addVCSPattern($pattern) { self::$vcsPatterns[] = $pattern; } public function sort(\Closure $closure) { $this->sort = $closure; return $this; } public function sortByName() { $this->sort = Iterator\SortableIterator::SORT_BY_NAME; return $this; } public function sortByType() { $this->sort = Iterator\SortableIterator::SORT_BY_TYPE; return $this; } public function sortByAccessedTime() { $this->sort = Iterator\SortableIterator::SORT_BY_ACCESSED_TIME; return $this; } public function sortByChangedTime() { $this->sort = Iterator\SortableIterator::SORT_BY_CHANGED_TIME; return $this; } public function sortByModifiedTime() { $this->sort = Iterator\SortableIterator::SORT_BY_MODIFIED_TIME; return $this; } public function filter(\Closure $closure) { $this->filters[] = $closure; return $this; } public function followLinks() { $this->followLinks = true; return $this; } public function in($dirs) { $dirs = (array) $dirs; foreach ($dirs as $dir) { if (!is_dir($dir)) { throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir)); } } $this->dirs = array_merge($this->dirs, $dirs); return $this; } public function getIterator() { if (0 === count($this->dirs)) { throw new \LogicException('You must call the in() method before iterating over a Finder.'); } if (1 === count($this->dirs) && 0 === count($this->iterators)) { return $this->searchInDirectory($this->dirs[0]); } $iterator = new \AppendIterator(); foreach ($this->dirs as $dir) { $iterator->append($this->searchInDirectory($dir)); } foreach ($this->iterators as $it) { $iterator->append($it); } return $iterator; } public function append($iterator) { if ($iterator instanceof \IteratorAggregate) { $this->iterators[] = $iterator->getIterator(); } elseif ($iterator instanceof \Iterator) { $this->iterators[] = $iterator; } elseif ($iterator instanceof \Traversable || is_array($iterator)) { $it = new \ArrayIterator(); foreach ($iterator as $file) { $it->append($file instanceof \SplFileInfo ? $file : new \SplFileInfo($file)); } $this->iterators[] = $it; } else { throw new \InvalidArgumentException('Finder::append() method wrong argument type.'); } } public function count() { return iterator_count($this->getIterator()); } private function searchInDirectory($dir) { $flags = \RecursiveDirectoryIterator::SKIP_DOTS; if ($this->followLinks) { $flags |= \RecursiveDirectoryIterator::FOLLOW_SYMLINKS; } $iterator = new \RecursiveIteratorIterator( new Iterator\RecursiveDirectoryIterator($dir, $flags), \RecursiveIteratorIterator::SELF_FIRST ); if ($this->depths) { $iterator = new Iterator\DepthRangeFilterIterator($iterator, $this->depths); } if ($this->mode) { $iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode); } if (static::IGNORE_VCS_FILES === (static::IGNORE_VCS_FILES & $this->ignore)) { $this->exclude = array_merge($this->exclude, self::$vcsPatterns); } if (static::IGNORE_DOT_FILES === (static::IGNORE_DOT_FILES & $this->ignore)) { $this->exclude[] = '\..+'; } if ($this->exclude) { $iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude); } if ($this->names || $this->notNames) { $iterator = new Iterator\FilenameFilterIterator($iterator, $this->names, $this->notNames); } if ($this->contains || $this->notContains) { $iterator = new Iterator\FilecontentFilterIterator($iterator, $this->contains, $this->notContains); } if ($this->sizes) { $iterator = new Iterator\SizeRangeFilterIterator($iterator, $this->sizes); } if ($this->dates) { $iterator = new Iterator\DateRangeFilterIterator($iterator, $this->dates); } if ($this->filters) { $iterator = new Iterator\CustomFilterIterator($iterator, $this->filters); } if ($this->sort) { $iteratorAggregate = new Iterator\SortableIterator($iterator, $this->sort); $iterator = $iteratorAggregate->getIterator(); } return $iterator; } } 2, 'JSONString' => 3, 'STRING' => 4, 'JSONNumber' => 5, 'NUMBER' => 6, 'JSONNullLiteral' => 7, 'NULL' => 8, 'JSONBooleanLiteral' => 9, 'TRUE' => 10, 'FALSE' => 11, 'JSONText' => 12, 'JSONValue' => 13, 'EOF' => 14, 'JSONObject' => 15, 'JSONArray' => 16, '{' => 17, '}' => 18, 'JSONMemberList' => 19, 'JSONMember' => 20, ':' => 21, ',' => 22, '[' => 23, ']' => 24, 'JSONElementList' => 25, '$accept' => 0, '$end' => 1, ); private $terminals_ = array( 2 => "error", 4 => "STRING", 6 => "NUMBER", 8 => "NULL", 10 => "TRUE", 11 => "FALSE", 14 => "EOF", 17 => "{", 18 => "}", 21 => ":", 22 => ",", 23 => "[", 24 => "]", ); private $productions_ = array( 0, array(3, 1), array(5, 1), array(7, 1), array(9, 1), array(9, 1), array(12, 2), array(13, 1), array(13, 1), array(13, 1), array(13, 1), array(13, 1), array(13, 1), array(15, 2), array(15, 3), array(20, 3), array(19, 1), array(19, 3), array(16, 2), array(16, 3), array(25, 1), array(25, 3) ); private $table = array(array(3 => 5, 4 => array(1,12), 5 => 6, 6 => array(1,13), 7 => 3, 8 => array(1,9), 9 => 4, 10 => array(1,10), 11 => array(1,11), 12 => 1, 13 => 2, 15 => 7, 16 => 8, 17 => array(1,14), 23 => array(1,15)), array( 1 => array(3)), array( 14 => array(1,16)), array( 14 => array(2,7), 18 => array(2,7), 22 => array(2,7), 24 => array(2,7)), array( 14 => array(2,8), 18 => array(2,8), 22 => array(2,8), 24 => array(2,8)), array( 14 => array(2,9), 18 => array(2,9), 22 => array(2,9), 24 => array(2,9)), array( 14 => array(2,10), 18 => array(2,10), 22 => array(2,10), 24 => array(2,10)), array( 14 => array(2,11), 18 => array(2,11), 22 => array(2,11), 24 => array(2,11)), array( 14 => array(2,12), 18 => array(2,12), 22 => array(2,12), 24 => array(2,12)), array( 14 => array(2,3), 18 => array(2,3), 22 => array(2,3), 24 => array(2,3)), array( 14 => array(2,4), 18 => array(2,4), 22 => array(2,4), 24 => array(2,4)), array( 14 => array(2,5), 18 => array(2,5), 22 => array(2,5), 24 => array(2,5)), array( 14 => array(2,1), 18 => array(2,1), 21 => array(2,1), 22 => array(2,1), 24 => array(2,1)), array( 14 => array(2,2), 18 => array(2,2), 22 => array(2,2), 24 => array(2,2)), array( 3 => 20, 4 => array(1,12), 18 => array(1,17), 19 => 18, 20 => 19 ), array( 3 => 5, 4 => array(1,12), 5 => 6, 6 => array(1,13), 7 => 3, 8 => array(1,9), 9 => 4, 10 => array(1,10), 11 => array(1,11), 13 => 23, 15 => 7, 16 => 8, 17 => array(1,14), 23 => array(1,15), 24 => array(1,21), 25 => 22 ), array( 1 => array(2,6)), array( 14 => array(2,13), 18 => array(2,13), 22 => array(2,13), 24 => array(2,13)), array( 18 => array(1,24), 22 => array(1,25)), array( 18 => array(2,16), 22 => array(2,16)), array( 21 => array(1,26)), array( 14 => array(2,18), 18 => array(2,18), 22 => array(2,18), 24 => array(2,18)), array( 22 => array(1,28), 24 => array(1,27)), array( 22 => array(2,20), 24 => array(2,20)), array( 14 => array(2,14), 18 => array(2,14), 22 => array(2,14), 24 => array(2,14)), array( 3 => 20, 4 => array(1,12), 20 => 29 ), array( 3 => 5, 4 => array(1,12), 5 => 6, 6 => array(1,13), 7 => 3, 8 => array(1,9), 9 => 4, 10 => array(1,10), 11 => array(1,11), 13 => 30, 15 => 7, 16 => 8, 17 => array(1,14), 23 => array(1,15)), array( 14 => array(2,19), 18 => array(2,19), 22 => array(2,19), 24 => array(2,19)), array( 3 => 5, 4 => array(1,12), 5 => 6, 6 => array(1,13), 7 => 3, 8 => array(1,9), 9 => 4, 10 => array(1,10), 11 => array(1,11), 13 => 31, 15 => 7, 16 => 8, 17 => array(1,14), 23 => array(1,15)), array( 18 => array(2,17), 22 => array(2,17)), array( 18 => array(2,15), 22 => array(2,15)), array( 22 => array(2,21), 24 => array(2,21)), ); private $defaultActions = array( 16 => array(2, 6) ); public function lint($input) { try { $this->parse($input); } catch (ParsingException $e) { return $e; } } public function parse($input) { $this->stack = array(0); $this->vstack = array(null); $this->lstack = array(); $yytext = ''; $yylineno = 0; $yyleng = 0; $recovering = 0; $TERROR = 2; $EOF = 1; $this->lexer = new Lexer(); $this->lexer->setInput($input); $yyloc = $this->lexer->yylloc; $this->lstack[] = $yyloc; $symbol = null; $preErrorSymbol = null; $state = null; $action = null; $a = null; $r = null; $yyval = new stdClass; $p = null; $len = null; $newState = null; $expected = null; $errStr = null; while (true) { $state = $this->stack[count($this->stack)-1]; if (isset($this->defaultActions[$state])) { $action = $this->defaultActions[$state]; } else { if ($symbol == null) { $symbol = $this->lex(); } $action = isset($this->table[$state][$symbol]) ? $this->table[$state][$symbol] : false; } if (!$action || !$action[0]) { if (!$recovering) { $expected = array(); foreach ($this->table[$state] as $p => $ignore) { if (isset($this->terminals_[$p]) && $p > 2) { $expected[] = "'" . $this->terminals_[$p] . "'"; } } $errStr = 'Parse error on line ' . ($yylineno+1) . ":\n" . $this->lexer->showPosition() . "\nExpected one of: " . implode(', ', $expected); $this->parseError($errStr, array( 'text' => $this->lexer->match, 'token' => !empty($this->terminals_[$symbol]) ? $this->terminals_[$symbol] : $symbol, 'line' => $this->lexer->yylineno, 'loc' => $yyloc, 'expected' => $expected, )); } if ($recovering == 3) { if ($symbol == $EOF) { throw new ParsingException($errStr ?: 'Parsing halted.'); } $yyleng = $this->lexer->yyleng; $yytext = $this->lexer->yytext; $yylineno = $this->lexer->yylineno; $yyloc = $this->lexer->yylloc; $symbol = $this->lex(); } while (true) { if (array_key_exists($TERROR, $this->table[$state])) { break; } if ($state == 0) { throw new ParsingException($errStr ?: 'Parsing halted.'); } $this->popStack(1); $state = $this->stack[count($this->stack)-1]; } $preErrorSymbol = $symbol; $symbol = $TERROR; $state = $this->stack[count($this->stack)-1]; $action = isset($this->table[$state][$TERROR]) ? $this->table[$state][$TERROR] : false; $recovering = 3; } if (is_array($action[0]) && count($action) > 1) { throw new ParsingException('Parse Error: multiple actions possible at state: ' . $state . ', token: ' . $symbol); } switch ($action[0]) { case 1: $this->stack[] = $symbol; $this->vstack[] = $this->lexer->yytext; $this->lstack[] = $this->lexer->yylloc; $this->stack[] = $action[1]; $symbol = null; if (!$preErrorSymbol) { $yyleng = $this->lexer->yyleng; $yytext = $this->lexer->yytext; $yylineno = $this->lexer->yylineno; $yyloc = $this->lexer->yylloc; if ($recovering > 0) { $recovering--; } } else { $symbol = $preErrorSymbol; $preErrorSymbol = null; } break; case 2: $len = $this->productions_[$action[1]][1]; $yyval->token = $this->vstack[count($this->vstack) - $len]; $yyval->store = array( 'first_line' => $this->lstack[count($this->lstack) - ($len ?: 1)]['first_line'], 'last_line' => $this->lstack[count($this->lstack) - 1]['last_line'], 'first_column' => $this->lstack[count($this->lstack) - ($len ?: 1)]['first_column'], 'last_column' => $this->lstack[count($this->lstack) - 1]['last_column'], ); $r = $this->performAction($yyval, $yytext, $yyleng, $yylineno, $action[1], $this->vstack, $this->lstack); if (!$r instanceof Undefined) { return $r; } if ($len) { $this->popStack($len); } $this->stack[] = $this->productions_[$action[1]][0]; $this->vstack[] = $yyval->token; $this->lstack[] = $yyval->store; $newState = $this->table[$this->stack[count($this->stack)-2]][$this->stack[count($this->stack)-1]]; $this->stack[] = $newState; break; case 3: return true; } } return true; } protected function parseError($str, $hash) { throw new ParsingException($str, $hash); } private function performAction(stdClass $yyval, $yytext, $yyleng, $yylineno, $yystate, &$tokens) { $len = count($tokens) - 1; switch ($yystate) { case 1: $yytext =preg_replace_callback('{(?:\\\\["bfnrt/\\\\]|\\\\u[a-fA-F0-9]{4})}', array($this, 'stringInterpolation'), $yytext); $yyval->token = $yytext; break; case 2: if (strpos($yytext, 'e') !== false || strpos($yytext, 'E') !== false) { $yyval->token = floatval($yytext); } else { $yyval->token = strpos($yytext, '.') === false ? intval($yytext) : floatval($yytext); } break; case 3: $yyval->token = null; break; case 4: $yyval->token = true; break; case 5: $yyval->token = false; break; case 6: return $yyval->token = $tokens[$len-1]; case 13: $yyval->token = new stdClass; break; case 14: $yyval->token = $tokens[$len-1]; break; case 15: $yyval->token = array($tokens[$len-2], $tokens[$len]); break; case 16: $yyval->token = new stdClass; $property = $tokens[$len][0] === '' ? '_empty_' : $tokens[$len][0]; $yyval->token->$property = $tokens[$len][1]; break; case 17: $yyval->token = $tokens[$len-2]; $tokens[$len-2]->{$tokens[$len][0]} = $tokens[$len][1]; break; case 18: $yyval->token = array(); break; case 19: $yyval->token = $tokens[$len-1]; break; case 20: $yyval->token = array($tokens[$len]); break; case 21: $tokens[$len-2][] = $tokens[$len]; $yyval->token = $tokens[$len-2]; break; } return new Undefined(); } private function stringInterpolation($match) { switch ($match[0]) { case '\\\\': return '\\'; case '\"': return '"'; case '\b': return chr(8); case '\f': return chr(12); case '\n': return "\n"; case '\r': return "\r"; case '\t': return "\t"; case '\/': return "/"; default: return html_entity_decode('&#x'.ltrim(substr($match[0], 2), '0').';', 0, 'UTF-8'); } } private function popStack($n) { $this->stack = array_slice($this->stack, 0, - (2 * $n)); $this->vstack = array_slice($this->vstack, 0, - $n); $this->lstack = array_slice($this->lstack, 0, - $n); } private function lex() { $token = $this->lexer->lex() ?: 1; if (!is_numeric($token)) { $token = isset($this->symbols[$token]) ? $this->symbols[$token] : $token; } return $token; } } '/^\s+/', 1 => '/^-?([0-9]|[1-9][0-9]+)(\.[0-9]+)?([eE][+-]?[0-9]+)?\b/', 2 => '{^"(\\\\["bfnrt/\\\\]|\\\\u[a-fA-F0-9]{4}|[^\0-\x09\x0a-\x1f\\\\"])*"}', 3 => '/^\{/', 4 => '/^\}/', 5 => '/^\[/', 6 => '/^\]/', 7 => '/^,/', 8 => '/^:/', 9 => '/^true\b/', 10 => '/^false\b/', 11 => '/^null\b/', 12 => '/^$/', 13 => '/^./', ); private $conditions = array( "INITIAL" => array( "rules" => array(0,1,2,3,4,5,6,7,8,9,10,11,12,13), "inclusive" => true, ), ); public function lex() { $r = $this->next(); if (!$r instanceof Undefined) { return $r; } return $this->lex(); } public function setInput($input) { $this->_input = $input; $this->_more = $this->_less = $this->done = false; $this->yylineno = $this->yyleng = 0; $this->yytext = $this->matched = $this->match = ''; $this->conditionStack = array('INITIAL'); $this->yylloc = array('first_line' => 1, 'first_column' => 0, 'last_line' => 1, 'last_column' => 0); return $this; } public function showPosition() { $pre = $this->pastInput(); $c = str_repeat('-', strlen($pre)); return $pre . $this->upcomingInput() . "\n" . $c . "^"; } protected function parseError($str, $hash) { throw new \Exception($str); } private function input() { $ch = $this->_input[0]; $this->yytext += $ch; $this->yyleng++; $this->match += $ch; $this->matched += $ch; if (strpos($ch, "\n") !== false) { $this->yylineno++; } array_shift($this->_input); return $ch; } private function unput($ch) { $this->_input = $ch . $this->_input; return $this; } private function more() { $this->_more = true; return $this; } private function pastInput() { $past = substr($this->matched, 0, strlen($this->matched) - strlen($this->match)); return (strlen($past) > 20 ? '...' : '') . str_replace("\n", '', substr($past, -20)); } private function upcomingInput() { $next = $this->match; if (strlen($next) < 20) { $next += substr($this->_input, 0, 20 - strlen($next)); } return str_replace("\n", '', substr($next, 0, 20) . (strlen($next) > 20 ? '...' : '')); } private function next() { if ($this->done) { return $this->EOF; } if (!$this->_input) { $this->done = true; } $token = null; $match = null; $col = null; $lines = null; if (!$this->_more) { $this->yytext = ''; $this->match = ''; } $rules = $this->_currentRules(); $rulesLen = count($rules); for ($i=0; $i < $rulesLen; $i++) { if (preg_match($this->rules[$rules[$i]], $this->_input, $match)) { preg_match_all('/\n.*/', $match[0], $lines); $lines = $lines[0]; if ($lines) { $this->yylineno += count($lines); } $this->yylloc = array( 'first_line' => $this->yylloc['last_line'], 'last_line' => $this->yylineno+1, 'first_column' => $this->yylloc['last_column'], 'last_column' => $lines ? strlen($lines[count($lines) - 1]) - 1 : $this->yylloc['last_column'] + strlen($match[0]), ); $this->yytext .= $match[0]; $this->match .= $match[0]; $this->matches = $match; $this->yyleng = strlen($this->yytext); $this->_more = false; $this->_input = substr($this->_input, strlen($match[0])); $this->matched .= $match[0]; $token = $this->performAction($rules[$i], $this->conditionStack[count($this->conditionStack)-1]); if ($token) { return $token; } return new Undefined(); } } if ($this->_input === "") { return $this->EOF; } $this->parseError( 'Lexical error on line ' . ($this->yylineno+1) . ". Unrecognized text.\n" . $this->showPosition(), array( 'text' => "", 'token' => null, 'line' => $this->yylineno, ) ); } private function begin($condition) { $this->conditionStack[] = $condition; } private function popState() { return array_pop($this->conditionStack); } private function _currentRules() { return $this->conditions[$this->conditionStack[count($this->conditionStack)-1]]['rules']; } private function performAction($avoiding_name_collisions, $YY_START) { $YYSTATE = $YY_START; switch ($avoiding_name_collisions) { case 0: break; case 1: return 6; break; case 2: $this->yytext = substr($this->yytext, 1, $this->yyleng-2); return 4; case 3: return 17; case 4: return 18; case 5: return 23; case 6: return 24; case 7: return 22; case 8: return 21; case 9: return 10; case 10: return 11; case 11: return 8; case 12: return 14; case 13: return 'INVALID'; } } } details = $details; parent::__construct($message); } public function getDetails() { return $this->details; } }checkMode); $validator->check($value, $schema); $this->addErrors($validator->getErrors()); } }incrementPath($path, $i); $this->validateCommonProperties($value, $schema, $path); $this->validateTypes($value, $schema, $path, $i); } public function validateTypes($value, $schema = null, $path = null, $i = null) { if (is_array($value)) { $this->checkArray($value, $schema, $path, $i); } if (is_object($value) && isset($schema->properties)) { $this->checkObject($value, $schema->properties, $path, isset($schema->additionalProperties) ? $schema->additionalProperties : null); } if (is_string($value)) { $this->checkString($value, $schema, $path, $i); } if (is_numeric($value)) { $this->checkNumber($value, $schema, $path, $i); } if (isset($schema->enum)) { $this->checkEnum($value, $schema, $path, $i); } } protected function validateCommonProperties($value, $schema = null, $path = null, $i = null) { if (isset($schema->extends)) { $this->checkUndefined($value, $schema->extends, $path, $i); } if (is_object($value) && $value instanceOf Undefined) { if (isset($schema->required) && $schema->required) { $this->addError($path, "is missing and it is required"); } } else { $this->checkType($value, $schema, $path); } if (isset($schema->disallow)) { $initErrors = $this->getErrors(); $this->checkUndefined($value, $schema->disallow, $path); if (count($this->getErrors()) == count($initErrors)) { $this->addError($path, " disallowed value was matched"); } else { $this->errors = $initErrors; } } } }checkMode = $checkMode; } public function addError($path, $message) { $this->errors[] = array( 'property' => $path, 'message' => $message ); } public function addErrors(array $errors) { $this->errors = array_merge($this->errors, $errors); } public function getErrors() { return array_unique($this->errors, SORT_REGULAR); } protected function incrementPath($path, $i) { if ($path !== '') { if (is_int($i)) { $path .= '[' . $i . ']'; } else if ($i == '') { $path .= ''; } else { $path .= '.' . $i; } } else { $path = $i; } return $path; } protected function checkArray($value, $schema = null, $path = null, $i = null) { $validator = new Collection($this->checkMode); $validator->check($value, $schema, $path, $i); $this->addErrors($validator->getErrors()); } protected function checkObject($value, $schema = null, $path = null, $i = null) { $validator = new Object($this->checkMode); $validator->check($value, $schema, $path, $i); $this->addErrors($validator->getErrors()); } protected function checkType($value, $schema = null, $path = null, $i = null) { $validator = new Type($this->checkMode); $validator->check($value, $schema, $path, $i); $this->addErrors($validator->getErrors()); } protected function checkUndefined($value, $schema = null, $path = null, $i = null) { $validator = new Undefined($this->checkMode); $validator->check($value, $schema, $path, $i); $this->addErrors($validator->getErrors()); } protected function checkString($value, $schema = null, $path = null, $i = null) { $validator = new String($this->checkMode); $validator->check($value, $schema, $path, $i); $this->addErrors($validator->getErrors()); } protected function checkNumber($value, $schema = null, $path = null, $i = null) { $validator = new Number($this->checkMode); $validator->check($value, $schema, $path, $i); $this->addErrors($validator->getErrors()); } protected function checkEnum($value, $schema = null, $path = null, $i = null) { $validator = new Enum($this->checkMode); $validator->check($value, $schema, $path, $i); $this->addErrors($validator->getErrors()); } public function isValid() { return !$this->getErrors(); } }checkUndefined($element, $schema, '', ''); } elseif (isset($element->{$this->inlineSchemaProperty})) { $this->checkUndefined($element, $element->{$this->inlineSchemaProperty}, '', ''); } else { throw new \InvalidArgumentException('no schema found to verify against'); } } }type) ? $schema->type : null; $isValid = true; if (is_array($type)) { $validatedOneType = false; $errors = array(); foreach ($type as $tp) { $validator = new Type($this->checkMode); $subSchema = new \stdClass(); $subSchema->type = $tp; $validator->check($value, $subSchema, $path, null); $error = $validator->getErrors(); if (!count($error)) { $validatedOneType = true; break; } else { $errors = $error; } } if (!$validatedOneType) { return $this->addErrors($errors); } } elseif (is_object($type)) { $this->checkUndefined($value, $type, $path); } else { $isValid = $this->validateType($value, $type); } if ($isValid === false) { $this->addError($path, gettype($value) . " value found, but a " . $type . " is required"); } } protected function validateType($value, $type) { if (!$type) { return true; } switch ($type) { case 'integer' : return (integer)$value == $value ? true : is_int($value); case 'number' : return is_numeric($value); case 'boolean' : return is_bool($value); case 'object' : return is_object($value); case 'array' : return is_array($value); case 'string' : return is_string($value); case 'null' : return is_null($value); case 'any' : return true; default: throw new \InvalidArgumentException((is_object($value) ? 'object' : $value) . ' is a invalid type for ' . $type); } } }minItems) && count($value) < $schema->minItems) { $this->addError($path, "There must be a minimum of " . $schema->minItems . " in the array"); } if (isset($schema->maxItems) && count($value) > $schema->maxItems) { $this->addError($path, "There must be a maximum of " . $schema->maxItems . " in the array"); } if (isset($schema->uniqueItems) && array_unique($value) != $value) { $this->addError($path, "There are no duplicates allowed in the array"); } if (isset($schema->items)) { $this->validateItems($value, $schema, $path, $i); } } protected function validateItems($value, $schema = null, $path = null, $i = null) { if (!is_array($schema->items)) { foreach ($value as $k => $v) { $initErrors = $this->getErrors(); if (!isset($schema->additionalItems) || $schema->additionalItems === false) { $this->checkUndefined($v, $schema->items, $path, $k); } if (count($initErrors) < count($this->getErrors()) && (isset($schema->additionalItems) && $schema->additionalItems !== false)) { $secondErrors = $this->getErrors(); $this->checkUndefined($v, $schema->additionalItems, $path, $k); } if (isset($secondErrors) && count($secondErrors) < $this->getErrors()) { $this->errors = $secondErrors; } elseif (isset($secondErrors) && count($secondErrors) == count($this->getErrors())) { $this->errors = $initErrors; } } } else { foreach ($value as $k => $v) { if (array_key_exists($k, $schema->items)) { $this->checkUndefined($v, $schema->items[$k], $path, $k); } else { if (array_key_exists('additionalItems', $schema) && $schema->additionalItems !== false) { $this->checkUndefined($v, $schema->additionalItems, $path, $k); } else { $this->addError( $path, 'The item ' . $i . '[' . $k . '] is not defined in the objTypeDef and the objTypeDef does not allow additional properties' ); } } } for ($k = count($value); $k < count($schema->items); $k++) { $this->checkUndefined(new Undefined(), $schema->items[$k], $path, $k); } } } }maxLength) && strlen($element) > $schema->maxLength) { $this->addError($path, "must be at most " . $schema->maxLength . " characters long"); } if (isset($schema->minLength) && strlen($element) < $schema->minLength) { $this->addError($path, "must be at least " . $schema->minLength . " characters long"); } if (isset($schema->pattern) && !preg_match('/' . $schema->pattern . '/', $element)) { $this->addError($path, "does not match the regex pattern " . $schema->pattern); } } }validateDefinition($element, $definition, $path); $this->validateElement($element, $definition, $path, $additionalProp); } public function validateElement($element, $objectDefinition = null, $path = null, $additionalProp = null) { foreach ($element as $i => $value) { $property = $this->getProperty($element, $i, new Undefined()); $definition = $this->getProperty($objectDefinition, $i); if ($this->getProperty($definition, 'required') && !$property) { $this->addError($path, "the property " . $i . " is required"); } if ($additionalProp === false && $this->inlineSchemaProperty !== $i && !$definition) { $this->addError($path, "The property " . $i . " is not defined and the definition does not allow additional properties"); } if ($additionalProp && !$definition) { $this->checkUndefined($value, $additionalProp, $path, $i); } $require = $this->getProperty($definition, 'requires'); if ($require && !$this->getProperty($element, $require)) { $this->addError($path, "the presence of the property " . $i . " requires that " . $require . " also be present"); } $this->checkUndefined($value, $definition ? : new \stdClass(), $path, $i); } } public function validateDefinition($element, $objectDefinition = null, $path = null) { foreach ($objectDefinition as $i => $value) { $property = $this->getProperty($element, $i, new Undefined()); $definition = $this->getProperty($objectDefinition, $i); $this->checkUndefined($property, $definition, $path, $i); } } protected function getProperty($element, $property, $fallback = null) { if (is_array($element) ) { return array_key_exists($property, $element) ? $element[$property] : $fallback; } else { return isset($element->$property) ? $element->$property : $fallback; } } }enum as $possibleValue) { if ($possibleValue == $element) { $found = true; break; } } if (!isset($found)) { $this->addError($path, "does not have a value in the enumeration " . implode(', ', $schema->enum)); } } }minimum) && $element < $schema->minimum) { $this->addError($path, "must have a minimum value of " . $schema->minimum); } if (isset($schema->maximum) && $element > $schema->maximum) { $this->addError($path, "must have a maximum value of " . $schema->maximum); } if (isset($schema->divisibleBy) && $element % $schema->divisibleBy != 0) { $this->addError($path, "is not divisible by " . $schema->divisibleBy); } } } $path) { $loader->add($namespace, $path); } $classMap = require $composerDir . '/autoload_classmap.php'; if ($classMap) { $loader->addClassMap($classMap); } $loader->register(); return $loader; }); $baseDir . '/vendor/symfony/process/', 'Symfony\\Component\\Finder' => $baseDir . '/vendor/symfony/finder/', 'Symfony\\Component\\Console' => $baseDir . '/vendor/symfony/console/', 'Seld\\JsonLint' => $baseDir . '/vendor/seld/jsonlint/src/', 'JsonSchema' => $baseDir . '/vendor/justinrainbow/json-schema/src/', 'Composer' => $baseDir . '/src/', ); prefixes; } public function getFallbackDirs() { return $this->fallbackDirs; } public function getClassMap() { return $this->classMap; } public function addClassMap(array $classMap) { if ($this->classMap) { $this->classMap = array_merge($this->classMap, $classMap); } else { $this->classMap = $classMap; } } public function add($prefix, $paths) { if (!$prefix) { foreach ((array) $paths as $path) { $this->fallbackDirs[] = $path; } return; } if (isset($this->prefixes[$prefix])) { $this->prefixes[$prefix] = array_merge( $this->prefixes[$prefix], (array) $paths ); } else { $this->prefixes[$prefix] = (array) $paths; } } public function setUseIncludePath($useIncludePath) { $this->useIncludePath = $useIncludePath; } public function getUseIncludePath() { return $this->useIncludePath; } public function register($prepend = false) { spl_autoload_register(array($this, 'loadClass'), true, $prepend); } public function unregister() { spl_autoload_unregister(array($this, 'loadClass')); } public function loadClass($class) { if ($file = $this->findFile($class)) { require $file; return true; } } public function findFile($class) { if (isset($this->classMap[$class])) { return $this->classMap[$class]; } if ('\\' == $class[0]) { $class = substr($class, 1); } if (false !== $pos = strrpos($class, '\\')) { $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR; $className = substr($class, $pos + 1); } else { $classPath = null; $className = $class; } $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; foreach ($this->prefixes as $prefix => $dirs) { if (0 === strpos($class, $prefix)) { foreach ($dirs as $dir) { if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { return $dir . DIRECTORY_SEPARATOR . $classPath; } } } } foreach ($this->fallbackDirs as $dir) { if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { return $dir . DIRECTORY_SEPARATOR . $classPath; } } if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) { return $file; } } } run(); Copyright (c) 2011 Nils Adermann, Jordi Boggiano Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. xW'7F02 *"GOGBMB