Add support for multiple address file directories

Fixes #13
This commit is contained in:
Oliver Davies 2018-12-27 22:00:52 +00:00
parent f80587b493
commit 6aeeb5b2c4
2 changed files with 18 additions and 14 deletions

View file

@ -7,11 +7,6 @@ namespace Opdavies\GmailFilterBuilder\Service;
*/
class Addresses
{
/**
* The name of the default directory containing the address files.
*/
const DIRECTORY_NAME = '.gmail-filters';
/**
* Load addresses from a file.
*
@ -21,7 +16,15 @@ class Addresses
*/
public static function load($filename = 'my-addresses.php')
{
if (file_exists($file = (new static())->getDirectoryPath() . $filename)) {
$file = (new static())->getDirectoryPaths()
->map(function ($path) use ($filename) {
return $path . $filename;
})
->first(function ($file) {
return file_exists($file);
});
if ($file) {
return include $file;
}
@ -29,14 +32,15 @@ class Addresses
}
/**
* Get the directory name containing the addresses file.
* Get the potential directory names containing the addresses file.
*
* Defaults to a .gmail-filters directory within the user's home directory.
*
* @return string
* @return \Illuminate\Support\Collection
*/
protected function getDirectoryPath()
protected function getDirectoryPaths()
{
return getenv('HOME') . DIRECTORY_SEPARATOR . self::DIRECTORY_NAME . DIRECTORY_SEPARATOR;
return collect([
getenv('HOME') . DIRECTORY_SEPARATOR . '.gmail-filters' . DIRECTORY_SEPARATOR,
getenv('HOME') . DIRECTORY_SEPARATOR . '.config/gmail-filters' . DIRECTORY_SEPARATOR,
]);
}
}

View file

@ -29,8 +29,8 @@ class FakeAddresses extends Addresses
/**
* {@inheritdoc}
*/
protected function getDirectoryPath()
protected function getDirectoryPaths()
{
return __DIR__ . '/../../stubs/addresses/';
return collect(__DIR__ . '/../../stubs/addresses/');
}
}