Skip to content
This repository has been archived by the owner on Oct 11, 2018. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Zogg committed Aug 24, 2017
0 parents commit f562c0c
Show file tree
Hide file tree
Showing 60 changed files with 3,058 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
.idea
.php_cs.cache
cache
phpunit
vendor
composer.lock
var
4 changes: 4 additions & 0 deletions .gitmodules
@@ -0,0 +1,4 @@
[submodule "vagrant-php"]
path = vagrant-php
url = https://github.com/vagrant-php/ubuntu.git
branch = 16.04
18 changes: 18 additions & 0 deletions .scrutinizer.yml
@@ -0,0 +1,18 @@
build:
tests:
override:
-
command: 'vendor/bin/phpunit --coverage-clover=coverage/clover.xml'
coverage:
file: 'coverage/clover.xml'
format: 'php-clover'

checks:
php: true

coding_style:
php: {}

filter:
excluded_paths:
- 'tests/*'
28 changes: 28 additions & 0 deletions .travis.yml
@@ -0,0 +1,28 @@
language: php

php:
- 7.0
- 7.1

services:
- mysql

matrix:
include:
- php: 7.0
env: dependencies=lowest
- php: 7.0
env: dependencies=highest
- php: 7.1
env: dependencies=lowest
- php: 7.1
env: dependencies=highest

before_script:
- echo "USE mysql;\nUPDATE user SET password=PASSWORD('root') WHERE user='root';\nFLUSH PRIVILEGES;\n" | mysql -u root
- composer self-update -q
- if [ -z "$dependencies" ]; then composer install; fi;
- if [ "$dependencies" = "lowest" ]; then composer update --prefer-lowest -n; fi;
- if [ "$dependencies" = "highest" ]; then composer update -n; fi;

script: vendor/bin/phpunit --coverage-text --verbose
85 changes: 85 additions & 0 deletions README.md
@@ -0,0 +1,85 @@
# chubbyphp/chubbyphp-api-slim-skeleton

