Webmozart\PathUtil\Path::isBasePath PHP Method

isBasePath() public static method

Dot segments ("." and "..") are removed/collapsed and all slashes turned into forward slashes. php Path::isBasePath('/webmozart', '/webmozart/css'); => true Path::isBasePath('/webmozart', '/webmozart'); => true Path::isBasePath('/webmozart', '/webmozart/..'); => false Path::isBasePath('/webmozart', '/puli'); => false
Since: 1.0 Added method.
Since: 2.0 Method now fails if $basePath or $ofPath is not a string.
public static isBasePath ( string $basePath, string $ofPath ) : boolean
$basePath string The base path to test.
$ofPath string The other path.
return boolean Whether the base path is a base path of the other path.
    public static function isBasePath($basePath, $ofPath)
    {
        Assert::string($basePath, 'The base path must be a string. Got: %s');
        $basePath = self::canonicalize($basePath);
        $ofPath = self::canonicalize($ofPath);
        // Append slashes to prevent false positives when two paths have
        // a common prefix, for example /base/foo and /base/foobar.
        // Don't append a slash for the root "/", because then that root
        // won't be discovered as common prefix ("//" is not a prefix of
        // "/foobar/").
        return 0 === strpos($ofPath . '/', rtrim($basePath, '/') . '/');
    }

Usage Example

Ejemplo n.º 1
0
 protected static function configureDir(Event $event, $name, $defaultInSkeleton, $prefix = '', $chmod = true)
 {
     $default = static::getOption($event, $name . '-dir', $defaultInSkeleton);
     $validator = function ($value) use($prefix, $name) {
         if ($prefix) {
             $basePath = Path::makeAbsolute($prefix, getcwd());
             $path = Path::makeAbsolute($value, $basePath);
             if (!Path::isBasePath($basePath, $path)) {
                 throw new \RuntimeException("The {$name} directory must be inside the {$prefix} directory.");
             }
         }
         return Path::canonicalize($value);
     };
     $default = $validator($default);
     $relative = $prefix ? '<comment>' . $prefix . '</comment>' : 'project root';
     $question = sprintf('<info>Where do you want your <comment>%s</comment> directory? (relative to %s) [default: <comment>%s</comment>] </info>', $name, $relative, $default);
     $dir = $event->getIO()->askAndValidate($question, $validator, null, $default);
     $fs = new Filesystem();
     $origin = $prefix . $defaultInSkeleton;
     $target = $prefix . $dir;
     $dirMode = static::configureDirMode($event);
     if ($dir !== $defaultInSkeleton) {
         $event->getIO()->writeError(sprintf('Moving <info>%s</info> directory from <info>%s</info> to <info>%s</info>', $name, $origin, $target));
         $fs->mkdir(dirname($target));
         // ensure parent directory exists
         $fs->rename($origin, $target);
     }
     if ($chmod) {
         $it = (new Finder())->directories()->in($target)->append([$target]);
         $fs->chmod($it, $dirMode);
     }
     return $target;
 }
All Usage Examples Of Webmozart\PathUtil\Path::isBasePath