Skip to content

Commit

Permalink
[phalcon#12676] - Merge branch '4.0.x' into T12676-add-methods-to-int…
Browse files Browse the repository at this point in the history
…erfaces

* 4.0.x:
  Merge for phalcon#12547
  Added some methods to Phalcon\Acl\Adapter\Memory
  [phalcon#12295] - Removed the final from the class definition
  [phalcon#12295] - Updated the changelog
  [phalcon#12295] - Added container tests
  [phalcon#12295] - Added Container object, PSR-11 compliant
  [phalcon#12295] - Added entry to the changelog
  [phalcon#12295] - Fixed interface
  [4.0.x] - Another correction to the test
  • Loading branch information
niden committed Dec 23, 2018
2 parents 08af4d4 + 40b604e commit e1e14af
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG-4.0.md
Expand Up @@ -45,6 +45,9 @@
- `Phalcon\Session\AdapterInterface` - `setId`, `status`
- `Phalcon\Validation\MessageInteraface` - `getCode`, `setCode`
- `Phalcon\CryptInterface` - `setPadding`
- Added `attach()` to `Phalcon\Mvc\RouterInterface`
- Added `Phalcon\Container`, a proxy container class to the `Phalcon\DI` implementing PSR-11 [#12295](https://github.com/phalcon/cphalcon/issues/12295)
- Added `Phalcon\Acl\Adapter\Memory::getActiveKey`, `Phalcon\Acl\Adapter\Memory::activeFunctionCustomArgumentsCount` and `Phalcon\Acl\Adapter\Memory::getActiveFunction` to get latest key, number of custom arguments, and function used to acquire access

## Changed
- By configuring `prefix` and `statsKey` the `Phalcon\Cache\Backend\Redis::queryKeys` no longer returns prefixed keys, now it returns original keys without prefix. [#13656](https://github.com/phalcon/cphalcon/pull/13656)
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -14,3 +14,4 @@ To see what we changed in particular framework branch refer to the relevant chan
- [**`3.0.x`**](resources/CHANGELOG-3.0.md)
- [**`2.0.x`**](resources/CHANGELOG-2.0.md)
- [**`1.x.x`**](resources/CHANGELOG-1.x.md)

35 changes: 35 additions & 0 deletions phalcon/acl/adapter/memory.zep
Expand Up @@ -162,6 +162,27 @@ class Memory extends Adapter
*/
protected _noArgumentsDefaultAction = Acl::ALLOW;

/**
* Returns latest key used to acquire access
*
* @var string|null
*/
protected _activeKey { get };

/**
* Returns latest function used to acquire access
*
* @var mixed
*/
protected _activeFunction { get };

/**
* Returns number of additional arguments(excluding role and resource) for active function
*
* @var int
*/
protected _activeFunctionCustomArgumentsCount = 0 { get };

/**
* Phalcon\Acl\Adapter\Memory constructor
*/
Expand Down Expand Up @@ -616,6 +637,10 @@ class Memory extends Adapter
let this->_activeOperation = operationName;
let this->_activeSubject = subjectName;
let this->_activeAccess = access;
let this->_activeKey = null;
let this->_activeFunction = null;
let this->_activeFunctionCustomArgumentsCount = 0;

let accessList = this->_access;
let eventsManager = <EventsManager> this->_eventsManager;
let funcList = this->_func;
Expand Down Expand Up @@ -654,7 +679,15 @@ class Memory extends Adapter
eventsManager->fire("acl:afterCheckAccess", this);
}

let this->_activeKey = accessKey;
let this->_activeFunction = funcAccess;

if haveAccess == null {
/**
* Change activeKey to most narrow if there was no access for any patterns found
*/
let this->_activeKey = operationName . "!" . subjectName . "!" . access;

return this->_defaultAccess == Acl::ALLOW;
}

Expand Down Expand Up @@ -712,6 +745,8 @@ class Memory extends Adapter
}
}

let this->_activeFunctionCustomArgumentsCount = userParametersSizeShouldBe;

if count(parameters) > userParametersSizeShouldBe {
trigger_error(
"Number of parameters in array is higher than the number of parameters in defined function when check " . operationName . " can " . access . " " . subjectName . ". Remember that more parameters than defined in function will be ignored.",
Expand Down
46 changes: 46 additions & 0 deletions phalcon/container.zep
@@ -0,0 +1,46 @@

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon;

use Psr\Container\ContainerInterface;
use Phalcon\DiInterface;

class Container implements ContainerInterface
{
/**
* @var <DiInterface>
*/
protected container;

/**
* Phalcon\Di constructor
*/
public function __construct(<DiInterface> container)
{
let this->container = container;
}

/**
* Return the service
*/
public function get(var name) -> var
{
return this->container->getService(name);
}

/**
* Whether a service exists or not in the container
*/
public function has(var name) -> bool
{
return this->container->has(name);
}
}
2 changes: 0 additions & 2 deletions phalcon/mvc/router.zep
Expand Up @@ -635,8 +635,6 @@ class Router implements InjectionAwareInterface, RouterInterface, EventsAwareInt
* Router::POSITION_FIRST
* );
* </code>
*
* @todo Add to the interface for 4.0.0
*/
public function attach(<RouteInterface> route, var position = Router::POSITION_LAST) -> <RouterInterface>
{
Expand Down
6 changes: 6 additions & 0 deletions phalcon/mvc/routerinterface.zep
Expand Up @@ -19,6 +19,7 @@

namespace Phalcon\Mvc;

use Phalcon\Mvc\Router;
use Phalcon\Mvc\Router\RouteInterface;
use Phalcon\Mvc\Router\GroupInterface;

Expand Down Expand Up @@ -110,6 +111,11 @@ interface RouterInterface
*/
public function addConnect(string! pattern, var paths = null) -> <RouteInterface>;

/**
* Attach Route object to the routes stack.
*/
public function attach(<RouteInterface> route, var position = Router::POSITION_LAST) -> <RouterInterface>;

/**
* Mounts a group of routes in the router
*/
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/Acl/Adapter/MemoryCest.php
Expand Up @@ -20,6 +20,7 @@
use Phalcon\Test\Fixtures\Acl\TestOperationSubjectAware;
use PHPUnit\Framework\Exception;
use UnitTester;
use Closure;

class MemoryCest
{
Expand Down Expand Up @@ -661,4 +662,46 @@ public function testAclNegationOfMultilayerInheritedOperations(UnitTester $I)
$actual = (bool) $acl->isAllowed('Members', 'Login', 'index');
$I->assertTrue($actual);
}

/**
* Tests checking active key method
*
* @author Wojciech Slawski <jurigag@gmail.com>
* @since 2017-01-13
*/
public function testActiveKey(UnitTester $I)
{
$acl = new Memory();
$acl->addOperation(new Operation("Guests"));
$acl->addSubject(new Subject('Post'), ['index', 'update', 'create']);

$acl->allow('Guests', 'Post', 'create');
$I->assertTrue($acl->isAllowed('Guests', 'Post', 'create'));
$I->assertEquals('Guests!Post!create', $acl->getActiveKey());
}

/**
* Tests checking active function method
*
* @author Wojciech Slawski <jurigag@gmail.com>
* @since 2017-01-13
*/
public function testActiveFunction(UnitTester $I)
{
$function = function ($a) {
return true;
};

$acl = new Memory();
$acl->addOperation(new Operation("Guests"));
$acl->addSubject(new Subject('Post'), ['index', 'update', 'create']);

$acl->allow('Guests', 'Post', 'create', $function);
$I->assertTrue($acl->isAllowed('Guests', 'Post', 'create', ['a' => 1]));
$returnedFunction = $acl->getActiveFunction();

$I->assertInstanceOf(Closure::class, $returnedFunction);
$I->assertEquals(1, $function(1));
$I->assertEquals(1, $acl->getActiveFunctionCustomArgumentsCount());
}
}
42 changes: 42 additions & 0 deletions tests/unit/Container/ConstructCest.php
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Unit\Container;

use Phalcon\Container;
use Phalcon\Di;
use Psr\Container\ContainerInterface;
use UnitTester;

/**
* Class ConstructCest
*/
class ConstructCest
{
/**
* Tests Phalcon\Container :: __construct()
*
* @param UnitTester $I
*
* @author Phalcon Team <team@phalconphp.com>
* @since 2018-11-13
*/
public function containerConstruct(UnitTester $I)
{
$I->wantToTest('Container - __construct()');

$container = new Di();
$class = ContainerInterface::class;
$actual = new Container($container);
$I->assertInstanceOf($class, $actual);
}
}
50 changes: 50 additions & 0 deletions tests/unit/Container/GetCest.php
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Unit\Container;

use Phalcon\Container;
use Phalcon\Di\Service;
use Phalcon\Filter;
use Phalcon\Test\Fixtures\Traits\DiTrait;
use UnitTester;

/**
* Class GetCest
*/
class GetCest
{
use DiTrait;

/**
* Tests Phalcon\Container :: get()
*
* @param UnitTester $I
*
* @author Phalcon Team <team@phalconphp.com>
* @since 2018-11-13
*/
public function containerGet(UnitTester $I)
{
$I->wantToTest('Container - get()');
$this->newDi();
$this->setDiFilter();

$container = new Container($this->container);

/** @var Service $service */
$service = $container->get('filter');
$expected = Filter::class;
$actual = $service->getDefinition();
$I->assertEquals($expected, $actual);
}
}
45 changes: 45 additions & 0 deletions tests/unit/Container/HasCest.php
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalconphp.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Phalcon\Test\Unit\Container;

use Phalcon\Container;
use Phalcon\Test\Fixtures\Traits\DiTrait;
use UnitTester;

/**
* Class HasCest
*/
class HasCest
{
use DiTrait;

/**
* Tests Phalcon\Container :: has()
*
* @param UnitTester $I
*
* @author Phalcon Team <team@phalconphp.com>
* @since 2018-11-13
*/
public function containerHas(UnitTester $I)
{
$I->wantToTest('Container - has()');
$this->newDi();
$this->setDiFilter();

$container = new Container($this->container);

$actual = $container->has('filter');
$I->assertTrue($actual);
}
}

0 comments on commit e1e14af

Please sign in to comment.