Filter invalid file types and use default list as fallback
parent
7dc44776f7
commit
f7175bc195
|
@ -112,7 +112,7 @@ class Indexer
|
||||||
public function index(): Promise
|
public function index(): Promise
|
||||||
{
|
{
|
||||||
return coroutine(function () {
|
return coroutine(function () {
|
||||||
$fileTypes = implode(',', $this->options->fileTypes);
|
$fileTypes = implode(',', $this->options->getFileTypes());
|
||||||
$pattern = Path::makeAbsolute('**/*{' . $fileTypes . '}', $this->rootPath);
|
$pattern = Path::makeAbsolute('**/*{' . $fileTypes . '}', $this->rootPath);
|
||||||
$uris = yield $this->filesFinder->find($pattern);
|
$uris = yield $this->filesFinder->find($pattern);
|
||||||
|
|
||||||
|
|
|
@ -9,29 +9,67 @@ class Options
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $fileTypes = [".php"];
|
private $fileTypes = [".php"];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \stdClass|null $options
|
* @param \Traversable|\stdClass|array|null $options
|
||||||
*/
|
*/
|
||||||
public function __construct(\stdClass $options = null)
|
public function __construct($options = null)
|
||||||
{
|
{
|
||||||
// Do nothing when the $options parameter is not an object
|
// Do nothing when the $options parameter is not an object
|
||||||
if (!is_object($options)) {
|
if (!is_object($options) && !is_array($options) && (!$options instanceof \Traversable)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->fileTypes = $options->fileTypes ?? $this->normalizeFileTypes($this->fileTypes);
|
foreach ($options as $option => $value) {
|
||||||
|
$method = 'set' . ucfirst($option);
|
||||||
|
|
||||||
|
call_user_func([$this, $method], $value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function normalizeFileTypes(array $fileTypes): array
|
/**
|
||||||
|
* Validate and set options for file types
|
||||||
|
*
|
||||||
|
* @param array $fileTypes List of file types
|
||||||
|
*/
|
||||||
|
public function setFileTypes(array $fileTypes)
|
||||||
{
|
{
|
||||||
return array_map(function (string $fileType) {
|
$fileTypes = filter_var_array($fileTypes, FILTER_SANITIZE_STRING);
|
||||||
if (substr($fileType, 0, 1) !== '.') {
|
$fileTypes = filter_var($fileTypes, FILTER_CALLBACK, ['options' => [$this, 'filterFileTypes']]);
|
||||||
$fileType = '.' . $fileType;
|
$fileTypes = array_filter($fileTypes);
|
||||||
}
|
|
||||||
|
|
||||||
|
$this->fileTypes = !empty($fileTypes) ? $fileTypes : $this->fileTypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get list of registered file types
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getFileTypes(): array
|
||||||
|
{
|
||||||
|
return $this->fileTypes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter valid file type
|
||||||
|
*
|
||||||
|
* @param string $fileType The file type to filter
|
||||||
|
* @return string|bool If valid it returns the file type, otherwise false
|
||||||
|
*/
|
||||||
|
private function filterFileTypes(string $fileType)
|
||||||
|
{
|
||||||
|
$fileType = trim($fileType);
|
||||||
|
|
||||||
|
if (empty($fileType)) {
|
||||||
return $fileType;
|
return $fileType;
|
||||||
}, $fileTypes);
|
}
|
||||||
|
|
||||||
|
if (substr($fileType, 0, 1) !== '.') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $fileType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue