Skip to content

Commit

Permalink
Merge branch 'release/1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
adrenth committed Apr 20, 2020
2 parents e563051 + 35f6731 commit 353ae72
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 110 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,4 @@ WRITTEN OFFER
The source code for any program binaries or compressed scripts that are
included with this plugin can be freely obtained at the following URL:

https://github.com/vdlp/oc-redirect-plugin
https://github.com/vdlp/oc-hashids-plugin
16 changes: 3 additions & 13 deletions Plugin.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
<?php

/** @noinspection PhpMissingParentCallCommonInspection */

declare(strict_types=1);

namespace Vdlp\Hashids;

use System\Classes\PluginBase;
use Vdlp\Hashids\ServiceProviders\HashidsServiceProvider;

/**
* Class Plugin
*
* @package Vdlp\Redirect
*/
class Plugin extends PluginBase
{
/**
* {@inheritdoc}
*/
public function pluginDetails(): array
{
return [
Expand All @@ -28,11 +21,8 @@ public function pluginDetails(): array
];
}

/**
* {@inheritdoc}
*/
public function register(): void
{
$this->app->register(HashidsServiceProvider::class);
$this->app->register(ServiceProvider::class);
}
}
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

Hashids is a small open-source library that generates short, unique, non-sequential ids from numbers.

It converts numbers like 347 into strings like yr8, or array of numbers like [27, 986] into 3kTMd.
It converts numbers like 347 into strings like "yr8", or array of numbers like [27, 986] into "3kTMd".

You can also decode those ids back. This is useful in bundling several parameters into one or simply using them as short UIDs.

## Requirements

* PHP 7.1 or higher
* October CMS build 420 or higher
* Preferably one of the latest October CMS build

## Installation

*CLI:*

`php artisan plugin:install Vdlp.Hashids`
```
php artisan plugin:install Vdlp.Hashids
```

*October CMS:*

Expand All @@ -25,7 +27,9 @@ Go to Settings > Updates & Plugins > Install plugins and search for 'Hashids'.

To configure this plugin execute the following command:

`php artisan vendor:publish --provider="Vdlp\Hashids\ServiceProviders\HashidsServiceProvider" --tag="config"`
```
php artisan vendor:publish --provider="Vdlp\Hashids\ServiceProviders\HashidsServiceProvider" --tag="config"
```

This will create a `config/hashids.php` file in your app where you can modify the configuration.

Expand All @@ -34,17 +38,17 @@ This will create a `config/hashids.php` file in your app where you can modify th
Here you can see an example of how to use this plugin. Out of the box, the default configuration used is `main`.