[![Build Status](https://api.travis-ci.org/chubbyphp/chubbyphp-api-slim-skeleton.png?branch=master)](https://travis-ci.org/chubbyphp/chubbyphp-api-slim-skeleton)
[![Total Downloads](https://poser.pugx.org/chubbyphp/chubbyphp-api-slim-skeleton/downloads.png)](https://packagist.org/packages/chubbyphp/chubbyphp-api-slim-skeleton)
[![Latest Stable Version](https://poser.pugx.org/chubbyphp/chubbyphp-api-slim-skeleton/v/stable.png)](https://packagist.org/packages/chubbyphp/chubbyphp-api-slim-skeleton)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/chubbyphp/chubbyphp-api-slim-skeleton/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/chubbyphp/chubbyphp-api-slim-skeleton/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/chubbyphp/chubbyphp-api-slim-skeleton/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/chubbyphp/chubbyphp-api-slim-skeleton/?branch=master)

## Description

A slim 3 skeleton to build web apis.

## Requirements

* php: ~7.0
* [chubbyphp/chubbyphp-api-http][1]: ~1.0@beta
* [chubbyphp/chubbyphp-deserialization][2]: ~1.1
* [chubbyphp/chubbyphp-deserialization-model][3]: ~1.0
* [chubbyphp/chubbyphp-lazy][4]: ~1.1
* [chubbyphp/chubbyphp-model][5]: ~3.0
* [chubbyphp/chubbyphp-model-doctrine-dbal][6]: ~1.0
* [chubbyphp/chubbyphp-serialization][7]: ^1.0.1
* [chubbyphp/chubbyphp-translation][8]: ^1.1.2
* [chubbyphp/chubbyphp-validation][9]: ~2.1
* [chubbyphp/chubbyphp-validation-model][10]: ~1.0
* [monolog/monolog][11]: ~1.21
* [ramsey/uuid][12]: ~3.5
* [silex/providers][13]: ^2.0.3
* [slim/slim][14]: ~3.5
* [symfony/console][15]: ~3.1
* [symfony/yaml][16]: ~3.1
* [willdurand/negotiation][17]: ^2.3

## Installation

### With vagrant-php

#### Install `create-slim3-project` command

[create-slim3-project][18]

#### Create project

```{.sh}
create-slim3-project --name=myproject --vagrantIp=10.15.10.15
```

### With php on host

```{.sh}
composer create-project chubbyphp/chubbyphp-api-slim-skeleton myproject dev-master --prefer-dist
```

## Setup

### Create database

```{.sh}
bin/console chubbyphp:model:dbal:database:create
```

### Create / Update schema

```{.sh}
bin/console chubbyphp:model:dbal:database:schema:update --dump --force
```

[1]: https://github.com/chubbyphp/chubbyphp-api-http
[2]: https://github.com/chubbyphp/chubbyphp-deserialization
[3]: https://github.com/chubbyphp/chubbyphp-deserialization-model
[4]: https://github.com/chubbyphp/chubbyphp-lazy
[5]: https://github.com/chubbyphp/chubbyphp-model
[6]: https://github.com/chubbyphp/chubbyphp-model-doctrine-dbal
[7]: https://github.com/chubbyphp/chubbyphp-serialization
[8]: https://github.com/chubbyphp/chubbyphp-translation
[9]: https://github.com/chubbyphp/chubbyphp-validation
[10]: https://github.com/chubbyphp/chubbyphp-validation-model
[11]: https://github.com/Seldaek/monolog
[12]: https://github.com/ramsey/uuid
[13]: https://github.com/silexphp/Silex-Providers
[14]: https://github.com/slimphp/Slim
[15]: https://github.com/symfony/console
[16]: https://github.com/symfony/yaml
[17]: https://github.com/willdurand/negotiation
[18]: https://github.com/vagrant-php/create-slim3-project
22 changes: 22 additions & 0 deletions app/ApiHttp/ResponseFactory.php
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\ApiSkeleton\ApiHttp;

use Chubbyphp\ApiHttp\Factory\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Slim\Http\Response;

final class ResponseFactory implements ResponseFactoryInterface
{
/**
* @param int $code
*
* @return ResponseInterface
*/
public function createResponse($code = 200): ResponseInterface
{
return new Response($code);
}
}
36 changes: 36 additions & 0 deletions app/Controller/IndexController.php
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\ApiSkeleton\Controller;

use Chubbyphp\ApiHttp\Manager\ResponseManagerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Chubbyphp\ApiSkeleton\Search\Index;

final class IndexController
{
/**
* @var ResponseManagerInterface
*/
private $responseManager;

/**
* @param ResponseManagerInterface $responseManager
*/
public function __construct(ResponseManagerInterface $responseManager)
{
$this->responseManager = $responseManager;
}

/**
* @param Request $request
*
* @return Response
*/
public function __invoke(Request $request)
{
return $this->responseManager->createResponse($request, 200, new Index());
}
}
104 changes: 104 additions & 0 deletions app/Controller/Sample/SampleCreateController.php
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\ApiSkeleton\Controller\Sample;

use Chubbyphp\ApiHttp\Manager\RequestManagerInterface;
use Chubbyphp\ApiHttp\Manager\ResponseManagerInterface;
use Chubbyphp\Model\RepositoryInterface;
use Chubbyphp\Validation\ValidatorInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Chubbyphp\ApiSkeleton\Error\Error;
use Chubbyphp\ApiSkeleton\Error\ErrorManager;
use Chubbyphp\ApiSkeleton\Model\Sample;

final class SampleCreateController
{
/**
* @var string
*/
private $defaultLanguage;

/**
* @var ErrorManager
*/
private $errorManager;

/**
* @var RepositoryInterface
*/
private $repository;

/**
* @var RequestManagerInterface
*/
private $requestManager;

/**
* @var ResponseManagerInterface
*/
private $responseManager;

/**
* @var ValidatorInterface
*/
private $validator;

/**
* @param string $defaultLanguage
* @param ErrorManager $errorManager
* @param RepositoryInterface $repository
* @param RequestManagerInterface $requestManager
* @param ResponseManagerInterface $responseManager
* @param ValidatorInterface $validator
*/
public function __construct(
string $defaultLanguage,
ErrorManager $errorManager,
RepositoryInterface $repository,
RequestManagerInterface $requestManager,
ResponseManagerInterface $responseManager,
ValidatorInterface $validator
) {
$this->defaultLanguage = $defaultLanguage;
$this->errorManager = $errorManager;
$this->repository = $repository;
$this->requestManager = $requestManager;
$this->responseManager = $responseManager;
$this->validator = $validator;
}

/**
* @param Request $request
*
* @return Response
*/
public function __invoke(Request $request): Response
{
/** @var Sample $sample */
$sample = $this->requestManager->getDataFromRequestBody($request, Sample::class);

if (null === $sample) {
return $this->responseManager->createResponse($request, 415);
}

if ([] !== $errors = $this->validator->validateObject($sample)) {
return $this->responseManager->createResponse(
$request,
422,
$this->errorManager->createByValidationErrors(
$errors,
$this->requestManager->getAcceptLanguage($request, $this->defaultLanguage),
Error::SCOPE_BODY,
'sample'
)
);
}

$this->repository->persist($sample);

return $this->responseManager->createResponse($request, 201, $sample);
}
}
70 changes: 70 additions & 0 deletions app/Controller/Sample/SampleDeleteController.php
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\ApiSkeleton\Controller\Sample;

use Chubbyphp\ApiHttp\Manager\ResponseManagerInterface;
use Chubbyphp\Model\RepositoryInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Chubbyphp\ApiSkeleton\Error\ErrorManager;
use Chubbyphp\ApiSkeleton\Model\Sample;

final class SampleDeleteController
{
/**
* @var ErrorManager
*/
private $errorManager;

/**
* @var RepositoryInterface
*/
private $repository;

/**
* @var ResponseManagerInterface
*/
private $responseManager;

/**
* @param ErrorManager $errorManager
* @param RepositoryInterface $repository
* @param ResponseManagerInterface $responseManager
*/
public function __construct(
ErrorManager $errorManager,
RepositoryInterface $repository,
ResponseManagerInterface $responseManager
) {
$this->errorManager = $errorManager;
$this->repository = $repository;
$this->responseManager = $responseManager;
}

/**
* @param Request $request
*
* @return Response
*/
public function __invoke(Request $request): Response
{
$id = $request->getAttribute('id');

/** @var Sample $sample */
$sample = $this->repository->find($id);

if (null === $sample) {
return $this->responseManager->createResponse(
$request,
404,
$this->errorManager->createByMissingModel('sample', ['id' => $id])
);
}

$this->repository->remove($sample);

return $this->responseManager->createResponse($request, 200);
}
}

0 comments on commit f562c0c

Please sign in to comment.