Skip to content

Commit

Permalink
Updates code, removes config, and adds ability to call method on User…
Browse files Browse the repository at this point in the history
… model
  • Loading branch information
Steven Peercy authored and Steven Peercy committed Mar 21, 2022
1 parent cf06f4f commit bde4f96
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 60 deletions.
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
"require": {
"statamic/cms": "^3.0"
},
"authors": [
{
"name": "Steven Peercy"
}
],
"support": {
"email": "steven@pttrn.co"
},
"autoload": {
"psr-4": {
"Pattern\\Silhouette\\": "src"
Expand All @@ -14,7 +22,7 @@
"extra": {
"statamic": {
"name": "Silhouette",
"description": "Dynamic user data for your static sites."
"description": "Dynamic user data for your static or cached sites."
},
"laravel": {
"providers": [
Expand Down
5 changes: 0 additions & 5 deletions config/silhouette.php

This file was deleted.

42 changes: 13 additions & 29 deletions src/Http/Controllers/SilhouetteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,26 @@

namespace Pattern\Silhouette\Http\Controllers;

use Illuminate\Http\Request;
use Statamic\Auth\Eloquent\User as StatamicUser;
use Statamic\Http\Controllers\Controller;

class SilhouetteController extends Controller
{
protected $methods;
protected $user;

public function __construct()
public function __invoke()
{
$this->middleware(function ($request, $next) {
$this->user = auth()->user() && config('silhouette.model')
? StatamicUser::fromModel(auth()->user())
: auth()->user();

return $next($request);
});

$this->methods = [
'email',
'initials'
];
}

public function __invoke(Request $request)
{
if ($this->user) {
if (auth()->check()) {
return response()
->json(
collect(
explode(',', $request->input('attributes'))
explode(',', request('attributes'))
)->flatMap(function ($attribute) {
$isMethod = false;
if (substr($attribute, -2) == '()') {
$attribute = substr($attribute, 0, -2);
$isMethod = true;
}
if ($attribute != 'password') {
return [
$attribute => $this->getAttribute($attribute)
$attribute => $this->getAttribute($attribute, $isMethod)
];
}
})
Expand All @@ -47,11 +31,11 @@ public function __invoke(Request $request)
return response()->json(false);
}

private function getAttribute($attribute)
private function getAttribute($attribute, $isMethod)
{
if (in_array($attribute, $this->methods)) {
return $this->user->{$attribute}();
if ($isMethod) {
return auth()->user()->{$attribute}();
}
return $this->user->value($attribute);
return auth()->user()->{$attribute};
}
}
3 changes: 2 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Pattern\Silhouette;

use Pattern\Silhouette\Tags\SilhouetteTag;
use Statamic\Providers\AddonServiceProvider;

class ServiceProvider extends AddonServiceProvider
Expand All @@ -11,7 +12,7 @@ class ServiceProvider extends AddonServiceProvider
];

protected $tags = [
\Pattern\Silhouette\Tags\SilhouetteTag::class,
SilhouetteTag::class,
];

public function boot()
Expand Down
36 changes: 12 additions & 24 deletions src/Tags/SilhouetteTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,39 @@ class SilhouetteTag extends Tags
{
protected static $handle = 'silhouette';

protected $attributes = 'name,email,avatar,initials';

public function index()
{
$attributes = $this->params->get('attributes') ?? 'name,email,avatar,initials';
$openingTag = '<div
x-data="{
silhouetteLoaded: false,
silhouette: {}
}"
$attributes = $this->params->get('attributes') ?? $this->attributes;
$open = '<div
x-data="{ silhouetteLoaded: false, silhouette: {} }"
x-init="
$watch(\'silhouette\', val => {
$dispatch(\'silhouette\', val);}
);
$watch(\'silhouette\', val => $dispatch(\'silhouette\', val));
$nextTick(() => {
fetch(\'/!/silhouette/user?attributes=' . $attributes . '\')
.then(response => response.json())
.then(json => {
setTimeout(() => silhouette = json, 50);
silhouetteLoaded = true;
})
.then(json => { silhouette = json; silhouetteLoaded = true; })
})
"
>';
$closingTag = '</div>';
$close = '</div>';

return $openingTag . $this->content . $closingTag;
return $open . $this->content . $close;
}

public function auth()
{
$openingTag = '<template x-if="silhouetteLoaded && silhouette">';
$closingTag = '</template>';

return $openingTag . $this->content . $closingTag;
return "<template x-if='silhouetteLoaded && silhouette' x-cloak>{$this->content}</template>";
}

public function guest()
{
$openingTag = '<template x-if="silhouetteLoaded && !silhouette">';
$closingTag = '</template>';

return $openingTag . $this->content . $closingTag;
return "<template x-if='silhouetteLoaded && !silhouette' x-cloak>{$this->content}</template>";
}

public function wildcard($tag)
{
return '<span x-text="silhouette.' . $tag . '"></span>';
return "<span x-text='silhouette.{$tag}'></span>";
}
}

0 comments on commit bde4f96

Please sign in to comment.