Skip to content

Commit

Permalink
No more singleton...
Browse files Browse the repository at this point in the history
  • Loading branch information
MarceauKa committed Aug 19, 2016
1 parent 0b85afb commit e5f959e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 58 deletions.
4 changes: 2 additions & 2 deletions README.en.md
Expand Up @@ -31,10 +31,10 @@ RewriteRule ^ index.php [L]

### Instanciation

Router is singleton based and can't directly constructed. Get the instance like that:
Router as no params.

```php
$router = Akibatech\Router::getInstance();
$router = new Router;
```

### Adding routes
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -31,10 +31,10 @@ RewriteRule ^ index.php [L]

### Instanciation

Router fonctionne autour d'un singleton et ne se construit pas directement. Appelez le ainsi :
Router ne prend aucun paramètre, instanciez le de cette façon:

```php
$router = Akibatech\Router::getInstance();
$router = new Router;
```

### Ajouter des routes
Expand Down
56 changes: 5 additions & 51 deletions src/Router.php
Expand Up @@ -9,11 +9,6 @@
*/
class Router
{
/**
* @var self
*/
protected static $router;

/**
* @var array
*/
Expand Down Expand Up @@ -89,51 +84,12 @@ class Router
*
* @param void
*/
protected function __construct()
{
self::$router = $this;
}

//-------------------------------------------------------------------------

/**
* Returns the Router instance (singleton).
*
* @param void
* @return Router
*/
public static function getInstance()
{
if (is_null(self::$router) === true)
{
self::$router = new self;
}

return self::$router;
}

//-------------------------------------------------------------------------

/**
* Renew the Router instance.
*
* @param void
* @return Router
*/
public static function renewInstance()
{
if (is_null(self::$router) === false)
{
self::$router = new self;
}

return self::getInstance();
}
public function __construct() { }

//-------------------------------------------------------------------------

/**
* Create instance from compiled.
* Create instance from a serialized instance.
*
* @param string $compiled
* @return Router
Expand All @@ -148,20 +104,18 @@ public static function fromCompiled($compiled)
throw new \RuntimeException("Given compiled data is invalid.");
}

self::$router = $router;

return self::getInstance();
return $router;
}

//-------------------------------------------------------------------------

/**
* Get serialized current instance.
* Get the serialized current instance.
*
* @param void
* @return string
*/
public function getSerialized()
public function getCompiled()
{
return serialize($this);
}
Expand Down
5 changes: 2 additions & 3 deletions tests/RouterTest.php
Expand Up @@ -28,7 +28,7 @@ class RouterTest extends TestCase
*/
protected function setUp()
{
$this->router = Router::renewInstance();
$this->router = new Router();
}

//-------------------------------------------------------------------------
Expand All @@ -42,7 +42,6 @@ public function testDefaultInstance()
{
$this->assertInstanceOf(Router::class, $this->router);
$this->assertEmpty($this->router->routes());
$this->assertEquals($this->router, Router::getInstance());
}

//-------------------------------------------------------------------------
Expand Down Expand Up @@ -189,7 +188,7 @@ public function testNamedRouteOnDuplicateException()
public function testSerializedRoutes()
{
$this->router->get('hello', 'Action@Method', 'name');
$compiled = $this->router->getSerialized();
$compiled = $this->router->getCompiled();

$router = Router::fromCompiled($compiled);

Expand Down

0 comments on commit e5f959e

Please sign in to comment.