Compare commits
	
		
			9 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| dd239c63bc | |||
|  | 0db34fe228 | ||
|  | fa4fe32277 | ||
|  | e20157a162 | ||
| 63c7dc23f9 | |||
| a4d99dfe08 | |||
|  | f199cc7085 | ||
|  | fcb332ae53 | ||
|  | 133672b347 | 
							
								
								
									
										129
									
								
								bin/migrate.php
									
									
									
									
									
								
							
							
						
						
									
										129
									
								
								bin/migrate.php
									
									
									
									
									
								
							| @ -1,129 +0,0 @@ | ||||
| #!/usr/bin/php
 | ||||
| <?php | ||||
| 
 | ||||
| array_shift($argv); | ||||
| 
 | ||||
| $opt = getopt('c::f:v', [ 'folder:', 'confirm', 'verbose', 'clean', 'old' ]); | ||||
| 
 | ||||
| if ( ! $opt ) { | ||||
|     exit("Bad arguments provided, you must specify a fullpath using the -f or --folder option\n"); | ||||
| } | ||||
| 
 | ||||
| $iterator = new RecursiveIteratorIterator(new class(new RecursiveDirectoryIterator($opt['folder'] ?? $opt['f'], FilesystemIterator::SKIP_DOTS)) extends RecursiveFilterIterator { | ||||
|     public function accept() : bool { | ||||
|         return true; | ||||
|     } | ||||
| }, RecursiveIteratorIterator::SELF_FIRST); | ||||
| 
 | ||||
| $files = array(); | ||||
| 
 | ||||
