2018-08-21 09:14:22 +01:00
|
|
|
<?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))
|
2018-08-21 09:38:08 +01:00
|
|
|
->map(function ($filename) {
|
2018-08-21 09:14:22 +01:00
|
|
|
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.
|
|
|
|
*/
|
2018-08-21 10:21:52 +01:00
|
|
|
protected function getFilePattern($directoryName)
|
2018-08-21 09:14:22 +01:00
|
|
|
{
|
2018-08-21 09:23:31 +01:00
|
|
|
return getcwd() . DIRECTORY_SEPARATOR . $directoryName . DIRECTORY_SEPARATOR . '*.php';
|
2018-08-21 09:14:22 +01:00
|
|
|
}
|
|
|
|
}
|