Skip to content

Commit 19bbb6f

Browse files
committed
refactor
1 parent 92253e6 commit 19bbb6f

Some content is hidden

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

42 files changed

+1032
-352
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
.gitattributes export-ignore
22
.gitignore export-ignore
3+
.travis.yml export-ignore
34
examples export-ignore
5+
tests export-ignore

.gitignore

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

.travis.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
language: php
2+
php:
3+
- 5.3.3
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- hhvm
8+
9+
matrix:
10+
allow_failures:
11+
- php: hhvm
12+
13+
script:
14+
- vendor/bin/tester tests -s -c tests/php-unix.ini
15+
- php code-checker/src/code-checker.php -d src
16+
17+
after_failure:
18+
# Print *.actual content
19+
- for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done
20+
21+
before_script:
22+
# Install Nette Tester & Code Checker
23+
- composer install --no-interaction --dev --prefer-source
24+
- composer create-project nette/code-checker code-checker ~2.2 --no-interaction --prefer-source

composer.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
"source": "https://github.com/dotblue/nette-webimages"
2424
},
2525
"require": {
26-
"php": ">=5.4",
27-
"nette/application": ">=2.2.0,<2.3.0",
28-
"nette/di": ">=2.2.0,<2.3.0",
29-
"nette/http": ">=2.2.0,<2.3.0",
30-
"latte/latte": ">=2.2.0,<2.3.0",
31-
"nette/utils": ">=2.2.0,<2.3.0"
26+
"php": ">=5.3.3",
27+
"nette/application": "~2.2.0",
28+
"nette/di": "~2.2.0",
29+
"nette/http": "~2.2.0",
30+
"latte/latte": "~2.2.0",
31+
"nette/utils": "~2.2.0"
32+
},
33+
"require-dev": {
34+
"nette/tester": "~1.0"
3235
},
3336
"autoload": {
3437
"classmap": ["src/"]

examples/DefaultImageProvider.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,19 @@ class DefaultImageProvider implements IProvider
1111
private $wwwDir;
1212

1313

14-
1514
public function __construct($wwwDir)
1615
{
1716
$this->wwwDir = $wwwDir;
1817
}
1918

2019

21-
22-
public function getImage($id, $width, $height, $algorithm = NULL)
20+
public function getImage($id, $width, $height, $flags = NULL)
2321
{
2422
$path = $this->wwwDir . '/originals/' . $id . '.jpg';
2523

2624
if (is_file($path)) {
2725
$image = Image::fromFile($path);
28-
$image->resize($width, $height, $algorithm);
26+
$image->resize($width, $height, $flags);
2927
return $image;
3028
}
3129
}

examples/FakeImageProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class FakeImageProvider implements IProvider
88
{
99

10-
public function getImage($id, $width, $height, $algorithm = NULL)
10+
public function getImage($id, $width, $height, $flags = NULL)
1111
{
1212
$source = "http://fakeimg.pl/{$width}x{$height}";
1313
return Image::fromString(file_get_contents($source));

examples/route-with-filters.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
$router = $container->getService('router');
44

5-
$router[] = new DotBlue\WebImages\Route('images/<id>-<width>x<height>.jpg', [
6-
'id' => [
7-
DotBlue\WebImages\Route::FILTER_IN => function ($slug) {
5+
$router[] = new DotBlue\WebImages\Route('images/<id>-<width>x<height>.jpg', array(
6+
'id' => array(
7+
DotBlue\WebImages\Route::FILTER_IN => function($slug) {
88
// ...
99
},
10-
DotBlue\WebImages\Route::FILTER_OUT => function ($id) {
10+
DotBlue\WebImages\Route::FILTER_OUT => function($id) {
1111
// ...
1212
},
13-
],
14-
], $container->getByType('DotBlue\WebImages\Generator'));
13+
),
14+
), $container->getByType('DotBlue\WebImages\Generator'));

readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#### Requirements
22

3-
- PHP 5.4+
3+
- PHP 5.3.3+
44
- [nette/application](https://github.com/nette/application) >= 2.2
55
- [nette/di](https://github.com/nette/di) >= 2.2
66
- [nette/http](https://github.com/nette/http) >= 2.2
@@ -15,7 +15,7 @@ $ composer require dotblue/nette-webimages@~1.0
1515
```
1616

1717
2) Register as Configurator's extension:
18-
```
18+
```yml
1919
extensions:
2020
webimages: DotBlue\WebImages\Extension
2121
```
@@ -39,15 +39,15 @@ First, you have to define your `DotBlue\WebImages\IProvider` implementation. Its
3939

4040
When you have it, register it in configuration:
4141

42-
```
42+
```yml
4343
webimages:
4444
providers:
4545
- <name of your class>
4646
```
4747

4848
Secondly you have to specify route where your images will be available. Central point of the route is `id` parameter, which should uniquely identify your image. Lets setup simple route:
4949

50-
```
50+
```yml
5151
webimages:
5252
routes:
5353
- images/<id>-<width>x<height>.jpg
@@ -65,4 +65,4 @@ This will result in following HTML:
6565
<img src="/images/foo-200x150.jpg">
6666
```
6767

68-
Creation of this file will handle your implementation of `DotBlue\WebImages\IProvider`.
68+
Creation of this file will be handled by your implementation of `DotBlue\WebImages\IProvider`.

src/Application/Response.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace DotBlue\WebImages\Application;
4+
5+
use Nette\Object;
6+
use Nette\Application\IResponse;
7+
use Nette\Utils\Image;
8+
use Nette\Http\IRequest;
9+
use Nette\Http\IResponse as HttpResponse;
10+
11+
12+
/**
13+
* Image response.
14+
*/
15+
class Response extends Object implements IResponse
16+
{
17+
18+
/** @var \Nette\Utils\Image|NULL */
19+
protected $image;
20+
21+
22+
/** @var int */
23+
protected $format;
24+
25+
26+
/**
27+
* @param \Nette\Utils\Image|NULL
28+
* @param int
29+
*/
30+
public function __construct(Image $image = NULL, $format = Image::JPEG)
31+
{
32+
$this->image = $image;
33+
$this->format = $format;
34+
}
35+
36+
37+
public function send(IRequest $httpRequest, HttpResponse $httpResponse)
38+
{
39+
if (!$this->image) {
40+
$httpResponse->setHeader('Content-Type', image_type_to_mime_type($this->format));
41+
$httpResponse->setCode(HttpResponse::S404_NOT_FOUND);
42+
return;
43+
}
44+
$this->image->send($this->format);
45+
}
46+
47+
}

src/Application/Route.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/**
4+
* Copyright (c) dotBlue (http://dotblue.net)
5+
*/
6+
7+
namespace DotBlue\WebImages\Application;
8+
9+
use Nette\Application\Routers\Route as BaseRoute;
10+
use Nette\Utils\Image;
11+
use DotBlue\WebImages\Generator;
12+
13+
14+
class Route extends BaseRoute
15+
{
16+
17+
/** @var array */
18+
protected $defaults = array(
19+
'flags' => Image::JPEG
20+
);
21+
22+
23+
/**
24+
* @param string
25+
* @param array
26+
* @param \DotBlue\WebImages\Generator
27+
*/
28+
public function __construct($mask, array $defaults, Generator $generator)
29+
{
30+
$this->defaults = array_replace($this->defaults, $defaults);
31+
$defaults['presenter'] = 'Nette:Micro';
32+
$me = $this;
33+
34+
$defaults[NULL][self::FILTER_OUT] = function($params) use ($me, $generator) {
35+
$width = $me->acquireArgument('width', $params);
36+
$height = $me->acquireArgument('height', $params);
37+
$flags = $me->acquireArgument('flags', $params);
38+
39+
if (!$generator->getValidator()->validate($width, $height, $flags)) {
40+
throw new \Nette\Application\ForbiddenRequestException("Image with params ({$width}x{$height}, {$flags}) is not allowed - check your 'webimages.rules' please.");
41+
}
42+
43+
return $params;
44+
};
45+
46+
$defaults['callback'] = function($presenter) use ($me, $generator) {
47+
$params = $presenter->getRequest()->getParameters();
48+
49+
$image = NULL;
50+
try {
51+
$id = $me->acquireArgument('id', $params);
52+
$width = $me->acquireArgument('width', $params);
53+
$height = $me->acquireArgument('height', $params);
54+
$flags = $me->acquireArgument('flags', $params);
55+
56+
$image = $generator->generateImage($id, $width, $height, $flags);
57+
} catch (\Exception $e) {}
58+
59+
return new Response($image);
60+
};
61+
62+
parent::__construct($mask, $defaults);
63+
}
64+
65+
66+
/**
67+
* @param string
68+
* @param array
69+
* @return mixed
70+
* @throws \Nette\InvalidStateException
71+
*/
72+
public function acquireArgument($name, array $data)
73+
{
74+
if (isset($data[$name])) {
75+
return $data[$name];
76+
} elseif (isset($this->defaults[$name])) {
77+
return $this->defaults[$name];
78+
} else {
79+
throw new \Nette\InvalidArgumentException("Missing parameter $name.");
80+
}
81+
}
82+
83+
}

0 commit comments

Comments
 (0)