Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Introduced Vue attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Oct 19, 2023
1 parent c320e59 commit 13f50a6
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 26 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,17 +282,19 @@ 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
<?php

namespace App\View\Components;

use Illuminate\View\Component;
use ProtoneMedia\SpladeCore\Attributes\Vue;

class UserProfile extends Component
{
#[Vue]
public function notify(string $message)
{
auth()->user()->notify($message);
Expand Down Expand Up @@ -324,17 +326,19 @@ 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
<?php

namespace App\View\Components;

use Illuminate\View\Component;
use ProtoneMedia\SpladeCore\Attributes\Vue;

class UserProfile extends Component
{
#[Vue]
public string $notification = 'Hey there!'

public function notify()
Expand Down
6 changes: 3 additions & 3 deletions app/app/View/Components/BladeMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
use ProtoneMedia\SpladeCore\Attributes\Vue;

class BladeMethod extends Component
{
/**
* Create a new component instance.
*/
#[Vue]
public function execute(string $input)
{
file_put_contents(
Expand All @@ -21,6 +20,7 @@ public function execute(string $input)
return $input;
}

#[Vue]
public function sleep()
{
sleep(2);
Expand Down
6 changes: 3 additions & 3 deletions app/app/View/Components/BladeMethodCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
use ProtoneMedia\SpladeCore\Attributes\Vue;

class BladeMethodCallbacks extends Component
{
/**
* Create a new component instance.
*/
#[Vue]
public function execute()
{
sleep(2);
}

#[Vue]
public function fail()
{
abort(500);
Expand Down
4 changes: 3 additions & 1 deletion app/app/View/Components/ChangeBladeProp.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
use ProtoneMedia\SpladeCore\Attributes\Vue;

class ChangeBladeProp extends Component
{
/**
* Create a new component instance.
*/
public function __construct(
public string $message = 'Hello World'
#[Vue] public string $message = 'Hello World'
) {
}

#[Vue]
public function setMessage(string $message)
{
$this->message = 'From the inside: '.$message;
Expand Down
8 changes: 0 additions & 8 deletions app/app/View/Components/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@

class Form extends Component
{
/**
* Create a new component instance.
*/
public function __construct()
{
//
}

/**
* Get the view / contents that represent the component.
*/
Expand Down
8 changes: 0 additions & 8 deletions app/app/View/Components/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@

class Layout extends Component
{
/**
* Create a new component instance.
*/
public function __construct()
{
//
}

/**
* Get the view / contents that represent the component.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Attributes/Vue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace ProtoneMedia\SpladeCore\Attributes;

#[Attribute]
class Vue
{
}
15 changes: 14 additions & 1 deletion src/ComponentSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use Illuminate\Support\Collection;
use Illuminate\View\Component;
use JsonSerializable;
use ProtoneMedia\SpladeCore\Attributes\Vue;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;

class ComponentSerializer implements Arrayable
{
Expand Down Expand Up @@ -94,6 +96,10 @@ public function getDataFromProperties(): array
continue;
}

if (empty($property->getAttributes(Vue::class))) {
continue;
}

$value = $property->getValue($this->component);

if ($value instanceof Model) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -141,6 +148,10 @@ public static function getDataFromComponentClass(string $componentClass): array
continue;
}

if (empty($property->getAttributes(Vue::class))) {
continue;
}

$values[$name] = '';
}

Expand All @@ -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();
}
Expand Down

0 comments on commit 13f50a6

Please sign in to comment.