Skip to content

Commit

Permalink
Rewrite utility classes making use of collect()
Browse files Browse the repository at this point in the history
Refactor directives using new utility classes as well as allowing additional arguments to be passed for ACF directives such as post ID
Remove @getsub and @Getfield
Other misc. clean up
  • Loading branch information
Log1x committed Oct 30, 2018
1 parent 8ee48ac commit 6db6189
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 60 deletions.
128 changes: 96 additions & 32 deletions src/Directives/ACF.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

namespace App\Directives;
use App\Util;

return [

Expand All @@ -16,7 +15,15 @@

/** Create @fields() Blade directive */
'fields' => function ($expression) {
return "<?php if (have_rows({$expression})) : while (have_rows({$expression})) : the_row(); ?>";
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);

return "<?php if (have_rows({$expression->get(0)}, {$expression->get(1)}) : ?>".
"<?php while (have_rows({$expression->get(0)}, {$expression->get(1)})) : the_row(); ?>";
}

return "<?php if (have_rows({$expression})) : ?>".
"<?php while (have_rows({$expression})) : the_row(); ?>";
},

/** Create @endfields Blade directive */
Expand All @@ -26,33 +33,53 @@

/** Create @field() Blade directive */
'field' => function ($expression) {
if (count(Util::Args($expression)) > 1) {
[$key, $value] = Util::Args($expression);
return "<?= get_field({$key})[{$value}]; ?>";
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);

if (!empty($expression->get(2)) && !is_string($expression->get(2))) {
return "<?= get_field({$expression->get(0)}, {$expression->get(2)})[{$expression->get(1)}]; ?>";
}

if (!is_string($expression->get(1))) {
return "<?= get_field({$expression->get(0)}, {$expression->get(1)}); ?>";
}

return "<?= get_field({$expression->get(0)})[{$expression->get(1)}]; ?>";
}

return "<?= get_field({$expression}); ?>";
},

/** Create @getfield() Blade directive */
'getfield' => function ($expression) {
return "<?php return get_field({$expression}); ?>";
},

/** Create @hasfield() Blade directive */
'hasfield' => function ($expression) {
if (count(Util::Args($expression)) > 1) {
[$key, $value] = Util::Args($expression);
return "<?php if (get_field({$key})[{$value}]) : ?>";
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);

if (!empty($expression->get(2)) && !is_string($expression->get(2))) {
return "<?php if (get_field({$expression->get(0)}, {$expression->get(2)})[{$expression->get(1)}]) : ?>";
}

if (!is_string($expression->get(1))) {
return "<?php if (get_field({$expression->get(0)}, {$expression->get(1)})) : ?>";
}

return "<?php if (get_field({$expression->get(0)})[{$expression->get(1)}]) : ?>";
}

return "<?php if (get_field({$expression})) : ?>";
},

/** Create @isfield() Blade directive */
'isfield' => function ($expression) {
[$name, $value] = Util::Args($expression);
return "<?php if (get_field({$name}) == {$value}): ?>";
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);

if (!empty($expression->get(2)) && !is_string($expression->get(2))) {
return "<?php if (get_field({$expression->get(0)}, {$expression->get(2)}) == {$expression->get(1)}) : ?>";
}

return "<?php if (get_field({$expression->get(0)}) == {$expression->get(1)}) : ?>";
}
},

/** Create @endfield Blade directive */
Expand All @@ -62,33 +89,53 @@

/** Create @sub() Blade directive */
'sub' => function ($expression) {
if (count(Util::Args($expression)) > 1) {
[$key, $value] = Util::Args($expression);
return "<?= get_sub_field({$key})[{$value}]; ?>";
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);

if (!empty($expression->get(2)) && !is_string($expression->get(2))) {
return "<?= get_sub_field({$expression->get(0)}, {$expression->get(2)})[{$expression->get(1)}]; ?>";
}

if (!is_string($expression->get(1))) {
return "<?= get_sub_field({$expression->get(0)}, {$expression->get(1)})";
}

return "<?= get_sub_field({$expression->get(0)})[{$expression->get(1)}]; ?>";
}

