Skip to content

Commit

Permalink
Merge pull request #15916 from twistersfury/5.0.x-initialization
Browse files Browse the repository at this point in the history
Adding InitializationAwareInterface
  • Loading branch information
niden authored Mar 16, 2022
2 parents 068df90 + 86fdc73 commit 552e903
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

## Added
- Added `Phalcon\Encryption\Crypt::isValidDecryptLength($input)` to allow checking for the length of the decryption string [#15879](https://github.com/phalcon/cphalcon/issues/15879)
- Added `Phalcon\Di\InitializationAwareInterface` to allow auto calling the `initialize` method when accessing service through DIC

# [5.0.0beta3](https://github.com/phalcon/cphalcon/releases/tag/v5.0.0beta3) (2022-02-06)

Expand Down
5 changes: 5 additions & 0 deletions phalcon/Di/Di.zep
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use Phalcon\Config\Adapter\Yaml;
use Phalcon\Config\ConfigInterface;
use Phalcon\Di\ServiceInterface;
use Phalcon\Events\ManagerInterface;
use Phalcon\Di\InitializationAwareInterface;
use Phalcon\Di\InjectionAwareInterface;
use Phalcon\Di\ServiceProviderInterface;

Expand Down Expand Up @@ -240,6 +241,10 @@ class Di implements DiInterface
if instance instanceof InjectionAwareInterface {
instance->setDI(this);
}

if instance instanceof InitializationAwareInterface {
instance->initialize();
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions phalcon/Di/InitializationAwareInterface.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Phalcon\Di;

interface InitializationAwareInterface
{
public function initialize() -> void;
}
36 changes: 36 additions & 0 deletions tests/_data/fixtures/Di/InitializationAwareComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

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

declare(strict_types=1);

namespace Phalcon\Tests\Fixtures\Di;

use Phalcon\Di\InitializationAwareInterface;

/**
* Class InitializationAwareComponent
*
* @package Phalcon\Tests\Fixtures\Di
*/
class InitializationAwareComponent implements InitializationAwareInterface
{
private bool $initialized = false;

public function initialize(): void
{
$this->initialized = true;
}

public function isInitialized(): bool
{
return $this->initialized;
}
}
43 changes: 43 additions & 0 deletions tests/unit/Di/InitializationAwareCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

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

declare(strict_types=1);

namespace Phalcon\Tests\Unit\Di;

use Phalcon\Di\Di;
use Phalcon\Tests\Fixtures\Di\InitializationAwareComponent;
use UnitTester;

/**
* Class InitializationAwareCest
*
* @package Phalcon\Tests\Unit\Di
*/
class InitializationAwareCest
{
/**
* Tests Phalcon\Di\Di :: initialization aware interface
*
* @param UnitTester $I
*
* @author Phalcon Team <team@phalcon.io>
* @since 2019-09-09
*/
public function diInitializationAware(UnitTester $I)
{
$I->wantToTest('Di - initialization aware interface');

$di = new Di();

$I->assertEquals(true, $di->get(InitializationAwareComponent::class)->isInitialized());
}
}

0 comments on commit 552e903

Please sign in to comment.