Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Use class name instead of self #614

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/AssetManipulationList.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ protected function getAssetKey($asset)
public function addAsset($asset, $state)
{
switch ($state) {
case self::STATE_PUBLIC:
case AssetManipulationList::STATE_PUBLIC:
return $this->addPublicAsset($asset);
case self::STATE_PROTECTED:
case AssetManipulationList::STATE_PROTECTED:
return $this->addProtectedAsset($asset);
case self::STATE_DELETED:
case AssetManipulationList::STATE_DELETED:
return $this->addDeletedAsset($asset);
default:
throw new \InvalidArgumentException("Invalid state {$state}");
Expand Down
26 changes: 13 additions & 13 deletions src/Dev/TestAssetStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ public static function activate($basedir)
Director::config()->set('alternate_base_url', '/');
DBFile::config()->set('force_resample', false);
File::config()->set('force_resample', false);
self::reset();
self::$basedir = $basedir;
TestAssetStore::reset();
TestAssetStore::$basedir = $basedir;

// Ensure basedir exists
SSFilesystem::makeFolder(self::base_path());
SSFilesystem::makeFolder(TestAssetStore::base_path());
}

/**
Expand All @@ -111,10 +111,10 @@ public static function activate($basedir)
*/
public static function base_path()
{
if (!self::$basedir) {
if (!TestAssetStore::$basedir) {
return null;
}
return ASSETS_PATH . '/' . self::$basedir;
return ASSETS_PATH . '/' . TestAssetStore::$basedir;
}

/**
Expand All @@ -123,14 +123,14 @@ public static function base_path()
public static function reset()
{
// Remove all files in this store
if (self::$basedir) {
$path = self::base_path();
if (TestAssetStore::$basedir) {
$path = TestAssetStore::base_path();
if (file_exists($path ?? '')) {
SSFilesystem::removeFolder($path);
}
}
self::$seekable_override = null;
self::$basedir = null;
TestAssetStore::$seekable_override = null;
TestAssetStore::$basedir = null;
}

/**
Expand All @@ -144,7 +144,7 @@ public static function reset()
public static function getLocalPath(AssetContainer $asset, $forceProtected = false, $relative = false)
{
if ($asset instanceof Folder) {
return self::base_path() . '/' . $asset->getFilename();
return TestAssetStore::base_path() . '/' . $asset->getFilename();
}
if ($asset instanceof File) {
$asset = $asset->File;
Expand Down Expand Up @@ -223,10 +223,10 @@ public function isGranted($fileID)

protected function isSeekableStream($stream)
{
if (isset(self::$seekable_override)) {
if (isset(TestAssetStore::$seekable_override)) {
// Unset the override so we don't get stuck in an infinite loop
self::$seekable_override = null;
return self::$seekable_override;
TestAssetStore::$seekable_override = null;
return TestAssetStore::$seekable_override;
}
return parent::isSeekableStream($stream);
}
Expand Down
34 changes: 17 additions & 17 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public function canEdit($member = null)
return $result;
}

if (Permission::checkMember($member, self::EDIT_ALL)) {
if (Permission::checkMember($member, File::EDIT_ALL)) {
return true;
}

Expand Down Expand Up @@ -455,7 +455,7 @@ public function canCreate($member = null, $context = [])
return $result;
}

if (Permission::checkMember($member, self::EDIT_ALL)) {
if (Permission::checkMember($member, File::EDIT_ALL)) {
return true;
}

Expand Down Expand Up @@ -491,7 +491,7 @@ public function canDelete($member = null)
}

// Default permission check
if (Permission::checkMember($member, self::EDIT_ALL)) {
if (Permission::checkMember($member, File::EDIT_ALL)) {
return true;
}

Expand Down Expand Up @@ -529,21 +529,21 @@ private function hasRestrictedPermissions(File $file): bool
InheritedPermissions::ONLY_THESE_USERS,
InheritedPermissions::ONLY_THESE_MEMBERS,
])) {
self::$has_restricted_permissions_cache[$id] = true;
File::$has_restricted_permissions_cache[$id] = true;
return true;
}
if ($canViewType == InheritedPermissions::INHERIT && $parentID != 0) {
if (isset(self::$has_restricted_permissions_cache[$parentID])) {
return self::$has_restricted_permissions_cache[$parentID];
if (isset(File::$has_restricted_permissions_cache[$parentID])) {
return File::$has_restricted_permissions_cache[$parentID];
}
$parent = $file->Parent();
if ($parent->exists()) {
$value = $this->hasRestrictedPermissions($parent);
self::$has_restricted_permissions_cache[$parentID] = $value;
File::$has_restricted_permissions_cache[$parentID] = $value;
return $value;
}
}
self::$has_restricted_permissions_cache[$id] = false;
File::$has_restricted_permissions_cache[$id] = false;
return false;
}

Expand Down Expand Up @@ -666,7 +666,7 @@ public static function get_category_extensions($categories)
*/
public function appCategory()
{
return self::get_app_category($this->getExtension());
return File::get_app_category($this->getExtension());
}

/**
Expand Down Expand Up @@ -754,7 +754,7 @@ public function onAfterRevertToLive()
{
// Force query of draft object and update (as source record is bound to live stage)
if (class_exists(Versioned::class) &&
$draftRecord = Versioned::get_by_stage(self::class, Versioned::DRAFT)->byID($this->ID)
$draftRecord = Versioned::get_by_stage(File::class, Versioned::DRAFT)->byID($this->ID)
) {
$draftRecord->updateDependantObjects();
}
Expand Down Expand Up @@ -998,7 +998,7 @@ public function setFilename($filename)
*/
public function getExtension()
{
return self::get_file_extension($this->Name);
return File::get_file_extension($this->Name);
}

/**
Expand Down Expand Up @@ -1053,7 +1053,7 @@ public static function get_icon_for_extension($extension)
*/
public function getFileType()
{
return self::get_file_type($this->getFilename());
return File::get_file_type($this->getFilename());
}

/**
Expand All @@ -1064,10 +1064,10 @@ public function getFileType()
*/
public static function get_file_type($filename)
{
$file_types = self::config()->get('file_types');
$file_types = static::config()->get('file_types');

// Get extension
$extension = strtolower(self::get_file_extension($filename) ?? '');
$extension = strtolower(File::get_file_extension($filename) ?? '');

if (isset($file_types[$extension])) {
return _t(
Expand Down Expand Up @@ -1154,7 +1154,7 @@ public function validate()
*/
public static function get_class_for_file_extension($ext)
{
$map = array_change_key_case(self::config()->get('class_for_file_extension') ?? [], CASE_LOWER);
$map = array_change_key_case(static::config()->get('class_for_file_extension') ?? [], CASE_LOWER);
return (array_key_exists(strtolower($ext ?? ''), $map ?? [])) ? $map[strtolower($ext)] : $map['*'];
}

Expand All @@ -1175,7 +1175,7 @@ public static function set_class_for_file_extension($exts, $class)
sprintf('Class "%s" (for extension "%s") is not a valid subclass of File', $class, $ext)
);
}
self::config()->merge('class_for_file_extension', [$ext => $class]);
static::config()->merge('class_for_file_extension', [$ext => $class]);
}
}

Expand Down Expand Up @@ -1415,7 +1415,7 @@ public function getPermissionChecker()
public function providePermissions()
{
return [
self::EDIT_ALL => [
File::EDIT_ALL => [
'name' => _t(__CLASS__.'.EDIT_ALL_DESCRIPTION', 'Edit any file'),
'category' => _t('SilverStripe\\Security\\Permission.CONTENT_CATEGORY', 'Content permissions'),
'sort' => -100,
Expand Down
2 changes: 1 addition & 1 deletion src/FileFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ protected function acceptDir($basename, $pathname, $depth)
}
}

if ($this->getOption('ignore_vcs') && in_array($basename, self::$vcs_dirs)) {
if ($this->getOption('ignore_vcs') && in_array($basename, FileFinder::$vcs_dirs)) {
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions src/FilenameParsing/AbstractFileIDHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function buildFileID($filename, $hash = null, $variant = null, $cleanfile
}

if ($variant) {
$filename = $this->swapExtension($filename, $variant, self::EXTENSION_VARIANT);
$filename = $this->swapExtension($filename, $variant, AbstractFileIDHelper::EXTENSION_VARIANT);
}

$name = basename($filename ?? '');
Expand Down Expand Up @@ -79,7 +79,7 @@ public function buildFileID($filename, $hash = null, $variant = null, $cleanfile
* or the variant extension.
*
* @param string $filename Original filename without variant
* @param int $extIndex One of self::EXTENSION_ORIGINAL or self::EXTENSION_VARIANT
* @param int $extIndex One of AbstractFileIDHelper::EXTENSION_ORIGINAL or AbstractFileIDHelper::EXTENSION_VARIANT
*/
protected function swapExtension(string $filename, string $variant, int $extIndex): string
{
Expand All @@ -102,7 +102,7 @@ protected function swapExtension(string $filename, string $variant, int $extInde

// Loop our variant list until we find our special file extension swap variant
// Reverse the list first so the variant extension we find is the last extension rewrite variant in a chain
$extSwapVariant = preg_quote(self::EXTENSION_REWRITE_VARIANT, '/');
$extSwapVariant = preg_quote(AbstractFileIDHelper::EXTENSION_REWRITE_VARIANT, '/');
foreach (array_reverse($subVariants) as $subVariant) {
if (preg_match("/^$extSwapVariant(?<base64>.+)$/", $subVariant, $matches)) {
// This array always contain 2 values: The original extension at index 0 and the variant extension at index 1
Expand Down
4 changes: 2 additions & 2 deletions src/FilenameParsing/HashFileIDHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function parseFileID($fileID)
$variant = $matches['variant'] ?: '';

if ($variant) {
$filename = $this->swapExtension($filename, $variant, self::EXTENSION_ORIGINAL);
$filename = $this->swapExtension($filename, $variant, HashFileIDHelper::EXTENSION_ORIGINAL);
}

return new ParsedFileID(
Expand Down Expand Up @@ -82,6 +82,6 @@ protected function getFileIDBase($shortFilename, $fullFilename, $hash, $variant)
*/
private function truncate($hash)
{
return substr($hash ?? '', 0, self::HASH_TRUNCATE_LENGTH);
return substr($hash ?? '', 0, HashFileIDHelper::HASH_TRUNCATE_LENGTH);
}
}
2 changes: 1 addition & 1 deletion src/FilenameParsing/NaturalFileIDHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function parseFileID($fileID)
$variant = $matches['variant'] ?: '';

if ($variant) {
$filename = $this->swapExtension($filename, $variant, self::EXTENSION_ORIGINAL);
$filename = $this->swapExtension($filename, $variant, NaturalFileIDHelper::EXTENSION_ORIGINAL);
}

return new ParsedFileID(
Expand Down
16 changes: 8 additions & 8 deletions src/FilenameParsing/ParsedFileID.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,37 +86,37 @@ public function getTuple()

/**
* @param string $fileID
* @return self
* @return ParsedFileID
*/
public function setFileID($fileID)
{
return new self($this->filename, $this->hash, $this->variant, $fileID);
return new ParsedFileID($this->filename, $this->hash, $this->variant, $fileID);
}

/**
* @param string $filename
* @return self
* @return ParsedFileID
*/
public function setFilename($filename)
{
return new self($filename, $this->hash, $this->variant, $this->fileID);
return new ParsedFileID($filename, $this->hash, $this->variant, $this->fileID);
}

/**
* @param string $variant
* @return self
* @return ParsedFileID
*/
public function setVariant($variant)
{
return new self($this->filename, $this->hash, $variant, $this->fileID);
return new ParsedFileID($this->filename, $this->hash, $variant, $this->fileID);
}

/**
* @param string $hash
* @return self
* @return ParsedFileID
*/
public function setHash($hash)
{
return new self($this->filename, $hash, $this->variant, $this->fileID);
return new ParsedFileID($this->filename, $hash, $this->variant, $this->fileID);
}
}
8 changes: 4 additions & 4 deletions src/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Filesystem
public static function makeFolder($folder)
{
if (!file_exists($base = dirname($folder ?? ''))) {
self::makeFolder($base);
Filesystem::makeFolder($base);
}
if (!file_exists($folder ?? '')) {
mkdir($folder ?? '', static::config()->folder_create_mask ?? 0);
Expand Down Expand Up @@ -93,7 +93,7 @@ public static function remove_folder_if_empty($folder, $recursive = true)
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
// if an empty folder is detected, remove that one first and move on
if ($recursive && is_dir($entry ?? '') && self::remove_folder_if_empty($entry)) {
if ($recursive && is_dir($entry ?? '') && Filesystem::remove_folder_if_empty($entry)) {
continue;
}
// if a file was encountered, or a subdirectory was not empty, return false.
Expand Down Expand Up @@ -124,7 +124,7 @@ public static function folderModTime($folder, $extensionList = null)
if ($item[0] != '.') {
// Recurse into folders
if (is_dir("$folder/$item")) {
$modTime = max($modTime, self::folderModTime("$folder/$item", $extensionList));
$modTime = max($modTime, Filesystem::folderModTime("$folder/$item", $extensionList));

// Check files
} else {
Expand All @@ -139,7 +139,7 @@ public static function folderModTime($folder, $extensionList = null)
}
}

//if(!$recursiveCall) self::$cache_folderModTime[$cacheID] = $modTime;
//if(!$recursiveCall) Filesystem::$cache_folderModTime[$cacheID] = $modTime;
return $modTime;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Flysystem/AssetAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class AssetAdapter extends LocalFilesystemAdapter
]
];

public function __construct($root = null, $writeFlags = LOCK_EX, $linkHandling = self::DISALLOW_LINKS)
public function __construct($root = null, $writeFlags = LOCK_EX, $linkHandling = AssetAdapter::DISALLOW_LINKS)
{
// Get root path, and ensure that this exists and is safe
$root = $this->findRoot($root);
Expand Down
Loading
Loading