Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
;

return PhpCsFixer\Config::create()
->setRules(array(
->setRules([
'@PSR2' => true,
))
'@PhpCsFixer' => true,
'php_unit_test_class_requires_covers' => false,
'ordered_imports' => [ 'sort_algorithm' => 'length' ],
])
->setFinder($finder)
;
;
1 change: 0 additions & 1 deletion .php_cs.cache

This file was deleted.

10 changes: 3 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
dist: trusty
language: php
php:
- 7.3
- 7.2
- 7.1
- 7.0
Expand All @@ -16,13 +18,7 @@ matrix:

script:
- composer test
- if [[ $PHPSTAN == 1 ]]; then composer require --dev phpstan/phpstan && composer static-analysis; fi

matrix:
include:
- php: 7.2
install: composer require --dev phpstan/phpstan
script: composer static-analysis
- if [[ $PHPSTAN == 1 ]]; then composer require --dev phpstan/phpstan && composer static-analysis && composer check-style; fi

after_success:
- bash <(curl -s https://codecov.io/bash)
32 changes: 32 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [Github](https://github.com/:vendor/:package_name).


## Pull Requests

- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - Check the code style with ``$ composer check-style`` and fix it with ``$ composer fix-style``.

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.

- **Create feature branches** - Don't ask us to pull from your master branch.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.


## Running Tests

``` bash
$ composer test
```


**Happy coding**!
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015-2018 Antonis Pavlakis . All rights reserved.
Copyright (c) 2015-2019 Antonis Pavlakis . All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
45 changes: 40 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
# pavlakis/slim-cli

[![Build Status](https://travis-ci.org/pavlakis/slim-cli.svg)](https://travis-ci.org/pavlakis/slim-cli)
[![Total Downloads](https://img.shields.io/packagist/dt/pavlakis/slim-cli.svg)](https://packagist.org/packages/pavlakis/slim-cli)
[![Latest Stable Version](https://img.shields.io/packagist/v/pavlakis/slim-cli.svg)](https://packagist.org/packages/pavlakis/slim-cli)
[![codecov](https://codecov.io/gh/pavlakis/slim-cli/branch/master/graph/badge.svg)](https://codecov.io/gh/pavlakis/slim-cli)
[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-brightgreen.svg?style=flat)](https://github.com/phpstan/phpstan)

# Slim 3 Framework CLI Request Middleware
Slim 3 Framework CLI Request Middleware

This middleware will transform a CLI call into a GET Request.
This middleware will transform a CLI call into a Request.

### Add it with composer
Note: Added support for the following HTTP Request methods:

```
'GET',
'HEAD',
'POST',
'PUT',
'DELETE',
'CONNECT',
'OPTIONS',
'TRACE',
'PATCH',
```

## Install

Via Composer

```
composer require pavlakis/slim-cli
```

## Usage

### Pass the parameters in this order
`route / method / query string`

Expand Down Expand Up @@ -80,8 +100,23 @@ $app->get('/status', 'PHPMinds\Action\EventStatusAction:dispatch')
->add(new Pavlakis\Middleware\Server\Sapi(["cli"]))
```

## Testing

``` bash
$ composer test
```

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.


## Credits

- [Antonios Pavlakis](https://github.com/pavlakis)

> Based on Bobby DeVeaux's ([@bobbyjason](https://twitter.com/bobbyjason)) [Gulp Skeleton](https://github.com/dvomedia/gulp-skeleton/blob/master/web/index.php)

### Credits
## License

Based on Bobby DeVeaux's ([@bobbyjason](https://twitter.com/bobbyjason)) [Gulp Skeleton](https://github.com/dvomedia/gulp-skeleton/blob/master/web/index.php)
The BSD 3-Clause License. Please see [License File](LICENSE) for more information.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@
"php": ">=5.5.0"
},
"require-dev": {
"slim/slim": "^3.0",
"phpunit/phpunit": "^4.0"
"friendsofphp/php-cs-fixer": "^2.2",
"phpunit/phpunit": "^4.0",
"slim/slim": "^3.0"
},
"config": {
"bin-dir": "bin",
"sort-packages": true,
"preferred-install": {
"*": "dist"
}
},
"scripts": {
"test": "phpunit -c tests/phpunit/phpunit.xml --colors=always",
"check-style": "php-cs-fixer fix --dry-run",
"fix-style": "php-cs-fixer fix",
"static-analysis": "phpstan analyse src --level 7"
}
}
134 changes: 78 additions & 56 deletions src/CliRequest.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,95 @@
<?php
/**
* Pavlakis Slim CLI Request
* Pavlakis Slim CLI Request.
*
* A Slim 3 middleware enabling a mock GET request to be made through the CLI.
* A Slim 3 middleware enabling a mock HTTP request to be made through the CLI.
* Use in the form: php public/index.php /status GET event=true
*
* @link https://github.com/pavlakis/slim-cli
* @copyright Copyright © 2018 Antonis Pavlakis
* @see https://github.com/pavlakis/slim-cli
*
* @copyright Copyright © 2019 Antonios Pavlakis
* @author Antonios Pavlakis
* @license https://github.com/pavlakis/slim-cli/blob/master/LICENSE (BSD 3-Clause License)
*/

namespace pavlakis\cli;

use pavlakis\cli\Environment\EnvironmentProperties;
use Psr\Http\Message\ServerRequestInterface;
use pavlakis\cli\Request\Request;
use Psr\Http\Message\ResponseInterface;
use pavlakis\cli\Request\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use pavlakis\cli\Environment\EnvironmentProperties;
use pavlakis\cli\Environment\EnvironmentPropertiesInterface;

class CliRequest
{
/**
* @var EnvironmentProperties
* @var ServerRequestInterface
*/
protected $request;
/**
* @var EnvironmentPropertiesInterface
*/
private $environment;

/**
* @var ServerRequestInterface
* @var RequestInterface
*/
protected $request = null;
private $requestAdapter;

/**
* @param EnvironmentProperties $environment
* @param null|EnvironmentPropertiesInterface $environment
* @param null|RequestInterface $request
*
* @throws Exception\DefaultPropertyExistsException
*/
public function __construct(EnvironmentProperties $environment = null)
public function __construct(EnvironmentPropertiesInterface $environment = null, RequestInterface $request = null)
{
# BC compatibility - always include DefaultEnvironment
if (is_null($environment)) {
// BC compatibility - always include DefaultEnvironment
if (null === $environment) {
$environment = new EnvironmentProperties();
}
$this->environment = $environment;

if (null === $request) {
$request = new Request();
}
$this->requestAdapter = $request;
}

/**
* Invoke middleware.
*
* @param ServerRequestInterface $request PSR7 request object
* @param ResponseInterface $response PSR7 response object
* @param callable $next Next middleware callable
*
* @return ResponseInterface PSR7 response object
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
global $argv;

$this->request = $request;

if (isset($argv)) {
$path = $this->get($argv, 1);
$method = $this->get($argv, 2);
$params = $this->get($argv, 3);

if ($this->requestAdapter->isMethodSupported($method)) {
$this->request = $this->getMockRequest($this->getUri($path, $params), $params, $method);
}

unset($argv);
}

return $next($this->request, $response);
}

/**
* Exposed for testing.
*
* @return ServerRequestInterface
*/
public function getRequest()
Expand All @@ -51,12 +98,13 @@ public function getRequest()
}

/**
* Get a value from an array if exists otherwise return a default value
* Get a value from an array if exists otherwise return a default value.
*
* @param array $argv
* @param int $key
* @param mixed $default
*
* @param array $argv
* @param integer $key
* @param mixed $default
* @return string
* @return string
*/
private function get($argv, $key, $default = '')
{
Expand All @@ -68,10 +116,11 @@ private function get($argv, $key, $default = '')
}

/**
* Construct the URI if path and params are being passed
* Construct the URI if path and params are being passed.
*
* @param string $path
* @param string $params
*
* @return string
*/
private function getUri($path, $params)
Expand All @@ -82,7 +131,7 @@ private function getUri($path, $params)
}

if (strlen($params) > 0) {
$uri .= '?' . $params;
$uri .= '?'.$params;
}

return $uri;
Expand All @@ -91,45 +140,18 @@ private function getUri($path, $params)
/**
* @param string $uri
* @param string $queryString
* @return \Slim\Http\Request
*/
private function getMockRequest($uri, $queryString)
{
return \Slim\Http\Request::createFromEnvironment(\Slim\Http\Environment::mock(
$this->environment->getProperties([
'REQUEST_URI' => $uri,
'QUERY_STRING' => $queryString
])
));
}

/**
* Invoke middleware
* @param string $method
*
* @param ServerRequestInterface $request PSR7 request object
* @param ResponseInterface $response PSR7 response object
* @param callable $next Next middleware callable
*
* @return ResponseInterface PSR7 response object
* @return \Slim\Http\Request
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
private function getMockRequest($uri, $queryString, $method)
{
global $argv;

$this->request = $request;
$environmentProperties = $this->environment->getProperties([
'REQUEST_METHOD' => strtoupper($method),
'REQUEST_URI' => $uri,
'QUERY_STRING' => $queryString,
]);

if (isset($argv)) {
$path = $this->get($argv, 1);
$method = $this->get($argv, 2);
$params = $this->get($argv, 3);

if (strtoupper($method) === $this->environment->getRequestMethod()) {
$this->request = $this->getMockRequest($this->getUri($path, $params), $params);
}

unset($argv);
}

return $next($this->request, $response);
return $this->requestAdapter->getMockRequest($environmentProperties);
}
}
Loading