| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Core; |
| 6: | |
| 7: | use Atk4\Ui\App; |
| 8: | |
| 9: | /** |
| 10: | * Typical software design will create the application scope. Most frameworks |
| 11: | * relies on "static" properties, methods and classes. This does puts some |
| 12: | * limitations on your implementation (you can't have multiple applications). |
| 13: | * |
| 14: | * App Scope will pass the 'app' property into all the object that you're |
| 15: | * adding, so that you know for sure which application you work with. |
| 16: | */ |
| 17: | trait AppScopeTrait |
| 18: | { |
| 19: | /** @var QuietObjectWrapper<App>|null */ |
| 20: | private ?QuietObjectWrapper $_app = null; |
| 21: | |
| 22: | /** |
| 23: | * When using mechanism for ContainerTrait, they inherit name of the |
| 24: | * parent to generate unique name for a child. In a framework it makes |
| 25: | * sense if you have a unique identifiers for all the objects because |
| 26: | * this enables you to use them as session keys, get arguments, etc. |
| 27: | * |
| 28: | * Unfortunately if those keys become too long it may be a problem, |
| 29: | * so ContainerTrait contains a mechanism for auto-shortening the |
| 30: | * name based around maxNameLength. The mechanism does only work |
| 31: | * if AppScopeTrait is used, $app property is set and has a |
| 32: | * maxNameLength defined. |
| 33: | * |
| 34: | * @var int<40, max> |
| 35: | */ |
| 36: | public int $maxNameLength = 60; |
| 37: | |
| 38: | /** |
| 39: | * As more names are shortened, the substituted part is being placed into |
| 40: | * this hash and the value contains the new key. This helps to avoid creating |
| 41: | * many sequential prefixes for the same character sequence. Those |
| 42: | * hashes can also be used to re-build the long name of the object, but |
| 43: | * this functionality is not essential and excluded from traits. You |
| 44: | * can find it in a test suite. |
| 45: | * |
| 46: | * @var array<string, string> |
| 47: | */ |
| 48: | public array $uniqueNameHashes = []; |
| 49: | |
| 50: | protected function assertInstanceOfApp(object $app): void |
| 51: | { |
| 52: | if (!$app instanceof App) { |
| 53: | // called from phpunit, allow to test this trait without Atk4\Ui\App class |
| 54: | if (class_exists(\PHPUnit\Framework\TestCase::class, false)) { |
| 55: | foreach (debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) { |
| 56: | if (str_starts_with($frame['class'] ?? '', 'Atk4\Core\Tests\\')) { |
| 57: | return; |
| 58: | } |
| 59: | } |
| 60: | } |
| 61: | |
| 62: | throw new Exception('App must be instance of Atk4\Ui\App'); |
| 63: | } |
| 64: | } |
| 65: | |
| 66: | public function issetApp(): bool |
| 67: | { |
| 68: | return $this->_app !== null; |
| 69: | } |
| 70: | |
| 71: | /** |
| 72: | * @return App |
| 73: | */ |
| 74: | public function getApp() |
| 75: | { |
| 76: | $app = $this->_app; |
| 77: | if ($app === null) { |
| 78: | throw new Exception('App is not set'); |
| 79: | } |
| 80: | |
| 81: | return $app->get(); |
| 82: | } |
| 83: | |
| 84: | /** |
| 85: | * @param App $app |
| 86: | * |
| 87: | * @return static |
| 88: | */ |
| 89: | public function setApp(object $app) |
| 90: | { |
| 91: | $this->assertInstanceOfApp($app); |
| 92: | |
| 93: | if ($this->issetApp()) { |
| 94: | throw new Exception('App is already set'); |
| 95: | } |
| 96: | |
| 97: | $this->_app = new QuietObjectWrapper($app); |
| 98: | |
| 99: | return $this; |
| 100: | } |
| 101: | } |
| 102: |