-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add central/singleton Factory and deprecate FactoryTrait trait #221
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
97d7f5b
Add central/singleton Factory and deprecate FactoryTrait trait
mvorisek a39a696
fix exception renderer fallback message
mvorisek 6e9134f
fix vendor dir when core is used as main project
mvorisek 9dd9446
fix CS
mvorisek 8bd3aef
simplify
mvorisek 96cff5f
improve translatability
mvorisek 6b0478b
small improvements
mvorisek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace atk4\core; | ||
|
||
class Factory | ||
{ | ||
/** @var Factory */ | ||
private static $_instance; | ||
|
||
protected function __construct() | ||
{ | ||
// singleton | ||
} | ||
|
||
final protected static function getInstance(): self | ||
{ | ||
if (self::$_instance === null) { | ||
self::$_instance = new self(); | ||
} | ||
|
||
return self::$_instance; | ||
} | ||
|
||
protected function _mergeSeeds($seed, $seed2, ...$more_seeds) | ||
{ | ||
// recursively merge extra seeds | ||
if (count($more_seeds) > 0) { | ||
$seed2 = $this->_mergeSeeds($seed2, ...$more_seeds); | ||
} | ||
|
||
if (is_object($seed) || is_object($seed2)) { | ||
if (is_object($seed)) { | ||
$passively = true; // set defaults but don't override existing properties | ||
} else { | ||
$passively = false; | ||
[$seed, $seed2] = [$seed2, $seed]; // swap seeds | ||
} | ||
|
||
if (is_array($seed2)) { | ||
$arguments = array_filter($seed2, 'is_numeric', ARRAY_FILTER_USE_KEY); // with numeric keys | ||
$injection = array_diff_key($seed2, $arguments); // with string keys | ||
if ($injection) { | ||
if (isset($seed->_DIContainerTrait)) { | ||
$seed->setDefaults($injection, $passively); | ||
} else { | ||
throw (new Exception('Property injection is possible only to objects that use \atk4\core\DIContainerTrait trait')) | ||
->addMoreInfo('object', $seed) | ||
->addMoreInfo('injection', $injection) | ||
->addMoreInfo('passively', $passively); | ||
} | ||
} | ||
} | ||
|
||
return $seed; | ||
} | ||
|
||
if (!is_array($seed)) { | ||
$seed = [$seed]; | ||
} | ||
|
||
if (!is_array($seed2)) { | ||
$seed2 = [$seed2]; | ||
} | ||
|
||
// merge seeds but prefer seed over seed2 | ||
// move numerical keys to the beginning and sort them | ||
$res = []; | ||
$res2 = []; | ||
foreach ($seed as $k => $v) { | ||
if (is_numeric($k)) { | ||
$res[$k] = $v; | ||
} elseif ($v !== null) { | ||
$res2[$k] = $v; | ||
} | ||
} | ||
foreach ($seed2 as $k => $v) { | ||
if (is_numeric($k)) { | ||
if (!isset($res[$k])) { | ||
$res[$k] = $v; | ||
} | ||
} elseif ($v !== null) { | ||
if (!isset($res2[$k])) { | ||
$res2[$k] = $v; | ||
} | ||
} | ||
} | ||
ksort($res, SORT_NUMERIC); | ||
$res = $res + $res2; | ||
|
||
return $res; | ||
} | ||
|
||
protected function createNewObject(string $className, array $ctorArgs): object | ||
{ | ||
return new $className(...$ctorArgs); | ||
} | ||
|
||
protected function _factory($seed, $defaults = []): object | ||
{ | ||
if ($defaults === null) { // should be deprecated soon | ||
$defaults = []; | ||
} | ||
|
||
if ($seed === null) { // should be deprecated soon | ||
$seed = []; | ||
} elseif (!is_array($seed)) { | ||
$seed = [$seed]; | ||
} | ||
|
||
if (is_array($defaults)) { | ||
array_unshift($defaults, null); // insert argument 0 | ||
} elseif (!is_object($defaults)) { | ||
$defaults = [null, $defaults]; | ||
} | ||
$seed = $this->_mergeSeeds($seed, $defaults); | ||
|
||
if (is_object($seed)) { | ||
// setDefaults() already called in _mergeSeeds() | ||
|
||
return $seed; | ||
} | ||
|
||
$arguments = array_filter($seed, 'is_numeric', ARRAY_FILTER_USE_KEY); // with numeric keys | ||
$injection = array_diff_key($seed, $arguments); // with string keys | ||
$object = array_shift($arguments); // first numeric key argument is object | ||
|
||
if (!is_object($object)) { | ||
if (!is_string($object)) { | ||
throw (new Exception('Class name is not specified by the seed')) | ||
->addMoreInfo('seed', $seed); | ||
} | ||
|
||
$object = $this->createNewObject($object, $arguments); | ||
} | ||
|
||
if ($injection) { | ||
$this->_mergeSeeds($injection, $object); | ||
} | ||
|
||
return $object; | ||
} | ||
|
||
/** | ||
* Given two seeds (or more) will merge them, prioritizing the first argument. | ||
* If object is passed on either of arguments, then it will setDefaults() remaining | ||
* arguments, respecting their positioning. | ||
* | ||
* To learn more about mechanics of factory trait, see documentation | ||
* | ||
* @return object|array if at least one seed is an object, will return object | ||
*/ | ||
final public static function mergeSeeds(...$seeds) | ||
{ | ||
return self::getInstance()->_mergeSeeds(...$seeds); | ||
} | ||
|
||
/** | ||
* Given a Seed (see doc) as a first argument, will create object of a corresponding | ||
* class, call constructor with numerical arguments of a seed and inject key/value | ||
* arguments. | ||
* | ||
* Argument $defaults has the same effect as the seed, but will not contain the class. | ||
* Class is always determined by seed, except if you pass object into defaults. | ||
* | ||
* To learn more about mechanics of factory trait, see documentation | ||
* | ||
* @param array $defaults | ||
*/ | ||
final public static function factory($seed, $defaults = []): object | ||
{ | ||
if (func_num_args() > 2) { // prevent bad usage | ||
throw new \Error('Too many method arguments'); | ||
} | ||
|
||
return self::getInstance()->_factory($seed, $defaults); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can reduce code here by simpy calling
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertInstanceOf
is computed even before the object is even created, which implies stronger security! (as seed can be an user input)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I see the
assertInstanceOf
is called before the$object
is returned only?The rest of the code is the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_fromSeedPrecheck
is called BEFORE object is constructed. Be very carefull around this and test for this behaviour will be highly welcomed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR for test: #311