Skip to content

Commit b1f64ae

Browse files
authored
Merge pull request #3 from dotkernel/develop
Develop
2 parents 1679679 + 9c24831 commit b1f64ae

File tree

121 files changed

+7912
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+7912
-2
lines changed

README.md

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,77 @@
1-
# api
2-
Instructions for creating an API based on zend-expppresive, doctrine, and api-skelton
1+
# DotKernel API
2+
DotKernel's PSR-15 API built around the Zend Expressive API skeleton.
3+
4+
5+
## Getting Started
6+
Step 1: Install project dependencies by running:
7+
```bash
8+
$ composer install
9+
```
10+
11+
Step 2: Prepare config files:
12+
* duplicate `config/autoload/local.php.dist` as `config/autoload/local.php`
13+
* duplicate `config/autoload/mail.local.php.dist` as `config/autoload/mail.local.php`
14+
15+
Step 3: Setup database:
16+
* create a new MySQL database - set collation to `utf8mb4_general_ci`
17+
* fill out the database connection params in `config/autoload/local.php`
18+
* run the database migrations by using the following command:
19+
```bash
20+
$ vendor/bin/doctrine-migrations migrate
21+
```
22+
23+
Step 4: (Optional) Enable development mode:
24+
```bash
25+
$ composer development-enable
26+
```
27+
28+
After the project has been successfully installed, you should modify the default OAuth2 client and it's secret phrase.
29+
30+
31+
## Using the CLI interface:
32+
You can access Zend Expressive's CLI by using the following command:
33+
```bash
34+
$ composer expressive
35+
```
36+
You can access Doctrine's CLI by using the following command:
37+
```bash
38+
$ php vendor/doctrine/orm/bin/doctrine
39+
```
40+
You can access Doctrine's migration tools by using the following command:
41+
```bash
42+
$ vendor/bin/doctrine-migrations
43+
```
44+
45+
46+
## Creating an endpoint
47+
Routes can be created in one of the following locations:
48+
* `config/routes.php`
49+
* `src/{module-name}/RoutesDelegator.php` (First, make sure you registered it in `src/App/ConfigProvider.php`'s `getDependencies` method, under the `delegators` key). By using this method, if you want to move a module between projects, you will automatically move the related endpoints as well.
50+
51+
52+
## Working with entities
53+
A good practice is storing all related entities in the same directory `Entity`, next to the module's `Handler` directory. For example:
54+
* `src/Module/src/Example/Entity/ExampleEntity.php`
55+
* `src/Module/src/Example/Entity/ExampleDetailEntity.php`
56+
* `src/Module/src/Example/Entity/ExampleCategoryEntity.php`
57+
58+
59+
## Running the application
60+
```bash
61+
$ php -S 0.0.0.0:8080 -t public
62+
```
63+
To test the application, visit the [home page](http://localhost:8080/). You should get the following message:
64+
```json
65+
{
66+
"message": "Welcome to DotKernel API!"
67+
}
68+
```
69+
70+
71+
## Documentation
72+
Visit [this link](http://localhost:8080/documentation) to access the application's documentation.
73+
Here, you can request an access token using the /oauth2/generate endpoint using the following credentials:
74+
```
75+
username: test@dotkernel.com
76+
password: dotkernel
77+
```

Vagrantfile

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Vagrant configuration for zend-expressive workshop
2+
# @author Enrico Zimuel (enrico@zend.com)
3+
4+
VAGRANTFILE_API_VERSION = '2'
5+
6+
$script = <<SCRIPT
7+
# Fix for Temporary failure resolving 'archive.ubuntu.com'
8+
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf > /dev/null
9+
10+
# Install dependencies
11+
apt-get update
12+
apt-get install -y nginx git curl sqlite3 php7.2 php7.2-cli php7.2-fpm php7.2-sqlite3 php7.2-pdo php7.2-xml php7.2-zip php7.2-mbstring
13+
14+
# Configure Nginx
15+
echo "server {
16+
listen 8080;
17+
18+
root /home/ubuntu/zend-expressive-api/public;
19+
server_name ubuntu-xenial;
20+
21+
# Logs
22+
access_log /home/ubuntu/zend-expressive-api/log/access_log;
23+
error_log /home/ubuntu/zend-expressive-api/log/error_log;
24+
25+
index index.php index.html index.htm;
26+
27+
location / {
28+
try_files \\$uri \\$uri/ /index.php;
29+
}
30+
location ~ \\.php\$ {
31+
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
32+
include snippets/fastcgi-php.conf;
33+
}
34+
# Block access to .htaccess
35+
location ~ \\.htaccess {
36+
deny all;
37+
}
38+
}" > /etc/nginx/sites-available/zend-expressive-api
39+
chmod 644 /etc/nginx/sites-available/zend-expressive-api
40+
ln -s /etc/nginx/sites-available/zend-expressive-api /etc/nginx/sites-enabled/zend-expressive-api
41+
service nginx restart
42+
43+
if [ -e /usr/local/bin/composer ]; then
44+
/usr/local/bin/composer self-update
45+
else
46+
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
47+
fi
48+
49+
chmod +r /home/ubuntu/zend-expressive-api/data/oauth2/*
50+
SCRIPT
51+
52+
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
53+
config.vm.box = 'bento/ubuntu-18.04'
54+
config.vm.network "forwarded_port", guest: 8080, host: 8080
55+
config.vm.synced_folder ".", "/home/ubuntu/zend-expressive-api", id: "vagrant-root",
56+
owner: "vagrant",
57+
group: "www-data",
58+
mount_options: ["dmode=775,fmode=660"]
59+
config.vm.provision 'shell', inline: $script
60+
61+
config.vm.provider "virtualbox" do |vb|
62+
vb.customize ["modifyvm", :id, "--memory", "1024"]
63+
vb.customize ["modifyvm", :id, "--name", "zend-expressive-api"]
64+
end
65+
end

bin/clear-config-cache.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Script for clearing the configuration cache.
4+
*
5+
* Can also be invoked as `composer clear-config-cache`.
6+
*
7+
* @see https://github.com/zendframework/zend-expressive-skeleton for the canonical source repository
8+
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
9+
* @license https://github.com/zendframework/zend-expressive-skeleton/blob/master/LICENSE.md New BSD License
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
chdir(__DIR__ . '/../');
15+
16+
require 'vendor/autoload.php';
17+
18+
$config = include 'config/config.php';
19+
20+
if (! isset($config['config_cache_path'])) {
21+
echo "No configuration cache path found" . PHP_EOL;
22+
exit(0);
23+
}
24+
25+
if (! file_exists($config['config_cache_path'])) {
26+
printf(
27+
"Configured config cache file '%s' not found%s",
28+
$config['config_cache_path'],
29+
PHP_EOL
30+
);
31+
exit(0);
32+
}
33+
34+
if (false === unlink($config['config_cache_path'])) {
35+
printf(
36+
"Error removing config cache file '%s'%s",
37+
$config['config_cache_path'],
38+
PHP_EOL
39+
);
40+
exit(1);
41+
}
42+
43+
printf(
44+
"Removed configured config cache file '%s'%s",
45+
$config['config_cache_path'],
46+
PHP_EOL
47+
);
48+
exit(0);

bin/console.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Console application bootstrap file
4+
*/
5+
6+
use Interop\Container\ContainerInterface;
7+
use ZF\Console\Application;
8+
9+
chdir(dirname(__DIR__));
10+
require 'vendor/autoload.php';
11+
12+
/**
13+
* Self-called anonymous function that creates its own scope and keep the global namespace clean.
14+
*/
15+
call_user_func(function () {
16+
/** @var ContainerInterface $container */
17+
$container = require 'config/container.php';
18+
19+
/** @var Application $app */
20+
$app = $container->get(Application::class);
21+
$app->setDebug(false);
22+
23+
$exit = $app->run();
24+
exit($exit);
25+
});

composer.json

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{
2+
"name": "dotkernel/api",
3+
"description": "DotKernel API",
4+
"type": "project",
5+
"homepage": "https://github.com/dotkernel/api",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "DotKernel Team",
10+
"email": "team@dotkernel.com"
11+
}
12+
],
13+
"config": {
14+
"sort-packages": true
15+
},
16+
"extra": {
17+
"zf": {
18+
"component-whitelist": [
19+
"zendframework/zend-expressive",
20+
"zendframework/zend-expressive-helpers",
21+
"zendframework/zend-expressive-router",
22+
"zendframework/zend-httphandlerrunner",
23+
"zendframework/zend-expressive-fastroute"
24+
]
25+
}
26+
},
27+
"require": {
28+
"php": "^7.2",
29+
"ext-json": "*",
30+
"dasprid/container-interop-doctrine": "^1.1",
31+
"doctrine/migrations": "^2.0",
32+
"dotkernel/dot-annotated-services": "^1.1",
33+
"dotkernel/dot-console": "^0.1.1",
34+
"dotkernel/dot-errorhandler": "^1.0",
35+
"dotkernel/dot-mail": "^1.0",
36+
"moontoast/math": "^1.1",
37+
"ramsey/uuid-doctrine": "^1.5",
38+
"roave/security-advisories": "dev-master",
39+
"swagger-api/swagger-ui": "^3.22",
40+
"tuupola/cors-middleware": "^0.9.4",
41+
"zendframework/zend-component-installer": "^2.1.1",
42+
"zendframework/zend-config": "^3.3",
43+
"zendframework/zend-config-aggregator": "^1.0",
44+
"zendframework/zend-db": "^2.9.3",
45+
"zendframework/zend-diactoros": "^1.7.1",
46+
"zendframework/zend-expressive": "^3.0.1",
47+
"zendframework/zend-expressive-authentication": "^1.0",
48+
"zendframework/zend-expressive-authentication-oauth2": "^1.0",
49+
"zendframework/zend-expressive-authorization-acl": "^1.0",
50+
"zendframework/zend-expressive-authorization-rbac": "^1.0",
51+
"zendframework/zend-expressive-fastroute": "^3.0",
52+
"zendframework/zend-expressive-hal": "^1.0.2",
53+
"zendframework/zend-expressive-helpers": "^5.0",
54+
"zendframework/zend-expressive-twigrenderer": "^2.4",
55+
"zendframework/zend-http": "^2.10",
56+
"zendframework/zend-hydrator": "^2.2",
57+
"zendframework/zend-inputfilter": "^2.8.1",
58+
"zendframework/zend-paginator": "^2.8.1",
59+
"zendframework/zend-problem-details": "^1.0",
60+
"zendframework/zend-servicemanager": "^3.3",
61+
"zendframework/zend-stdlib": "^3.1",
62+
"zendframework/zend-text": "^2.7"
63+
},
64+
"require-dev": {
65+
"phpunit/phpunit": "^7.0.1",
66+
"zendframework/zend-coding-standard": "~1.0.0",
67+
"zendframework/zend-expressive-tooling": "^1.0",
68+
"zfcampus/zf-development-mode": "^3.1"
69+
},
70+
"autoload": {
71+
"psr-4": {
72+
"Api\\App\\": "src/App/src/",
73+
"Api\\Console\\": "src/Console/src/",
74+
"Api\\User\\": "src/User/src/"
75+
}
76+
},
77+
"autoload-dev": {
78+
"psr-4": {
79+
"AppTest\\": "test/AppTest/"
80+
}
81+
},
82+
"scripts": {
83+
"post-create-project-cmd": [
84+
"@development-enable"
85+
],
86+
"development-disable": "zf-development-mode disable",
87+
"development-enable": "zf-development-mode enable",
88+
"development-status": "zf-development-mode status",
89+
"expressive": "expressive --ansi",
90+
"check": [
91+
"@cs-check",
92+
"@test",
93+
"@analyze"
94+
],
95+
"analyze": "phpstan analyze -l max -c ./phpstan.installer.neon ./src ./config",
96+
"clear-config-cache": "php bin/clear-config-cache.php",
97+
"cs-check": "phpcs",
98+
"cs-fix": "phpcbf",
99+
"serve": "php -S 0.0.0.0:8080 -t public/",
100+
"test": "phpunit --colors=always",
101+
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml"
102+
}
103+
}

config/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
development.config.php

config/autoload/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
local.php
2+
*.local.php
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Api\User\Entity\UserRoleEntity;
6+
7+
return [
8+
/**
9+
* Example:
10+
'roles' => [
11+
'A' => [],
12+
'B' => ['A'],
13+
'C' => ['B'],
14+
],
15+
* A has no parent role.
16+
* B has A as a parent. That means A inherits the permissions of B.
17+
* C has B as a parent. That means C inherits the permissions of B, and A inherits the permissions of C.
18+
*/
19+
'zend-expressive-authorization-rbac' => [
20+
'roles' => [
21+
UserRoleEntity::ROLE_ADMIN => [],
22+
UserRoleEntity::ROLE_MEMBER => [UserRoleEntity::ROLE_ADMIN]
23+
],
24+
'permissions' => [
25+
UserRoleEntity::ROLE_ADMIN => [
26+
'user:activate',
27+
'user:avatar',
28+
'user:list,create',
29+
'user:delete,view,update',
30+
],
31+
UserRoleEntity::ROLE_MEMBER => [
32+
'my-account:avatar',
33+
'my-account:me',
34+
],
35+
],
36+
]
37+
];

0 commit comments

Comments
 (0)