return "<?= get_sub_field({$expression}); ?>";
},

/** Create @getsub() Blade directive */
'getsub' => function ($expression) {
return "<?php return get_sub_field({$expression}); ?>";
},

/** Create @hassub() Blade directive */
'hassub' => function ($expression) {
if (count(Util::Args($expression)) > 1) {
[$key, $value] = Util::Args($expression);
return "<?php if (get_sub_field({$key})[{$value}]) : ?>";
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);

if (!empty($expression->get(2)) && !is_string($expression->get(2))) {
return "<?php if (get_sub_field({$expression->get(0)}, {$expression->get(2)})[{$expression->get(1)}]) : ?>";
}

if (!is_string($expression->get(1))) {
return "<?php if (get_sub_field({$expression->get(0)}, {$expression->get(1)})) : ?>";
}

return "<?php if (get_sub_field({$expression->get(0)})[{$expression->get(1)}]) : ?>";
}

return "<?php if (get_sub_field({$expression})) : ?>";
},

/** Create @issub() Blade directive */
'issub' => function ($expression) {
[$name, $value] = Util::Args($expression);
return "<?php if (get_sub_field({$name}) == {$value}) : ?>";
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);

if (!empty($expression->get(2)) && !is_string($expression->get(2))) {
return "<?php if (get_sub_field({$expression->get(0)}, {$expression->get(2)}) == {$expression->get(1)}) : ?>";
}

return "<?php if (get_sub_field({$expression->get(0)}) == {$expression->get(1)}) : ?>";
}
},

/** Create @endsub Blade directive */
Expand All @@ -98,12 +145,20 @@

/** Create @layouts() Blade directive */
'layouts' => function ($expression) {
return "<?php while (have_rows({$expression})) : the_row(); ?>";
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);

return "<?php if (have_rows({$expression->get(0)}, {$expression->get(1)})) : ?>".
"<?php while (have_rows({$expression->get(0)}, {$expression->get(1)})) : the_row(); ?>";
}

return "<?php if (have_rows({$expression})) : ?>".
"<?php while (have_rows({$expression})) : the_row(); ?>";
},

/** Create @endlayouts Blade directive */
'endlayouts' => function () {
return "<?php endwhile; ?>";
return "<?php endwhile; endif; ?>";
},

/** Create @layout() Blade directive */
Expand All @@ -118,7 +173,15 @@

/** Create @group() Blade directive */
'group' => function ($expression) {
return "<?php if (have_rows({$expression})) : while (have_rows({$expression})) : the_row(); ?>";
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);

return "<?php if (have_rows({$expression->get(0)}, {$expression->get(1)})) : ?>".
"<?php while (have_rows({$expression->get(0)}, {$expression->get(1)})) : the_row(); ?>";
}

return "<?php if (have_rows({$expression})) : ?>".
"<?php while (have_rows({$expression})) : the_row(); ?>";
},

/** Create @endgroup Blade directive */
Expand All @@ -143,7 +206,8 @@

/** Create @options() Blade directive */
'options' => function ($expression) {
return "<?php if (have_rows({$expression}, 'options')) : while (have_rows({$expression}, 'options')) : the_row(); ?>";
return "<?php if (have_rows({$expression}, 'options')) : ?>".
"<?php while (have_rows({$expression}, 'options')) : the_row(); ?>";
},

/** Create @endoptions Blade directive */
Expand Down
34 changes: 20 additions & 14 deletions src/Directives/Helpers.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

namespace App\Directives;
use App\Util;

return [

Expand All @@ -15,14 +14,16 @@
*/

/** Create @asset() Blade directive */
'asset' => function ($asset) {
return "<?= App\\asset_path({$asset}); ?>";
'asset' => function ($expression) {
return "<?= App\\asset_path({$expression}); ?>";
},

/** Create @condition() Blade directive */
'condition' => function ($expression) {
[$condition, $value] = Util::Args($expression);
return "<?php if ($condition) { echo $value; } ?>";
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);
return "<?php if ({$expression->get(0)}) { echo {$expression->get(1)}; } ?>";
}
},

