Skip to content

Commit

Permalink
laravel, not even once
Browse files Browse the repository at this point in the history
  • Loading branch information
pjc09h committed Sep 24, 2023
1 parent 186304d commit 4233a91
Show file tree
Hide file tree
Showing 5 changed files with 424 additions and 40 deletions.
109 changes: 71 additions & 38 deletions app/ENV.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class ENV
private static $instance = null;

# config option receptacles
public static $public = []; # site meta, options, resources, etc.
private static $private = []; # passwords, app keys, database, etc.
public $public = []; # site meta, options, resources, etc.
private $private = []; # passwords, app keys, database, etc.


/**
Expand Down Expand Up @@ -59,22 +59,22 @@ public function __wakeup()
);
}

# $this->key returns self::$public["key"]
public function __get(mixed $key)
# $this->key returns $this->public[$key]
public function __get(mixed $key): mixed
{
return self::$public[$key] ?? null;
return $this->public[$key] ?? null;
}

# $this->key = "value"
public function __set(mixed $key, mixed $value)
public function __set(mixed $key, mixed $value): void
{
return self::$public[$key] = $this->toObject($value);
$this->public[$key] = $this->toObject($value);
}

# isset
public function __isset(mixed $key)
public function __isset(mixed $key): bool
{
return isset(self::$public[$key]);
return isset($this->public[$key]);
}


Expand Down Expand Up @@ -108,11 +108,11 @@ public function private(mixed $key, mixed $value = null): mixed
{
# get
if (!$value) {
return self::$private[$key] ?? null;
return $this->private[$key] ?? null;
}

# set
return self::$private[$key] = $this->toObject($value);
return $this->private[$key] = $this->toObject($value);
}


Expand All @@ -128,7 +128,7 @@ public function private(mixed $key, mixed $value = null): mixed
*/
public function toArray(mixed $object): mixed
{
if (is_object($object) || is_array($object)) {
if (is_iterable($object)) {
$return = (array) $object;

foreach ($return as &$item) {
Expand All @@ -154,7 +154,7 @@ public function toArray(mixed $object): mixed
*/
public function toObject(mixed $array): mixed
{
if (is_object($array) || is_array($array)) {
if (is_iterable($array)) {
$return = new RecursiveArrayObject($array);

foreach ($return as &$item) {
Expand Down Expand Up @@ -384,53 +384,80 @@ public function pluck(iterable $object, mixed $value, mixed $key = null): Recurs
class RecursiveArrayObject extends ArrayObject
{
/**
* __functions
* __construct
*
* @param mixed $input the input to construct
* @param int $flags the flags to set
* @param string $iteratorClass the iterator class to set
*
* @see https://www.php.net/manual/en/arrayobject.construct.php
*/

# __construct
public function __construct(mixed $input = null, int $flags = self::ARRAY_AS_PROPS, string $iteratorClass = "ArrayIterator")
public function __construct(mixed $input = [], int $flags = self::ARRAY_AS_PROPS, string $iteratorClass = "ArrayIterator")
{
# set the flage and iterator
$this->setFlags($flags);
$this->setIteratorClass($iteratorClass);

# set the input properties
foreach ($input as $key => $value) {
$this->__set($key, $value);
}

# return the object
return $this;
}

# __get
public function __get(mixed $key)
{
if ($this->offsetExists($key)) {
return $this->offsetGet($key);
}

if (array_key_exists($key, $this)) {
return $this[$key];
}

throw new InvalidArgumentException("the instance doesn't have the property {$key}");
/**
* __get
*
* @param mixed $key the key to get
* @return mixed the value of the key
*/
public function __get(mixed $key): mixed
{
return $this->offsetGet($key) ?? null;
}

# __set
public function __set(mixed $key, mixed $value)

/**
* __set
*
* @param mixed $key the key to set
* @param mixed $value the value to set
* @return void
*/
public function __set(mixed $key, mixed $value): void
{
if (is_array($value) || is_object($value)) {
$this->offsetSet($key, (new self($value)));
if (is_iterable($value)) {
$this->offsetSet($key, new self($value));
} else {
$this->offsetSet($key, $value);
}
}

# __isset
public function __isset(mixed $key)

/**
* __isset
*
* @param mixed $key the key to check
* @return bool whether the key is set
*/
public function __isset(mixed $key): bool
{
return array_key_exists($key, $this);
return $this->offsetExists($key);
}

# __unset
public function __unset(mixed $key)

/**
* __unset
*
* @param mixed $key the key to unset
* @return void
*/
public function __unset(mixed $key): void
{
unset($this[$key]);
$this->offsetUnset($key);
}


Expand All @@ -447,9 +474,15 @@ public function __unset(mixed $key)
*/
public function __call(string $callback, mixed $arguments = null)
{
if (!is_callable($callback)) {
throw new BadMethodCallException(__CLASS__ . "->" . $callback);
}

/*
if (!is_callable($callback) || !str_starts_with($callback, "array_")) {
throw new BadMethodCallException(__CLASS__ . "->" . $callback);
}
*/

return call_user_func_array(
$callback,
Expand Down
2 changes: 1 addition & 1 deletion app/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private static function factory(array $options = []): Twig\Environment

# globals: app and env
$twig->addGlobal("app", $app);
$twig->addGlobal("env", $app->env::$public);
$twig->addGlobal("env", $app->env->public);

# user and authenticated
$twig->addGlobal("user", $app->user);
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"snipe/banbuilder": "^2.3",
"sofa/eloquence": "dev-master",
"spatie/emoji": "^4.0",
"spatie/laravel-collection-macros": "^7.12",
"spatie/once": "^3.1",
"twig/twig": "^3.7.1",
"vanilla/nbbc": "^2.4",
Expand Down
80 changes: 79 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4233a91

Please sign in to comment.