1. Go to this page and download the library: Download honeystone/context 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/ */
declare(strict_types=1);
namespace App\Context\Resolvers;
use Honeystone\Context\ContextResolver;
class MyResolver extends ContextResolver
{
public function __construct(
private ?Team = null,
private ?Project = null,
) {}
public function resolveTeam(): Team
{
//your resolution logic
return $this->team;
}
public function resolveProject(): Project
{
//your resolution logic
return $this->project;
}
public static function deserialize(array $data): static
{
//you must also declare a deserialization method,
//to reinstate the serialised data
return new static($team, $project);
}
}
class MyResolver extends ContextResolver
{
//...
public function serializeTeam(Team $team): int
{
return $team->id;
}
}
class MyResolver extends ContextResolver
{
//...
public function checkProject(
DefinesContext $definition,
Project $project,
array $resolved,
): bool {
return $project->id === $resolved['team']->project_id;
}
}
declare(strict_types=1);
namespace App\Context\Receivers;
use Honeystone\Context\Contracts\ReceivesContext;
class TeamReceiver implements ReceivesContext
{
public function receiveContext(string $name, ?object $context): void
{
//process received context
}
}
declare(strict_types=1);
namespace App\Models;
use Honeystone\Context\Models\Concerns\BelongsToContext;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
use BelongsToContext;
protected static array $context = ['team'];
//optionally specify context aliases
protected static array $context = ['team' => 'my_team'];
//optionally configure a context foreign key
protected function getTeamContextForeignKey(Team $team): string
{
return 'my_team_id'; //the default is generated like this
}
//optionally configure a context owner id
protected function getTeamContextOwnerId(Team $team): int
{
return $context->id; //defaults to the id prop
}
//optionally configure the relationship name
protected function getTeamContextRelationName(Team $team): string
{
return 'my_team'; //the default is generated like this
}
}
use function Honeystone\Context\context;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.