Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port for Slim 4 Usage #105

Merged
merged 9 commits into from
Apr 28, 2019
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.idea
composer.lock
clover.xml
coverage
vendor
28 changes: 20 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@ language: php

dist: trusty

php:
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm
matrix:
include:
- php: 7.1
env: ANALYSIS='true'
- php: 7.2
- php: 7.3
- php: nightly
allow_failures:
- php: nightly

before_script: composer install
before_script:
- composer require php-coveralls/php-coveralls:^2.1.0
- composer install -n

script: vendor/bin/phpunit --coverage-text --configuration phpunit.xml.dist
script:
- if [[ "$ANALYSIS" == 'true' ]]; then vendor/bin/phpunit --coverage-clover clover.xml ; fi
- vendor/bin/phpunit
- vendor/bin/phpcs
- vendor/bin/phpstan analyse Slim

after_success:
- if [[ "$ANALYSIS" == 'true' ]]; then vendor/bin/php-coveralls --coverage_clover=clover.xml -v ; fi
63 changes: 35 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,35 @@ This is a Slim Framework view helper built on top of the Twig templating compone
Via [Composer](https://getcomposer.org/)

```bash
$ composer require slim/twig-view
$ composer require slim/twig-view:3.0.0-alpha
```

Requires Slim Framework 3 and PHP 5.5.0 or newer.
Requires Slim Framework 4 and PHP 7.1 or newer.

## Usage

```php
// Create Slim app
$app = new \Slim\App();
use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigExtension;
use Slim\Views\TwigMiddleware;

// Fetch DI Container
$container = $app->getContainer();
require __DIR__ . '/vendor/autoload.php';

// Register Twig View helper
$container['view'] = function ($c) {
$view = new \Slim\Views\Twig('path/to/templates', [
'cache' => 'path/to/cache'
]);

// Instantiate and add Slim specific extension
$router = $c->get('router');
$uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER));
$view->addExtension(new \Slim\Views\TwigExtension($router, $uri));
// Create Container
$container = new Container();
AppFactory::setContainer($container);

// Create App
$app = new AppFactory::create();

return $view;
};
// Add Twig-View Middleware
$basePath = '/base-path';
$routeParser = $app->getRouteCollector()->getRouteParser();
$twig = new Twig('path/to/templates', ['cache' => 'path/to/cache']);
$twigMiddleware = new TwigMiddleware($twig, $container, $routeParser, $basePath);
$app->add($twigMiddleware);

// Define named route
$app->get('/hello/{name}', function ($request, $response, $args) {
Expand All @@ -61,29 +63,33 @@ $app->run();

`TwigExtension` provides these functions to your Twig templates:

* `path_for()` - returns the URL for a given route.
* `base_url()` - returns the `Uri` object's base URL.
* `is_current_path()` - returns true is the provided route name and parameters are valid for the current path.
* `current_path()` - renders the current path, with or without the query string.
* `url_for()` - returns the URL for a given route. e.g.: /hello/world
* `full_url_for()` - returns the URL for a given route. e.g.: http://www.example.com/hello/world
* `is_current_url()` - returns true is the provided route name and parameters are valid for the current path.
* `current_url()` - returns the current path, with or without the query string.
* `get_uri()` - returns the `UriInterface` object from the incoming `ServerRequestInterface` object


You can use `path_for` to generate complete URLs to any Slim application named route and use `is_current_path` to determine if you need to mark a link as active as shown in this example Twig template:
You can use `url_for` to generate complete URLs to any Slim application named route and use `is_current_url` to determine if you need to mark a link as active as shown in this example Twig template:

{% extends "layout.html" %}

{% block body %}
<h1>User List</h1>
<ul>
<li><a href="{{ path_for('profile', { 'name': 'josh' }) }}" {% if is_current_path('profile', { 'name': 'josh' }) %}class="active"{% endif %}>Josh</a></li>
<li><a href="{{ path_for('profile', { 'name': 'andrew' }) }}">Andrew</a></li>
<li><a href="{{ url_for('profile', { 'name': 'josh' }) }}" {% if is_current_url('profile', { 'name': 'josh' }) %}class="active"{% endif %}>Josh</a></li>
<li><a href="{{ url_for('profile', { 'name': 'andrew' }) }}">Andrew</a></li>
</ul>
{% endblock %}


## Testing
## Tests

To execute the test suite, you'll need to clone the repository and install the dependencies.

```bash
phpunit
$ git clone https://github.com/slimphp/Twig-View
$ composer install
$ composer test
```

## Contributing
Expand All @@ -97,6 +103,7 @@ If you discover any security related issues, please email security@slimframework
## Credits

- [Josh Lockhart](https://github.com/codeguy)
- [Pierre Bérubé](https://github.com/l0gicgate)

## License

Expand Down
22 changes: 15 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "slim/twig-view",
"type": "library",
"description": "Slim Framework 3 view helper built on top of the Twig 2 templating component",
"description": "Slim Framework 4 view helper built on top of the Twig 2 templating component",
"keywords": ["slim","framework","view","template","twig"],
"homepage": "http://slimframework.com",
"license": "MIT",
Expand All @@ -10,20 +10,28 @@
"name": "Josh Lockhart",
"email": "hello@joshlockhart.com",
"homepage": "http://joshlockhart.com"
},
{
"name": "Pierre Berube",
"email": "pierre@lgse.com",
"homepage": "http://www.lgse.com"
}
],
"require": {
"php": ">=5.5.0",
"twig/twig": "^1.38|^2.7",
"psr/http-message": "^1.0"
"php": "^7.1",
"psr/http-message": "^1.0",
"twig/twig": "^2.8"
},
"require-dev": {
"phpunit/phpunit": "^4.8|^5.7",
"slim/slim": "^3.10"
"phpunit/phpunit": "^7.5",
"php-di/php-di": "^6.0",
"psr/http-factory": "^1.0",
"slim/slim": "^4.0.0-alpha"
},
"autoload": {
"psr-4": {
"Slim\\Views\\": "src"
"Slim\\Views\\": "src",
"Slim\\Tests\\": "tests"
}
}
}
24 changes: 19 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/7.1/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Slim Test Suite">
<testsuite name="Twig-View Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src</directory>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory>./src/</directory>
</whitelist>
</filter>

<logging>
<log
type="coverage-html"
target="./coverage"
lowUpperBound="20"
highLowerBound="50"
/>
</logging>
</phpunit>
Loading