Skip to content

Commit

Permalink
Fix issues with current php v
Browse files Browse the repository at this point in the history
  • Loading branch information
hsemix committed Jun 21, 2022
1 parent b9438f1 commit 27679b2
Show file tree
Hide file tree
Showing 22 changed files with 154 additions and 99 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"require": {
"php": ">= 7.1",
"tracy/tracy": "^2.6",
"nesbot/carbon": "^2.24.0",
"josegonzalez/dotenv": "dev-master",
"symfony/console": "~4.0",
"symfony/process": "^5.3",
Expand Down
42 changes: 0 additions & 42 deletions src/Yuga/Application/Router.php

This file was deleted.

82 changes: 67 additions & 15 deletions src/Yuga/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ReflectionClass;
use ReflectionMethod;
use ReflectionFunction;
use ReflectionNamedType;
use ReflectionParameter;
use InvalidArgumentException;
use Yuga\Container\Support\ClassNotInstantiableException;
Expand Down Expand Up @@ -126,9 +127,15 @@ public function resolve($key, array $arguments = [])
$class = $key;
}



if ($this->isSingleton($key) && $this->singletonResolved($key)) {
return $this->getSingletonInstance($key);
}

// echo '<pre>';
// print_r($arguments);
// die();

$object = $this->buildObject($class, $arguments);
return $this->prepareObject($key, $object);
Expand All @@ -146,18 +153,57 @@ public function inSingletons($class)

protected function buildDependencies($arguments, $dependencies, $className)
{

foreach ($dependencies as $dependency) {
if ($dependency->isOptional()) continue;
if ($dependency->isArray()) continue;

$class = $dependency->getClass();
if ($class === null) continue;
$name = $dependency->getName();

$type = $dependency->getType();

if ($dependency->isOptional()) continue;

if ($dependency->getType() === null) continue;

if ($type->isBuiltIn()) continue;

if (!$type) continue;

if (get_class($this) === $class->name) {
$arguments[] = $this;
continue;
if ($type instanceof ReflectionUnionType) {
throw new Exception('Failed to resolve class "' . $dependency . '" because of a union type');
}
$arguments[] = $this->resolve($class->name);

if ($type && $type instanceof ReflectionNamedType) {

if (get_class($this) === $type->getName()) {
$arguments[] = $this;
continue;
}
// make instance of this class :
$paramInstance = $this->resolve($type->getName());

// push to $dependencies array
$arguments[] = $paramInstance;

} else {

$name = $dependency->getName(); // get the name of param

// check this param value exist in $parameters
if (array_key_exists($name, $arguments)) { // if exist

// push value to $dependencies sequencially
$dependencies[] = $arguments[$name];

} else { // if not exist

if (!$dependency->isOptional()) { // check if not optional
throw new Exception("Class {$name} cannot be Instantiated");
}

}

}

}

return $arguments;
Expand All @@ -167,20 +213,23 @@ protected function buildDependencies($arguments, $dependencies, $className)
protected function buildObject($class, array $arguments = [])
{

$className = isset($class['value']) ? $class['value'] : $class;
$className = is_array($class) ? $class['value'] : $class;

$reflector = new ReflectionClass($className);

if (!$reflector->isInstantiable()) {
throw new ClassNotInstantiableException("Class {$className} cannot be Instantiated");
}

if ($reflector->getConstructor() !== null) {


if (!is_null($reflector->getConstructor())) {
$constructor = $reflector->getConstructor();
$dependencies = $constructor->getParameters();

$arguments = $this->buildDependencies($arguments, $dependencies, $class);
$object = $reflector->newInstanceArgs($arguments);
$object = $reflector->newInstanceArgs($arguments);

} else {
$object = new $reflector->name;
}
Expand Down Expand Up @@ -321,12 +370,15 @@ protected function addDependencyForCallParameter(ReflectionParameter $parameter,
$dependencies[] = $parameters[$parameter->name];

unset($parameters[$parameter->name]);
} else if ($parameter->getClass()) {
$dependencies[] = $this->buildObject($parameter->getClass()->name);
} else if ($parameter->getType()) {
$class = $parameter->getType() ? $parameter->getType()->getName() : null;

if (!$parameter->isOptional()) {
if (!\is_null($class))
$dependencies[] = $this->buildObject($class);
}
} else if ($parameter->isDefaultValueAvailable()) {
$dependencies[] = $parameter->getDefaultValue();
}
}


}
19 changes: 10 additions & 9 deletions src/Yuga/Cookie/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@ public static function get($name)
return $_COOKIE[$name];
}

public static function put($name, $value, $expiry = null, $domain = null, $secure = null, $path = '/')
public static function put($name, $value, $expiry = null, $domain = null, $secure = true, $path = '/')
{
$expiry = ($expiry === null) ? time() + 60 * 60 * 24 * 6004 : time() + $expiry;
return setcookie($name, $value, (($expiry > 0) ? $expiry : null), $path, $domain, $secure);
return self::create($name, $value, $expiry, $domain, $secure, $path);
}

public static function create($name, $value, $expiry = null, $domain = null, $secure = null, $path = '/')
public static function create($name, $value, $expiry = null, $domain = null, $secure = true, $path = '/')
{
if ($domain === null) {
$sub = explode('.', request()->getHost());
$domain = (count($sub) > 2) ? request()->getHost() : '.' . request()->getHost();
if (!empty($name) && !is_null($name)) {
if ($domain === null) {
$sub = explode('.', request()->getHost());
$domain = (count($sub) > 2) ? request()->getHost() : '.' . request()->getHost();
}
$expiry = ($expiry === null) ? time() + 60 * 60 * 24 * 6004 : time() + $expiry;
return setcookie($name, $value, (($expiry > 0) ? $expiry : null), $path, $domain, $secure);
}
$expiry = ($expiry === null) ? time() + 60 * 60 * 24 * 6004 : time() + $expiry;
return setcookie($name, $value, (($expiry > 0) ? $expiry : null), $path, $domain, $secure);
}

public static function delete($name)
Expand Down
19 changes: 15 additions & 4 deletions src/Yuga/Database/Elegant/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function find($key, $default = null)
* Make the object act like an array when at access time
*
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
Expand All @@ -95,44 +96,52 @@ public function offsetSet($offset, $value)
}
}

