Maravel Framework 10.59.0
Home » Development  »  Maravel Framework 10.59.0

Maravel Framework 10.59.0


maravel-framework 10.59.0 adds the ability to disable PHP Attributes (via Reflection) usage and the ability to disable auto discover and boot traits in models. This can improve the execution speed by reducing the execution steps, reflection and class_uses_recursive helper usage.

 These two new static properties were added to the model:

/**
* Override it in your model/baseModel instead of dynamically changing it to false!
* @see \Illuminate\Database\Eloquent\Attributes\ObservedBy;
* @see \Illuminate\Database\Eloquent\Attributes\ScopedBy;
* @see \Illuminate\Database\Eloquent\Casts\Attribute;
*/
protected static bool $modelShouldUsePhpAttributes = true;

/**
* Override it in your model/baseModel instead of dynamically changing it to false!
* When overridden with false, the extra traits that you might use must be manually booted
* and initialized in the boot method
* @see static::bootTraits()
*/
protected static bool $modelShouldAutoDiscoverAndBootTraits = true;

Usage:

For the traits part:

 /**
* Boot all the bootable traits on the model.
*
* If you override static::$modelShouldAutoDiscoverAndBootTraits as false and your model uses
* other traits you MUST boot and initialize those extra traits manually.
*
* Example for SoftDeletes Trait:
*
* 1) By overriding the boot method:
*
* protected static function boot()
* {
* static::bootSoftDeletes();
*
* // see also the explicit solution below
* static::$traitInitializers[static::class][] = 'initializeSoftDeletes';
* static::$traitInitializers[static::class] = \array_unique(static::$traitInitializers[$class]);
*
* parent::boot();
* }
*
* 2) Explicitly initialize the trait by overriding the initializeTraits method:
*
* protected function initializeTraits()
* {
* $this->initializeSoftDeletes(); // skip this if you want deleted_at column TO NOT be casted
* parent::initializeTraits()
* }
*
* Example for HasUuids:
*
* public $usesUniqueIds = true;
*
* @return void
*/
protected static function bootTraits()

For the attribute part, if you override modelShouldUsePhpAttributes to false, you should NOT use PHP Attributes in model or Accessor/Mutator Attribute.

 * @see \Illuminate\Database\Eloquent\Attributes\ObservedBy;
* @see \Illuminate\Database\Eloquent\Attributes\ScopedBy;
* @see \Illuminate\Database\Eloquent\Casts\Attribute;

and use the older solutions for them.

- Override \Illuminate\Foundation\Support\Providers\EventServiceProvider::shouldDiscoverEventsAsObservers to return true, cache observers via event:cache artisan command in Maravelith. If you do this, calling Model::observe is not needed anymore. In Maravel the event:cache command is not available and the observers must be manually registered as listeners:

<?php

namespace App\Providers;

use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'eloquent.updated: ' . \App\Models\ExampleModel::class => [
\App\Observers\ExampleModelObserver::class . '@updated',
],
];
}

- Define scopes manually in your model:

protected static function booted(): void
{
static::addGlobalScope(new AncientScope);
}

- Use accessor like get{column}Attribute() and mutator like set{column}Attribute() methods. Take care for the case when column is empty string because getAttribute() will be called.

/**
* Always use ? (nullable) type param because on newly instantiated models,
* the value will come as null
*/
public function getFirstNameAttribute(?string $value): ?string
{
return $value === null ? null : \ucfirst($value);
}

public function setFirstNameAttribute(?string $value): void
{
$this->attributes['first_name'] = $value === null ?
null :
\strtolower($value);
}

Note:

Both Maravelith and Maravel templates can take advantage of this improvement.