Add ability to load filters from partials

Issue #12
This commit is contained in:
Oliver Davies 2018-08-21 09:14:22 +01:00
parent bac720c679
commit d8b10d8f49
4 changed files with 101 additions and 0 deletions

43
src/Service/Partials.php Normal file
View file

@ -0,0 +1,43 @@
<?php
namespace Opdavies\GmailFilterBuilder\Service;
/**
* Load and combine partials from multiple files.
*/
class Partials
{
/**
* Load partials from a directory.
*
* @param string $directoryName The name of the directory containing the partials.
*
* @return array The loaded filters.
*/
public static function load($directoryName = 'filters')
{
$files = (new static())->getFilePattern($directoryName);
return collect(glob($files))
->map(function (string $filename) {
return include $filename;
})
->flatten(1)
->all();
}
/**
* Build the glob pattern to load partials from the directory.
*
* Assumes it is always relative to the current directory, and that the
* filters are always in files with a .php extension.
*
* @param string $directoryName The name of the directory.
*
* @return string The full path.
*/
protected function getFilePattern(string $directoryName)
{
return __DIR__ . DIRECTORY_SEPARATOR . $directoryName . DIRECTORY_SEPARATOR . '/*.php';
}
}