8000 Merge pull request #43 from eurides-eu/feature/setup-fractal · joelharkes/framework_old@e390eaf · GitHub
[go: up one dir, main page]

Skip to content

Commit e390eaf

Browse files
authored
Merge pull request laravel#43 from eurides-eu/feature/setup-fractal
Setup fractal and an example transformer
2 parents e576122 + 890ab74 commit e390eaf

File tree

11 files changed

+391
-3
lines changed
  • Traits
  • Organizations
  • Providers
  • routes
  • 11 files changed

    +391
    -3
    lines changed
    Lines changed: 54 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,54 @@
    1+
    <?php
    2+
    3+
    namespace App\Entities\Repositories;
    4+
    5+
    use Illuminate\Database\Eloquent\Model;
    6+
    7+
    class EloquentRepository
    8+
    {
    9+
    /**
    10+
    * @var Model
    11+
    */
    12+
    private $items;
    13+
    14+
    /**
    15+
    * @param Model $items
    16+
    */
    17+
    public function __construct(Model $items)
    18+
    {
    19+
    $this->items = $items;
    20+
    }
    21+
    22+
    /**
    23+
    * Find a particular item.
    24+
    *
    25+
    * @param int|string $id
    26+
    *
    27+
    * @return Model
    28+
    */
    29+
    public function find($id)
    30+
    {
    31+
    return $this->items->query()->findOrFail($id);
    32+
    }
    33+
    34+
    /**
    35+
    * Return all items.
    36+
    *
    37+
    * @param int|null $perPage
    38+
    * @param int|null $page
    39+
    *
    40+
    * @return \Traversable
    41+
    */
    42+
    public function all($perPage = null, $page = null)
    43+
    {
    44+
    if (!is_null($perPage) && !is_null($page)) {
    45+
    foreach ($this->items->query()->paginate($perPage, ['*'], 'page', $page)->items() as $item) {
    46+
    yield $item;
    47+
    }
    48+
    }
    49+
    // Revert to a memory safe and efficient way to retrieve them all.
    50+
    foreach ($this->items->query()->cursor() as $item) {
    51+
    yield $item;
    52+
    }
    53+
    }
    54+
    }
    Lines changed: 10 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,10 @@
    1+
    <?php
    2+
    3+
    namespace App\Http\Controllers;
    4+
    5+
    use App\Http\Controllers\Traits\ApiResponse;
    6+
    7+
    class ApiController extends Controller
    8+
    {
    9+
    use ApiResponse;
    10+
    }

    app/Http/Controllers/Controller.php

    Lines changed: 1 addition & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -3,12 +3,11 @@
    33
    namespace App\Http\Controllers;
    44

    55
    use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
    6-
    use Illuminate\Foundation\Bus\DispatchesJobs;
    76
    use Illuminate\Foundation\Validation\ValidatesRequests;
    87
    use Illuminate\Routing\Controller as BaseController;
    98
    use Madewithlove\Tactician\Traits\DispatchesJobs as DispatchesCommands;
    109

    1110
    class Controller extends BaseController
    1211
    {
    13-
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests, DispatchesCommands;
    12+
    use AuthorizesRequests, ValidatesRequests, DispatchesCommands;
    1413
    }
    Lines changed: 38 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,38 @@
    1+
    <?php
    2+
    3+
    namespace App\Http\Controllers;
    4+
    5+
    use App\Organizations\Repositories\ReadRepository;
    6+
    use App\Organizations\Transformers\OrganizationTransformer;
    7+
    8+
    class OrganizationsController extends ApiController
    9+
    {
    10+
    /**
    11+
    * @var ReadRepository
    12+
    */
    13+
    private $organizationsReadRepository;
    14+
    15+
    /**
    16+
    * @param OrganizationTransformer $organizationTransformer
    17+
    * @param ReadRepository $organizationsReadRepository
    18+
    */
    19+
    public function __construct(
    20+
    OrganizationTransformer $organizationTransformer,
    21+
    ReadRepository $organizationsReadRepository
    22+
    ) {
    23+
    $this->setTransformer($organizationTransformer);
    24+
    $this->organizationsReadRepository = $organizationsReadRepository;
    25+
    }
    26+
    27+
    /**
    28+
    * @param string $organizationId
    29+
    *
    30+
    * @return \Psr\Http\Message\ResponseInterface
    31+
    */
    32+
    public function get($organizationId)
    33+
    {
    34+
    $organization = $this->organizationsReadRepository->find($organizationId);
    35+
    36+
    return $this->responseItem($organization);
    37+
    }
    38+
    }

    app/Http/Traits/ApiResponse.php

    Lines changed: 167 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,167 @@
    1+
    <?php
    2+
    3+
    namespace App\Http\Controllers\Traits;
    4+
    5+
    use ArrayIterator;
    6+
    use Illuminate\Contracts\Pagination\LengthAwarePaginator;
    7+
    use Illuminate\Database\Eloquent\Model;
    8+
    use Illuminate\Http\Response;
    9+
    use League\Fractal\Manager;
    10+
    use League\Fractal\Pagination\IlluminatePaginatorAdapter;
    11+
    use League\Fractal\Resource\Collection;
    12+
    use League\Fractal\Resource\Item;
    13+
    use League\Fractal\TransformerAbstract;
    14+
    use Psr\Http\Message\ResponseInterface;
    15+
    use Zend\Diactoros\Response\JsonResponse;
    16+
    use Zend\Diactoros\Stream;
    17+
    18+
    trait ApiResponse
    19+
    {
    20+
    /**
    21+
    * @var \League\Fractal\Manager
    22+
    */
    23+
    protected $manager;
    24+
    25+
    /**
    26+
    * @var \League\Fractal\TransformerAbstract
    27+
    */
    28+
    protected $transformer;
    29+
    30+
    /**
    31+
    * @param \League\Fractal\Manager $manager
    32+
    *
    33+
    * @return $this
    34+
    */
    35+
    public function setFractal(Manager $manager)
    36+
    {
    37+
    $this->manager = $manager;
    38+
    39+
    return $this;
    40+
    }
    41+
    42+
    /**
    43+
    * @param \League\Fractal\TransformerAbstract $transformer
    44+
    *
    45+
    * @return $this
    46+
    */
    47+
    protected function setTransformer(TransformerAbstract $transformer)
    48+
    {
    49+
    $this->transformer = $transformer;
    50+
    51+
    return $this;
    52+
    }
    53+
    54+
    /**
    55+
    * @param mixed $item
    56+
    *
    57+
    * @return \Psr\Http\Message\ResponseInterface
    58+
    */
    59+
    protected function responseItem($item)
    60+
    {
    61+
    $data = $this->manager->createData(new Item($item, $this->transformer))->toArray();
    62+
    63+
    return $this->wrapResponse($data);
    64+
    }
    65+
    66+
    /**
    67+
    * @param array|ArrayIterator $collection
    68+
    *
    69+
    * @return \Psr\Http\Message\ResponseInterface
    70+
    */
    71+
    protected function responseCollection($collection)
    72+
    {
    73+
    $data = $this->manager->createData(new Collection($collection, $this->transformer))->toArray();
    74+
    75+
    return $this->wrapResponse($data);
    76+
    }
    77+
    78+
    /**
    79+
    * @return JsonResponse
    80+
    */
    81+
    protected function responseNoContent()
    82+
    {
    83+
    return new< 1241 /span> JsonResponse(null, Response::HTTP_NO_CONTENT);
    84+
    }
    85+
    86+
    /**
    87+
    * @param \Illuminate\Database\Eloquent\Model $item
    88+
    * @param string $route
    89+
    *
    90+
    * @return \Psr\Http\Message\ResponseInterface
    91+
    */
    92+
    protected function responseCreated(Model $item, $route = null)
    93+
    {
    94+
    $response = $this->responseItem($item)->withStatus(201);
    95+
    96+
    return empty($route) ? $response : $response->withHeader('Location', $route);
    97+
    }
    98+
    99+
    /**
    100+
    * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator
    101+
    * @param array $meta
    102+
    *
    103+
    * @return \Psr\Http\Message\ResponseInterface
    104+
    */
    105+
    protected function responsePaginator(LengthAwarePaginator $paginator, array $meta = [])
    106+
    {
    107+
    $collection = (new Collection($paginator->items(), $this->transformer))
    108+
    ->setPaginator(new IlluminatePaginatorAdapter($paginator));
    109+
    110+
    $response = $this->withMeta($this->manager->createData($collection)->toArray(), $meta);
    111+
    112+
    return $this->wrapResponse($response);
    113+
    }
    114+
    115+
    /**
    116+
    * @param string $message
    117+
    */
    118+
    protected function responseNotFound($message = '')
    119+
    {
    120+
    throw new NotFoundHttpException($message);
    121+
    }
    122+
    123+
    /**
    124+
    * @param array $data
    125+
    *
    126+
    * @return \Psr\Http\Message\ResponseInterface
    127+
    */
    128+
    private function wrapResponse(array $data)
    129+
    {
    130+
    return new JsonResponse($data);
    131+
    }
    132+
    133+
    /**
    134+
    * @param array|ResponseInterface $response
    135+
    * @param array $meta
    136+
    *
    137+
    * @return array|ResponseInterface
    138+
    */
    139+
    protected function withMeta($response, array $meta)
    140+
    {
    141+
    $contents = $response;
    142+
    143+
    // If the response is an instance of ResponseInterface
    144+
    // get and decode its contents
    145+
    if ($response instanceof ResponseInterface) {
    146+
    $contents = $response->getBody()->getContents();
    147+
    $contents = json_decode($contents, true);
    148+
    }
    149+
    150+
    if (!empty($meta)) {
    151+
    // Makes sure we never overwrite existent metadata
    152+
    $meta = array_merge($meta, array_get($contents, 'meta', []));
    153+
    array_set($contents, 'meta', $meta);
    154+
    }
    155+
    156+
    // Replace the response's contents with the new one
    157+
    if ($response instanceof ResponseInterface) {
    158+
    $body = new Stream('php://temp', 'wb+');
    159+
    $body->write(json_encode($contents));
    160+
    $body->rewind();
    161+
    162+
    return $response->withBody($body);
    163+
    }
    164+
    165+
    return $contents;
    166+
    }
    167+
    }
    Lines changed: 17 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,17 @@
    1+
    <?php
    2+
    3+
    namespace App\Organizations\Repositories;
    4+
    5+
    use App\Entities\Repositories\EloquentRepository;
    6+
    use App\Models\Organization;
    7+
    8+
    class ReadRepository extends EloquentRepository
    9+
    {
    10+
    /**
    11+
    * @param Organization $items
    12+
    */
    13+
    public function __construct(Organization $items)
    14+
    {
    15+
    parent::__construct($items);
    16+
    }
    17+
    }
    Lines changed: 25 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -0,0 +1,25 @@
    1+
    <?php
    2+
    3+
    namespace App\Organizations\Transformers;
    4+
    5+
    use App\Models\Organization;
    6+
    use League\Fractal\TransformerAbstract;
    7+
    8+
    class OrganizationTransformer extends TransformerAbstract
    9+
    {
    10+
    /**
    11+
    * @param Organization $organization
    12+
    *
    13+
    * @return array
    14+
    */
    15+
    public function transform(Organization $organization)
    16+
    {
    17+
    return [
    18+
    'id' => $organization->id,
    19+
    'name' => $organization->name,
    20+
    'reference' => $organization->reference_code,
    21+
    'services' => [],
    22+
    'defaultBudget' => null,
    23+
    ];
    24+
    }
    25+
    }

    app/Providers/AppServiceProvider.php

    Lines changed: 6 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -2,7 +2,9 @@
    22

    33
    namespace App\Providers;
    44

    5+
    use App\Http\Controllers\ApiController;
    56
    use Illuminate\Support\ServiceProvider;
    7+
    use League\Fractal\Manager;
    68

    79
    class AppServiceProvider extends ServiceProvider
    810
    {
    @@ -18,5 +20,9 @@ public function boot()
    1820
    */
    1921
    public function register()
    2022
    {
    23+
    $this->app->resolving(ApiController::class, function (ApiController $controller, $app) {
    24+
    $manager = $this->app->make(Manager::class);
    25+
    $controller->setFractal($manager);
    26+
    });
    2127
    }
    2228
    }

    composer.json

    Lines changed: 1 addition & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -12,6 +12,7 @@
    1212
    "laravel/framework": "5.6.*",
    1313
    "laravel/passport": "^6.0",
    1414
    "laravel/tinker": "^1.0",
    15+
    "league/fractal": "^0.17.0",
    1516
    "madewithlove/tactician-laravel": "dev-master",
    1617
    "ramsey/uuid": "^3.7",
    1718
    "rcrowe/twigbridge": "^0.9.6"

    0 commit comments

    Comments
     (0)
    0