Skip to content

Commit e176efb

Browse files
authored
Version 1.2.0 (#6)
* Use tap * Laravel 7 & guzzle 7 * Immutable client (#1) * Replace/mutate client * Bug fixes and test * Update changelog * Defer service provider * Revert "Defer service provider" This reverts commit 8bce8a6. * Fix version * Merges config recursively (#3) * Temporary fix for merging config * Merge recursive distinct * Correct json driver merging * Rename helper method to avoid conflict * Update changelog * Update changelog * Update changelog * Use splat operator * Update changelog * Add trait to interaction with HTTP message * codecov config * Update changelog * Use interface for transformable response body * Update changelog * Rename transformable to parsable * Fix callable middleware * Update readme * Update changelog * Add php-cs-fixer config * Bug fixes for lazy evaluation and improvements * Update syntax * Invoke the lazy callable * Invoke lazy closure with using container * Update changelog * Remove unused helper function
1 parent 12b4e14 commit e176efb

16 files changed

+433
-97
lines changed

.php_cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__)
5+
->name('*.php')
6+
->notName('*.blade.php')
7+
->ignoreDotFiles(true)
8+
->ignoreVCS(true);
9+
10+
return PhpCsFixer\Config::create()
11+
->setCacheFile(__DIR__.'/.php_cs.cache')
12+
->setRules([
13+
'@PSR2' => true,
14+
'align_multiline_comment' => true,
15+
'array_indentation' => true,
16+
'array_syntax' => ['syntax' => 'short'],
17+
'blank_line_after_opening_tag' => true,
18+
'class_attributes_separation' => ['elements' => ['method', 'property']],
19+
'combine_consecutive_issets' => true,
20+
'combine_consecutive_unsets' => true,
21+
'compact_nullable_typehint' => true,
22+
'concat_space' => ['spacing' => 'none'],
23+
'explicit_indirect_variable' => true,
24+
'function_typehint_space' => true,
25+
'linebreak_after_opening_tag' => true,
26+
'lowercase_cast' => true,
27+
'lowercase_static_reference' => true,
28+
'magic_constant_casing' => true,
29+
'magic_method_casing' => true,
30+
'method_argument_space' => ['keep_multiple_spaces_after_comma' => false, 'on_multiline' => 'ignore'],
31+
'method_chaining_indentation' => true,
32+
'multiline_comment_opening_closing' => true,
33+
'multiline_whitespace_before_semicolons' => true,
34+
'native_function_casing' => true,
35+
'no_alternative_syntax' => true,
36+
'no_blank_lines_after_class_opening' => true,
37+
'no_blank_lines_after_phpdoc' => true,
38+
'no_empty_phpdoc' => true,
39+
'no_empty_statement' => true,
40+
'no_extra_blank_lines' => true,
41+
'no_leading_import_slash' => true,
42+
'no_leading_namespace_whitespace' => true,
43+
'no_multiline_whitespace_around_double_arrow' => true,
44+
'no_short_bool_cast' => true,
45+
'no_spaces_around_offset' => true,
46+
'no_singleline_whitespace_before_semicolons' => true,
47+
'no_trailing_comma_in_list_call' => true,
48+
'no_trailing_comma_in_singleline_array' => true,
49+
'no_unneeded_control_parentheses' => true,
50+
'no_unneeded_curly_braces' => true,
51+
'no_unused_imports' => true,
52+
'no_whitespace_before_comma_in_array' => true,
53+
'no_whitespace_in_blank_line' => true,
54+
'normalize_index_brace' => true,
55+
'not_operator_with_successor_space' => true,
56+
'object_operator_without_whitespace' => true,
57+
'ordered_imports' => true,
58+
'phpdoc_annotation_without_dot' => true,
59+
'phpdoc_indent' => true,
60+
'phpdoc_inline_tag' => true,
61+
'phpdoc_no_alias_tag' => true,
62+
'phpdoc_order' => true,
63+
'phpdoc_return_self_reference' => true,
64+
'phpdoc_scalar' => true,
65+
'phpdoc_single_line_var_spacing' => true,
66+
'phpdoc_summary' => true,
67+
'phpdoc_to_comment' => true,
68+
'phpdoc_trim' => true,
69+
'phpdoc_trim_consecutive_blank_line_separation' => true,
70+
'phpdoc_types' => true,
71+
'phpdoc_var_without_name' => true,
72+
'return_assignment' => true,
73+
'return_type_declaration' => true,
74+
'semicolon_after_instruction' => true,
75+
'short_scalar_cast' => true,
76+
'single_blank_line_before_namespace' => true,
77+
'single_line_comment_style' => ['comment_types' => ['hash']],
78+
'single_quote' => true,
79+
'space_after_semicolon' => true,
80+
'standardize_increment' => true,
81+
'standardize_not_equals' => true,
82+
'ternary_operator_spaces' => true,
83+
'trailing_comma_in_multiline_array' => true,
84+
'trim_array_spaces' => true,
85+
'unary_operator_spaces' => true,
86+
'whitespace_after_comma_in_array' => true,
87+
])
88+
->setFinder($finder);

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,22 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
1616
1717
### Fixed -->
1818

19+
## [1.2.0](https://github.com/jenky/hermes/compare/1.1.1...1.2.0) - 2020-02-28
20+
21+
### Added
22+
- Add some syntax sugar methods to interact with HTTP response message
23+
- Lazy evaluation to deal with config dependencies
24+
25+
### Changed
26+
- Rename `array_merge_recursive_unique` to `array_merge_recursive_distinct` and move it to `functions.php` file with namespace
27+
- Use splat operator for `array_merge_recursive_distinct` instead of `func_get_args()`
28+
- Use `Jenky\Hermes\Parsable` interface for response handler that needs to parse response body to native type
29+
1930
## [1.1.1](https://github.com/jenky/hermes/compare/1.1.0...1.1.1) - 2020-02-25
2031

2132
### Fixed
2233
- Fixed merges config recursively
34+
- Fix callable middleware
2335

2436
### Changed
2537
- `ResponseHandler` interceptor constructor now accepts class name instead of `ResponseHandlerInterface` instance

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ Configure guzzle [Middleware](http://docs.guzzlephp.org/en/stable/handlers-and-m
106106

107107
> The package ships with 2 interceptors. You can read about the interceptors in the [middleware](#middleware) section.
108108
109+
**Lazy evaluation**
110+
111+
If your middleware use Laravel service container binding implementations such as config, session driver, logger inside the `hermes` config file, you'll need to create your middleware using `Jenky\Hermes\lazy()` function. This is because those implementations are not yet bound to the container when the `hermes` config is loaded. The `lazy` function will wrap your middleware inside a `Closure` then invokes when parsing the configuration.
112+
113+
``` php
114+
'interceptors' => [
115+
// This won't work
116+
GuzzleHttp\Middleware::log(logs(), new GuzzleHttp\MessageFormatter),
117+
118+
// This should work
119+
Jenky\Hermes\lazy(function () {
120+
return GuzzleHttp\Middleware::log(logs(), new GuzzleHttp\MessageFormatter);
121+
}),
122+
],
123+
```
109124

110125
### Customizing the guzzle handler stack
111126

codecov.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
coverage:
2+
range: 70..90
3+
round: down
4+
precision: 2

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"laravel",
77
"guzzle",
88
"http",
9+
"http client",
910
"http message",
1011
"PSR http",
1112
"psr-7",
@@ -22,10 +23,10 @@
2223
}
2324
],
2425
"require": {
25-
"php" : "^7.1.3",
26-
"illuminate/container": "^5.7|^6.0|^7.0",
26+
"php": "^7.1.3",
27+
"guzzlehttp/guzzle": "^6.3|^7.0",
2728
"illuminate/config": "^5.7|^6.0|^7.0",
28-
"guzzlehttp/guzzle": "^6.3|^7.0"
29+
"illuminate/container": "^5.7|^6.0|^7.0"
2930
},
3031
"require-dev": {
3132
"orchestra/testbench": "^3.5|^4.0",
@@ -37,6 +38,7 @@
3738
"Jenky\\Hermes\\": "src"
3839
},
3940
"files": [
41+
"src/functions.php",
4042
"src/helpers.php"
4143
]
4244
},

