本文整理汇总了PHP中Webmozart\PathUtil\Path::makeRelative方法的典型用法代码示例。如果您正苦于以下问题:PHP Path::makeRelative方法的具体用法?PHP Path::makeRelative怎么用?PHP Path::makeRelative使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Webmozart\PathUtil\Path
的用法示例。
在下文中一共展示了Path::makeRelative方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: generateNewInstance
/**
* {@inheritdoc}
*/
public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
{
Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.');
$options = array_replace(self::$defaultOptions, $options);
Assert::stringNotEmpty($options['path'], 'The "path" option should be a non-empty string. Got: %s');
Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s');
Assert::boolean($options['serialize-strings'], 'The "serialize-strings" option should be a boolean. Got: %s');
Assert::boolean($options['serialize-arrays'], 'The "serialize-arrays" option should be a boolean. Got: %s');
Assert::boolean($options['escape-slash'], 'The "escape-slash" option should be a boolean. Got: %s');
Assert::boolean($options['pretty-print'], 'The "pretty-print" option should be a boolean. Got: %s');
$path = Path::makeAbsolute($options['path'], $options['root-dir']);
$relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory());
$flags = array();
if (!$options['serialize-strings']) {
$flags[] = 'JsonFileStore::NO_SERIALIZE_STRINGS';
}
if (!$options['serialize-arrays']) {
$flags[] = 'JsonFileStore::NO_SERIALIZE_ARRAYS';
}
if (!$options['serialize-arrays']) {
$flags[] = 'JsonFileStore::NO_ESCAPE_SLASH';
}
if ($options['pretty-print']) {
$flags[] = 'JsonFileStore::PRETTY_PRINT';
}
$targetMethod->getClass()->addImport(new Import('Webmozart\\KeyValueStore\\JsonFileStore'));
$targetMethod->addBody(sprintf('$%s = new JsonFileStore(%s%s%s);', $varName, $flags ? "\n " : '', '__DIR__.' . var_export('/' . $relPath, true), $flags ? ",\n " . implode("\n | ", $flags) . "\n" : ''));
}
示例2: generateNewInstance
/**
* {@inheritdoc}
*/
public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
{
Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.');
$options = array_replace_recursive(self::$defaultOptions, $options);
if (!isset($options['path'])) {
$options['path'] = $targetMethod->getClass()->getDirectory() . '/path-mappings.json';
}
Assert::stringNotEmpty($options['path'], 'The "path" option should be a non-empty string. Got: %s');
Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s');
Assert::boolean($options['optimize'], 'The "optimize" option should be a boolean. Got: %s');
Assert::isArray($options['change-stream'], 'The "change-stream" option should be an array. Got: %s');
$path = Path::makeAbsolute($options['path'], $options['root-dir']);
$relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory());
$relBaseDir = Path::makeRelative($options['root-dir'], $targetMethod->getClass()->getDirectory());
$escPath = '__DIR__.' . var_export('/' . $relPath, true);
$escBaseDir = $relBaseDir ? '__DIR__.' . var_export('/' . $relBaseDir, true) : '__DIR__';
if ($options['optimize']) {
$streamGenerator = $generatorRegistry->getServiceGenerator(GeneratorRegistry::CHANGE_STREAM, $options['change-stream']['type']);
$streamOptions = $options['change-stream'];
$streamOptions['root-dir'] = $options['root-dir'];
$streamGenerator->generateNewInstance('stream', $targetMethod, $generatorRegistry, $streamOptions);
$targetMethod->getClass()->addImport(new Import('Puli\\Repository\\OptimizedJsonRepository'));
$targetMethod->addBody(sprintf('$%s = new OptimizedJsonRepository(%s, %s, false, $stream);', $varName, $escPath, $escBaseDir));
} else {
$targetMethod->getClass()->addImport(new Import('Puli\\Repository\\JsonRepository'));
$targetMethod->addBody(sprintf('$%s = new JsonRepository(%s, %s, true);', $varName, $escPath, $escBaseDir));
}
}
示例3: generateNewInstance
/**
* {@inheritdoc}
*/
public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
{
Assert::keyExists($options, 'rootDir', 'The "rootDir" option is missing.');
$options = array_replace(self::$defaultOptions, $options);
if (!isset($options['path'])) {
$options['path'] = $targetMethod->getClass()->getDirectory() . '/repository';
}
Assert::string($options['path'], 'The "path" option should be a string. Got: %s');
Assert::string($options['rootDir'], 'The "rootDir" option should be a string. Got: %s');
Assert::boolean($options['symlink'], 'The "symlink" option should be a boolean. Got: %s');
$path = Path::makeAbsolute($options['path'], $options['rootDir']);
$relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory());
$escPath = $relPath ? '__DIR__.' . var_export('/' . $relPath, true) : '__DIR__';
if ($relPath) {
$targetMethod->addBody(<<<EOF
if (!file_exists({$escPath})) {
mkdir({$escPath}, 0777, true);
}
EOF
);
}
$targetMethod->getClass()->addImport(new Import('Puli\\Repository\\FilesystemRepository'));
$targetMethod->addBody(sprintf('$%s = new FilesystemRepository(%s, %s);', $varName, $escPath, var_export($options['symlink'], true)));
}
示例4: makeConfigPathRelative
/**
* Concat two path member only if the second is not absolute
* and make the result relative to the last parameter.
*/
public static function makeConfigPathRelative($config_path, $option_path, $current_path = null)
{
$current_path = $current_path === null ? getcwd() : $current_path;
$config_path = Path::makeAbsolute($config_path, $current_path);
$absolute_path = Path::makeAbsolute($option_path, $config_path);
$relative_path = Path::makeRelative($absolute_path, $current_path);
return $relative_path === '' ? '.' : $relative_path;
}
示例5: collect
/**
* {@inheritDoc}
*/
public function collect() : AssetCollection
{
$files = [];
$this->finder->files()->ignoreDotFiles(false)->in((string) $this->srcDir)->name('/\\.txt$/')->sortByType();
foreach ($this->finder as $file) {
$files[] = new TextFile(new File($file->getPathname()), $this->filesystem, dirname(Path::makeRelative($file->getPathname(), (string) $this->srcDir)));
}
return new AssetCollection($files);
}
示例6: generateNewInstance
/**
* {@inheritdoc}
*/
public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
{
Assert::keyExists($options, 'rootDir', 'The "rootDir" option is missing.');
$options = array_replace(self::$defaultOptions, $options);
$path = Path::makeAbsolute($options['path'], $options['rootDir']);
$relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory());
$targetMethod->getClass()->addImport(new Import('Webmozart\\KeyValueStore\\JsonFileStore'));
$targetMethod->addBody(sprintf('$%s = new JsonFileStore(%s, %s);', $varName, '__DIR__.' . var_export('/' . $relPath, true), $options['cache'] ? 'true' : 'false'));
}
示例7: collect
/**
* {@inheritDoc}
*/
public function collect() : AssetCollection
{
$templates = [];
$this->finder->files()->ignoreDotFiles(false)->in((string) $this->srcDir)->name('/\\.twig$/')->sortByType();
foreach ($this->finder as $template) {
$templates[] = new TwigTemplateFile(Path::makeRelative($template->getPathname(), (string) $this->srcDir), $this->twigData, $this->twig, $this->filesystem);
}
return new AssetCollection($templates);
}
示例8: onGadgetResult
/**
* @param GadgetResultEvent $event
*/
public function onGadgetResult(GadgetResultEvent $event)
{
$result = $event->getResult();
$path = $event->getPath();
foreach ($result->getIssues() as $issue) {
if (!$issue->getFile()) {
continue;
}
$issue->setFile(Path::makeRelative($issue->getFile(), $path));
}
}
示例9: postAutoloadDump
public function postAutoloadDump(Event $event)
{
// This method is called twice. Run it only once.
if (!$this->runPostAutoloadDump) {
return;
}
$this->runPostAutoloadDump = false;
$config = $this->composer->getConfig();
$suffix = $config->get('autoloader-suffix');
$vendorDir = $config->get('vendor-dir');
$binDir = $config->get('bin-dir');
$autoloadFile = $vendorDir . '/autoload.php';
if (!file_exists($autoloadFile)) {
throw new \RuntimeException(sprintf('Could not adjust autoloader: The file %s was not found.', $autoloadFile));
}
if (!$suffix && !$config->get('autoloader-suffix') && is_readable($autoloadFile)) {
$content = file_get_contents($vendorDir . '/autoload.php');
if (preg_match('{' . self::COMPOSER_AUTOLOADER_BASE . '([^:\\s]+)::}', $content, $match)) {
$suffix = $match[1];
}
}
$contents = file_get_contents($autoloadFile);
$constant = '';
$values = array('AUTOLOAD_CLASS' => var_export(self::COMPOSER_AUTOLOADER_BASE . $suffix, true));
foreach ($values as $key => $value) {
$this->io->write('<info>Generating ' . $this->constantPrefix . $key . ' constant</info>');
$constant .= "if (!defined('{$this->constantPrefix}{$key}')) {\n";
$constant .= sprintf(" define('{$this->constantPrefix}{$key}', %s);\n", $value);
$constant .= "}\n\n";
}
$values = array_map(function ($value) {
return var_export($value, true);
}, array('BASE_DIR' => Path::makeRelative(getcwd(), $vendorDir), 'BIN_DIR' => Path::makeRelative($binDir, $vendorDir), 'FILE' => Path::makeRelative(realpath(Factory::getComposerFile()), $vendorDir)));
foreach ($values as $key => $value) {
$this->io->write('<info>Generating ' . $this->constantPrefix . $key . ' constant</info>');
$constant .= "if (!defined('{$this->constantPrefix}{$key}')) {\n";
$constant .= sprintf(" define('{$this->constantPrefix}{$key}', realpath(__DIR__ . DIRECTORY_SEPARATOR . %s));\n", $value);
$constant .= "}\n\n";
}
$values = array('VENDOR_DIR' => $vendorDir);
foreach ($values as $key => $value) {
$this->io->write('<info>Generating ' . $this->constantPrefix . $key . ' constant</info>');
$constant .= "if (!defined('{$this->constantPrefix}{$key}')) {\n";
$constant .= sprintf(" define('{$this->constantPrefix}{$key}', realpath(__DIR__));\n");
$constant .= "}\n\n";
}
// Regex modifiers:
// "m": \s matches newlines
// "D": $ matches at EOF only
// Translation: insert before the last "return" in the file
$contents = preg_replace('/\\n(?=return [^;]+;\\s*$)/mD', "\n" . $constant, $contents);
file_put_contents($autoloadFile, $contents);
}
示例10: generateNewInstance
/**
* {@inheritdoc}
*/
public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
{
Assert::keyExists($options, 'rootDir', 'The "rootDir" option is missing.');
$options = array_replace_recursive(self::$defaultOptions, $options);
$kvsGenerator = $generatorRegistry->getServiceGenerator(GeneratorRegistry::KEY_VALUE_STORE, $options['store']['type']);
$kvsOptions = $options['store'];
$kvsOptions['rootDir'] = $options['rootDir'];
$kvsGenerator->generateNewInstance('store', $targetMethod, $generatorRegistry, $kvsOptions);
$relPath = Path::makeRelative($options['rootDir'], $targetMethod->getClass()->getDirectory());
$escPath = $relPath ? '__DIR__.' . var_export('/' . $relPath, true) : '__DIR__';
$className = ($options['optimize'] ? 'Optimized' : '') . 'PathMappingRepository';
$targetMethod->getClass()->addImport(new Import('Puli\\Repository\\' . $className));
$targetMethod->addBody(sprintf('$%s = new %s($store, %s);', $varName, $className, $escPath));
}
示例11: generateNewInstance
/**
* {@inheritdoc}
*/
public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
{
Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.');
if (!isset($options['path'])) {
$options['path'] = $targetMethod->getClass()->getDirectory() . '/change-stream.json';
}
Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s');
Assert::stringNotEmpty($options['path'], 'The "path" option should be a non-empty string. Got: %s');
$path = Path::makeAbsolute($options['path'], $options['root-dir']);
$relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory());
$escPath = '__DIR__.' . var_export('/' . $relPath, true);
$targetMethod->getClass()->addImport(new Import('Puli\\Repository\\ChangeStream\\JsonChangeStream'));
$targetMethod->addBody(sprintf('$%s = new JsonChangeStream(%s);', $varName, $escPath));
}
示例12: generateNewInstance
/**
* {@inheritdoc}
*/
public function generateNewInstance($varName, Method $targetMethod, GeneratorRegistry $generatorRegistry, array $options = array())
{
Assert::keyExists($options, 'root-dir', 'The "root-dir" option is missing.');
if (!isset($options['path'])) {
$options['path'] = $targetMethod->getClass()->getDirectory() . '/bindings.json';
}
Assert::stringNotEmpty($options['root-dir'], 'The "root-dir" option should be a non-empty string. Got: %s');
Assert::stringNotEmpty($options['path'], 'The "path" option should be a non-empty string. Got: %s');
$path = Path::makeAbsolute($options['path'], $options['root-dir']);
$relPath = Path::makeRelative($path, $targetMethod->getClass()->getDirectory());
$escPath = '__DIR__.' . var_export('/' . $relPath, true);
$targetMethod->getClass()->addImport(new Import('Puli\\Discovery\\JsonDiscovery'));
$targetMethod->getClass()->addImport(new Import('Puli\\Discovery\\Binding\\Initializer\\ResourceBindingInitializer'));
$targetMethod->addBody(sprintf("\$%s = new JsonDiscovery(%s, array(\n new ResourceBindingInitializer(\$repo),\n));", $varName, $escPath));
}
示例13: filterReferences
public static function filterReferences($content, $callback)
{
return CssUtils::filterReferences($content, function ($matches) use($callback) {
// The referenced path is a repository path
// e.g. "/webmozart/puli/images/bg.png"
$referencedPath = $matches['url'];
// Ignore empty URLs
if ('' === $referencedPath) {
return $matches[0];
}
// Ignore non-local paths
if (!Path::isLocal($referencedPath)) {
return $matches[0];
}
// Ignore "data:" URLs
if (0 === strpos($referencedPath, 'data:')) {
return $matches[0];
}
// If the referenced path is not absolute, resolve it relative to
// the directory of the source file
if (!Path::isAbsolute($referencedPath)) {
$referencedPath = Path::makeAbsolute($referencedPath, $repoDir);
}
// The referenced asset must be known
if (!array_key_exists($referencedPath, $pathMap)) {
throw new AssetException(sprintf('The asset "%s" referenced in "%s" could not be found.', $matches['url'], $repoPath));
}
// The target path of the referenced file must be set
if (!$pathMap[$referencedPath]) {
throw new AssetException(sprintf('The referenced path "%s" in "%s" cannot be resolved, because ' . 'the target path of "%s" is not set.', $matches['url'], $repoPath, $matches['url']));
}
// The target path of the source file must be set
if (!$targetPath) {
throw new AssetException(sprintf('The referenced path "%s" in "%s" cannot be resolved, because ' . 'the target path of "%s" is not set.', $matches['url'], $repoPath, $repoPath));
}
// Get the relative path from the source directory to the reference
// e.g. "/css/style.css" + "/images/bg.png" = "../images/bg.png"
$relativePath = Path::makeRelative($pathMap[$referencedPath], $targetDir);
return str_replace($matches['url'], $relativePath, $matches[0]);
});
}
示例14: makeRelative
/**
* Turns a URL into a relative path.
*
* The result is a canonical path. This class is using functionality of Path class.
*
* @see Path
*
* @param string $url A URL to make relative.
* @param string $baseUrl A base URL.
*
* @return string
*
* @throws InvalidArgumentException If the URL and base URL does
* not match.
*/
public static function makeRelative($url, $baseUrl)
{
Assert::string($url, 'The URL must be a string. Got: %s');
Assert::string($baseUrl, 'The base URL must be a string. Got: %s');
Assert::contains($baseUrl, '://', '%s is not an absolute Url.');
list($baseHost, $basePath) = self::split($baseUrl);
if (false === strpos($url, '://')) {
if (0 === strpos($url, '/')) {
$host = $baseHost;
} else {
$host = '';
}
$path = $url;
} else {
list($host, $path) = self::split($url);
}
if ('' !== $host && $host !== $baseHost) {
throw new InvalidArgumentException(sprintf('The URL "%s" cannot be made relative to "%s" since their host names are different.', $host, $baseHost));
}
return Path::makeRelative($path, $basePath);
}
示例15: backup
/**
* {@inheritdoc}
*
* @throws FileExistsException
* @throws \InvalidArgumentException
* @throws FileNotFoundException
* @throws LogicException
*/
public function backup(Filesystem $source, Filesystem $destination, Database $database, array $parameter)
{
if (0 === $source->has($parameter['directory'])) {
$this->output->writeln(sprintf(' Directory "%s" not found.', $parameter['directory']));
return;
}
// TODO make it smoother
$files = $source->listFiles($parameter['directory'], true);
if (0 === count($files)) {
$this->output->writeln(sprintf(' No files found in directory "%s".', $parameter['directory']));
return;
}
$progressBar = new ProgressBar($this->output, count($files));
$progressBar->setOverwrite(true);
$progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%');
$progressBar->start();
foreach ($files as $file) {
$destination->writeStream(Path::makeRelative($file['path'], $parameter['directory']), $source->readStream($file['path']));
$progressBar->advance();
}
$progressBar->finish();
}