Skip to content

Commit

Permalink
Implements v2.0.0 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
roukmoute committed Mar 27, 2018
1 parent d6232af commit 0b4896a
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 377 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG-2.0.md
@@ -0,0 +1,12 @@
# CHANGELOG for 2.0.x

This changelog references the relevant changes done in 2.0 version.

### 2.0.0(2018-03-26)

* This new version is only for PHP ⩾7.1.
* Update all dependencies (remove all doctrine dependencies)
* DoctrineParamConverter is not used anymore
* This bundle only converts hashid into an id
* Replace `autowire` with `passthrough`.
This new parameter which allows to continue with the next param converters available.
137 changes: 66 additions & 71 deletions README.md
Expand Up @@ -2,55 +2,58 @@

# HashidsBundle

Integrates [hashids/hashids][1] in a Symfony2 project.
Integrates [hashids/hashids](https://github.com/ivanakimov/hashids.php) in a Symfony project.

## Installation
## Installation using composer

Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:
These commands requires you to have Composer installed globally.
Open a command console, enter your project directory and execute the following
commands to download the latest stable version of this bundle:

### Using Symfony Flex

```
composer require roukmoute/hashids-bundle
composer config extra.symfony.allow-contrib true
composer req roukmoute/hashids-bundle
```

This command requires you to have Composer installed globally.
### Using Symfony 4 Framework

## Enable the Bundle
```
composer require roukmoute/hashids-bundle
```

Then, enable the bundle by adding the following line in the ``app/AppKernel.php``
file of your project:
If this has not been done automatically, enable the bundle by adding the
following line in the `config/bundles.php` file of your project:

```php
<?php
// app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// …
new Roukmoute\HashidsBundle\RoukmouteHashidsBundle()
);
// …

return [
…,
Roukmoute\HashidsBundle\RoukmouteHashidsBundle::class => ['all' => true],
];
```

The configuration looks as follows :
## Configuration

The configuration (`config/packages/roukmoute_hashids.yaml`) looks as follows :

```yaml
roukmoute_hashids:

# if set, the hashids will differ from everyone else's
salt: ""
salt: ""

# if set, will generate minimum length for the id
# 0 — meaning hashes will be the shortest possible length
min_hash_length: 0
min_hash_length: 0

# if set, will use only characters of alphabet string
alphabet: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
alphabet: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

# if set to true, guess automatically hashid
autowire: false
# if set to true, it will continue with the next available param converters
passthrough: false
```

## Usage
Expand All @@ -59,7 +62,7 @@ roukmoute_hashids:
$hashids = $this->get('hashids');
```

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

## Other Features

Expand All @@ -75,99 +78,91 @@ $this->get('hashids')->setMinHashLength($minHashLength)->encode(1, 2, 3);
$this->get('hashids')->encodeWithCustomHashLength($minHashLength, 1, 2, 3);
```

Hashids Converter
=================
## Hashids Converter

Converter Name: `hashids.converter`

The hashids converter attempts to convert request hashid attributes to a
id for fetch a Doctrine entity.
The hashids converter attempts to convert `hashid`/`id` attribute set in the route into an integer parameter.

For specify to use hashids converter just add `"id" = "hashid"` in
options.
You could use `hashid` or `id` to add :

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

You could have several hashids one the same URL.
Just finish your word option with "hashid":
or

```php
/**
* @Route("/users/{userHashid}/status/{statusHashid}")
* @ParamConverter("user", class="RoukmouteBundle\Entity\User", options={"id" = "userHashid"})
* @ParamConverter("status", class="RoukmouteBundle\Entity\Notification", options={"id" = "statusHashid"})
* @Route("/users/{id}")
*/
public function getAction(User $user, Status $status)
public function getAction(int $user)
{
}
```

Defining Hashid Automatically (Autowiring)
==========================================

Autowiring allows to guess hashid with minimal configuration.
It automatically resolves the variable in route.
The ParamConverter component will be able to automatically guess
the hashid when configuration has autowire to true.

```
roukmoute_hashids:
autowire: true
```

The autowiring subsystem will detect the hashid.

Base on the example above:
For specific case, just add `"hashid" = "{parameter_name}"` in ParamConverter
options:

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

Now you can do simply that:
You could have several hashids one the same URL:

```php
/**
* @Route("/users/{hashid}")
* @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(User $user)
public function getAction(int $user, int $status)
{
}
```

Or can directly use `id` now !
## Using Passthrough

`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 :

```yaml
roukmoute_hashids:
passthrough: true
```

Base on the example above:

```php
/**
* @Route("/users/{id}")
* @Route("/users/{hashid}")
*/
public function getAction(User $user)
{
}
```

As you can see, the autowiring feature reduces the amount of
configuration required to define a hashid.
As you can see, the passthrough feature allows to use `DoctrineParamConverter`
or any another `ParamConverter` you would have created.

## Twig Extension
### Usage

# Twig Extension
## Usage
```twig
{{ path('users.show', {'hashid': user.id | hashids_encode }) }}
{{ app.request.query.get('hashid') | hashids_decode }}
```

[1]: https://github.com/ivanakimov/hashids.php
[2]: http://hashids.org/php/
3 changes: 1 addition & 2 deletions Resources/config/services.xml
Expand Up @@ -17,8 +17,7 @@
class="Roukmoute\HashidsBundle\ParamConverter\HashidsParamConverter"
>
<argument type="service" id="hashids"/>
<argument type="service" id="doctrine"/>
<argument>%hashids.autowire%</argument>
<argument>%hashids.passthrough%</argument>
<tag name="request.param_converter" priority="1" converter="hashids.converter" />
</service>

Expand Down
77 changes: 77 additions & 0 deletions UPGRADE-2.0.md
@@ -0,0 +1,77 @@
# UPGRADE FROM 1.x to 2.0

### Configuration

* Removed the `autowire` parameter in favor of the `passthrough` parameter.

Before:

```yml
roukmoute_hashids:
autowire: false
```

After:

```yml
roukmoute_hashids:
passthrough: false
```

### HashidsParamConverter

* Replace `id` key option with `hashid`.

Before:

```php
/**
* @Route("/users/{userHashid}/status/{statusHashid}")
* @ParamConverter("user", class="RoukmouteBundle\Entity\User", options={"id" = "userHashid"})
* @ParamConverter("status", class="RoukmouteBundle\Entity\Notification", options={"id" = "statusHashid"})
*/
public function getAction(User $user, Status $status)
{
}
```

After:

```php
/**
* @Route("/users/{userHashid}/status/{statusHashid}")
* @ParamConverter("user", class="RoukmouteBundle\Entity\User", options={"hashid" = "userHashid"})
* @ParamConverter("status", class="RoukmouteBundle\Entity\Notification", options={"hashid" = "statusHashid"})
*/
public function getAction(User $user, Status $status)
{
}
```

* Remove the requirement to have `Hashid` at the end of the value option

Before:

```php
/**
* @Route("/users/{userHashid}/status/{statusHashid}")
* @ParamConverter("user", class="RoukmouteBundle\Entity\User", options={"id" = "userHashid"})
* @ParamConverter("status", class="RoukmouteBundle\Entity\Notification", options={"id" = "statusHashid"})
*/
public function getAction(User $user, Status $status)
{
}
```

After:

```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(User $user, Status $status)
{
}
```
20 changes: 12 additions & 8 deletions composer.json
Expand Up @@ -15,22 +15,26 @@
"Roukmoute\\HashidsBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"spec\\Roukmoute\\HashidsBundle\\": "spec/"
}
},
"require": {
"php": ">=5.6",
"doctrine/doctrine-bundle": "^1.8",
"doctrine/orm": "^2.6",
"hashids/hashids": "^2.0",
"sensio/framework-extra-bundle": "^3.0",
"symfony/http-kernel": "~3.3|~4.0"
"php": ">=7.1",
"hashids/hashids": "^3.0",
"sensio/framework-extra-bundle": "^5.1",
"symfony/http-kernel": "^4.0"
},
"require-dev": {
"bossa/phpspec2-expect": "^3.0",
"phpspec/phpspec": "^4.0",
"phpspec/phpspec": "^4.3",
"roave/security-advisories": "dev-master",
"twig/twig": "^2.4"
},
"extra": {
"branch-alias": {
"dev-master": "1.5-dev"
"dev-master": "2.1-dev"
}
},
"suggest": {
Expand Down

0 comments on commit 0b4896a

Please sign in to comment.