Skip to content

Commit

Permalink
Simplified frontend controller factory
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Jan 7, 2019
1 parent bc3c0c8 commit 8086a84
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 250 deletions.
56 changes: 9 additions & 47 deletions controller/frontend/src/Controller/Frontend.php
Expand Up @@ -18,7 +18,7 @@
class Frontend
{
static private $cache = true;
static private $controllers = [];
static private $objects = [];


/**
Expand All @@ -29,35 +29,8 @@ class Frontend
*/
static public function cache( $value )
{
$old = self::$cache;
self::$cache = (boolean) $value;

return $old;
}


/**
* Removes all controller objects from the cache
*
* If neither a context ID nor a path is given, the complete cache will be pruned.
*
* @param integer $id Context ID the objects have been created with (string of \Aimeos\MShop\Context\Item\Iface)
* @param string $path Path describing the controller to clear, e.g. "basket"
*/
static public function clear( $id = null, $path = null )
{
if( $id !== null )
{
if( $path !== null ) {
self::$controllers[$id][$path] = null;
} else {
self::$controllers[$id] = [];
}

return;
}

self::$controllers = [];
self::$objects = [];
}


Expand All @@ -82,36 +55,25 @@ static public function create( \Aimeos\MShop\Context\Item\Iface $context, $path
throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Controller path is empty' ) );
}

$id = (string) $context;

if( self::$cache === false || !isset( self::$controllers[$id][$path] ) )
if( self::$cache === false || !isset( self::$objects[$path] ) )
{
$parts = explode( '/', $path );

foreach( $parts as $key => $part )
{
if( ctype_alnum( $part ) === false ) {
throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Invalid characters in controller name "%1$s" in "%2$s"', $part, $path ) );
}

$parts[$key] = ucwords( $part );
if( ctype_alnum( $path ) === false ) {
throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Invalid characters in controller name "%1$s"', $path ) );
}

$factory = '\\Aimeos\\Controller\\Frontend\\' . join( '\\', $parts ) . '\\Factory';
$factory = '\\Aimeos\\Controller\\Frontend\\' . ucfirst( $path ) . '\\Factory';

if( class_exists( $factory ) === false ) {
throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Class "%1$s" not available', $factory ) );
}

$manager = call_user_func_array( array( $factory, 'create' ), array( $context ) );

if( $manager === false ) {
if( ( $controller = call_user_func_array( [$factory, 'create'], [$context] ) ) === false ) {
throw new \Aimeos\Controller\Frontend\Exception( sprintf( 'Invalid factory "%1$s"', $factory ) );
}

self::$controllers[$id][$path] = $manager;
self::$objects[$path] = $controller;
}

return clone self::$controllers[$id][$path];
return clone self::$objects[$path];
}
}
45 changes: 0 additions & 45 deletions controller/frontend/src/Controller/Frontend/Factory.php

This file was deleted.

Expand Up @@ -76,7 +76,7 @@ public static function create( \Aimeos\MShop\Context\Item\Iface $context, $name
$iface = '\\Aimeos\\Controller\\Frontend\\Product\\Iface';
$classname = '\\Aimeos\\Controller\\Frontend\\Product\\' . $name;

$manager = self::createController( $context, $classname, $iface );
$controller = self::createController( $context, $classname, $iface );

/** controller/frontend/product/decorators/excludes
* Excludes decorators added by the "common" option from the product frontend controllers
Expand Down Expand Up @@ -152,6 +152,6 @@ public static function create( \Aimeos\MShop\Context\Item\Iface $context, $name
* @see controller/frontend/product/decorators/excludes
* @see controller/frontend/product/decorators/global
*/
return self::addControllerDecorators( $context, $manager, 'product' );
return self::addControllerDecorators( $context, $controller, 'product' );
}
}
105 changes: 0 additions & 105 deletions controller/frontend/tests/Controller/Frontend/FactoryTest.php

This file was deleted.

52 changes: 3 additions & 49 deletions controller/frontend/tests/Controller/FrontendTest.php
Expand Up @@ -46,61 +46,15 @@ public function testCreateSubControllerNotExisting()
}


public function testClear()
public function testCache()
{
$cache = \Aimeos\Controller\Frontend::cache( true );

$context = \TestHelperFrontend::getContext();
\Aimeos\Controller\Frontend::cache( true );

$controller1 = \Aimeos\Controller\Frontend::create( $context, 'basket' );
\Aimeos\Controller\Frontend::clear();
$controller2 = \Aimeos\Controller\Frontend::create( $context, 'basket' );

\Aimeos\Controller\Frontend::cache( $cache );

\Aimeos\Controller\Frontend::cache( false );
$this->assertNotSame( $controller1, $controller2 );
}


public function testClearSite()
{
$cache = \Aimeos\Controller\Frontend::cache( true );

$context = \TestHelperFrontend::getContext();

$basket1 = \Aimeos\Controller\Frontend::create( $context, 'basket' );
$catalog1 = \Aimeos\Controller\Frontend::create( $context, 'catalog' );

\Aimeos\Controller\Frontend::clear( (string) $context );

$basket2 = \Aimeos\Controller\Frontend::create( $context, 'basket' );
$catalog2 = \Aimeos\Controller\Frontend::create( $context, 'catalog' );

\Aimeos\Controller\Frontend::cache( $cache );

$this->assertNotSame( $basket1, $basket2 );
$this->assertNotSame( $catalog1, $catalog2 );
}


public function testClearSpecific()
{
$cache = \Aimeos\Controller\Frontend::cache( true );

$context = \TestHelperFrontend::getContext();

$basket1 = \Aimeos\Controller\Frontend::create( $context, 'basket' );
$catalog1 = \Aimeos\Controller\Frontend::create( $context, 'catalog' );

\Aimeos\Controller\Frontend::clear( (string) $context, 'basket' );

$basket2 = \Aimeos\Controller\Frontend::create( $context, 'basket' );
$catalog2 = \Aimeos\Controller\Frontend::create( $context, 'catalog' );

\Aimeos\Controller\Frontend::cache( $cache );

$this->assertNotSame( $basket1, $basket2 );
$this->assertSame( $catalog1, $catalog2 );
}

}
2 changes: 0 additions & 2 deletions controller/frontend/tests/TestHelperFrontend.php
Expand Up @@ -15,8 +15,6 @@ class TestHelperFrontend
public static function bootstrap()
{
self::getAimeos();
\Aimeos\MShop::cache( false );
\Aimeos\Controller\Frontend\Factory::setCache( false );
}


Expand Down

0 comments on commit 8086a84

Please sign in to comment.