Setting up the framework #1

Merged
PootisPenserHere merged 3 commits from settingUpTheFramework into master 2018-08-05 00:33:04 +00:00
18 changed files with 2260 additions and 0 deletions

4
api-payroll/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/vendor/
/logs/*
!/logs/README.md
.idea/*

View File

@ -0,0 +1,14 @@
# How to Contribute
## Pull Requests
1. Fork the Slim Skeleton repository
2. Create a new branch for each feature or improvement
3. Send a pull request from each feature branch to the **3.x** branch
It is very important to separate new features or improvements into separate feature branches, and to send a
pull request for each branch. This allows us to review and pull in new features or improvements individually.
## Style Guide
All pull requests must adhere to the [PSR-2 standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md).

27
api-payroll/README.md Normal file
View File

@ -0,0 +1,27 @@
# Slim Framework 3 Skeleton Application
Use this skeleton application to quickly setup and start working on a new Slim Framework 3 application. This application uses the latest Slim 3 with the PHP-View template renderer. It also uses the Monolog logger.
This skeleton application was built for Composer. This makes setting up a new Slim Framework application quick and easy.
## Install the Application
Run this command from the directory in which you want to install your new Slim Framework application.
php composer.phar create-project slim/slim-skeleton [my-app-name]
Replace `[my-app-name]` with the desired directory name for your new application. You'll want to:
* Point your virtual host document root to your new application's `public/` directory.
* Ensure `logs/` is web writeable.
To run the application in development, you can run these commands
cd [my-app-name]
php composer.phar start
Run this command in the application directory to run the test suite
php composer.phar test
That's it! Now go build something cool.

21
api-payroll/buildspec.yml Normal file
View File

@ -0,0 +1,21 @@
version: 0.2
phases:
install:
commands:
- echo Entered the install phase...
- cd api-payroll
- composer install
pre_build:
commands:
- echo Entered the pre_build phase...
build:
commands:
- echo Entered the build phase...
- echo Build started on `date`
- composer test
post_build:
commands:
- echo Entered the post_build phase...
- echo Build completed on `date`

36
api-payroll/composer.json Normal file
View File

@ -0,0 +1,36 @@
{
"name": "slim/slim-skeleton",
"description": "A Slim Framework skeleton application for rapid development",
"keywords": ["microframework", "rest", "router", "psr7"],
"homepage": "http://github.com/slimphp/Slim-Skeleton",
"license": "MIT",
"authors": [
{
"name": "Josh Lockhart",
"email": "info@joshlockhart.com",
"homepage": "http://www.joshlockhart.com/"
}
],
"require": {
"php": ">=5.5.0",
"slim/slim": "^3.1",
"slim/php-view": "^2.0",
"monolog/monolog": "^1.17"
},
"require-dev": {
"phpunit/phpunit": ">=4.8 < 6.0"
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"config": {
"process-timeout" : 0
},
"scripts": {
"start": "php -S localhost:8080 -t public",
"test": "phpunit"
}
}

1870
api-payroll/composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,18 @@
version: '2'
volumes:
logs:
driver: local
services:
slim:
image: php:7-alpine
working_dir: /var/www
command: php -S 0.0.0.0:8080 -t public
environment:
docker: "true"
ports:
- 8080:8080
volumes:
- .:/var/www
- logs:/var/www/logs

View File

@ -0,0 +1 @@
Your Slim Framework application's log files will be written to this directory.

7
api-payroll/phpunit.xml Normal file
View File

@ -0,0 +1,7 @@
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="SlimSkeleton">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@ -0,0 +1,21 @@
<IfModule mod_rewrite.c>
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the index.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
# absolute physical path to the directory that contains this htaccess file.
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>

View File

@ -0,0 +1,30 @@
<?php
if (PHP_SAPI == 'cli-server') {
// To help the built-in PHP dev server, check if the request was actually for
// something which should probably be served as a static file
$url = parse_url($_SERVER['REQUEST_URI']);
$file = __DIR__ . $url['path'];
if (is_file($file)) {
return false;
}
}
require __DIR__ . '/../vendor/autoload.php';
session_start();
// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
// Set up dependencies
require __DIR__ . '/../src/dependencies.php';
// Register middleware
require __DIR__ . '/../src/middleware.php';
// Register routes
require __DIR__ . '/../src/routes.php';
// Run app
$app->run();

View File

@ -0,0 +1,19 @@
<?php
// DIC configuration
$container = $app->getContainer();
// view renderer
$container['renderer'] = function ($c) {
$settings = $c->get('settings')['renderer'];
return new Slim\Views\PhpRenderer($settings['template_path']);
};
// monolog
$container['logger'] = function ($c) {
$settings = $c->get('settings')['logger'];
$logger = new Monolog\Logger($settings['name']);
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], $settings['level']));
return $logger;
};

View File

@ -0,0 +1,4 @@
<?php
// Application middleware
// e.g: $app->add(new \Slim\Csrf\Guard);

View File

@ -0,0 +1,14 @@
<?php
use Slim\Http\Request;
use Slim\Http\Response;
// Routes
$app->get('/[{name}]', function (Request $request, Response $response, array $args) {
// Sample log message
$this->logger->info("Slim-Skeleton '/' route");
// Render index view
return $this->renderer->render($response, 'index.phtml', $args);
});

View File

@ -0,0 +1,19 @@
<?php
return [
'settings' => [
'displayErrorDetails' => true, // set to false in production
'addContentLengthHeader' => false, // Allow the web server to send the content-length header
// Renderer settings
'renderer' => [
'template_path' => __DIR__ . '/../templates/',
],
// Monolog settings
'logger' => [
'name' => 'slim-app',
'path' => isset($_ENV['docker']) ? 'php://stdout' : __DIR__ . '/../logs/app.log',
'level' => \Monolog\Logger::DEBUG,
],
],
];

View File

@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Slim 3</title>
<link href='//fonts.googleapis.com/css?family=Lato:300' rel='stylesheet' type='text/css'>
<style>
body {
margin: 50px 0 0 0;
padding: 0;
width: 100%;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-align: center;
color: #aaa;
font-size: 18px;
}
h1 {
color: #719e40;
letter-spacing: -3px;
font-family: 'Lato', sans-serif;
font-size: 100px;
font-weight: 200;
margin-bottom: 0;
}
</style>
</head>
<body>
<h1>Slim</h1>
<div>a microframework for PHP</div>
<?php if (isset($name)) : ?>
<h2>Hello <?= htmlspecialchars($name); ?>!</h2>
<?php else: ?>
<p>Try <a href="http://www.slimframework.com">SlimFramework</a></p>
<?php endif; ?>
</body>
</html>

View File

@ -0,0 +1,77 @@
<?php
namespace Tests\Functional;
use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\Environment;
/**
* This is an example class that shows how you could set up a method that
* runs the application. Note that it doesn't cover all use-cases and is
* tuned to the specifics of this skeleton app, so if your needs are
* different, you'll need to change it.
*/
class BaseTestCase extends \PHPUnit_Framework_TestCase
{
/**
* Use middleware when running application?
*
* @var bool
*/
protected $withMiddleware = true;
/**
* Process the application given a request method and URI
*
* @param string $requestMethod the request method (e.g. GET, POST, etc.)
* @param string $requestUri the request URI
* @param array|object|null $requestData the request data
* @return \Slim\Http\Response
*/
public function runApp($requestMethod, $requestUri, $requestData = null)
{
// Create a mock environment for testing with
$environment = Environment::mock(
[
'REQUEST_METHOD' => $requestMethod,
'REQUEST_URI' => $requestUri
]
);
// Set up a request object based on the environment
$request = Request::createFromEnvironment($environment);
// Add request data, if it exists
if (isset($requestData)) {
$request = $request->withParsedBody($requestData);
}
// Set up a response object
$response = new Response();
// Use the application settings
$settings = require __DIR__ . '/../../src/settings.php';
// Instantiate the application
$app = new App($settings);
// Set up dependencies
require __DIR__ . '/../../src/dependencies.php';
// Register middleware
if ($this->withMiddleware) {
require __DIR__ . '/../../src/middleware.php';
}
// Register routes
require __DIR__ . '/../../src/routes.php';
// Process the application
$response = $app->process($request, $response);
// Return the response
return $response;
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace Tests\Functional;
class HomepageTest extends BaseTestCase
{
/**
* Test that the index route returns a rendered response containing the text 'SlimFramework' but not a greeting
*/
public function testGetHomepageWithoutName()
{
$response = $this->runApp('GET', '/');
$this->assertEquals(200, $response->getStatusCode());
$this->assertContains('SlimFramework', (string)$response->getBody());
$this->assertNotContains('Hello', (string)$response->getBody());
}
/**
* Test that the index route with optional name argument returns a rendered greeting
*/
public function testGetHomepageWithGreeting()
{
$response = $this->runApp('GET', '/name');
$this->assertEquals(200, $response->getStatusCode());
$this->assertContains('Hello name!', (string)$response->getBody());
}
/**
* Test that the index route won't accept a post request
*/
public function testPostHomepageNotAllowed()
{
$response = $this->runApp('POST', '/', ['test']);
$this->assertEquals(405, $response->getStatusCode());
$this->assertContains('Method not allowed', (string)$response->getBody());
}
}