Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Zend Framework integration to Laminas #343

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,54 +337,69 @@ You can then use the `Slugify::slugify()` method in your controllers:
$url = Slugify::slugify("welcome to the homepage");
```

### Zend Framework 2
### Mezzio and laminas-mvc

Slugify can be easely used in Zend Framework 2 applications. Included bridge provides a service and a view helper
Slugify can be easily used in Mezzio or laminas-mvc applications. Included bridge provides a filter and a view helper
already registered for you.

Just enable the module in your configuration like this.

```php
return [
//...

"modules" => [
"Application",
"ZfcBase",
"Cocur\Slugify\Bridge\ZF2", // <- Add this line
'modules' => [
'Application',
'Cocur\Slugify\Bridge\Laminas' // <- Add this line
loco8878 marked this conversation as resolved.
Show resolved Hide resolved
//...
],

//...
]
];
```

It will automatically inject the config-provider or the module in the configuration during the installation process.
After that you can retrieve the `Cocur\Slugify\Slugify` service (or the `slugify` alias) and generate a slug.

```php
/** @var \Zend\ServiceManager\ServiceManager $sm */
$slugify = $sm->get("Cocur\Slugify\Slugify");
$slug = $slugify->slugify("Hällo Wörld");
$anotherSlug = $slugify->slugify("Hällo Wörld", "_");
/** @var \Laminas\ServiceManager\ServiceManager $sm */
$slugify = $sm->get('Cocur\Slugify\Slugify');
loco8878 marked this conversation as resolved.
Show resolved Hide resolved
$slug = $slugify->slugify('Hällo Wörld');
$anotherSlug = $slugify->slugify('Hällo Wörld', '_');
```

It can be used in form filters as follows.

```php
'my_form_input' => [
'filters' => [
[
'name' => SlugifyFilter::class,
'options' => [
'regexp' => Slugify::LOWERCASE_NUMBERS_DASHES,
'strip_tags' => true,
//...
]
],
],
//...
],
//...
```

In your view templates use the `slugify` helper to generate slugs.

```php
<?php echo $this->slugify("Hällo Wörld"); ?>
<?php echo $this->slugify("Hällo Wörld", "_"); ?>
<?php echo $this->slugify('Hällo Wörld') ?>
<?php echo $this->slugify('Hällo Wörld', '_') ?>
```

The service (which is also used in the view helper) can be customized by defining this configuration key.

```php
return [
"cocur_slugify" => [
"reg_exp" => "/([^a-zA-Z0-9]|-)+/",
],
'cocur_slugify' => [
'reg_exp' => '/([^a-zA-Z0-9]|-)+/'
]
];
```


### Nette Framework

Slugify contains a Nette extension that allows you to use it as a service in your Nette application. You only need to
Expand Down
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
"php": "~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0",
"ext-mbstring": "*"
},
"extra": {
"laminas": {
"config-provider": "Cocur\\Slugify\\Bridge\\Laminas\\ConfigProvider",
"module": "Cocur\\Slugify\\Bridge\\Laminas"
}
},
"conflict": {
"symfony/config": "<3.4 || >=4,<4.3",
"symfony/dependency-injection": "<3.4 || >=4,<4.3",
Expand All @@ -42,9 +48,8 @@
"symfony/http-kernel": "^3.4 || ^4.3 || ^5.0 || ^6.0",
"symfony/phpunit-bridge": "^5.4 || ^6.0",
"twig/twig": "^2.12.1 || ~3.0",
"zendframework/zend-modulemanager": "~2.2",
"zendframework/zend-servicemanager": "~2.2",
"zendframework/zend-view": "~2.2"
"laminas/laminas-view": "~2.9",
"laminas/laminas-filter": "^2.31"
},
"autoload": {
"psr-4": {
Expand Down
72 changes: 72 additions & 0 deletions src/Bridge/Laminas/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Cocur\Slugify\Bridge\Laminas;

use Cocur\Slugify\Slugify;
use Laminas\ServiceManager\Factory\InvokableFactory;

class ConfigProvider
{
/**
* Retrieve laminas default configuration.
*
* @return array
*/
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencyConfig(),
'filters' => $this->filterConfig(),
'view_helpers' => $this->getViewHelperConfig(),
];
}

/**
* Retrieve laminas default dependency configuration.
*
* @return array
*/
public function getDependencyConfig(): array
{
return [
'factories' => [
Slugify::class => SlugifyService::class,
],
'aliases' => [
'slugify' => Slugify::class,
]
];
}

loco8878 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Retrieve laminas view helper dependency configuration.
*
* @return array
*/
public function getViewHelperConfig(): array
{
return [
'aliases' => [
'slugify' => SlugifyViewHelper::class
],
'factories' => [
SlugifyViewHelper::class => SlugifyViewHelperFactory::class
]
];
}

/**
* @return array
*/
private function filterConfig(): array
{
return [
'factories' => [
SlugifyFilter::class => InvokableFactory::class,
],
'aliases' => [
'slugify' => SlugifyFilter::class,
],
];
}
}
24 changes: 24 additions & 0 deletions src/Bridge/Laminas/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Cocur\Slugify\Bridge\Laminas;

/**
* Class Module
* @package cocur/slugify
* @subpackage bridge
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class Module
{
public const CONFIG_KEY = 'cocur_slugify';

public function getConfig(): array
{
$provider = new ConfigProvider();
$config = $provider();
$config['service_manager'] = $config['dependencies'];
unset($config['dependencies']);

return $config;
}
}
70 changes: 70 additions & 0 deletions src/Bridge/Laminas/SlugifyFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Cocur\Slugify\Bridge\Laminas;

use Cocur\Slugify\Slugify;
use Laminas\Filter\FilterInterface;

/**
* Class SlugifyFilter
*
* @package cocur/slugify
* @subpackage bridge
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class SlugifyFilter implements FilterInterface
{
/**
* @var array
* @see Slugify::$options
*/
protected array $options = [
'regexp' => Slugify::LOWERCASE_NUMBERS_DASHES,
'separator' => '-',
'lowercase' => true,
'lowercase_after_regexp' => false,
'trim' => true,
'strip_tags' => false
];

/**
* @param array $options
*/
public function __construct(array $options = [])
{
if (!empty($options)) {
$this->setOptions($options);
}
}

/**
* Returns the result of filtering $value
*
* @param mixed $value
*
* @return mixed
*/
public function filter($value)
{
if (!empty($value)) {
$slugify = new Slugify($this->options);
return $slugify->slugify((string) $value);
}

return $value;
}

/**
* @param array $options
*
* @return void
*/
protected function setOptions(array $options)
{
foreach ($options as $key => $option) {
if (array_key_exists($key, $this->options)) {
$this->options[$key] = $option;
}
}
}
}
35 changes: 35 additions & 0 deletions src/Bridge/Laminas/SlugifyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Cocur\Slugify\Bridge\Laminas;

use Cocur\Slugify\Slugify;
use Psr\Container\ContainerInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
* Class SlugifyService
* @package cocur/slugify
* @subpackage bridge
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class SlugifyService
{
/**
* @param ContainerInterface $container
*
* @return Slugify
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container): Slugify
{
$config = $container->get('Config');

$slugifyOptions = $config[Module::CONFIG_KEY]['options'] ?? [];
$provider = $config[Module::CONFIG_KEY]['provider'] ?? null;

return new Slugify($slugifyOptions, $provider);
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Cocur\Slugify\Bridge\ZF2;
namespace Cocur\Slugify\Bridge\Laminas;

use Cocur\Slugify\SlugifyInterface;
use Zend\View\Helper\AbstractHelper;
use Laminas\View\Helper\AbstractHelper;

/**
* Class SlugifyViewHelper
Expand Down Expand Up @@ -34,7 +34,7 @@ public function __construct(SlugifyInterface $slugify)
*
* @return string
*/
public function __invoke(string $string, string $separator = null)
public function __invoke($string, $separator = null)
{
return $this->slugify->slugify($string, $separator);
}
Expand Down
32 changes: 32 additions & 0 deletions src/Bridge/Laminas/SlugifyViewHelperFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Cocur\Slugify\Bridge\Laminas;

use Psr\Container\ContainerInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
* Class SlugifyViewHelperFactory
* @package cocur/slugify
* @subpackage bridge
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class SlugifyViewHelperFactory
{

/**
* @param ContainerInterface $container
*
* @return SlugifyViewHelper
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container): SlugifyViewHelper
{
$slugify = $container->get('Cocur\Slugify\Slugify');

return new SlugifyViewHelper($slugify);
}

}
Loading
Loading