Skip to content

Commit e53e17e

Browse files
committed
first commit
0 parents  commit e53e17e

36 files changed

+5448
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
/vendor/
3+
/build

.travis.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
language: php
2+
3+
dist: trusty
4+
5+
matrix:
6+
include:
7+
- php: 7.2
8+
- php: 7.3
9+
- php: nightly
10+
11+
allow_failures:
12+
- php: nightly
13+
14+
install:
15+
- composer install --prefer-dist --dev --no-interaction
16+
17+
script:
18+
- mkdir -p build/logs
19+
- vendor/bin/phpunit
20+
21+
after_script:
22+
- travis_retry vendor/bin/php-coveralls -v

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Casbin skeleton application with Slim Framework 4
2+
3+
[![Build Status](https://travis-ci.org/php-casbin/casbin-with-slim.svg?branch=master)](https://travis-ci.org/php-casbin/casbin-with-slim)
4+
[![Coverage Status](https://coveralls.io/repos/github/php-casbin/casbin-with-slim/badge.svg)](https://coveralls.io/github/php-casbin/casbin-with-slim)
5+
[![Latest Stable Version](https://poser.pugx.org/casbin/casbin-with-slim/v/stable)](https://packagist.org/packages/casbin/casbin-with-slim)
6+
[![Total Downloads](https://poser.pugx.org/casbin/casbin-with-slim/downloads)](https://packagist.org/packages/casbin/casbin-with-slim)
7+
[![License](https://poser.pugx.org/casbin/casbin-with-slim/license)](https://packagist.org/packages/casbin/casbin-with-slim)
8+
9+
Use this skeleton application to quickly setup and start working on a new Slim Framework 4 application. This application uses the latest Slim 4 with Slim PSR-7 implementation and PHP-DI container implementation. It also uses the Monolog logger.
10+
11+
This skeleton application was built for Composer. This makes setting up a new [Casbin](https://github.com/php-casbin/casbin-with-slim) skeleton application with Slim Framework quick and easy.
12+
13+
## Install the Application
14+
15+
Run this command from the directory in which you want to install your new Slim Framework application.
16+
17+
```bash
18+
composer create-project casbin/casbin-with-slim [my-app-name]
19+
```
20+
21+
Replace `[my-app-name]` with the desired directory name for your new application. You'll want to:
22+
23+
* Point your virtual host document root to your new application's `public/` directory.
24+
* Ensure `logs/` is web writable.
25+
26+
To run the application in development, you can run these commands
27+
28+
```bash
29+
cd [my-app-name]
30+
composer start
31+
```
32+
33+
Or you can use PHP Built-in web server:
34+
35+
```
36+
php -S localhost:8888 -t public
37+
```
38+
39+
Or you can use `docker-compose` to run the app with `docker`, so you can run these commands:
40+
```bash
41+
cd [my-app-name]
42+
docker-compose up -d
43+
```
44+
After that, open `http://localhost:8888` in your browser.
45+
46+
Run this command in the application directory to run the test suite
47+
48+
```bash
49+
composer test
50+
```
51+
52+
That's it! Now go build something cool.

app/app.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Slim\Factory\AppFactory;
4+
use DI\ContainerBuilder;
5+
6+
// Instantiate PHP-DI ContainerBuilder
7+
$containerBuilder = new ContainerBuilder();
8+
9+
// Set up settings
10+
$settings = require ROOT_PATH.'/app/settings.php';
11+
$settings($containerBuilder);
12+
13+
// Set up dependencies
14+
$dependencies = require ROOT_PATH.'/app/dependencies.php';
15+
$dependencies($containerBuilder);
16+
17+
// Set up repositories
18+
$repositories = require ROOT_PATH.'/app/repositories.php';
19+
$repositories($containerBuilder);
20+
21+
// Build PHP-DI Container instance
22+
$container = $containerBuilder->build();
23+
24+
// Instantiate the app
25+
$app = AppFactory::createFromContainer($container);
26+
27+
// Add Error Handling Middleware
28+
$app->addErrorMiddleware(true, false, false);
29+
30+
// Register middleware
31+
$middleware = require ROOT_PATH.'/app/middleware.php';
32+
$middleware($app);
33+
34+
// Register routes
35+
$routes = require ROOT_PATH.'/app/routes.php';
36+
$routes($app);
37+
38+
return $app;

app/dependencies.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DI\ContainerBuilder;
6+
use Monolog\Handler\StreamHandler;
7+
use Monolog\Logger;
8+
use Monolog\Processor\UidProcessor;
9+
use Psr\Container\ContainerInterface;
10+
use Psr\Log\LoggerInterface;
11+
12+
return function (ContainerBuilder $containerBuilder) {
13+
$containerBuilder->addDefinitions([
14+
LoggerInterface::class => function (ContainerInterface $c) {
15+
$settings = $c->get('settings');
16+
17+
$loggerSettings = $settings['logger'];
18+
$logger = new Logger($loggerSettings['name']);
19+
20+
$processor = new UidProcessor();
21+
$logger->pushProcessor($processor);
22+
23+
$handler = new StreamHandler($loggerSettings['path'], $loggerSettings['level']);
24+
$logger->pushHandler($handler);
25+
26+
return $logger;
27+
},
28+
]);
29+
};

app/middleware.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use App\Middleware\Authorization;
6+
use Tuupola\Middleware\HttpBasicAuthentication;
7+
use Slim\App;
8+
9+
return function (App $app) {
10+
$app->add(Authorization::class);
11+
12+
$app->add(new HttpBasicAuthentication([
13+
'users' => [
14+
'root' => 't00r',
15+
'somebody' => 'passw0rd',
16+
],
17+
'before' => function ($request, $arguments) {
18+
return $request->withAttribute('user', $arguments['user']);
19+
},
20+
]));
21+
};

app/repositories.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use App\Domain\User\UserRepository;
6+
use App\Infrastructure\Persistence\User\InMemoryUserRepository;
7+
use DI\ContainerBuilder;
8+
9+
return function (ContainerBuilder $containerBuilder) {
10+
// Here we map our UserRepository interface to its in memory implementation
11+
$containerBuilder->addDefinitions([
12+
UserRepository::class => \DI\autowire(InMemoryUserRepository::class),
13+
]);
14+
};

app/routes.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use App\Actions\User\ListUsersAction;
6+
use App\Actions\User\ViewUserAction;
7+
use Psr\Http\Message\ResponseInterface as Response;
8+
use Psr\Http\Message\ServerRequestInterface as Request;
9+
use Slim\App;
10+
use Slim\Interfaces\RouteCollectorProxyInterface as Group;
11+
12+
return function (App $app) {
13+
$app->get('/', function (Request $request, Response $response) {
14+
$response->getBody()->write('Hello Casbin !');
15+
16+
return $response;
17+
});
18+
19+
$app->group('/users', function (Group $group) {
20+
$group->get('', ListUsersAction::class);
21+
$group->get('/{id}', ViewUserAction::class);
22+
});
23+
};

app/settings.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use DI\ContainerBuilder;
6+
use Monolog\Logger;
7+
8+
return function (ContainerBuilder $containerBuilder) {
9+
// Global Settings Object
10+
$containerBuilder->addDefinitions([
11+
'settings' => [
12+
'displayErrorDetails' => true, // Should be set to false in production
13+
'logger' => [
14+
'name' => 'slim-app',
15+
'path' => isset($_ENV['docker']) ? 'php://stdout' : ROOT_PATH.'/logs/app.log',
16+
'level' => Logger::DEBUG,
17+
],
18+
],
19+
]);
20+
};

composer.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "casbin/casbin-with-slim",
3+
"description": "Casbin skeleton application with Slim Framework",
4+
"keywords": [
5+
"casbin",
6+
"microframework",
7+
"rest",
8+
"router",
9+
"psr7",
10+
"permission",
11+
"roles"
12+
],
13+
"type": "project",
14+
"license": "Apache-2.0",
15+
"authors": [
16+
{
17+
"name": "TechLee",
18+
"email": "[email protected]",
19+
"homepage": "https://github.com/techoner"
20+
}
21+
],
22+
"require": {
23+
"casbin/casbin": "^1.0",
24+
"monolog/monolog": "^2.0",
25+
"php-di/php-di": "^6.0",
26+
"slim/psr7": "^0.5.0",
27+
"slim/slim": "^4.2",
28+
"tuupola/slim-basic-auth": "^3.2"
29+
},
30+
"require-dev": {
31+
"phpunit/phpunit": "^7.5",
32+
"php-coveralls/php-coveralls": "^2.1"
33+
},
34+
"config": {
35+
"process-timeout": 0,
36+
"sort-packages": true
37+
},
38+
"autoload": {
39+
"psr-4": {
40+
"App\\": "src/"
41+
}
42+
},
43+
"autoload-dev": {
44+
"psr-4": {
45+
"Tests\\": "tests/"
46+
}
47+
},
48+
"scripts": {
49+
"start": "php -S localhost:8888 -t public",
50+
"test": "phpunit"
51+
}
52+
}

0 commit comments

Comments
 (0)