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 9 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
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
"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-modulemanager": "~2.8",
"laminas/laminas-servicemanager": "~3.3",
"laminas/laminas-view": "~2.9"
},
"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;
}
}
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 Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;

/**
* Class SlugifyService
* @package cocur/slugify
* @subpackage bridge
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class SlugifyService implements FactoryInterface
loco8878 marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* @param ContainerInterface $container
* @param $requestedName
* @param array|null $options
* @return Slugify
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): Slugify
loco8878 marked this conversation as resolved.
Show resolved Hide resolved
{
$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 Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;

/**
* Class SlugifyViewHelperFactory
* @package cocur/slugify
* @subpackage bridge
* @license http://www.opensource.org/licenses/MIT The MIT License
*/
class SlugifyViewHelperFactory implements FactoryInterface
loco8878 marked this conversation as resolved.
Show resolved Hide resolved
{

/**
* @param ContainerInterface $container
* @param $requestedName
* @param array|null $options
* @return SlugifyViewHelper
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): SlugifyViewHelper
loco8878 marked this conversation as resolved.
Show resolved Hide resolved
{
$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.

30 changes: 0 additions & 30 deletions src/Bridge/ZF2/SlugifyService.php

This file was deleted.

28 changes: 0 additions & 28 deletions src/Bridge/ZF2/SlugifyViewHelperFactory.php

This file was deleted.

Loading
Loading