Skip to content

Commit

Permalink
Merge pull request #39 from ebln/polish
Browse files Browse the repository at this point in the history
Polish
  • Loading branch information
ebln committed Jun 9, 2024
2 parents f4bc25e + 7b45d98 commit 4f1f572
Show file tree
Hide file tree
Showing 46 changed files with 620 additions and 186 deletions.
1 change: 1 addition & 0 deletions .github/workflows/buildTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
./vendor/bin/psalm --monochrome --no-progress --stats --show-info=false
- name: phpstan
run: |
./vendor/bin/phpstan --version
./vendor/bin/phpstan --no-interaction --no-ansi analyse
- name: Mess Detector
run: |
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/.web-server-pid
/app/config/parameters.yml
/build/
/phpunit.xml
.php-cs-fixer*
!.php-cs-fixer.dist.*
/var/*
Expand Down Expand Up @@ -29,3 +28,4 @@
/composer.phar
/coverage/
/.phpunit.result.cache
/.provision/cache/
123 changes: 62 additions & 61 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,69 +50,69 @@
'fully_qualified_strict_types' => false,
'single_space_around_construct' => [
'constructs_followed_by_a_single_space' => [ // default, minus const
'abstract',
'as',
'attribute',
'break',
'case',
'catch',
'class',
'clone',
'comment',
'const_import',
'continue',
'do',
'echo',
'else',
'elseif',
'enum',
'extends',
'final',
'finally',
'for',
'foreach',
'function',
'function_import',
'global',
'goto',
'if',
'implements',
'include',
'include_once',
'instanceof',
'insteadof',
'interface',
'match',
'named_argument',
'namespace',
'new',
'open_tag_with_echo',
'php_doc',
'php_open',
'print',
'private',
'protected',
'public',
'readonly',
'require',
'require_once',
'return',
'static',
'switch',
'throw',
'trait',
'try',
'type_colon',
'use',
'use_lambda',
'use_trait',
'var',
'while',
'yield',
'yield_from',
'abstract',
'as',
'attribute',
'break',
'case',
'catch',
'class',
'clone',
'comment',
'const_import',
'continue',
'do',
'echo',
'else',
'elseif',
'enum',
'extends',
'final',
'finally',
'for',
'foreach',
'function',
'function_import',
'global',
'goto',
'if',
'implements',
'include',
'include_once',
'instanceof',
'insteadof',
'interface',
'match',
'named_argument',
'namespace',
'new',
'open_tag_with_echo',
'php_doc',
'php_open',
'print',
'private',
'protected',
'public',
'readonly',
'require',
'require_once',
'return',
'static',
'switch',
'throw',
'trait',
'try',
'type_colon',
'use',
'use_lambda',
'use_trait',
'var',
'while',
'yield',
'yield_from',
],

],
'void_return' => true,
]
)
->setFinder($finder)
Expand All @@ -123,6 +123,7 @@
echo "\n\n# DUMPING EFFECTIVE RULES #################\n";
$rules = $resolver->getRules();
ksort($rules);
/** @noinspection ForgottenDebugOutputInspection */
var_export($rules);
echo "\n\n###########################################\n";

Expand Down
3 changes: 1 addition & 2 deletions .provision/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ RUN apt-get update \
&& useradd --uid 1000 --gid app --shell /bin/bash --create-home app \
&& touch /var/log/xdebug.log && chgrp 1000 /var/log/xdebug.log && chmod g+w /var/log/xdebug.log \
&& sed -i -e '/alias l\|export\|eval/s/#//' /root/.bashrc \
&& mkdir --parents /var/www/src /var/www/tests \
&& chown 1000:1000 /var/www/src /var/www/tests \
&& mkdir --parents /var/www/src /var/www/tests /var/www/coverage \
&& true

