Skip to content

Commit

Permalink
Implements v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
roukmoute committed Dec 5, 2021
1 parent ce7ee2c commit 4a3c0d2
Show file tree
Hide file tree
Showing 23 changed files with 521 additions and 267 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/spec export-ignore
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
updates:
- package-ecosystem: composer
directory: "/"
schedule:
interval: daily
time: "04:00"
open-pull-requests-limit: 10
43 changes: 43 additions & 0 deletions .github/workflows/static-analysis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Static analysis

on:
pull_request:
push:

defaults:
run:
shell: bash

jobs:
static_analysis:
name: Static analysis
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
coverage: none
ini-values: "memory_limit=-1"
php-version: "8.0"

- name: Install dependencies
run: |
composer update --prefer-dist --no-progress
- name: Run php-cs-fixer
run: |
php vendor/bin/php-cs-fixer fix --diff --dry-run -v
- name: Run phpstan
run: |
php vendor/bin/phpstan
- name: Run phpmd
run: |
php vendor/bin/phpmd src xml pmd-ruleset.xml
49 changes: 49 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Unit tests

on:
pull_request:
push:

defaults:
run:
shell: bash

jobs:
tests:
name: Unit tests
runs-on: ubuntu-latest

strategy:
matrix:
include:
- php: '7.4'
mode: low-deps
- php: '7.4'
- php: '8.0'
mode: low-deps
- php: '8.0'
fail-fast: false

steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
coverage: none
extensions: "intl, bcmath"
php-version: "${{ matrix.php }}"

- name: Validate composer.json and composer.lock
run: composer validate --strict --no-check-lock

- name: Install dependencies
run: |
composer update --prefer-dist --no-progress $([ '${{ matrix.mode }}' = low-deps ] && echo '--prefer-lowest')
- name: Run phpspec
run: |
php vendor/bin/phpspec run
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/composer.lock
/vendor/
78 changes: 78 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

$finder = PhpCsFixer\Finder::create()
->files()
->name('*.php')
->in(__DIR__ . '/')
->exclude('Resources/config')
->exclude('src/DependencyInjection')
->exclude('test_e2e')
;

$config = new PhpCsFixer\Config();
$config->setRiskyAllowed(true)
->setRules(
[
'@DoctrineAnnotation' => true,
'@Symfony' => true,
'@Symfony:risky' => true,

// @Symfony code styles rules blacklisting:
'method_chaining_indentation' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_trailing_comma_in_list_call' => false,
'php_unit_fqcn_annotation' => false,
'phpdoc_align' => false,
'phpdoc_annotation_without_dot' => false,
'phpdoc_indent' => false,
'phpdoc_inline_tag_normalizer' => false,
'phpdoc_no_access' => false,
'phpdoc_no_alias_tag' => false,
'phpdoc_no_empty_return' => false,
'phpdoc_no_package' => false,
'phpdoc_no_useless_inheritdoc' => false,
'phpdoc_return_self_reference' => false,
'phpdoc_scalar' => false,
'phpdoc_separation' => false,
'phpdoc_single_line_var_spacing' => false,
'phpdoc_summary' => false,
'phpdoc_to_comment' => false,
'phpdoc_trim' => false,
'phpdoc_types' => false,
'phpdoc_var_without_name' => false,
'error_suppression' => false,
'standardize_not_equals' => false,

// @Symfony customised rules
'concat_space' => ['spacing' => 'one'],
'native_function_invocation' => false,
'single_quote' => ['strings_containing_single_quote_chars' => true],
'visibility_required' => ['elements' => ['property', 'method', 'const']],
'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false],

// Additional code style rules whitelisting:
'align_multiline_comment' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],
'combine_consecutive_issets' => true,
'declare_strict_types' => true,
'explicit_indirect_variable' => true,
'explicit_string_variable' => true,
'fully_qualified_strict_types' => true,
'linebreak_after_opening_tag' => true,
'list_syntax' => ['syntax' => 'short'],
'mb_str_functions' => true,
'multiline_comment_opening_closing' => true,
'multiline_whitespace_before_semicolons' => ['strategy' => 'new_line_for_chained_calls'],
'no_alternative_syntax' => true,
'no_superfluous_elseif' => true,
'ordered_imports' => true,
'ordered_interfaces' => true,
]
)
->setRiskyAllowed(true)
->setFinder($finder)
->setUsingCache(false)
;

return $config;
13 changes: 13 additions & 0 deletions CHANGELOG-3.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# CHANGELOG for 3.0.x

This changelog references the relevant changes (bug and security fixes) done in 3.0 version.

### 3.0.0 (2021-03-26)

