Created
February 5, 2018 11:11
-
-
Save wbarcovsky/eab477c6aa028c2dbc39fd9e44395771 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <?php | |
| trait CacheTrait | |
| { | |
| protected static $__lazy = []; | |
| protected static function cache($createFunction, $id = null) | |
| { | |
| if ($id === null) { | |
| $funcInfo = new ReflectionFunction($createFunction); | |
| $source = file($funcInfo->getFileName()); | |
| $body = implode("", array_slice($source, $funcInfo->getStartLine(), $funcInfo->getEndLine())); | |
| $id = md5($body); | |
| } | |
| if (isset(static::$__lazy[$id])) { | |
| return static::$__lazy[$id]; | |
| } | |
| $object = $createFunction(); | |
| if ($id !== false) { | |
| static::$__lazy[$id] = $object; | |
| } | |
| return $object; | |
| } | |
| public static function clearCache() | |
| { | |
| self::$__lazy = []; | |
| } | |
| } | |
| abstract class JsonFileFactory | |
| { | |
| use CacheTrait; | |
| public $modelClass; | |
| abstract function jsonFilePath(): string; | |
| /** | |
| * @param null $filters | |
| * @return array | |
| */ | |
| public function getAll($filters = null) | |
| { | |
| $allData = self::cache(function () { | |
| return json_decode(file_get_contents(self::jsonFilePath())); | |
| }); | |
| $result = []; | |
| foreach ($allData as $item) { | |
| $model = new $this->modelClass(); | |
| foreach ($item as $field => $value) { | |
| $model->$field = $value; | |
| } | |
| if ($filters == null || (is_callable($filters) && $filters($model))) { | |
| $result[] = $model; | |
| } | |
| } | |
| return $result; | |
| } | |
| public function filterArray($array, $filters) | |
| { | |
| } | |
| } | |
| class UserFactory extends JsonFileFactory | |
| { | |
| public $modelClass = TestUser::class; | |
| public function jsonFilePath(): string | |
| { | |
| return __DIR__ . '/data/users.json'; | |
| } | |
| /** | |
| * @inheritdoc | |
| * @return TestUser[] | |
| */ | |
| public function getAll($filters = null) | |
| { | |
| return parent::getAll($filters); | |
| } | |
| } | |
| class TestUser | |
| { | |
| use CacheTrait; | |
| public $id; | |
| public $title; | |
| public $friendIds; | |
| public $enabled = true; | |
| /** | |
| * @return TestUser[] | |
| */ | |
| public function getFriends() | |
| { | |
| $cacheId = 'friends#' . $this->id; | |
| return self::cache(function () { | |
| return App::users()->getAll(function(TestUser $user) { | |
| return $user->enabled && in_array($this->id, $user->friendIds); | |
| }); | |
| }, $cacheId); | |
| } | |
| } | |
| class App | |
| { | |
| use CacheTrait; | |
| public static function users(): UserFactory | |
| { | |
| return self::cache(function () { | |
| return new UserFactory(); | |
| }); | |
| } | |
| } | |
| $user = App::users()->getAll(['id'])[0]; | |
| var_dump($user->getFriends()[0]->id); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment