Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structure refactor #6

Open
wants to merge 4 commits into
base: feature/add-routing-manifest-support
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 32 additions & 109 deletions src/APIResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,33 @@
namespace Juampi92\APIResources;

use Exception;
use Illuminate\Support\Str;
use Juampi92\APIResources\Exceptions\ResourceNotFoundException;
use Juampi92\APIResources\Resolvers\ResolverFactory;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Juampi92\APIResources\Resolvers\ClassnameResolverFactory;
use Juampi92\APIResources\Support\Version;

class APIResourceManager
{
protected string $current;
private Repository $config;

protected string $latest;
private ClassnameResolverFactory $classnameResolverFactory;

/**
* @var string
*/
protected $path;
private ?APIResourceVersion $version = null;

/**
* @var string
*/
protected $apiName;

/**
* @var string
*/
protected $resources;

/**
* @var string|null
*/
protected $routePath;
public function __construct(Repository $config, ClassnameResolverFactory $classnameResolverFactory)
{
$this->config = $config;
$this->classnameResolverFactory = $classnameResolverFactory;
}

public function __construct()
private function getResourceVersion(): APIResourceVersion
{
if (! $this->version) {
throw new Exception('APIResource not initialised. Please use APIResource::setVersion($version) to specify the current version');
}

return $this->version;
}

/**
Expand All @@ -46,17 +38,9 @@ public function __construct()
* @param string $route
* @return string
*/
public function getRouteName($route)
public function getRouteName(string $route)
{
if (! $this->routePath) {
// Grab route_prefix config first. If it's not set,
// grab the resources, and replace `\` with `.`, and
// transform it all to lowercase.
$this->routePath = $this->getConfig('route_prefix')
?: str_replace('\\', '.', strtolower($this->getConfig('resources')));
}

return "{$this->routePath}.v{$this->current}" . Str::after($route, $this->routePath);
return $this->getResourceVersion()->getRouteName($route);
}

/**
Expand All @@ -70,25 +54,6 @@ public function getRoute($name, $parameters = [], bool $absolute = true): string
return route($this->getRouteName((string) $name), $parameters, $absolute);
}

/**
* Get config considering the API name if present.
*
* @param string $cfg Config path
* @param string $name Name of api if present
*
* @return mixed The result of the config
*/
protected function getConfig($cfg, $name = null)
{
if (is_null($name)) {
$name = $this->apiName;
}

$name = $name ? ".$name" : '';

return config("api.$cfg{$name}");
}

/**
* Sets the current API version.
*
Expand All @@ -97,28 +62,12 @@ protected function getConfig($cfg, $name = null)
*
* @return $this
*/
public function setVersion(string $current, $apiName = null)
public function setVersion(string $current, ?string $apiName = null)
{
$this->apiName = $apiName;

$latestVersion = $this->getConfig('version');

if (! $latestVersion) {
throw new Exception('You must define a config(\'api\') with a latest version. Do: php artisan vendor:publish --provider="Juampi92/APIResources/APIResourcesServiceProvider"');
}

// Reset pre-cached properties
$this->current = $current;
$this->routePath = null;
$this->latest = $this->getConfig('version');

// Path can be only one or one for each api
$this->path = config('api.resources_path');
if (is_array($this->path)) {
$this->path = $this->getConfig('resources_path');
}
$config = new Config($this->config, $apiName);
$resolver = $this->classnameResolverFactory->make($config);

$this->resources = $this->getConfig('resources');
$this->version = new APIResourceVersion($config, $current, $resolver);

return $this;
}
Expand All @@ -130,12 +79,12 @@ public function setVersion(string $current, $apiName = null)
*/
public function getVersion()
{
return $this->current;
return $this->getResourceVersion()->getVersion();
}

public function getLatestVersion(): string
{
return $this->latest;
return $this->getResourceVersion()->getLatestVersion();
}

/**
Expand All @@ -147,21 +96,11 @@ public function getLatestVersion(): string
*/
public function isLatest(string $current = null): bool
{
if (! isset($current)) {
$current = $this->current;
if (is_null($current)) {
$current = $this->getResourceVersion()->getVersion();
}

return $this->latest === $current;
}

public function getBasePath(): string
{
return $this->path;
}

public function getResourcesPath(): ?string
{
return $this->resources;
return $this->getResourceVersion()->getLatestVersion() === $current;
}

/**
Expand All @@ -176,15 +115,17 @@ public function getResourcesPath(): ?string
*/
public function resolve(string $classname): APIResource
{
return new APIResource(ResolverFactory::make($classname)->run());
return new APIResource(
$this->resolveClassname($classname)
);
}

/**
* @throws ResourceNotFoundException
* @returns class-string<JsonResource>
*/
public function resolveClassname(string $classname): string
{
return ResolverFactory::make($classname)->run();
return $this->getResourceVersion()->resolveClassname($classname);
}

/**
Expand Down Expand Up @@ -212,22 +153,4 @@ public function collection($classname, ...$args)

return $resource->collection(...$args);
}

/**
* @return array<string>
*/
public function getVersionsBetweenCurrentAndLatest(): array
{
return $this->getVersionsBetween($this->current, $this->latest);
}

/**
* @return array<string>
*/
private function getVersionsBetween(string $current, string $latest): array
{
$versions = $this->getConfig('versions') ?: [$current, $latest];

return Version::fromRange($versions, $current, $latest);
}
}
86 changes: 86 additions & 0 deletions src/APIResourceVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Juampi92\APIResources;

use Exception;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;
use Juampi92\APIResources\Resolvers\ClassnameResolver;
use Juampi92\APIResources\Support\Version;

class APIResourceVersion
{
private Config $config;

private string $version;

private ClassnameResolver $resolver;

/** Cache of the Route Path */
private ?string $routePath = null;

public function __construct(Config $config, string $version, ClassnameResolver $resolver)
{
$this->config = $config;
$this->version = $version;
$this->resolver = $resolver;

if (! $this->getLatestVersion()) {
throw new Exception('You must define a config(\'api\') with a latest version. Do: php artisan vendor:publish --provider="Juampi92/APIResources/APIResourcesServiceProvider"');
}
}

/**
* Returns the name of the versioned route.
*
* @param string $route
* @return string
*/
public function getRouteName(string $route): string
{
if (! isset($this->routePath)) {
// Grab route_prefix config first. If it's not set,
// grab the resources, and replace `\` with `.`, and
// transform it all to lowercase.
$this->routePath = $this->config->get('route_prefix')
?: str_replace('\\', '.', strtolower($this->config->get('resources')));
}

return "{$this->routePath}.v{$this->version}" . Str::after($route, $this->routePath);
}

public function getVersion(): string
{
return $this->version;
}

public function getLatestVersion(): string
{
return $this->config->get('version');
}

/**
* @return array<string>
*/
private function getVersionsArray(): array
{
$current = $this->getVersion();
$latest = $this->getLatestVersion();

$versions = $this->config->get('versions') ?: [$current, $latest];

return Version::fromRange($versions, $current, $latest);
}

/**
* @param string $classname
* @return class-string<JsonResource>
*/
public function resolveClassname(string $classname): string
{
return $this->resolver->resolve(
$classname,
$this->getVersionsArray()
);
}
}
2 changes: 1 addition & 1 deletion src/APIResourcesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function boot(): void
public function register(): void
{
$this->app->singleton('apiresource', function () {
return new APIResourceManager();
return resolve(APIResourceManager::class);
});
}

Expand Down
43 changes: 43 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Juampi92\APIResources;

use Illuminate\Contracts\Config\Repository;

class Config
{
private Repository $config;
private ?string $name;

public function __construct(Repository $config, ?string $name = null)
{
$this->config = $config;
$this->name = $name;
}

/**
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get(string $key, $default = null)
{
$fullKey = $this->wrapInNamespace($key);

return $this->config->get("api.{$fullKey}", $default);
}

private function wrapInNamespace(string $key): string
{
if (! $this->name) {
return $key;
}

// When the key itself is not an array, we don't need the namespace.
if (! is_array($this->config->get($key))) {
return $key;
}

return "{$this->name}.$key";
}
}
Loading