PHP code example of pupadevs / laramain

1. Go to this page and download the library: Download pupadevs/laramain 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/ */

    

pupadevs / laramain example snippets


use Pupadevs\Laramain\Shared\CQRS\Command\CommandBus;
use Pupadevs\Laramain\Shared\CQRS\Query\QueryBus;

namespace App\Commands;

class CreateUserCommand implements Command
{
    public string $name;
    public string $email;
    public string $password;

    public function __construct(string $name, string $email, string $password)
    {
        $this->name = $name;
        $this->email = $email;
        $this->password = $password;
    }
}

namespace App\Commands;

use App\Commands\CreateUserCommand;
use App\Repositories\UserRepository;

class CreateUserCommandHandler
{
    protected $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function handle(CreateUserCommand $command)
    {
        // Lógica para crear un usuario
        $this->userRepository->create([
            'name' => $command->name,
            'email' => $command->email,
            'password' => bcrypt($command->password),
        ]);
    }
}

class CheckEmailQuery implements Query
{
   
    public Email $email;

    public function __construct( Email $email)
    {
      
        $this->email = $email;
     
    }

    public function getEmail(){
        return $this->email;
    }
}


namespace App\Query;

use App\Queyr\CheckEmailQuery;
use App\Repositories\UserRepository;

class CheckEmailQueryHanlder
{
    protected $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function handle(CheckEmailQuery $query)
    {
       return $this->userRepository->findEmail($query->toString());
    }
}

class YourService
{
    protected CommandBus $command;
    protected QueryBus $query;

    public function __construct(CommandBus $commandBus, QueryBus $queryBus)
    {
        $this->command = $commandBus;
        $this->query = $queryBus;
    }

    public function someMethod($email)
    {
        $this->query->handle(new CheckEmailQuery(new Email($email)));
    }
}

Class SomeController{

    protected CommandBus $command;
    protected QueryBus $query;

    public function __construct(CommandBus $command, QueryBus $query)
    {
        $this->command = $commandBus;
        $this->query = $queryBus;
    } 

    public function showSomeThings(Request $request){
        $this->query->handle(new CheckEmailQuery($request->email));
        $this->command->execute(new CreateUserCommand($request->all()));
    }
}

interface StringValueObject
{
    public function toString(): string;

    public function __toString(): string;
}


use Pupadevs\Laramain\Shared\YourNamespace\Identifier; 

// Genera un nuevo identificador único (UUID)
$identifier = new Identifier();

// Usando el método toString()
echo $identifier->toString(); // Imprime el UUID generado

bash
  php artisan laramain:install {nombreDominio}
  
bash
php artisan vendor:publish --provider="Pupadevs\Laramain\Providers\PackageServiceProvider"
bash
php artisan laramain:install-basic