Skip to content

Commit

Permalink
v1.4.4 (#20)
Browse files Browse the repository at this point in the history
* enhance(partials): Move partial `get()` method to a Concern
* fix(partials): Fix partial `get()` method when passed a path
* enhance(acf-composer): Move field group composing to the boot() method so field classes can access Acorn properly
  • Loading branch information
Log1x authored Jul 6, 2020
1 parent 671e966 commit 1abb571
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 48 deletions.
48 changes: 21 additions & 27 deletions src/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,37 @@

namespace Log1x\AcfComposer;

use ReflectionClass;
use Log1x\AcfComposer\Contracts\Fields as FieldsContract;
use StoutLogic\AcfBuilder\FieldsBuilder;
use Roots\Acorn\Application;
use Roots\Acorn\Filesystem\Filesystem;
use Illuminate\Support\Str;

abstract class Composer implements FieldsContract
{
use Concerns\RetrievesPartials;

/**
* The application instance.
*
* @var \Roots\Acorn\Application
*/
protected $app;

/**
* The filesystem instance.
*
* @var \Roots\Acorn\Filesystem\Filesystem
*/
protected $files;

/**
* The field keys.
*
* @var array
*/
protected $keys = ['fields', 'sub_fields', 'layouts'];

/**
* The field groups.
*
Expand All @@ -40,6 +56,7 @@ abstract class Composer implements FieldsContract
public function __construct(Application $app)
{
$this->app = $app;
$this->files = new Filesystem();

$this->defaults = collect(
$this->app->config->get('acf.defaults')
Expand Down Expand Up @@ -72,7 +89,7 @@ protected function register()
return acf_add_local_field_group(
$this->build($this->fields)
);
});
}, 20);
}

/**
Expand All @@ -85,14 +102,14 @@ protected function build($fields = [])
{
return collect($fields)->map(function ($value, $key) {
if (
! Str::contains($key, ['fields', 'sub_fields', 'layouts']) ||
! Str::contains($key, $this->keys) ||
(Str::is($key, 'type') && ! $this->defaults->has($value))
) {
return $value;
}

return array_map(function ($field) {
if (collect($field)->keys()->intersect(['fields', 'sub_fields', 'layouts'])->isNotEmpty()) {
if (collect($field)->keys()->intersect($this->keys)->isNotEmpty()) {
return $this->build($field);
}

Expand All @@ -101,29 +118,6 @@ protected function build($fields = [])
})->all();
}

/**
* Returns a field partial instance.
*
* @param mixed $partial
* @return mixed
*/
protected function get($partial = null)
{
if (
is_subclass_of($partial, Partial::class) &&
! (new ReflectionClass($partial))->isAbstract()
) {
return (new $partial($this->app))->compose();
}

return is_string($partial) ? include $this->app->path(
Str::finish(
Str::finish($path, '/'),
Str::finish($name, '.php')
)
) : $partial;
}

/**
* {@inheritdoc}
*/
Expand Down
40 changes: 40 additions & 0 deletions src/Concerns/RetrievesPartials.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Log1x\AcfComposer\Concerns;

use ReflectionClass;
use Illuminate\Support\Str;
use Log1x\AcfComposer\Partial;
use StoutLogic\AcfBuilder\FieldsBuilder;

trait RetrievesPartials
{
/**
* Compose a field partial instance or file.
*
* @param mixed $partial
* @return array
*/
protected function get($partial = null)
{
if (
is_subclass_of($partial, Partial::class) &&
! (new ReflectionClass($partial))->isAbstract()
) {
return (new $partial($this->app))->compose();
}

if (is_a($partial, FieldsBuilder::class) || is_array($partial)) {
return $partial;
}

return $this->files->exists(
$partial = $this->app->path(
Str::finish(
strtr($partial, ['.php' => '', '.' => '/']),
'.php'
)
)
) ? include $partial : [];
}
}
50 changes: 29 additions & 21 deletions src/Providers/AcfComposerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AcfComposerServiceProvider extends ServiceProvider
/**
* Default Paths
*
* @var array
* @var \Illuminate\Support\Collection
*/
protected $paths = [
'Fields',
Expand All @@ -35,26 +35,6 @@ public function register()
})->filter(function ($path) {
return is_dir($path);
});

if ($this->paths->isEmpty() || ! function_exists('acf')) {
return;
}

foreach ((new Finder())->in($this->paths->all())->files() as $composer) {
$composer = $this->app->getNamespace() . str_replace(
['/', '.php'],
['\\', ''],
Str::after($composer->getPathname(), $this->app->path() . DIRECTORY_SEPARATOR)
);

if (
is_subclass_of($composer, Composer::class) &&
! is_subclass_of($composer, Partial::class) &&
! (new ReflectionClass($composer))->isAbstract()
) {
(new $composer($this->app))->compose();
}
}
}

/**
Expand All @@ -64,6 +44,10 @@ public function register()
*/
public function boot()
{
if (function_exists('acf') && ! $this->paths->isEmpty()) {
$this->compose();
}

$this->publishes([
__DIR__ . '/../../config/acf.php' => $this->app->configPath('acf.php'),
], 'config');
Expand All @@ -76,4 +60,28 @@ public function boot()
\Log1x\AcfComposer\Console\OptionsMakeCommand::class,
]);
}

/**
* Find and compose the available field groups.
*
* @return void
*/
public function compose()
{
foreach ((new Finder())->in($this->paths->all())->files() as $composer) {
$composer = $this->app->getNamespace() . str_replace(
['/', '.php'],
['\\', ''],
Str::after($composer->getPathname(), $this->app->path() . DIRECTORY_SEPARATOR)
);

if (
is_subclass_of($composer, Composer::class) &&
! is_subclass_of($composer, Partial::class) &&
! (new ReflectionClass($composer))->isAbstract()
) {
(new $composer($this->app))->compose();
}
}
}
}

0 comments on commit 1abb571

Please sign in to comment.