Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcercal committed Oct 1, 2015
2 parents 39c566f + a2475d5 commit 9e689d4
Show file tree
Hide file tree
Showing 11 changed files with 424 additions and 37 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.4
- 5.5
- 5.6

Expand Down
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,71 @@
# SilexManagerProvider

[![Build Status](https://img.shields.io/travis/jpcercal/silex-manager-provider/master.svg?style=flat-square)](http://travis-ci.org/jpcercal/silex-manager-provider)
[![Coverage Status](https://coveralls.io/repos/jpcercal/silex-manager-provider/badge.svg)](https://coveralls.io/r/jpcercal/silex-manager-provider)
[![Latest Stable Version](https://img.shields.io/packagist/v/cekurte/silex-manager-provider.svg?style=flat-square)](https://packagist.org/packages/cekurte/silex-manager-provider)
[![License](https://img.shields.io/packagist/l/cekurte/silex-manager-provider.svg?style=flat-square)](https://packagist.org/packages/cekurte/silex-manager-provider)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/b00b1e95-36e7-4ac7-b576-6617e89e7d9f/mini.png)](https://insight.sensiolabs.com/projects/b00b1e95-36e7-4ac7-b576-6617e89e7d9f)

- A simple silex service provider that adds a Manager to register other Service Providers to increase the power of your application, **contribute with this project**!

## Installation

The package is available on [Packagist](http://packagist.org/packages/cekurte/silex-manager-provider).
The source files is [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) compatible.
Autoloading is [PSR-4](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md) compatible.

```shell
composer require cekurte/silex-manager-provider
```

## Documentation

To use this library you need register the [Cekurte\Silex\Manager\Provider\ManagerServiceProvider](https://github.com/jpcercal/silex-manager-provider/blob/v0.0.2/src/Provider/ManagerServiceProvider.php). See also the library Environment [cekurte/environment](http://packagist.org/packages/cekurte/environment).

```php
<?php

use Cekurte\Environment\Environment;
use Cekurte\Silex\Manager\Provider\ManagerServiceProvider;

// ...
$app['cekurte.manager.providers'] = [
// ...
'Silex\Provider\SessionServiceProvider' => [
'register' => true,
],
'Silex\Provider\SwiftmailerServiceProvider' => [
'register' => true,
'type' => 'array',
'src' => [
'swiftmailer.use_spool' => Environment::get('SWIFTMAILER_USE_SPOOL'),
'swiftmailer.options' => [
'host' => Environment::get('SMTP_HOST'),
'port' => Environment::get('SMTP_PORT'),
'username' => Environment::get('SMTP_USERNAME'),
'password' => Environment::get('SMTP_PASSWORD'),
'encryption' => Environment::get('SMTP_ENCRYPTION'),
'auth_mode' => Environment::get('SMTP_AUTH_MODE'),
],
],
],
// ...
];

$app->register(new ManagerServiceProvider());

// ...
```

If you liked of this library, give me a *star* **=)**.

Contributing
------------

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Make your changes
4. Run the tests, adding new ones for your own code if necessary (`vendor/bin/phpunit`)
5. Commit your changes (`git commit -am 'Added some feature'`)
6. Push to the branch (`git push origin my-new-feature`)
7. Create new Pull Request
1 change: 0 additions & 1 deletion bin/git/hooks/pre-commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
require $autoload;

use League\CLImate\CLImate;
use StaticReview\Issue\Issue;
use StaticReview\Reporter\Reporter;
use StaticReview\Review\Composer\ComposerLintReview;
use StaticReview\Review\Composer\ComposerSecurityReview;
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "MIT",
"minimum-stability": "stable",
"require": {
"php": "^5.6",
"php": "^5.5",
"silex/silex": "^1.3"
},
"require-dev": {
Expand Down Expand Up @@ -34,7 +34,7 @@
},
"scripts": {
"post-autoload-dump": [
"sh vendor/bin/static-review.php hook:install bin/git/hooks/pre-commit.php .git/hooks/pre-commit"
"vendor/bin/static-review.php hook:install --force bin/git/hooks/pre-commit.php .git/hooks/pre-commit"
]
}
}
File renamed without changes.
8 changes: 3 additions & 5 deletions src/Provider/ManagerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,21 @@ class ManagerServiceProvider implements ServiceProviderInterface
*/
public function boot(Application $app)
{
if (isset($app['cors'])) {
$app->after($app["cors"]);
}

}

/**
* {@inheritdoc}
*/
public function register(Application $app)
{
if (!empty($app['cekurte.manager.providers'])) {
if (isset($app['cekurte.manager.providers']) && !empty($app['cekurte.manager.providers'])) {
foreach ($app['cekurte.manager.providers'] as $provider => $config) {
if (isset($config['register']) && $config['register'] === true) {
$loader = new LoaderService($provider);

if (isset($config['type'])) {
$configuration = ConfigFactory::create($config['type']);
$configuration = (new ConfigFactory($config['type']))->process();

if (isset($config['src'])) {
$configuration->setResource($config['src']);
Expand Down
41 changes: 40 additions & 1 deletion src/Service/LoaderService/AbstractConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,52 @@ protected function isType($type)
/**
* {@inheritdoc}
*/
public function setType($type)
public function setType(array $type)
{
if (empty($type)) {
throw new \InvalidArgumentException('The type can not be empty');
}

if (count($type) !== 1) {
throw new \InvalidArgumentException('The type must be one element only');
}

if (!is_string(key($type))) {
throw new \InvalidArgumentException('The type key must be a string');
}

if (!is_string(current($type))) {
throw new \InvalidArgumentException('The type value must be a string');
}

$allowedConfigTypes = $this->getAllowedConfigTypes();

if (!array_key_exists(key($type), $allowedConfigTypes)) {
throw new \InvalidArgumentException(sprintf(
'The type "%s" is invalid, use one of this types %s.',
key($type),
implode(', ', array_keys($allowedConfigTypes))
));
}

$this->type = $type;

return $this;
}

public function setTypeAsString($type)
{
$allowedConfigTypes = $this->getAllowedConfigTypes();

foreach ($allowedConfigTypes as $key => $value) {
if (strtolower($type) === strtolower($key)) {
return $this->setType([$key => $value]);
}
}

throw new \InvalidArgumentException(sprintf('The type with key "%s" was not found', $type));
}

/**
* {@inheritdoc}
*/
Expand Down
39 changes: 14 additions & 25 deletions src/Service/LoaderService/ConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,35 @@

namespace Cekurte\Silex\Manager\Service\LoaderService;

use Cekurte\Silex\Manager\Service\LoaderService\AbstractConfig;
use Cekurte\Silex\Manager\Service\LoaderService\ConfigInterface;

class ConfigFactory
class ConfigFactory extends AbstractConfig
{
use ConfigTrait;

/**
* Disable the constructor
* @var ConfigInterface
*/
private function __construct()
{

}
private $config;

/**
* Create a instance of Config using the Static Factory Pattern
*
* @param string $type
*
* @return ConfigInterface
*
* @throws \InvalidArgumentException
*/
public static function create($type)
public function __construct($type)
{
$allowedConfigTypes = self::getAllowedConfigTypes();
$this->setTypeAsString($type);
}

if (!array_key_exists($type, $allowedConfigTypes)) {
throw new \InvalidArgumentException(sprintf(
'The type "%s" is invalid, use one of this types %s.',
$type,
implode(', ', array_keys($allowedConfigTypes))
));
}
/**
* {@inheritdoc}
*/
public function process()
{
$class = __NAMESPACE__ . '\\' . current($this->getType());

foreach ($allowedConfigTypes as $configType => $class) {
if ($type === $configType) {
$class = __NAMESPACE__ . '\\' . $class;
return new $class();
}
}
return new $class();
}
}
4 changes: 2 additions & 2 deletions src/Service/LoaderService/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ interface ConfigInterface
/**
* Set the type of configuration
*
* @param string $type
* @param array $type
*
* @return ConfigInterface
*/
public function setType($type);
public function setType(array $type);

/**
* Set the resource of configuration
Expand Down
126 changes: 126 additions & 0 deletions test/Provider/ManagerServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Cekurte\Silex\Translation\Test\Provider;

use Cekurte\Silex\Manager\Provider\ManagerServiceProvider;
use Cekurte\Tdd\ReflectionTestCase;
use Silex\Application;

class ManagerServiceProviderTest extends ReflectionTestCase
{
public function testImplementsServiceProviderInterface()
{
$reflection = new \ReflectionClass(
'\\Cekurte\\Silex\\Manager\\Provider\\ManagerServiceProvider'
);

$this->assertTrue($reflection->implementsInterface(
'\\Silex\\ServiceProviderInterface'
));
}

public function testRegisterNotIssetServiceProviders()
{
$app = new Application();

$app->register(new ManagerServiceProvider());
}

public function testRegisterEmptyServiceProviders()
{
$app = new Application();

$app['cekurte.manager.providers'] = [];

$app->register(new ManagerServiceProvider());
}

public function testRegisterServiceProvidersWithRegisterNotIsset()
{
$app = new Application();

$app['cekurte.manager.providers'] = [
'Silex\Provider\HttpFragmentServiceProvider'
];

$app->register(new ManagerServiceProvider());

$app->boot();

$this->assertFalse(isset($app['fragment.path']));
}

public function testRegisterServiceProvidersWithRegisterWithInvalidValue()
{
$app = new Application();

$app['cekurte.manager.providers'] = [
'Silex\Provider\HttpFragmentServiceProvider' => [
'register' => 'invalid-value'
],
];

$app->register(new ManagerServiceProvider());

$app->boot();

$this->assertFalse(isset($app['fragment.path']));
}

public function testRegisterServiceProvidersWithRegisterWithValidValue()
{
$app = new Application();

$app['cekurte.manager.providers'] = [
'Silex\Provider\HttpFragmentServiceProvider' => [
'register' => true
],
];

$app->register(new ManagerServiceProvider());

$app->boot();

$this->assertTrue(isset($app['fragment.path']));
}

public function testRegisterServiceProvidersWithRegisterWithValidValueAndParameters()
{
$app = new Application();

$app['cekurte.manager.providers'] = [
'Silex\Provider\HttpFragmentServiceProvider' => [
'register' => true,
'type' => 'array',
'src' => [
'fragment.path' => 'fake'
]
],
];

$app->register(new ManagerServiceProvider());

$app->boot();

$this->assertEquals('fake', $app['fragment.path']);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testRegisterServiceProvidersWithRegisterWithValidValueAndParametersWithTypeError()
{
$app = new Application();

$app['cekurte.manager.providers'] = [
'Silex\Provider\HttpFragmentServiceProvider' => [
'register' => true,
'type' => 'invalid-value',
],
];

$app->register(new ManagerServiceProvider());

$app->boot();
}
}

0 comments on commit 9e689d4

Please sign in to comment.