88class XLivewireBaseComponent extends Component
99{
1010
11- public $ slot ;
12- public $ attributes ;
11+ public string $ slot ;
12+ public string $ attributes ;
13+
14+
15+ /**
16+ * The serialized array of custom slots passed to the component.
17+ * Basically, x-livewire's version of the default Blade $__laravel_slot variable.
18+ *
19+ *
20+ * @var string
21+ */
22+ public string $ laravelSlots ;
1323
1424 public function slot ()
1525 {
@@ -21,12 +31,47 @@ public function attributes()
2131 return unserialize ($ this ->attributes );
2232 }
2333
24- public function setProps ()
34+ public function laravelSlots ()
35+ {
36+ return unserialize ($ this ->laravelSlots );
37+ }
38+
39+
40+ public function mount (){
41+ $ this ->setProps ();
42+ }
43+
44+ /**
45+ * Set the component's class properties from the attributes passed into it's x-livewire tag.
46+ *
47+ * What this method does is loop through all it's public properties and set them to the values given in the tag.
48+ * If the property is not public, it is added to the $tagAttributes array.
49+ *
50+ * E.g: Let's say we have an x-livewire tag as follows:
51+ * <x-livewire: _="my-component" :my-attribute="my-value" another-attribute="foofoo" my-tag-attribute="dont-add-me" />
52+ * And its component class is defined as follows:
53+ * class MyComponent extends XLivewireBaseComponent{... public $myAttribute; public $anotherAttribute; ...}
54+ *
55+ * In this case, the component will have the following properties set:
56+ * $this->myAttribute = "my-value"
57+ * $this->anotherAttribute = "foofoo"
58+ *
59+ * There won't be any properties set for the `my-tag-attribute`, as there was no public property with that name($myTagAttribute).
60+ * It would have been added to the $tagAttributes array instead.
61+ * To access it, you would use the following code:
62+ * $this->tagAttributes["my-tag-attribute"]
63+ *
64+ * This method should be the first method called in the component's `mount()` method.
65+ *
66+ * @return void
67+ */
68+ public function setProps (): void
2569 {
26- // We name it this way to avoid conflicts with the component's own $attributes property.
70+ // The collection of all attributes that were set in the x-livewire tag.
71+ // We name it this way to avoid conflicts with the component's actual $attributes property.
2772 $ this ->attributesCollection = collect ($ this ->attributes ());
2873
29- // Get a collection the names of all the public properties.
74+ // Get a collection of the names of all the public properties.
3075 $ r_object = new \ReflectionObject ($ this );
3176 $ public_props = collect ($ r_object ->getProperties (\ReflectionProperty::IS_PUBLIC ))->transform (fn ($ prop ) => $ prop ->name );
3277
@@ -47,10 +92,10 @@ public function setProps()
4792 // This is the name of the property component's backend
4893 $ prop_livewire_name = Str::snake ($ prop ,'- ' );
4994
50- // Check if the property was explicitely set in the $attributes array.
95+ // Check if the property was explicitely set in the $attributes array....
5196 if ($ this ->attributesCollection ->has ($ prop_livewire_name )){
5297
53- // If it was explicitely set, set the property to the value from the $attributes array.
98+ // ... If the property was explicitely set, set the property to the value from the $attributes array.
5499 $ this ->$ prop = $ this ->attributesCollection ->get ($ prop_livewire_name );
55100 }
56101 }
0 commit comments