Skip to content

Commit

Permalink
symfony 5 compatibility (#3)
Browse files Browse the repository at this point in the history
* symfony 5 compatibility
  • Loading branch information
IndraGunawan authored Mar 7, 2020
1 parent 5fc5015 commit cffd28a
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitignore export-ignore
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ composer.lock
composer.phar
.php_cs.cache
/build
.phpunit.result.cache
/var
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
language: php

sudo: false

cache:
directories:
- $HOME/.composer/cache

matrix:
jobs:
include:
- php: '7.0'
- php: '7.1'
- php: '7.2'
- php: '7.2'
env: deps=low
- php: '7.3'
- php: '7.4'

before_install:
- composer self-update
Expand Down
6 changes: 2 additions & 4 deletions AbstractFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ abstract class AbstractFacade

/**
* Facade service container.
*
* @param ContainerInterface $container
*/
public static function setFacadeContainer(ContainerInterface $container)
{
Expand All @@ -41,15 +39,15 @@ abstract protected static function getFacadeAccessor();
* Handle dynamic calls to the service.
*
* @param string $method
* @param array $arguments
* @param array $arguments
*
* @return mixed
*
* @throws \RuntimeException
*/
public static function __callStatic($method, $arguments)
{
$class = get_called_class();
$class = \get_called_class();

if (!static::$container->has($class)) {
throw new \RuntimeException(sprintf('"%s" facade has not been register.', $class));
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

v0.2.0
------

* Symfony 5 compatibility

v0.1.0
------

Expand Down
6 changes: 3 additions & 3 deletions DependencyInjection/Compiler/AddFacadePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public function process(ContainerBuilder $container)
$r->setAccessible(true);
$ref = $r->invoke(null);

if (!is_string($ref)) {
throw new InvalidArgumentException(sprintf('Facade accessor must be string, "%s" given.', is_object($ref) ? get_class($ref) : gettype($ref)));
if (!\is_string($ref)) {
throw new InvalidArgumentException(sprintf('Facade accessor must be string, "%s" given.', \is_object($ref) ? \get_class($ref) : \gettype($ref)));
}

$ref = is_string($ref) && 0 === strpos($ref, '@') ? substr($ref, 1) : $ref;
$ref = \is_string($ref) && 0 === strpos($ref, '@') ? substr($ref, 1) : $ref;
$facades[$id] = new Reference($ref);
}

Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
[![Source](https://img.shields.io/badge/source-IndraGunawan%2Ffacade--bundle-blue.svg)](https://github.com/IndraGunawan/facade-bundle)
[![Packagist](https://img.shields.io/badge/packagist-indragunawan%2Ffacade--bundle-blue.svg)](https://packagist.org/packages/indragunawan/facade-bundle)


Support Facades for Symfony service.

thanks to:

- [Service Locator](https://symfony.com/blog/new-in-symfony-3-3-service-locators) - for making all the referenced facade service lazy-loaded.
- [Service Autoconfiguration](https://symfony.com/blog/new-in-symfony-3-3-service-autoconfiguration) - for making all classes that extend `Indragunawan\FacadeBundle\AbstractFacade` class automatically tagged as facade.

## Documentation

* [Installation](#installation)
* [Creating Facade](#creating-facade)
- [Installation](#installation)
- [Creating Facade](#creating-facade)

### Installation

Expand All @@ -32,8 +32,8 @@ If you install without using Symfony Flex, first add the bundle by using compose

### Creating Facade


To create a facade create a class that extends base `Indragunawan\FacadeBundle\AbstractFacade` class and implement the `getFacadeAccessor` method that returns the `service id`, support **private** and **public** service.

```php
<?php

Expand All @@ -52,7 +52,6 @@ class Foo extends AbstractFacade

Now the facade now ready. Simply import the facade namespace. When you call any static method on the `Foo` facade, then it will resolve the service that you define in `getFacadeAccessor` method and call the requested method from the service.

License
-------
## License

This bundle is under the MIT license. See the complete [license](LICENSE)
10 changes: 5 additions & 5 deletions Tests/FacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ public function testRegisteredFacade()
$container
->expects($this->exactly(2))
->method('has')
->will($this->returnValueMap([
->willReturnMap([
[Foo::class, true],
]))
])
;

$container
->expects($this->exactly(2))
->method('get')
->will($this->returnValueMap([
->willReturnMap([
[Foo::class, new FooService()],
]))
])
;

AbstractFacade::setFacadeContainer($container);
Expand All @@ -57,7 +57,7 @@ public function testNotRegisteredFacade()
$container
->expects($this->once())
->method('has')
->will($this->returnValueMap([]))
->willReturnMap([])
;

AbstractFacade::setFacadeContainer($container);
Expand Down
30 changes: 30 additions & 0 deletions Tests/Fixtures/Kernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the FacadeBundle
*
* (c) Indra Gunawan <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Indragunawan\FacadeBundle\Tests\Fixtures;

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;

final class Kernel extends BaseKernel
{
public function registerBundles()
{
return [
new \Indragunawan\FacadeBundle\IndragunawanFacadeBundle(),
];
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
return null;
}
}
27 changes: 3 additions & 24 deletions Tests/IndragunawanFacadeBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,19 @@

namespace Indragunawan\FacadeBundle\Tests;

use Indragunawan\FacadeBundle\IndragunawanFacadeBundle;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\HttpKernel\Kernel;

/**
* @author Indra Gunawan <[email protected]>
*/
class IndragunawanFacadeBundleTest extends TestCase
class IndragunawanFacadeBundleTest extends KernelTestCase
{
public function testBundle()
{
$kernel = $this->getKernel();
$kernel->boot();
$kernel = $this->bootKernel(['environment' => 'test', 'debug' => true]);

$this->assertTrue($kernel->getContainer()->has('indragunawan.facade.container'));
$this->assertInstanceOf(ServiceLocator::class, $kernel->getContainer()->get('indragunawan.facade.container'));
}

protected function getKernel()
{
$kernel = $this
->getMockBuilder(Kernel::class)
->setMethods(['registerBundles'])
->setConstructorArgs(['test', false])
->getMockForAbstractClass()
;
$kernel->method('registerBundles')
->will($this->returnValue([new IndragunawanFacadeBundle()]))
;
$p = new \ReflectionProperty($kernel, 'rootDir');
$p->setAccessible(true);
$p->setValue($kernel, sys_get_temp_dir());

return $kernel;
}
}
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "indragunawan/facade-bundle",
"description": "Support Facades for Symfony service.",
"description": "Support Facades for Symfony services.",
"type": "symfony-bundle",
"license": "MIT",
"authors": [
Expand All @@ -19,12 +19,13 @@
]
},
"require": {
"php": "^7.0",
"php": "^7.1.3",
"psr/container": "^1.0",
"symfony/framework-bundle": "^3.3 || ^4.0"
"symfony/dependency-injection": "^3.4 || ^4.0 || ^5.0"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
"phpunit/phpunit": "^7.5.2 || ^8.0",
"symfony/framework-bundle": "^3.4 || ^4.0 || ^5.0"
},
"config": {
"preferred-install": {
Expand Down
5 changes: 5 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
bootstrap="./vendor/autoload.php"
colors="true">

<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_CLASS" value="\Indragunawan\FacadeBundle\Tests\Fixtures\Kernel" />
</php>

<testsuites>
<testsuite name="FacadeBundle test suite">
<directory suffix="Test.php">./Tests</directory>
Expand Down

0 comments on commit cffd28a

Please sign in to comment.