/** * Creates the logging service. * * A logger name can be specified in config variable 'plugin'. * An instance of "Loops\Logger\%Logger" will be attached to this logger * with % being the camelized version of the loggers name. * An array can also be passed to attach multiple loggers. * Alternatively multiple loggers can be passed as a comma separated * string. * Other configuration parameters are passed to the constructor(s) of the * logging class(es). */ public static function getService(ArrayObject $config, Loops $loops) { $logger = parent::getService($config, $loops); $plugin = $config->offsetExists("plugin") ? $config->offsetGet("plugin") : "stderr"; foreach (array_filter(is_array($plugin) ? $plugin : explode(",", $plugin)) as $plugin) { $classname = "Loops\\Logger\\" . Misc::camelize($plugin) . "Logger"; $logger->attach(Misc::reflectionInstance($classname, $config)); } return $logger; }
public static function getService(ArrayObject $config, Loops $loops) { $config = static::getConfig($loops, $config); $classname = str_replace("%", $config->plugin, static::getClassname($loops)); if (!class_exists($classname)) { throw new Exception("Class '{$classname}' is requested by '" . get_called_class() . "' but it was not found."); } $reflection = new ReflectionClass($classname); $interface = static::getInterface($loops); if (!$reflection->implementsInterface($interface)) { throw new Exception("Class '{$classname}' must implement '{$interface}'."); } return Misc::reflectionInstance($classname, $config); }
public function getAddForm() { if (is_string($this->persist)) { $arguments["entity"] = $this->entity; $arguments["filter"] = $this->persist_filter; $arguments["fields"] = $this->persist_fields; $arguments["context"] = $this; $arguments["loops"] = $this->getLoops(); return Misc::reflectionInstance($this->persist, $arguments); } if ($this->persist) { if ($this->persist instanceof Form) { return $this->persist; } return new PersistEntityForm($this->entity, $this->persist_filter, $this->persist_fields, $this, $this->getLoops()); } }
private function resolveOutput($url, $is_ajax) { $loops = $this->getLoops(); foreach ($this->resolvePage($url) as $set) { list($pageclass, $this->page_parameter, $this->parameter) = $set; $this->page = $pageclass; $this->page = Misc::reflectionInstance($pageclass, ["parameter" => $this->page_parameter, "loops" => $loops]); $action_result = $this->page->action($this->parameter); if (is_string($action_result)) { return $action_result; } if ($action_result === TRUE) { return $this->page; } if (is_object($action_result)) { return $is_ajax ? $action_result : $this->page; } if (is_integer($action_result)) { if ($action_result == 404) { continue; } if ($action_result <= 0) { return ""; } $loops->getService("response")->setStatusCode($action_result); $this->page = Misc::reflectionInstance($this->error_page, ['status_code' => $action_result, 'loops' => $loops]); return $this->page; } } $this->page_parameter = []; $this->parameter = $url == "/" ? [] : explode("/", ltrim($url, "/")); $this->page = Misc::reflectionInstance($this->error_page, ['status_code' => 404, 'loops' => $loops]); $loops->getService("response")->setStatusCode(404); return $this->page; }
/** * Creates a new instance of a service * * @param $name The service name * @param array $config * @return Loops\Service\ServiceInterface The new service */ public function createService($name, ArrayObject $config = NULL, $merge_into_config = FALSE) { if ($merge_into_config) { $service_config = $this->config->offsetExists($name) ? $this->config->offsetGet($name) : new ArrayObject(); if (is_array($service_config)) { $service_config = ArrayObject::fromArray($service_config); } if ($config) { $service_config->merge($config); } } else { $service_config = $config ?: new ArrayObject(); } if (!$this->hasService($name)) { throw new Exception("Service '{$name}' does not exist."); } $service = $this->services[$name]; if (array_key_exists("classname", $service)) { $reflection = new ReflectionClass($service["classname"]); $params = new ArrayObject($service["params"]); $params->merge($service_config); if ($reflection->implementsInterface("Loops\\ServiceInterface")) { return call_user_func_array([$service["classname"], "getService"], [$params, $this]); } else { $params->offsetSet("loops", $this); return Misc::reflectionInstance($service["classname"], $params); } } if (array_key_exists("callback", $service)) { $params = new ArrayObject($service["params"]); $params->merge($service_config); return call_user_func_array($service["callback"], $params->toArray()); } if (array_key_exists("object", $service)) { return $service["object"]; } }
private function getJobs() { $loops = $this->getLoops(); $application = $loops->getService("application"); $classnames = array_filter($application->definedClasses(), function ($classname) { if (!class_exists($classname)) { return FALSE; } $reflection = new ReflectionClass($classname); if ($reflection->isAbstract()) { return FALSE; } if (!$reflection->implementsInterface("Loops\\Jobs\\JobInterface")) { return FALSE; } return TRUE; }); $jobs = []; foreach ($classnames as $classname) { if (substr($classname, 0, 5) == "Jobs\\") { $name = substr($classname, 5); } $job = Misc::reflectionInstance($classname, ["loops" => $loops]); $jobs[$name] = $job; } return $jobs; }
public function testReflectionInstance_setexclude() { $object = Misc::reflectionInstance("LoopsMiscTest_Mock4", ["arg1" => "test1", "param1" => "test", "param2" => "test", "param3" => "test"], TRUE, FALSE, ["param2"]); $this->assertEquals("test", $object->param1); $this->assertNull($object->param2); $this->assertEquals("test", $object->param3); }
public function factory($context = NULL, Loops $loops = NULL) { if ($this->type == "CALLBACK") { if (!$context) { throw new Exception("Context needed for callback based annotation."); } $callback = [$context, $this->class]; if (!is_callable($callback)) { throw new Exception("Can't call method '{$this->class}' on object of class '" . get_class($context) . "'."); } $arguments = array_values(array_merge($this->arguments, [$context, $loops])); return call_user_func_array($callback, $arguments); } elseif ($this->type == "PROPERTY") { if (!$context) { throw new Exception("Context needed for property based annotation."); } $key = $this->class; if ($context instanceof ArrayAcces) { return $context->offsetGet($this->class); } elseif (property_exists($context, $this->class) || method_exists($context, "__get")) { return $context->{$key}; } throw new Exception("Property '{$key}' does not exist on object of class '" . get_class($context) . "'."); } elseif ($this->delegate_annotation_namespace && !$this->delegating) { $annotation_classname = $this->delegate_annotation_namespace . $this->class; if (class_exists($annotation_classname)) { $this->delegating = TRUE; $vars = get_object_vars($this); $reflection = new ReflectionClass($annotation_classname); if ($reflection->getConstructor()) { $vars = array_merge($vars, $this->arguments); $vars["arguments"] = []; $annotation = $reflection->newInstance($vars); } else { $annotation = $reflection->newInstance(); foreach ($vars as $key => $value) { if (!property_exists($annotation, $key)) { continue; } $annotation->{$key} = $value; } } $result = $annotation->factory($context, $loops); $this->delegating = FALSE; return $result; } } $arguments = $this->arguments; self::transformAnnotations($arguments, $context, $loops); $arguments["context"] = $context; $arguments["loops"] = $loops; return Misc::reflectionInstance($this->getClassname($context), new ArrayObject($arguments), TRUE, FALSE, ["context", "loops"]); }