```
// You can use this class also with Dependency Injection
// You can use this class with Dependency Injection
use Vdlp\Hashids\Classes\HashidsManager;
/** @var HashidsManager $hashids */
$hashidsManager = resolve(HashidsManager::class);
// Encodes the integer 1 to an hashid using the default configuration
// Encodes the integer 1 to a hashid using the default configuration
$hashidsManager->encode(1);
$hashidsManager->instance()->encode(1);
// Encodes the integer 1 to an hashid using a different configuration
// Encodes the integer 1 to a hashid using a different configuration
$hashidsManager->instance('different-configuration')->encode(1);
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,38 @@

declare(strict_types=1);

namespace Vdlp\Hashids\ServiceProviders;
namespace Vdlp\Hashids;

use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Container\Container;
use October\Rain\Support\ServiceProvider;
use October\Rain\Support\ServiceProvider as BaseServiceProvider;
use Vdlp\Hashids\Classes\HashidsFactory;
use Vdlp\Hashids\Classes\HashidsManager;

/**
* Class HashidsServiceProvider
*
* @package Vdlp\Hashids\ServiceProviders
*/
class HashidsServiceProvider extends ServiceProvider
class ServiceProvider extends BaseServiceProvider
{
/**
* @return void
*/
public function boot(): void
{
$this->publishes([
__DIR__ . '/../config.php' => config_path('hashids.php'),
__DIR__ . '/config.php' => config_path('hashids.php'),
], 'config');

$this->mergeConfigFrom(__DIR__ . '/../config.php', 'hashids');
$this->mergeConfigFrom(__DIR__ . '/config.php', 'hashids');
}

/**
* @return void
*/
public function register(): void
{
$this->app->singleton(HashidsFactory::class, function (): HashidsFactory {
$this->app->singleton(HashidsFactory::class, static function (): HashidsFactory {
return new HashidsFactory();
});

$this->app->singleton(HashidsManager::class, function (Container $container): HashidsManager {
$this->app->singleton(HashidsManager::class, static function (Container $container): HashidsManager {
return new HashidsManager(
$container->make(Repository::class),
$container->make(HashidsFactory::class)
Expand Down
27 changes: 0 additions & 27 deletions classes/Hashids.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,20 @@
use Hashids\Hashids as HashidsHelper;
use Hashids\HashidsInterface;

/**
* Class Hashids
*
* @package Vdlp\Hashids\Classes
*/
class Hashids implements HashidsInterface
{
/**
* @var HashidsHelper
*/
private $hashids;

/**
* @param HashidsHelper $hashids
*/
public function __construct(HashidsHelper $hashids)
{
$this->hashids = $hashids;
}

/**
* @param array $numbers
* @return string
*/
public function encode(...$numbers): string
{
return $this->hashids->encode($numbers);
}

/**
* @param string $hash
* @return int
*/
public function decode($hash): int
{
$result = $this->hashids->decode($hash);
Expand All @@ -50,19 +31,11 @@ public function decode($hash): int
return 0;
}

/**
* @param string $str
* @return string
*/
public function encodeHex($str): string
{
return $this->hashids->encodeHex($str);
}

/**
* @param string $hash
* @return string
*/
public function decodeHex($hash): string
{
return $this->hashids->decodeHex($hash);
Expand Down
13 changes: 0 additions & 13 deletions classes/HashidsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,16 @@
use Hashids\Hashids as HashidsBase;
use Hashids\HashidsException;

/**
* Class HashidsFactory
*
* @package Vdlp\Hashids\Classes
*/
class HashidsFactory
{
/**
* @param array $config
* @return Hashids
* @throws HashidsException
*/
public function make(array $config): Hashids
{
return $this->getInstance($this->getConfig($config));
}

/**
* @param array $config
* @return array
*/
protected function getConfig(array $config): array
{
return [
Expand All @@ -38,8 +27,6 @@ protected function getConfig(array $config): array
}

/**
* @param array $config
* @return Hashids
* @throws HashidsException
*/
protected function getInstance(array $config): Hashids
Expand Down
37 changes: 0 additions & 37 deletions classes/HashidsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,26 @@
use InvalidArgumentException;

/**
* Class HashidsManager
*
* @package Vdlp\Hashids\Classes
*
* @mixin Hashids
*/
class HashidsManager
{
/**
* @var Repository
*/
protected $config;

/**
* @var HashidsFactory
*/
protected $factory;

/**
* @var Hashids[]
*/
protected $instances = [];

/**
* @param Repository $config
* @param HashidsFactory $factory
*/
public function __construct(Repository $config, HashidsFactory $factory)
{
$this->config = $config;
$this->factory = $factory;
}

/**
* @param string|null $name
* @return Hashids
* @throws InvalidArgumentException
*/
public function instance(string $name = null): Hashids
Expand All @@ -59,8 +43,6 @@ public function instance(string $name = null): Hashids
}

/**
* @param string|null $name
* @return Hashids
* @throws InvalidArgumentException
*/
public function reloadInstance(string $name = null): Hashids
Expand All @@ -72,36 +54,23 @@ public function reloadInstance(string $name = null): Hashids
return $this->instance($name);
}

/**
* @param string|null $name
* @return void
*/
public function removeInstance(string $name = null): void
{
$name = $name ?: $this->getDefaultInstance();
unset($this->instances[$name]);
}

/**
* @return string
*/
public function getDefaultInstance(): string
{
return $this->config->get('hashids.default');
}

/**
* @param string $name
* @return void
*/
public function setDefaultInstance(string $name): void
{
$this->config->set('hashids.default', $name);
}

/**
* @param string|null $name
* @return array
* @throws InvalidArgumentException
*/
public function getInstanceConfig(string $name = null): array
Expand All @@ -120,8 +89,6 @@ public function getInstanceConfig(string $name = null): array
}

/**
* @param string $method
* @param array $parameters
* @return mixed
* @throws InvalidArgumentException
*/
Expand All @@ -131,8 +98,6 @@ public function __call(string $method, array $parameters)
}

/**
* @param string $name
* @return Hashids
* @throws InvalidArgumentException
*/
protected function makeInstance(string $name): Hashids
Expand All @@ -142,8 +107,6 @@ protected function makeInstance(string $name): Hashids
}

/**
* @param array $config
* @return Hashids
* @throws HashidsException
*/
protected function createInstance(array $config): Hashids
Expand Down
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@
"require": {
"php": ">=7.1",
"hashids/hashids": "^3.0"
},
"archive": {
"exclude": [
".gitignore",
".idea/"
]
}
}
3 changes: 3 additions & 0 deletions updates/version.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
1.0.0: First version of Vdlp.Hashids
1.0.1: Add PHP >= 7.1 as dependency
1.1.0:
- Code optimizations
- Moved service provider

0 comments on commit 353ae72

Please sign in to comment.