#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->items[$offset]);
}

#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->items[$offset]);
}

#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return isset($this->items[$offset]) ? $this->items[$offset] : null;
}

#[\ReturnTypeWillChange]
public function rewind()
{
reset($this->items);
}


#[\ReturnTypeWillChange]
public function current()
{
$var = current($this->items);
return $var;
}


#[\ReturnTypeWillChange]
public function key()
{
$var = key($this->items);
return $var;
}


#[\ReturnTypeWillChange]
public function next()
{
$var = next($this->items);
return $var;
}


#[\ReturnTypeWillChange]
public function valid()
{
$key = key($this->items);
Expand All @@ -145,6 +154,7 @@ public function valid()
*
* @return int
*/
#[\ReturnTypeWillChange]
public function count()
{
return count($this->items);
Expand Down Expand Up @@ -219,6 +229,7 @@ public function get($key = null, $default = null)
return $this->items;
}

#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->items;
Expand Down
5 changes: 5 additions & 0 deletions src/Yuga/Database/Elegant/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ public function getPagination()
*
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
Expand All @@ -355,6 +356,7 @@ public function offsetSet($offset, $value)
*
* @return boolean
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->attributes[$offset]);
Expand All @@ -367,6 +369,7 @@ public function offsetExists($offset)
*
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->attributes[$offset]);
Expand All @@ -379,6 +382,7 @@ public function offsetUnset($offset)
*
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return isset($this->attributes[$offset]) ? $this->attributes[$offset] : null;
Expand Down Expand Up @@ -415,6 +419,7 @@ public function __toString()
*
* @return array
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
$attributes = (array) $this->attributes;
Expand Down
3 changes: 3 additions & 0 deletions src/Yuga/Database/ElegantServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public function load(Application $app)
if (!is_null(env('DATABASE_NAME'))) {
$config = $app->config->load('config.Config');
$connection = $app->singleton('connection', Connection::class);

// print_r($connection);
// die();
$app->resolve('connection', [
$config->get('db.'.$config->get('db.defaultDriver'))
]);
Expand Down
7 changes: 6 additions & 1 deletion src/Yuga/Database/Query/ActiveRecord/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function __unset($key)
*
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
Expand All @@ -83,6 +84,7 @@ public function offsetSet($offset, $value)
*
* @return boolean
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->attributes[$offset]);
Expand All @@ -95,6 +97,7 @@ public function offsetExists($offset)
*
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->attributes[$offset]);
Expand All @@ -107,6 +110,7 @@ public function offsetUnset($offset)
*
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return isset($this->attributes[$offset]) ? $this->attributes[$offset] : null;
Expand Down Expand Up @@ -143,13 +147,14 @@ public function __toString()
*
* @return array
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
$attributes = (array) $this->attributes;
$attributes = array_map(function($attribute) {
if (!is_array($attribute)) {
if (!is_object($attribute)) {
$json_attribute = json_decode($attribute, true);
$json_attribute = json_decode($attribute ?? '', true);
if (json_last_error() == JSON_ERROR_NONE)
return $json_attribute;
} else {
Expand Down
Loading

0 comments on commit 27679b2

Please sign in to comment.