/** Create @global() Blade directive */
Expand All @@ -32,30 +33,35 @@

/** Create @set() Blade directive */
'set' => function ($expression) {
[$name, $value] = Util::Args($expression);
return "<?php $name = $value; ?>";
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);
return "<?php {$expression->get(0)} = {$expression->get(1)}; ?>";
}
},

/** Create @unset() Blade directive */
'unset' => function ($expression) {
$expression = Util::Args($expression);
return "<?php unset {$expression}; ?>";
return "<?php unset({$expression}); ?>";
},

/** Create @extract() Blade directive */
'extract' => function ($expression) {
return "<?php @extract({$expression}); ?>";
return "<?php extract({$expression}); ?>";
},

/** Create @explode() Blade directive */
'explode' => function ($expression) {
[$delimiter, $string] = Util::Args($expression);
return "<?= explode({$delimiter}, {$string}); ?>";
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);
return "<?= explode({$expression->get(0)}, {$expression->get(1)}); ?>";
}
},

/** Create @implode() Blade directive */
'implode' => function ($expression) {
[$delimiter, $array] = Util::Args($expression);
return "<?= implode({$delimiter}, {$array}); ?>";
if (str_contains($expression, ',')) {
$expression = Util::parse($expression);
return "<?= implode({$expression->get(0)}, {$expression->get(1)}); ?>";
}
},
];
19 changes: 10 additions & 9 deletions src/Directives/WordPress.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

namespace App\Directives;
use App\Util;

return [

Expand All @@ -16,41 +15,43 @@

/** Create @query() Blade directive */
'query' => function ($expression) {
return "<?php global \$query; \$query = new WP_Query({$expression}); ?>";
return "<?php global \$query; ?>".
"<?php \$query = new WP_Query({$expression}); ?>";
},

/** Create @posts Blade directive */
'posts' => function () {
return '<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>';
return "<?php if (\$query->have_posts()) : ?>".
"<?php while (\$query->have_posts()) : \$query->the_post(); ?>";
},

/** Create @endposts Blade directive */
'endposts' => function () {
return '<?php endwhile; wp_reset_postdata(); endif; ?>';
return "<?php endwhile; wp_reset_postdata(); endif; ?>";
},

/** Create @shortcode() Blade directive */
'shortcode' => function ($expression) {
return "<?= do_shortcode($expression); ?>";
return "<?= do_shortcode({$expression}); ?>";
},

/** Create @user Blade directive */
'user' => function () {
return '<?php if (is_user_logged_in()) : ?>';
return "<?php if (is_user_logged_in()) : ?>";
},

/** Create @enduser Blade directive */
'enduser' => function () {
return '<?php endif; ?>';
return "<?php endif; ?>";
},

/** Create @guest Blade directive */
'guest' => function () {
return '<?php if (!is_user_logged_in()) : ?>';
return "<?php if (!is_user_logged_in()) : ?>";
},

/** Create @endguest Blade directive */
'endguest' => function () {
return '<?php endif; ?>';
return "<?php endif; ?>";
},
];
24 changes: 19 additions & 5 deletions src/Utilities.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
<?php

namespace App;
namespace App\Directives;

class Util
{
/**
* Removes the paranthesis passed through the Blade directive when multiple arguments are present.
* Parse expression passed to directive.
*
* @param mixed $args
* @param string $expression
* @return \Illuminate\Support\Collection
*/
public static function parse($expression)
{
return collect(explode(',', $expression))
->map(function($item) {
return trim($item);
});
}

/**
* Strip single quotes from expression.
*
* @param string $expression
* @return string
*/
public static function Args($args)
public static function strip($expression)
{
return explode(', ', strtr($args, ['(' => '', ')' => '']));
return str_replace("'", '', $expression);
}
}

0 comments on commit 6db6189

Please sign in to comment.