COPY 30-xdebug-config.ini /usr/local/etc/php/conf.d/
Expand Down
1 change: 0 additions & 1 deletion .provision/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.6'
services:
php:
build:
Expand Down
24 changes: 20 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,25 @@ Intended to follow [«Keep a Changelog»](https://keepachangelog.com/en/)
## [Unreleased] (meant as staging area)

### Added
- Two new CookieTranscriptors
- `CookieHeaderTranscriptor`; squeezing `Set-Cookie` headers from PSR-7 into the cookie management of `sfWebRequest`
- `AbstractCookieDispatchTranscriptor`; more powerful, extensible, yet more dangerous approach – using black magic.
- Comes with new `CookieDispatcher`, `DispatchSubstitutor` and a bunch of other cookie related classes
### Changed
- General maintainance: annotations, spelling
- Improving test coverage calculation
- using `@covers` & `@uses` annotations
- enforce using new phpunit settings
- Improve test coverage
- Improve DX (docker, Makefile, gitattributes, cs-fixer rules)
- Extend mocks, due to new functionality
### Deprecated
-

### TODO

## TODO
- Clone the CookieDispatcher-Approach for the ResponseAdapter
- but instead of reading CookieContainer from options → in-situ intercept them → onStatusSent
- Cookies: Write to response
- Cookies: Read from request
- Cookies: write and overwrite to request
Expand Down Expand Up @@ -52,9 +68,9 @@ Intended to follow [«Keep a Changelog»](https://keepachangelog.com/en/)
- Updated dev dependencies
- Update code style

### Fixed
### Security

- downstream vulnerabilities by bumping `guzzlehttp/psr7`
- fixed downstream vulnerabilities by bumping `guzzlehttp/psr7`

## [1.4.0] - 2021-10-19

Expand All @@ -70,7 +86,7 @@ Intended to follow [«Keep a Changelog»](https://keepachangelog.com/en/)
### Changed

- Updated dev dependencies
- Renamed "Utillity" to `brnc\Symfony1\Message\Utility`, technically [BREAKING] yet was never supposed to be used in user land
- Renamed "Utility" to `brnc\Symfony1\Message\Utility`, technically [BREAKING] yet was never supposed to be used in user land

### Fixed

Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ $response = $entryPoint->handler($response);

Assume you couldn't use other means, and you're confronted with an arbitrary PSR-7 response you can use the `ResponseTranscriptor` to copy the data from your PSR-7 response to your `\sfWebResponse`.

Currently the `ResponseTranscriptor` doesn't support cookies, and will fail fast and hard if it encounters some. You are free to implement your own Cookie-Handler implementing `CookieTranscriptorInterface` and pass it as an optional constructor argument
The `ResponseTranscriptor` by default uses `NoCookieTranscriptor`, which fails hard in the presence of `Set-Cookie'` headers.
Incorporating (present-day) Cookies into the `\sfWebResponse` is not strait-forward. However, you are free to implement your own Cookie-Handler implementing `CookieTranscriptorInterface` and pass it as an optional constructor argument.

```php
// Given arbitrary PSR-7 response…
Expand All @@ -100,6 +101,22 @@ $transcriptor = new \brnc\Symfony1\Message\Transcriptor\ResponseTranscriptor();
$sfWebResponse = $transcriptor->transcribe($psr7response, $sfWebResponse);
```

### Implemented `CookieTranscriptorInterface`s

There are a few CookieTranscriptors already implemented; each come with their specific compromises.

#### `CookieHeaderTranscriptor`
Transcribes `Set-Cookie` headers from your PSR-7 response, into the cookie management of the Symfony1 response.
This comes with all downsides of the legacy signature of `setrawcookie()`. Foremost it's not supporting `SameSite`-attribute, nor everything else being `extension-av` as of RFC 265.

#### `AbstractCookieDispatchTranscriptor`
The (abstract) CookieDispatchTranscriptor uses reflection and swaps the response's EventDispatcher against a new one.
It is very tied against the original implementation of `sfWebResponse::sendHttpHeaders` especially its logging mechanism via events.
The `CookieDispatcher` puts itself between `sfWebResponse` and the original `sfEventDispatcher`, and fires the cookies from the PSR-7 response right before Symfony1 would have sent theirs.
You need to implement `AbstractCookieDispatchTranscriptor`'s `transcribeCookies()` method, depending on your source for the cookies being set. E.g. if your using a 3rd party library.
Your code eventually needs to return `CookieContainerInterface` full of `CookieInterface`s. There is already a `HeaderCookie`, that uses `header()` and expects an already crafted and complete `Set-Cookie`-headerline.
There are also `SetCookie` and `SetRawCookie` which will use the respective methods with the new signature – i.e. three arguments, with the options-array as a third one.

## Pass it down to http-foundation i.e. present-day Symfony

Combine this PSR7-Symfony1 Adapter and `symfony/psr-http-message-bridge` to connect your Symfony1 stack via PSR-7 to `symfony/http-foundation` objects and leverage using embedded (present-day) Symfony components.
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@
"tests/",
"mock/",
".*",
"phpunit.xml.dist",
"coverage/"
"phpunit.xml",
"coverage/",
".provision/cache"
]
},
"config": {
Expand Down
7 changes: 5 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ DOCKER_COMPOSE=docker-compose -f $(DOCKER_COMPOSE_YML)
MAKE=make -s
.DEFAULT_GOAL := help

.PHONY: help build rm down stop enter test quality style-fix coverage
.PHONY: help build rm down stop enter test quality style-fix coverage baseline

help: ## Show this help.
@grep -E '^[a-zA-Z_-]+:.*?##\s*.*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?##\\s*"}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Expand Down Expand Up @@ -35,4 +35,7 @@ style-fix:##Apply code style
$(DOCKER_COMPOSE) run ${DEFAULT_CONTAINER} composer style-fix

coverage:##Generate coverage report
$(DOCKER_COMPOSE) run ${DEFAULT_CONTAINER} composer coverage
$(DOCKER_COMPOSE) run ${DEFAULT_CONTAINER} sh -c "composer coverage && chown -R root:root coverage"

baseline:##Run unit tests
$(DOCKER_COMPOSE) run ${DEFAULT_CONTAINER} sh -c "vendor/bin/phpstan --generate-baseline=phpstan-baseline.php && vendor/bin/psalm --update-baseline"
10 changes: 10 additions & 0 deletions mock/sfEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class sfEventDispatcher
/** @var callable[][] */
private $listeners = [];

public function __construct()
{
$this->listeners['default'] = [fn(\sfEvent $e) => $e->setReturnValue(($e->getReturnValue() ?? 0) + 1)];
}

public function connect(string $name, callable $listener)
{
if (!isset($this->listeners[$name])) {
Expand All @@ -19,6 +24,11 @@ public function connect(string $name, callable $listener)
$this->listeners[$name][] = $listener;
}

public function getListeners(string $name): array
{
return $this->listeners[$name] ?? [];
}

public function filter(sfEvent $event, $value): sfEvent
{
$eventName = $event->getName();
Expand Down
2 changes: 0 additions & 2 deletions mock/sfWebResponse.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

/** @noinspection PhpUnusedParameterInspection */

/** @noinspection ReturnTypeCanBeDeclaredInspection */

/**
Expand Down
6 changes: 0 additions & 6 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,5 @@
'count' => 1,
'path' => __DIR__ . '/tests/Adapter/ServerRequestUploadTest.php',
];
$ignoreErrors[] = [
// identifier: missingType.iterableValue
'message' => '#^Method brnc\\\\Tests\\\\Symfony1\\\\Message\\\\ResponseMockTest\\:\\:createResponse\\(\\) return type has no value type specified in iterable type array\\.$#',
'count' => 1,
'path' => __DIR__ . '/tests/ResponseMockTest.php',
];

return ['parameters' => ['ignoreErrors' => $ignoreErrors]];
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ includes:

parameters:
level: max
tmpDir: .provision/cache
paths:
- src
- tests
Expand Down
28 changes: 28 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".provision/cache/.phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
convertDeprecationsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".provision/cache/.phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
<directory suffix=".php">mock</directory>
</include>
</coverage>
</phpunit>
14 changes: 0 additions & 14 deletions phpunit.xml.dist

This file was deleted.

Loading

0 comments on commit 4f1f572

Please sign in to comment.