1. Go to this page and download the library: Download meius/laravel-flag-forge library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
meius / laravel-flag-forge example snippets
return [
// Other service providers...
Meius\LaravelFlagForge\Providers\FlagForgeServiceProvider::class,
];
namespace App\Models;
use App\Enums\Permission; // Your bitwise enum must implement Bitwiseable
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Meius\LaravelFlagForge\Casts\AsMask;
/**
* ChatUser Model
*
* This eloquent model demonstrates seamless integration of bitwise flag management.
* The `permissions` attribute is automatically cast to a `FlagManager` instance,
* allowing for sophisticated flag operations within your domain logic.
*
* @property string $id
* @property string $chat_id
* @property string $user_id
* @property int $permissions
*/
class ChatUser extends Pivot
{
use HasUuids;
protected $fillable = ['permissions'];
protected function casts(): array
{
return [
// `AsMask` converts numeric bitmask values into a `FlagManager` instance.
// Append the fully qualified bitwise enum class after the colon.
'permissions' => AsMask::class . ':' . Permission::class,
];
}
}
// Retrieve a ChatUser instance
$chatUser = ChatUser::query()->first();
// Verify if the user has the permission to send messages
if ($chatUser->permissions->has(Permission::SendMessages)) {
echo "User is allowed to send messages.";
}
// Add the DeleteMessages flag to the user's permissions
$chatUser->permissions->add(Permission::DeleteMessages);
// Remove a flag if necessary
$chatUser->permissions->remove(Permission::SendMessages);
if ($flags->has(Permission::SendMessages)) {
// Execute logic when the flag is active
}
if ($flags->doesntHave(Permission::RemoveUsers)) {
// Execute alternative logic when the flag is not active
}
use App\Enums\Permission;
use Meius\LaravelFlagForge\Facades\Flag;
// Create a comprehensive flag manager instance with multiple permissions
$flagManager = Flag::add(Permission::SendMessages)
->combine(Permission::DeleteMessages, Permission::AddUsers)
->toggle(Permission::PinMessages);
// Alternatively, initialize a flag manager with a single permission for a quick check
$singleFlag = Flag::add(Permission::DeleteMessages);
use App\Enums\Permission;
use App\Models\ChatUser;
// Retrieve all users who have the SendMessages permission set
$users = ChatUser::query()
->whereHasFlag('permissions', Permission::SendMessages)
->get();
// Retrieve users who do not have the RemoveUsers flag
$users = ChatUser::query()
->whereDoesntHaveFlag('permissions', Permission::RemoveUsers)
->get();
use Meius\LaravelFlagForge\Facades\Flag;
// Retrieve users who have either the combination of SendMessages and AddUsers or the composite flag built dynamically
$users = ChatUser::query()
->whereAllFlagsSet('permissions', [Permission::SendMessages, Permission::AddUsers])
->orWhereAllFlagsSet('permissions', Flag::add(Permission::DeleteMessages)
->combine(Permission::PinMessages))
->get();
// Retrieve users who have at least one flag among RemoveUsers or PinMessages set
$users = ChatUser::query()
->whereAnyFlagSet('permissions', Flag::add(Permission::RemoveUsers)->add(Permission::PinMessages))
->get();
namespace App\Policies;
use App\Models\User;
use App\Models\Chat;
use App\Enums\Permission;
class ChatPolicy
{
/**
* Determine whether the user can send messages.
*/
public function send(User $user, Chat $chat): bool
{
return $user->chats()
->where('id', '=', $chat->id)
->whereHasFlag('permissions', Permission::SendMessages)
->exists();
}
/**
* Determine whether the user can moderate the chat.
*/
public function update(User $user, Chat $chat): bool
{
return $user->chats()
->where('id', '=', $chat->id)
->whereHasFlag('permissions', Permission::ManageChat)
->exists();
}
/**
* Determine whether the auth user can exclude the user from the chat.
*/
public function exclude(User $auth, Chat $chat, User $user): bool
{
return $auth->id === $user->id || $auth->chats()
->where('id', '=', $chat->id)
->whereHasFlag('permissions', Permission::RemoveUsers)
->exists();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.