src/Concerns/InteractsWithConfiguration.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GuzzleHttp\HandlerStack;
66
use Illuminate\Support\Str;
7+
use Jenky\Hermes\LazyEvaluation;
78

89
trait InteractsWithConfiguration
910
{
@@ -143,11 +144,24 @@ protected function parseInterceptor($key, $value)
143144
$name = is_numeric($key) ? '' : $key;
144145

145146
if (is_callable($value)) {
147+
// $value = $this->isLazyEvaluable($value) ? $this->app->call($value) : $value;
148+
$value = $this->isLazyEvaluable($value) ? $value() : $value;
149+
146150
return [$value, $name];
147151
}
148152

149153
[$class, $arguments] = $this->parseClassAndArguments($key, $value);
150154

151155
return [$this->app->make($class, $arguments), $class];
152156
}
157+
158+
/**
159+
* Determine if the callable is lazy evaluated.
160+
*
161+
* @return bool
162+
*/
163+
protected function isLazyEvaluable(callable $callable)
164+
{
165+
return $callable instanceof LazyEvaluation;
166+
}
153167
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace Jenky\Hermes\Concerns;
4+
5+
trait InteractsWithMessage
6+
{
7+
/**
8+
* Retrieve a header from the request.
9+
*
10+
* @param string|null $key
11+
* @param string|array|null $default
12+
* @return string|array|null
13+
*/
14+
public function header($header = null, $default = null)
15+
{
16+
if ($header) {
17+
return $this->getHeader($header)[0] ?? $default;
18+
}
19+
20+
return array_map(function ($values) {
21+
return $values[0] ?? null;
22+
}, $this->getHeaders());
23+
}
24+
25+
/**
26+
* Get or check the status code.
27+
*
28+
* @param int|null $code
29+
* @return int|bool
30+
*/
31+
public function status($code = null)
32+
{
33+
$statusCode = $this->getStatusCode();
34+
35+
return $code ? $statusCode == $code : $statusCode;
36+
}
37+
38+
/**
39+
* Get the response body as string.
40+
*
41+
* @return string
42+
*/
43+
public function body()
44+
{
45+
return (string) $this->getBody();
46+
}
47+
48+
/**
49+
* Determine that response status code is ok.
50+
*
51+
* @return bool
52+
*/
53+
public function ok(): bool
54+
{
55+
return $this->status(200);
56+
}
57+
58+
/**
59+
* Determine that response status code is created.
60+
*
61+
* @return bool
62+
*/
63+
public function created(): bool
64+
{
65+
return $this->status(201);
66+
}
67+
68+
/**
69+
* Determine that response status code is bad request.
70+
*
71+
* @return bool
72+
*/
73+
public function badRequest(): bool
74+
{
75+
return $this->status(400);
76+
}
77+
78+
/**
79+
* Determine that response status code is unauthorized.
80+
*
81+
* @return bool
82+
*/
83+
public function unauthorized(): bool
84+
{
85+
return $this->status(401);
86+
}
87+
88+
/**
89+
* Determine that response status code is forbidden.
90+
*
91+
* @return bool
92+
*/
93+
public function forbidden(): bool
94+
{
95+
return $this->status(403);
96+
}
97+
98+
/**
99+
* Determine that response status code is not found.
100+
*
101+
* @return bool
102+
*/
103+
public function notFound(): bool
104+
{
105+
return $this->status(404);
106+
}
107+
108+
/**
109+
* Determine that response status code is unprocessable.
110+
*
111+
* @return bool
112+
*/
113+
public function unprocessable(): bool
114+
{
115+
return $this->status(422);
116+
}
117+
118+
/**
119+
* Determine that response status code is internal server error.
120+
*
121+
* @return bool
122+
*/
123+
public function serverError(): bool
124+
{
125+
return $this->status(500);
126+
}
127+
}

