From 13f50a6f0553a83955ffa434285d6b3c6cda6c47 Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Thu, 19 Oct 2023 09:54:01 +0200 Subject: [PATCH] Introduced `Vue` attribute --- README.md | 8 ++++++-- app/app/View/Components/BladeMethod.php | 6 +++--- app/app/View/Components/BladeMethodCallbacks.php | 6 +++--- app/app/View/Components/ChangeBladeProp.php | 4 +++- app/app/View/Components/Form.php | 8 -------- app/app/View/Components/Layout.php | 8 -------- src/Attributes/Vue.php | 8 ++++++++ src/ComponentSerializer.php | 15 ++++++++++++++- 8 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 src/Attributes/Vue.php diff --git a/README.md b/README.md index adf24ce..e134fa9 100644 --- a/README.md +++ b/README.md @@ -282,7 +282,7 @@ Note that you can use `props.modelValue` without defining it. Splade Core automa ### Calling methods on the Blade Component -If your Blade Component has a `public` method, you may call it from the template, either in the script or in the template. Splade Core detects the HTTP Middleware of the current page and applies it to subsequent requests. +If your Blade Component has a `public` method, you may call it from the template, either in the script or in the template. Splade Core detects the HTTP Middleware of the current page and applies it to subsequent requests. The only thing you have to do is add the `Vue` attribute to the method: ```php user()->notify($message); @@ -324,7 +326,7 @@ Note that you can use `notify.loading` to check if the method is currently runni #### Blade Variables -Public properties of the Blade Component are automatically passed as Vue props. You may even update them on the frontend, and when you call a Blade Component method, the value will be updated on the backend. +Public properties of the Blade Component are automatically passed as Vue props. You may even update them on the frontend, and when you call a Blade Component method, the value will be updated on the backend. The only thing you have to do is add the `Vue` attribute to the property: ```php message = 'From the inside: '.$message; diff --git a/app/app/View/Components/Form.php b/app/app/View/Components/Form.php index 6584995..50857a4 100644 --- a/app/app/View/Components/Form.php +++ b/app/app/View/Components/Form.php @@ -8,14 +8,6 @@ class Form extends Component { - /** - * Create a new component instance. - */ - public function __construct() - { - // - } - /** * Get the view / contents that represent the component. */ diff --git a/app/app/View/Components/Layout.php b/app/app/View/Components/Layout.php index 576d1a0..4ba3514 100644 --- a/app/app/View/Components/Layout.php +++ b/app/app/View/Components/Layout.php @@ -8,14 +8,6 @@ class Layout extends Component { - /** - * Create a new component instance. - */ - public function __construct() - { - // - } - /** * Get the view / contents that represent the component. */ diff --git a/src/Attributes/Vue.php b/src/Attributes/Vue.php new file mode 100644 index 0000000..7c7bf34 --- /dev/null +++ b/src/Attributes/Vue.php @@ -0,0 +1,8 @@ +getAttributes(Vue::class))) { + continue; + } + $value = $property->getValue($this->component); if ($value instanceof Model) { @@ -131,6 +137,7 @@ public static function getDataFromComponentClass(string $componentClass): array $values = []; foreach ($properties as $property) { + /** @var ReflectionProperty $property */ if ($property->isStatic() || ! $property->isPublic()) { continue; } @@ -141,6 +148,10 @@ public static function getDataFromComponentClass(string $componentClass): array continue; } + if (empty($property->getAttributes(Vue::class))) { + continue; + } + $values[$name] = ''; } @@ -157,8 +168,10 @@ public static function getFunctionsFromComponentClass(string $componentClass): a $functions = (new ReflectionClass($componentClass))->getMethods(ReflectionMethod::IS_PUBLIC); return Collection::make($functions) + ->reject(fn ($function) => in_array($function->getName(), $ignoredFunctions)) + ->reject(fn (ReflectionMethod $function) => $function->isStatic()) + ->reject(fn (ReflectionMethod $function) => empty($function->getAttributes(Vue::class))) ->map(fn ($function) => $function->getName()) - ->reject(fn ($function) => in_array($function, $ignoredFunctions)) ->values() ->all(); }