isDot() || str_starts_with($value->getFilename(), '.')) { continue; } if ($value->isDir()) { if (in_array($value->getRealPath() . '/', $skip)) { continue; } $this->load_directory($value->getRealPath()); } else if ($value->getExtension() == 'php') { $this->load_file($value->getRealPath()); } } } /** * @param string $file * @return string */ private function rename(string $file): string { $filter = array_filter(explode('/', $file), function ($value) { if (empty($value)) { return false; } return ucfirst($value); }); return ucfirst(implode('\\', $filter)); } /** * @param string $path * @return void * @throws */ private function load_file(string $path): void { try { require_once "$path"; $path = str_replace($_SERVER['PWD'], '', $path); $path = str_replace('.php', '', $path); $this->parseFile($path); } catch (\Throwable $throwable) { error($throwable); } } /** * @param $file * @return void * @throws */ protected function parseFile($file): void { $class = $this->rename($file); if (class_exists($class)) { $reflect = $this->container->getReflectionClass($class); if ($reflect->isInstantiable()) { $attributes = $this->skipNames($reflect); if (in_array(Skip::class, $attributes) || in_array(\Attribute::class, $attributes)) { return; } $object = $this->container->parse($class); foreach ($reflect->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { if ($method->isStatic() || $method->getDeclaringClass()->getName() != $class) { continue; } $attributes = $method->getAttributes(); foreach ($attributes as $attribute) { if (!class_exists($attribute->getName())) { continue; } $attribute->newInstance()->dispatch($object, $method->getName()); } } } } } /** * @param ReflectionClass $reflect * @return array */ protected function skipNames(ReflectionClass $reflect): array { $attributes = $reflect->getAttributes(); $names = []; foreach ($attributes as $attribute) { $names[] = $attribute->getName(); } return $names; } }