Skip to content

Commit

Permalink
Merge pull request #62 from ajthinking/refactor-to-explicit-endpoints
Browse files Browse the repository at this point in the history
Move PHPFile resources to explicit methods
  • Loading branch information
ajthinking authored Apr 19, 2022
2 parents 9ffd7c1 + 4e83429 commit 01791d1
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 203 deletions.
24 changes: 0 additions & 24 deletions src/Endpoints/EndpointProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,12 @@
namespace Archetype\Endpoints;

use Archetype\PHPFile;
use Archetype\Traits\ExposesPublicMethodsAsEndpoints;
use Archetype\Traits\HasDirectiveHandlers;

abstract class EndpointProvider
{
use ExposesPublicMethodsAsEndpoints;
use HasDirectiveHandlers;

protected $reservedMethods = [
'__call',
'__construct',
'canHandle',
'getEndpoints',
];

protected $directives;

public function __construct(PHPFile $file = null)
Expand All @@ -28,21 +19,6 @@ public function __construct(PHPFile $file = null)
$this->directives = $this->file ? $this->file->directives() : [];
}

public function canHandle($signature, $args)
{
return (boolean) $this->getHandlerMethod($signature, $args);
}

public function getEndpoints()
{
return $this->ownNonReservedPublicMethods();
}

protected function getHandlerMethod($signature, $args)
{
return $this->ownNonReservedPublicMethods()->contains($signature) ? $signature : false;
}

protected function ast()
{
return $this->file->ast();
Expand Down
1 change: 1 addition & 0 deletions src/Endpoints/Laravel/ModelProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ protected function getHandlerMethod($signature, $args)

public function getEndpoints()
{
echo "HEY CALLED";
return collect($this->propertyMap)->keys();
}

Expand Down
39 changes: 0 additions & 39 deletions src/Endpoints/SyntacticSweetener.php

This file was deleted.

138 changes: 125 additions & 13 deletions src/LaravelFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,138 @@

namespace Archetype;

use Archetype\Endpoints\Laravel\BelongsTo;
use Archetype\Endpoints\Laravel\BelongsToMany;
use Archetype\Endpoints\Laravel\HasMany;
use Archetype\Endpoints\Laravel\HasOne;
use Archetype\Endpoints\Laravel\LaravelMake;
use Archetype\Endpoints\Laravel\ModelProperties;
use Archetype\PHPFile;

class LaravelFile extends PHPFile
{
protected const endpointProviders = [
// Utilities
Endpoints\Laravel\LaravelMake::class,
protected string $fileQueryBuilder = Endpoints\Laravel\LaravelFileQueryBuilder::class;

// Resources
Endpoints\Laravel\BelongsTo::class,
Endpoints\Laravel\BelongsToMany::class,
Endpoints\Laravel\HasMany::class,
Endpoints\Laravel\HasOne::class,
Endpoints\Laravel\ModelProperties::class,
];
public function user(...$args)
{
return $this->query()->user(...$args);
}

protected string $fileQueryBuilder = Endpoints\Laravel\LaravelFileQueryBuilder::class;
public function controllers(...$args)
{
return $this->query()->controllers(...$args);
}

public function models(...$args)
{
return $this->query()->controllers(...$args);
}

public function endpointProviders()
public function casts(...$args)
{
$handler = new ModelProperties($this);
return $handler->casts(...$args);
}

public function connection(...$args)
{
$handler = new ModelProperties($this);
return $handler->connection(...$args);
}

public function table(...$args)
{
$handler = new ModelProperties($this);
return $handler->table(...$args);
}

public function dates(...$args)
{
$handler = new ModelProperties($this);
return $handler->dates(...$args);
}

public function timestamps(...$args)
{
$handler = new ModelProperties($this);
return $handler->timestamps(...$args);
}

public function visible(...$args)
{
$handler = new ModelProperties($this);
return $handler->visible(...$args);
}

public function guarded(...$args)
{
$handler = new ModelProperties($this);
return $handler->guarded(...$args);
}

public function unguarded(...$args)
{
$handler = new ModelProperties($this);
return $handler->unguarded(...$args);
}

public function fillable(...$args)
{
$handler = new ModelProperties($this);
return $handler->fillable(...$args);
}

public function hidden(...$args)
{
$handler = new ModelProperties($this);
return $handler->hidden(...$args);
}

public function model($name)
{
return parent::endpointProviders()->concat(self::endpointProviders);
$handler = new LaravelMake($this);
$handler->model($name);
}

public function controller($name)
{
$handler = new LaravelMake($this);
$handler->controller($name);
}

public function migration($name)
{
$handler = new LaravelMake($this);
$handler->migration($name);
}

public function factory($name)
{
$handler = new LaravelMake($this);
$handler->factory($name);
}

public function belongsTo($targets)
{
$handler = new BelongsTo($this);
return $handler->belongsTo($targets);
}

public function belongsToMany($targets)
{
$handler = new BelongsToMany($this);
return $handler->belongsToMany($targets);
}

public function hasMany($targets)
{
$handler = new HasMany($this);
return $handler->hasMany($targets);
}

public function hasOne($targets)
{
$handler = new HasOne($this);
return $handler->hasOne($targets);
}
}
Loading

0 comments on commit 01791d1

Please sign in to comment.