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 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,54 +337,54 @@ You can then use the `Slugify::slugify()` method in your controllers:
$url = Slugify::slugify("welcome to the homepage");
```

### Zend Framework 2
### Laminas

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 Laminas applications. Included bridge provides a service and a view helper
already registered for you.

Just enable the module in your configuration like this.

```php
return [
return array(
//...

"modules" => [
"Application",
"ZfcBase",
"Cocur\Slugify\Bridge\ZF2", // <- Add this line
'modules' => array(
'Application',
'Cocur\Slugify\Bridge\Laminas' // <- Add this line
//...
],
)

//...
];
);
```

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');
$slug = $slugify->slugify('Hällo Wörld');
$anotherSlug = $slugify->slugify('Hällo Wörld', '_');
```

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]|-)+/",
],
];
return array(
'cocur_slugify' => array(
'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-inputfilter": "~2.24"
},
"autoload": {
"psr-4": {
Expand Down
55 changes: 55 additions & 0 deletions src/Bridge/Laminas/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Cocur\Slugify\Bridge\Laminas;

use Cocur\Slugify\Slugify;

class ConfigProvider
{
/**
* Retrieve laminas default configuration.
*
* @return array
*/
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencyConfig(),
'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,
]
];
}

/**
* Retrieve laminas view helper dependency configuration.
*
* @return array
*/
public function getViewHelperConfig(): array
{
return [
'aliases' => [
'slugify' => SlugifyViewHelper::class
],
'factories' => [
SlugifyViewHelper::class => SlugifyViewHelperFactory::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;
}
}
56 changes: 56 additions & 0 deletions src/Bridge/Laminas/SlugifyFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Cocur\Slugify\Bridge\Laminas;

use Cocur\Slugify\Slugify;
use Laminas\Filter\AbstractFilter;

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

/**
* @param array|null $options
*/
public function __construct(?array $options = null)
{
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;
}
}
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);
}

}
50 changes: 0 additions & 50 deletions src/Bridge/ZF2/Module.php

This file was deleted.

Loading
Loading