diff --git a/src/Service/Partials.php b/src/Service/Partials.php new file mode 100644 index 0000000..7b7d857 --- /dev/null +++ b/src/Service/Partials.php @@ -0,0 +1,43 @@ +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'; + } +} diff --git a/tests/Unit/Service/PartialsTest.php b/tests/Unit/Service/PartialsTest.php new file mode 100644 index 0000000..702c3e6 --- /dev/null +++ b/tests/Unit/Service/PartialsTest.php @@ -0,0 +1,36 @@ +assertCount(3, $filters); + + $this->assertSame('foo@example.com', $filters[0]->getProperties()['from'][0]); + $this->assertSame('bar@example.com', $filters[1]->getProperties()['from'][0]); + $this->assertSame('baz@example.com', $filters[2]->getProperties()['from'][0]); + } +} + +class FakePartials extends Partials +{ + /** + * {@inheritdoc} + */ + protected function getFilePattern(string $directoryName) + { + return __DIR__ . '/../../stubs/filters/*.php'; + } +} diff --git a/tests/stubs/filters/a.php b/tests/stubs/filters/a.php new file mode 100644 index 0000000..8c09331 --- /dev/null +++ b/tests/stubs/filters/a.php @@ -0,0 +1,13 @@ +from('foo@example.com') + ->labelAndArchive('Test'), + + Filter::create() + ->from('bar@example.com') + ->labelAndArchive('Test 2'), +]; diff --git a/tests/stubs/filters/b.php b/tests/stubs/filters/b.php new file mode 100644 index 0000000..42cfc61 --- /dev/null +++ b/tests/stubs/filters/b.php @@ -0,0 +1,9 @@ +from('baz@example.com') + ->labelAndArchive('Test 3'), +];