Skip to content

Commit

Permalink
Revert "Rewrite resource spec generation to use Resource's properties"
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzarbn authored Aug 17, 2022
1 parent 918b9f8 commit 043e9ea
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 367 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"illuminate/http": ">=5.7",
"illuminate/pagination": ">=5.7",
"illuminate/support": ">=5.7",
"laravel/legacy-factories": "^1.3",
"symfony/yaml": "^5.3|^6.0"
},
"require-dev": {
Expand Down
19 changes: 0 additions & 19 deletions src/Concerns/ExtendsResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,9 @@
namespace Orion\Concerns;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\MergeValue;

trait ExtendsResources
{
public static $mergeAll = false;

/**
* Override when to force merge when mergeAll is enabled
*/
protected function when($condition, $value, $default = null)
{
return static::$mergeAll ? value($value) : parent::when($condition, $value, $default);
}

/**
* Override mergeWhen to force merge when mergeAll is enabled
*/
protected function mergeWhen($condition, $value)
{
return static::$mergeAll ? new MergeValue(value($value)) : parent::mergeWhen($condition, $value);
}

/**
* Merges transformed resource with the given data.
*
Expand Down
28 changes: 15 additions & 13 deletions src/Specs/Builders/Components/Model/BaseModelComponentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Doctrine\DBAL\Schema\Column;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Orion\Http\Resources\Resource;
use Orion\Specs\Builders\Components\ModelComponentBuilder;
use Orion\ValueObjects\Specs\Component;

Expand All @@ -21,7 +20,7 @@ class BaseModelComponentBuilder extends ModelComponentBuilder
* @return Component
* @throws Exception
*/
public function build(Model $resourceModel, Resource $resourceResource): Component
public function build(Model $resourceModel): Component
{
$component = new Component();
$component->title = class_basename($resourceModel);
Expand All @@ -48,18 +47,21 @@ protected function getPropertiesFromSchema(Model $resourceModel, array $excluded
$columns = $this->schemaManager->getSchemaColumns($resourceModel);

return collect($columns)
->filter(function (Column $column) use ($excludedColumns) {
return !in_array($column->getName(), $excludedColumns, true);
})
->filter(function (Column $column) use ($resourceModel) {
return $resourceModel->isFillable($column->getName());
})
->map(function (Column $column) use ($resourceModel) {
$propertyClass = $this->schemaManager->resolveSchemaPropertyClass($column, $resourceModel);
->filter(
function (Column $column) use ($excludedColumns) {
return !in_array($column->getName(), $excludedColumns, true);
}
)->filter(
function (Column $column) use ($resourceModel) {
return $resourceModel->isFillable($column->getName());
}
)->map(
function (Column $column) use ($resourceModel) {
$propertyClass = $this->schemaManager->resolveSchemaPropertyClass($column, $resourceModel);

return $this->propertyBuilder->build($column, $propertyClass);
})
->values()
return $this->propertyBuilder->build($column, $propertyClass);
}
)->values()
->keyBy('name')
->toArray();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,81 +7,57 @@
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\Column;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Resources\MergeValue;
use Orion\Http\Resources\Resource;
use Orion\Specs\Builders\Components\ModelComponentBuilder;
use Orion\ValueObjects\Specs\ModelResourceComponent;

class ModelResourceComponentBuilder extends ModelComponentBuilder
{
/**
* @param Model $resourceModel
* @param Resource $resourceResource
* @return ModelResourceComponent
* @throws Exception
*/
public function build(Model $resourceModel, Resource $resourceResource): ModelResourceComponent
public function build(Model $resourceModel): ModelResourceComponent
{
$resourceComponentBaseName = class_basename($resourceModel);

$component = new ModelResourceComponent();
$component->title = class_basename($resourceResource);
$component->title = "{$resourceComponentBaseName}Resource";
$component->type = 'object';

$resourceProperties = $this->getPropertiesFromResource($resourceResource);
$includedProperties = array_keys($resourceProperties);

$component->properties = array_merge(
$resourceProperties,
$this->getPropertiesFromSchema($resourceModel, $includedProperties)
);
$component->properties = [
'allOf' => [
['$ref' => "#/components/schemas/{$resourceComponentBaseName}"],
[
'type' => 'object',
'properties' => $this->getPropertiesFromSchema($resourceModel)
],
],
];

return $component;
}

/**
* @param Resource $resourceResource
* @return array
* @throws Exception
*/
protected function getPropertiesFromResource(Resource $resourceResource): array
{
$properties = $this->resourceManager->getResourceProperties($resourceResource);

return collect($properties)
->mapWithKeys(function ($value, $property) {
return is_a($value, MergeValue::class) ? $value->data : [$property => $value];
})
->filter(function ($value, $property) {
return is_string($property);
})
->map(function ($value, $property) {
$propertyClass = $this->resourceManager->resolveResourcePropertyClass($property, $value);

return $this->propertyBuilder->buildFromResource($property, true, $propertyClass);
})
->values()
->keyBy('name')
->toArray();
}

/**
* @param Model $resourceModel
* @return array
* @throws Exception
*/
protected function getPropertiesFromSchema(Model $resourceModel, array $includedProperties): array
protected function getPropertiesFromSchema(Model $resourceModel): array
{
$columns = $this->schemaManager->getSchemaColumns($resourceModel);

return collect($columns)
->filter(function (Column $column) use ($includedProperties) {
return in_array($column->getName(), $includedProperties, true);
})
->map(function (Column $column) use ($resourceModel) {
$propertyClass = $this->schemaManager->resolveSchemaPropertyClass($column, $resourceModel);
->filter(
function (Column $column) use ($resourceModel) {
return !$resourceModel->isFillable($column->getName());
}
)->map(
function (Column $column) use ($resourceModel) {
$propertyClass = $this->schemaManager->resolveSchemaPropertyClass($column, $resourceModel);

return $this->propertyBuilder->build($column, $propertyClass);
})
->values()
return $this->propertyBuilder->build($column, $propertyClass);
}
)->values()
->keyBy('name')
->toArray();
}
Expand Down
10 changes: 2 additions & 8 deletions src/Specs/Builders/Components/ModelComponentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Orion\Specs\Builders\Components;

use Orion\Specs\Builders\PropertyBuilder;
use Orion\Specs\Managers\ResourceManager;
use Orion\Specs\Managers\SchemaManager;

abstract class ModelComponentBuilder
Expand All @@ -14,10 +13,6 @@ abstract class ModelComponentBuilder
* @var SchemaManager
*/
protected $schemaManager;
/**
* @var ResourceManager
*/
protected $resourceManager;
/**
* @var PropertyBuilder
*/
Expand All @@ -27,13 +22,12 @@ abstract class ModelComponentBuilder
* BaseModelComponentBuilder constructor.
*
* @param SchemaManager $schemaManager
* @param ResourceManager $resourceManager
* @param PropertyBuilder $propertyBuilder
*/
public function __construct(SchemaManager $schemaManager, ResourceManager $resourceManager, PropertyBuilder $propertyBuilder)
public function __construct(SchemaManager $schemaManager, PropertyBuilder $propertyBuilder)
{
$this->schemaManager = $schemaManager;
$this->resourceManager = $resourceManager;
$this->propertyBuilder = $propertyBuilder;
}

}
5 changes: 1 addition & 4 deletions src/Specs/Builders/ComponentsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,10 @@ protected function buildModelComponents(Collection $components, array $resources
$resourceModelClass = app()->make($resource->controller)->resolveResourceModelClass();
$resourceModel = app()->make($resourceModelClass);

$resourceResourceClass = app()->make($resource->controller)->getResource();
$resourceResource = app()->make($resourceResourceClass, ['resource' => optional()]);

foreach (static::SCHEMA_MODEL_COMPONENT_BUILDERS as $modelComponentBuilderClass) {
$modelComponentBuilder = app()->make($modelComponentBuilderClass);

$modelComponent = $modelComponentBuilder->build($resourceModel, $resourceResource);
$modelComponent = $modelComponentBuilder->build($resourceModel);

$schemas[$modelComponent->title] = $modelComponent->toArray();
}
Expand Down
17 changes: 0 additions & 17 deletions src/Specs/Builders/PropertyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,4 @@ public function build(Column $column, string $concretePropertyClass): SchemaProp

return $property;
}

/**
* @param string $name
* @param bool $nullable
* @param string|SchemaProperty $concretePropertyClass
*
* @return SchemaProperty
*/
public function buildFromResource(string $name, bool $nullable, string $concretePropertyClass): SchemaProperty
{
/** @var SchemaProperty $property */
$property = new $concretePropertyClass();
$property->name = $name;
$property->nullable = $nullable;

return $property;
}
}
67 changes: 0 additions & 67 deletions src/Specs/Managers/ResourceManager.php

This file was deleted.

11 changes: 6 additions & 5 deletions src/ValueObjects/Specs/ModelResourceComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class ModelResourceComponent extends Component
{
public function toArray(): array
{
return [
'title' => $this->title,
'type' => $this->type,
'properties' => $this->properties,
];
return array_merge(
[
'title' => $this->title,
],
$this->properties
);
}
}
20 changes: 0 additions & 20 deletions tests/Fixtures/app/Http/Controllers/ProductsController.php

This file was deleted.

Loading

0 comments on commit 043e9ea

Please sign in to comment.