/** * @param string $class_name */ protected function getClass($class_name, $subclass_of = 'Cyan\\Framework\\Controller', array $arguments = [], \Closure $newInstance = null) { $required_traits = ['Cyan\\Framework\\TraitSingleton']; $reflection_class = new ReflectionClass($class_name); foreach ($required_traits as $required_trait) { if (!in_array($required_trait, $reflection_class->getTraitNames())) { throw new TraitException(sprintf('%s class must use %s', $class_name, $required_trait)); } } if (!is_subclass_of($class_name, $subclass_of)) { throw new TraitException(sprintf('%s class must be a instance of %s', $class_name, $subclass_of)); } if (is_callable($newInstance)) { $instance = call_user_func_array($newInstance, $arguments); } else { $instance = !empty($arguments) ? call_user_func_array([$class_name, 'getInstance'], $arguments) : $class_name::getInstance(); } if ($this->hasContainer('application')) { if (!$instance->hasContainer('application')) { $instance->setContainer('application', $this->getContainer('application')); } if (!$instance->hasContainer('factory_plugin')) { $instance->setContainer('factory_plugin', $this->getContainer('application')->getContainer('factory_plugin')); } } if (is_callable([$instance, 'initialize']) || method_exists($instance, 'initialize')) { $instance->initialize(); } return $instance; }
/** * Get Path * * @return string * * @since 1.0.0 */ public function getBasePath() { if (empty($this->base_path)) { $reflection_class = new ReflectionClass($this); $file_path = explode(DIRECTORY_SEPARATOR, dirname($reflection_class->getFileName())); $base_path = array_filter(array_slice($file_path, 0, array_search($this->getComponentName(), $file_path) + 1)); $base_path_prefix = ''; if (dirname($reflection_class->getFileName())[0] == DIRECTORY_SEPARATOR) { $base_path_prefix = DIRECTORY_SEPARATOR; } $this->base_path = $base_path_prefix . implode(DIRECTORY_SEPARATOR, $base_path); unset($reflection_class); } return $this->base_path; }
/** * Return name * * @return string * * @since 1.0.0 */ public function getName() { if (empty($this->name)) { $reflection = new ReflectionClass(__CLASS__); $self_class_name = strtolower($reflection->getShortName()); $class_parts = explode('_', Inflector::underscore(get_called_class())); $class_name = implode(array_slice($class_parts, array_search($self_class_name, $class_parts) + 1)); if (empty($class_name)) { $remove_parts = []; $remove_parts[] = $self_class_name; $remove_parts[] = strtolower($this->getContainer('application')->getName()); $class_name = implode(array_unique(array_diff($class_parts, $remove_parts))); } $this->name = $class_name; } return $this->name; }
/** * @param $class_name * @param $class_path * @param string $instance_of */ protected function checkClass($class_name, $class_path, $instance_of = 'Cyan\\Framework\\Controller') { if (!class_exists($class_name)) { throw new ApplicationException(sprintf('Class "%s" not found in %s', $class_name, $class_path)); } $required_traits = ['Cyan\\Framework\\TraitSingleton']; $reflection_class = new ReflectionClass($class_name); foreach ($required_traits as $required_trait) { if (!in_array($required_trait, $reflection_class->getTraitNames())) { throw new ApplicationException(sprintf('%s class must use %s', $class_name, $required_trait)); } } if (!is_subclass_of($class_name, $instance_of)) { throw new ApplicationException(sprintf('%s class must be a instance of %s', $class_name, $instance_of)); } }