| foreach ($iterator as $info) { | ||||
|     if ($info->getExtension() !== "php" || $info->isDir() ) { | ||||
|         continue; | ||||
|     } | ||||
| 
 | ||||
|     echo sprintf("\n### MIGRATING FILE %s\n", $info->getFilename()); | ||||
| 
 | ||||
|     $opened = false; | ||||
| 
 | ||||
|     $attr = $newContent = []; | ||||
| 
 | ||||
|     $content = file_get_contents($info->getPathname()); | ||||
| 
 | ||||
|     if ( isset($opt['clean']) ) { | ||||
|         foreach (explode(PHP_EOL, $content) as $lineIdx => $line) { | ||||
|             if ( $pos = strpos($line, '# migrated from: ') ) { | ||||
|                 $attr = true; | ||||
| 
 | ||||
|                 $newContent[] = $newLine = substr($line, 0, $pos); | ||||
| 
 | ||||
|                 echo sprintf("Changed line '%s' \n", trim($newLine)); | ||||
|             } | ||||
|             else { | ||||
|                 $newContent[] = $line; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
|     else { | ||||
|         foreach (explode(PHP_EOL, $content) as $lineIdx => $line) { | ||||
|             $tline = trim($line); | ||||
| 
 | ||||
|             if ($tline === "/**") { | ||||
|                 $opened = true; | ||||
|             } elseif ($tline === '*/') { | ||||
|                 $opened = false; | ||||
|             } elseif (substr($tline, 0, 1) === '*') { | ||||
|                 $annotation = trim(substr($tline, 1)); | ||||
| 
 | ||||
|                 if (strpos($annotation, '@') === 0) { | ||||
|                     $argpos = strpos($annotation, '('); | ||||
| 
 | ||||
|                     if ($argpos !== false) { | ||||
|                         $name = substr($annotation, 1, $argpos - 1); | ||||
|                         $args = substr($annotation, $argpos + 1, strrpos($annotation, ')') - $argpos - 1); | ||||
| 
 | ||||
|                         try { | ||||
|                             $array = eval("return [{$args}];"); | ||||
| 
 | ||||
|                             $renderedArgs = []; | ||||
| 
 | ||||
|                             foreach ($array as $key => $argument) { | ||||
| 
 | ||||
|                                 if (is_array($argument)) { | ||||
|                                     $argument = var_export($argument, true); | ||||
|                                 } elseif (is_bool($argument)) { | ||||
|                                     $argument = $argument ? 'true' : 'false'; | ||||
|                                 } elseif (!is_numeric($argument)) { | ||||
|                                     $argument = "\"$argument\"";
 | ||||
|                                 } | ||||
| 
 | ||||
|                                 if (is_numeric($key)) { | ||||
|                                     $renderedArgs[] = $argument; | ||||
|                                 } else { | ||||
|                                     $renderedArgs[] = "$key: $argument"; | ||||
|                                 } | ||||
|                             } | ||||
| 
 | ||||
|                             $renderedArgs = implode(', ', $renderedArgs); | ||||
|                         } catch (\Throwable $e) { | ||||
|                             echo sprintf("!!! WARNING : %s in file (%s) line %d ; using previous notation.\n", $e->getMessage(), $info->getPathname(), $lineIdx + 1); | ||||
| 
 | ||||
|                             $renderedArgs = str_replace(' => ', ': ', $args); | ||||
|                         } | ||||
|                     } else { | ||||
|                         $name = substr($annotation, 1); | ||||
|                         $renderedArgs = ""; | ||||
|                     } | ||||
| 
 | ||||
|                     $attr[] = $attribute = sprintf("%s#[%s%s]%s", str_repeat(" ", strlen($line) - strlen(ltrim($line)) >= 4 ? 4 : 0), $name, $renderedArgs ? "($renderedArgs)" : "", $renderedArgs ? ( isset($opt['old']) ? " # migrated from: $args" : "" ) : ""); | ||||
| 
 | ||||
|                     $newContent[] = $attribute; | ||||
|                 } | ||||
|             } else { | ||||
|                 $newContent[] = $line; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     $newContent = implode(PHP_EOL, $newContent); | ||||
| 
 | ||||
|     if ($attr) { | ||||
|         if ( isset($opt['verbose']) || isset($opt['v']) ) { | ||||
|             echo $newContent; | ||||
|         } | ||||
| 
 | ||||
|         if ( isset($opt['confirm']) || isset($opt['c']) ) { | ||||
|             file_put_contents($info->getPathname(), $newContent); | ||||
| 
 | ||||
|             echo sprintf("\n### FILE CONVERSION COMPLETED %s\n", $info->getFilename()); | ||||
|         } | ||||
|         else { | ||||
|             echo sprintf("\n### FILE CONVERSION ANALYZED, NOTHING WRITTEN UNTIL --confirm or -c FLAG IS PROVIDED %s\n", $info->getFilename()); | ||||
|         } | ||||
|     } | ||||
|     else { | ||||
|         echo sprintf("\n### NOTHING TO DO ON FILE %s\n", $info->getFilename()); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -33,8 +33,9 @@ class AttributeReader | ||||
|     { | ||||
|         switch(true) { | ||||
|             case $reflect instanceof ReflectionMethod : | ||||
|             case $reflect instanceof ReflectionProperty : | ||||
|                 return $reflect->class . "::" . $reflect->name; | ||||
|             case $reflect instanceof ReflectionProperty : | ||||
|                 return $reflect->class . "::$" . $reflect->name; | ||||
| 
 | ||||
|             case $reflect instanceof ReflectionClass : | ||||
|                 return $reflect->name; | ||||
|  | ||||
| @ -21,7 +21,6 @@ abstract class Reflected | ||||
|         $type = strtolower($type); | ||||
| 
 | ||||
|         foreach($this->getTypes() as $item) { | ||||
| 
 | ||||
|             if ($type === "null") { | ||||
|                 if ($item->type === "null" || $item->nullable) { | ||||
|                      return true; | ||||
| @ -46,6 +45,8 @@ abstract class Reflected | ||||
|             $type = $property->getType(); | ||||
| 
 | ||||
|             if ($type instanceof \ReflectionUnionType ) { | ||||
|                 $this->type = []; | ||||
| 
 | ||||
|                 foreach($type->getTypes() as $item) { | ||||
|                     $this->type[] = new ReflectedPropertyType($item->getName(), $item->isBuiltIn(), $item->allowsNull()); | ||||
|                 } | ||||
|  | ||||
							
								
								
									
										82
									
								
								src/Common/Reflected.php~
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								src/Common/Reflected.php~
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,82 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace Notes\Common; | ||||
| 
 | ||||
| use Notes\Attribute\Ignore; | ||||
| 
 | ||||
| abstract class Reflected | ||||
| { | ||||
|     public function hasIgnoreAttribute() : bool | ||||
|     { | ||||
|         return [] !== array_filter($this->attributes, fn($e) => $e->object instanceof Ignore); | ||||
|     } | ||||
| 
 | ||||
|     public function allowsNull() : bool | ||||
|     { | ||||
|         # dump($this);
 | ||||
|         return empty($this->type) || $this->expectType("null"); | ||||
|     } | ||||
| 
 | ||||
|     public function expectType(string $type) : bool | ||||
|     { | ||||
|         $type = strtolower($type); | ||||
| 
 | ||||
|         foreach($this->getTypes() as $item) { | ||||
|             if ($type === "null") { | ||||
|                 if ($item->type === "null" || $item->nullable) { | ||||
|                      return true; | ||||
|                 } | ||||
|             } | ||||
|             elseif ($type === $item->type) { | ||||
|                 return true; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     public function getTypes() : array | ||||
|     { | ||||
|         return is_array($this->type) ? $this->type : [ $this->type ]; | ||||
|     } | ||||
| 
 | ||||
|     public function typeFromReflection(\ReflectionProperty|\ReflectionParameter $property) : void | ||||
|     { | ||||
|         if ( $property->hasType() ) { | ||||
|             $type = $property->getType(); | ||||
| 
 | ||||
|             if ($type instanceof \ReflectionUnionType ) { | ||||
|                 foreach($type->getTypes() as $item) { | ||||
|                     $this->type[] = new ReflectedPropertyType($item->getName(), $item->isBuiltIn(), $item->allowsNull()); | ||||
|                 } | ||||
|             } | ||||
|             else { | ||||
|                 $this->type = new ReflectedPropertyType($type->getName(), $type->isBuiltIn(), $type->allowsNull()); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public function getAttributes(null|string|array $attributeType = null): array | ||||
|     { | ||||
|         if ($attributeType) { | ||||
|             $list = []; | ||||
| 
 | ||||
|             foreach($this->attributes as $attribute) { | ||||
|                 foreach((array) $attributeType as $type) { | ||||
|                     if ($attribute->object instanceof $type) { | ||||
|                         $list[] = $attribute; | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|             return array_reverse($list); | ||||
|         } | ||||
| 
 | ||||
|         return array_reverse($this->attributes); | ||||
|     } | ||||
| 
 | ||||
|     public function getAttribute(string|array $attributeType): ?object | ||||
|     { | ||||
|         return $this->getAttributes($attributeType)[0] ?? null; | ||||
|     } | ||||
| } | ||||
| @ -7,5 +7,6 @@ class Cronard implements \Notes\Attribute { | ||||
|     public function __construct( | ||||
|         public string $cron, | ||||
|         public null|string $method = null, | ||||
|         public null|string $name = null, | ||||
|     ) {} | ||||
| } | ||||
|  | ||||
| @ -23,10 +23,7 @@ class ObjectReflection { | ||||
|         $this->classname = ltrim($class instanceof ReflectionClass ? $class->getName() : $class, '\\'); | ||||
| 
 | ||||
|         $this->cache = $cache; | ||||
| 
 | ||||
|         #if ( ! $this->cache || ! $this->cache->has($class) ) {
 | ||||
|         $this->classReflection = $class instanceof ReflectionClass ? $class : new ReflectionClass($class); | ||||
|         # }
 | ||||
|     } | ||||
| 
 | ||||
|     public static function fromClass(ReflectionClass|string $class, ? CacheInterface $cache = null) : self | ||||
| @ -66,7 +63,6 @@ class ObjectReflection { | ||||
| 
 | ||||
|         foreach($this->classReflection->getProperties($filter) as $property) { | ||||
|             $reflected = new ReflectedProperty($property->getName()); | ||||
| 
 | ||||
|             $reflected->attributes = AttributeReader::reflectAttributes($property); | ||||
| 
 | ||||
|             if ( $reflected->hasIgnoreAttribute() ) { | ||||
| @ -87,11 +83,9 @@ class ObjectReflection { | ||||
|     } | ||||
| 
 | ||||
|     public function reflectMethods(int $filter = | ||||
|                                    ReflectionMethod::IS_PUBLIC    | | ||||
|                                    ReflectionMethod::IS_PROTECTED | | ||||
|                                    ReflectionMethod::IS_PRIVATE   | | ||||
|                                    ReflectionMethod::IS_STATIC    | | ||||
|                                    ReflectionMethod::IS_FINAL | ||||
|        ReflectionMethod::IS_PUBLIC  | ReflectionMethod::IS_PROTECTED | | ||||
|        ReflectionMethod::IS_PRIVATE | ReflectionMethod::IS_STATIC    | | ||||
|        ReflectionMethod::IS_FINAL | ||||
|     ) : array | ||||
|     { | ||||
|         $list = []; | ||||
|  | ||||
| @ -25,6 +25,11 @@ class Route implements \Notes\Attribute { | ||||
|                 "/" . ltrim($this->route, "/"), "/"), '/'); | ||||
|     } | ||||
| 
 | ||||
|     public function getRegistrableRoute() : string | ||||
|     { | ||||
|         return preg_replace('/(\=.*)(?=\})/i', '', $this->getRoute()); | ||||
|     } | ||||
| 
 | ||||
|     public function matchRouteName(string $name) : bool | ||||
|     { | ||||
|         return strtolower($this->name) === strtolower($name); | ||||
|  | ||||
| @ -14,11 +14,11 @@ class SecurityHandler { | ||||
| 
 | ||||
|     protected ResponseInterface $redirectResponse; | ||||
| 
 | ||||
|     protected ? \Closure $unauthorizeResponse; | ||||
|     protected \Closure|ResponseInterface|null $unauthorizeResponse; | ||||
| 
 | ||||
|     protected ? Taxus $taxus; | ||||
| 
 | ||||
|     public function __construct(ResponseInterface $redirectResponse, ? \Closure $unauthorizeResponse = null, ? Taxus $taxus = null) { | ||||
|     public function __construct(ResponseInterface $redirectResponse, \Closure|ResponseInterface|null $unauthorizeResponse = null, ? Taxus $taxus = null) { | ||||
|         $this->redirectResponse = $redirectResponse; | ||||
|         $this->unauthorizeResponse = $unauthorizeResponse; | ||||
|         $this->taxus = $taxus; | ||||
| @ -40,25 +40,41 @@ class SecurityHandler { | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     public function taxus(string $className, string $methodName, object $user = null) : ? ResponseInterface | ||||
|     public function taxus(string $className, string $methodName, ... $arguments) : ? ResponseInterface | ||||
|     { | ||||
|         $fromObject = $this->findAttributes(Attribute\Taxus::class, $className); | ||||
|         $fromMethod = $this->findAttributes(Attribute\Taxus::class, $className, $methodName); | ||||
| 
 | ||||
|         if ($fromMethod || $fromObject) { | ||||
|             if ( $this->taxusGrantPermission($fromMethod, $user) || $this->taxusGrantPermission($fromObject, $user) ) { | ||||
|                 return null; | ||||
|             } | ||||
|         $granted = $this->hasGrantPermission($className , $methodName, ...$arguments); | ||||
| 
 | ||||
|         if ($granted) { | ||||
|             return null; | ||||
|         } | ||||
|         else { | ||||
|             if ($this->unauthorizeResponse) { | ||||
|                 return call_user_func_array($this->unauthorizeResponse, [ $user, ['method' => $fromMethod, 'object' => $fromObject ], $className, $methodName ]); | ||||
|                 if ($this->unauthorizeResponse instanceof ResponseInterface) { | ||||
|                     return $this->unauthorizeResponse; | ||||
|                 } | ||||
| 
 | ||||
|                 return call_user_func_array($this->unauthorizeResponse, [ ...$arguments, ['method' => $methodName, 'object' => $className ], $className, $methodName ]); | ||||
|             } | ||||
|             else { | ||||
|                 throw new \ErrorException("Unauthorized response given."); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|         return null; | ||||
|     public function hasGrantPermission(string $className, string $methodName, ... $arguments) : bool | ||||
|     { | ||||
|         $fromObject = $this->findAttributes(Attribute\Taxus::class, $className); | ||||
|         $fromMethod = $this->findAttributes(Attribute\Taxus::class, $className, $methodName); | ||||
| 
 | ||||
|         if ($fromMethod || $fromObject) { | ||||
|             if ( $this->taxusGrantPermission($fromMethod, ... $arguments) || $this->taxusGrantPermission($fromObject, ... $arguments) ) { | ||||
|                 return true; | ||||
|             } | ||||
| 
 | ||||
|             return false; | ||||
|         } | ||||
| 
 | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     protected function findAttributes(string $attribute, string $class, ? string $method = null) : array | ||||
| @ -86,10 +102,10 @@ class SecurityHandler { | ||||
|         return []; | ||||
|     } | ||||
| 
 | ||||
|     protected function taxusGrantPermission(array $attributeList, object $user = null) : bool | ||||
|     protected function taxusGrantPermission(array $attributeList, ...$arguments) : bool | ||||
|     { | ||||
|         foreach ($attributeList as $item) { | ||||
|             if ( $grant = $this->taxus->granted($item->privilege, $user, $item) ) { | ||||
|             if ( $grant = $this->taxus->granted($item->privilege, ... array_merge($arguments, [ $item ])) ) { | ||||
|                 if (is_bool($grant) ? $grant : $grant === TaxusGrantEnum::Authorized) { | ||||
|                     return true; | ||||
|                 } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user