src/GuzzleManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ protected function configurationFor($name)
112112
*/
113113
protected function resolve($name, array $options = [])
114114
{
115-
$config = array_merge_recursive_unique(
115+
$config = array_merge_recursive_distinct(
116116
$this->configurationFor($name), $options
117117
);
118118

@@ -199,7 +199,7 @@ protected function createGuzzleDriver(array $config)
199199
protected function createJsonDriver(array $config)
200200
{
201201
return new Client($this->makeClientOptions(
202-
array_merge_recursive_unique([
202+
array_merge_recursive_distinct([
203203
'options' => [
204204
'response_handler' => JsonResponse::class,
205205
],

src/JsonResponse.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
use Illuminate\Contracts\Support\Jsonable;
88
use JsonSerializable;
99

10-
class JsonResponse extends Response implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
10+
class JsonResponse extends Response implements Parsable, ArrayAccess, Arrayable, Jsonable, JsonSerializable
1111
{
1212
use Concerns\InteractsWithResponse;
1313

1414
/**
15-
* Transform the response body to array.
15+
* Parse the response body to array.
1616
*
1717
* @return void
1818
*/
19-
protected function transform()
19+
public function parse()
2020
{
2121
$this->data = json_decode(
2222
$this->getBody()->__toString(), true

0 commit comments

Comments
 (0)