* This new version is only for PHP >=7.4.
* Bump all dependencies
* Tag @ParamConverter not used anymore.
* Fix #8 problem with `{id/hashid}` in `@Route` and with multiple arguments in
method used for the `Route`.
* Feature #16 Add `LogicException` when hash value could not be decoded.
* No more extra feature for `Hashids`
80 changes: 48 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/e79d4122-c9ad-454f-a1ac-981dd683144f/mini.png)](https://insight.sensiolabs.com/projects/e79d4122-c9ad-454f-a1ac-981dd683144f) [![Scrutinizer](https://scrutinizer-ci.com/g/roukmoute/HashidsBundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/roukmoute/HashidsBundle/)
[![SymfonyInsight](https://insight.symfony.com/projects/be961d5c-da56-44b1-a094-e27066802a2d/mini.svg)](https://insight.symfony.com/projects/be961d5c-da56-44b1-a094-e27066802a2d)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/roukmoute/hashids-bundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/roukmoute/hashids-bundle/?branch=master)
![Packagist Downloads](https://img.shields.io/packagist/dt/roukmoute/hashids-bundle)

# HashidsBundle

Expand All @@ -17,7 +19,7 @@ commands to download the latest stable version of this bundle:
composer req roukmoute/hashids-bundle
```

### Using Symfony 4 Framework
### Using Symfony Framework only

```
composer require roukmoute/hashids-bundle
Expand Down Expand Up @@ -54,37 +56,33 @@ roukmoute_hashids:

# if set to true, it will continue with the next available param converters
passthrough: false
```
## Usage

```php
$hashids = $this->get('hashids');
# if set to true, it tries to convert all arguments passed to the controller
auto_convert: false
```
Next it's the same things of [official documentation](http://hashids.org/php/).

## Other Features

The `Roukmoute\HashidsBundle\Hashids` has extra features:
## Usage
```php
$minHashLength = 42;

// Edit the minimum hash length.
$this->get('hashids')->setMinHashLength($minHashLength)->encode(1, 2, 3);
use Hashids\HashidsInterface;

// Encode with a custom minimum hash length.
$this->get('hashids')->encodeWithCustomHashLength($minHashLength, 1, 2, 3);
public function postShow(HashidsInterface $hashids): Response
{
$hashids->…
}
```

Next it's the same things of [official documentation](https://hashids.org/php/).

## Hashids Converter

Converter Name: `hashids.converter`

The hashids converter attempts to convert `hashid`/`id` attribute set in the route into an integer parameter.
The hashids converter attempts to convert any attribute set in the route into
an integer parameter.

You could use `hashid` or `id` to add :
You could use `hashid` or `id`:

```php
/**
Expand All @@ -106,37 +104,55 @@ public function getAction(int $user)
}
```

For specific case, just add `"hashid" = "{parameter_name}"` in ParamConverter
options:
You could have several hashids in the same URL prefixed with `_hash_`.

```php
/**
* @Route("/users/{slug}")
*
* @ParamConverter("user", class="RoukmouteBundle\Entity\User", options={"hashid" = "user"})
* @Route("/users/{_hash_user}/status/{_hash_status}")
*/
public function getAction(int $user)
public function getAction(int $user, int $status)
{
}
```

You could have several hashids one the same URL:
The keys must be the same as in parameters controller:

```php
/**
* _hash_user _hash_status
* ↕ ↕
* public function getAction(int $user, int $status)
*/
```

You will receive a `LogicException` if a hash could not be decoded correctly.

## Using auto_convert

`auto_convert` tries to convert all arguments in controller.

```yaml
roukmoute_hashids:
auto_convert: true
```
Base on the example above:
```php
/**
* @Route("/users/{user}/status/{status}")
*
* @ParamConverter("user", class="RoukmouteBundle\Entity\User", options={"hashid" = "user"})
* @ParamConverter("status", class="RoukmouteBundle\Entity\Notification", options={"hashid" = "status"})
*/
public function getAction(int $user, int $status)
{
}
```

## Using Passthrough
It will not be possible to get an exception of type `LogicException` from the
bundle if it is activated.

## Using passthrough

`Passthrough` allows to continue with the next available param converters.
`passthrough` allows to continue with the next available param converters.
So if you would like to retrieve an object instead of an integer, just active
passthrough :

Expand All @@ -157,7 +173,7 @@ public function getAction(User $user)
```

As you can see, the passthrough feature allows to use `DoctrineParamConverter`
or any another `ParamConverter` you would have created.
or any another `ParamConverterInterface` you would have created.

## Twig Extension
### Usage
Expand Down
25 changes: 25 additions & 0 deletions Resources/config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Hashids\Hashids;
use Hashids\HashidsInterface;
use Roukmoute\HashidsBundle\ParamConverter\HashidsParamConverter;
use Roukmoute\HashidsBundle\Twig\HashidsExtension;

return static function (ContainerConfigurator $container) {
$container->services()
->set(HashidsInterface::class, Hashids::class)
->args([param('hashids.salt'), param('hashids.min_hash_length'), param('hashids.alphabet')])

->set('hashids.converter', HashidsParamConverter::class)
->args([service(HashidsInterface::class), param('hashids.passthrough'), param('hashids.auto_convert'), param('hashids.alphabet')])
->tag('request.param_converter', ['priority' => 1, 'converter' => 'hashids.converter'])

->set('hashids.twig.extension', HashidsExtension::class)
->args([service(HashidsInterface::class)])
->tag('twig.extension')
;
};
Loading

0 comments on commit 4a3c0d2

Please sign in to comment.