-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH Refactor FileIDHelper classes to reduce code duplication (#587)
- Loading branch information
1 parent
2ed92f8
commit 3326749
Showing
3 changed files
with
94 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Assets\FilenameParsing; | ||
|
||
use SilverStripe\Core\Injector\Injectable; | ||
|
||
abstract class AbstractFileIDHelper implements FileIDHelper | ||
{ | ||
use Injectable; | ||
|
||
public function buildFileID($filename, $hash = null, $variant = null, $cleanfilename = true) | ||
{ | ||
if ($filename instanceof ParsedFileID) { | ||
$hash = $filename->getHash(); | ||
$variant = $filename->getVariant(); | ||
$filename = $filename->getFilename(); | ||
} | ||
|
||
$this->validateFileParts($filename, $hash, $variant); | ||
|
||
// Since we use double underscore to delimit variants, eradicate them from filename | ||
if ($cleanfilename) { | ||
$filename = $this->cleanFilename($filename); | ||
} | ||
|
||
$name = basename($filename ?? ''); | ||
|
||
// Split extension | ||
$extension = null; | ||
if (($pos = strpos($name ?? '', '.')) !== false) { | ||
$extension = substr($name ?? '', $pos ?? 0); | ||
$name = substr($name ?? '', 0, $pos); | ||
} | ||
|
||
$fileID = $this->getFileIDBase($name, $filename, $hash, $variant); | ||
|
||
// Add directory | ||
$dirname = ltrim(dirname($filename ?? ''), '.'); | ||
if ($dirname) { | ||
$fileID = $dirname . '/' . $fileID; | ||
} | ||
|
||
// Add variant | ||
if ($variant) { | ||
$fileID .= '__' . $variant; | ||
} | ||
|
||
// Add extension | ||
if ($extension) { | ||
$fileID .= $extension; | ||
} | ||
|
||
return $fileID; | ||
} | ||
|
||
public function cleanFilename($filename) | ||
{ | ||
// Swap backslash for forward slash | ||
$filename = str_replace('\\', '/', $filename ?? ''); | ||
|
||
// Since we use double underscore to delimit variants, eradicate them from filename | ||
return preg_replace('/_{2,}/', '_', $filename ?? ''); | ||
} | ||
|
||
public function lookForVariantRecursive(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
abstract protected function getFileIDBase($shortFilename, $fullFilename, $hash, $variant): string; | ||
|
||
abstract protected function validateFileParts($filename, $hash, $variant): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters