/** * 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; }
/** * Checks if a service exists * * @param string $name The name of the service * @param bool $resolve Specified if Loops should try to resolve unloaded services from the Loops\Service namespace. * @return bool TRUE if the service is registered (or can be created from the Loops\Service namespace if $resolve is TRUE, see getService for details) */ public function hasService($name, $resolve = TRUE) { if (array_key_exists($name, $this->services)) { return TRUE; } if (!$resolve) { return FALSE; } $classname = "Loops\\Service\\" . Misc::camelize($name); if (class_exists($classname)) { $reflection = new ReflectionClass($classname); if ($reflection->implementsInterface("Loops\\ServiceInterface")) { if (!$classname::hasService($this)) { return FALSE; } $this->registerService($name, $classname, [], $classname::isShared($this)); } } return $this->hasService($name, FALSE); }
public function offsetGet($key) { if (parent::offsetExists($key)) { return parent::offsetGet($key); } return $this->repository(Misc::camelize($key)); }
/** * Implements parsing of a modules action * * See class documentation for details */ protected function parse($strict = TRUE) { if (!($arguments = $this->flags->args())) { $message = "Module not specified.\n"; $message .= "\n"; $message .= "To get help type:\n"; $message .= "\n"; $message .= " {$this->command} " . implode(" ", array_merge($this->arguments, ["help"])); return $this->printError($message); } $module = array_shift($arguments); // check if we are in help mode $is_help = $module == "help"; if ($is_help) { if (!$arguments) { $modules = []; if (class_exists("Loops\\Application\\LoopsAdmin\\Cache")) { $modules[] = "cache"; } if (class_exists("Loops\\Application\\LoopsAdmin\\Jobs")) { $modules[] = "jobs"; } $message = "Welcome to the Loops admin.\n"; $message .= "\n"; $message .= "You can run Loops internal commands via this interface.\n"; $message .= "Loops internal commands are grouped into modules.\n"; $message .= "\n"; $message .= "Available (and known) modules:\n"; $message .= "\n"; $message .= $this->ident(implode(", ", $modules)) . "\n"; $message .= "\n"; $message .= "\n"; $message .= "To get help about a module:\n"; $message .= "\n"; $message .= " {$this->command} [<flags>...] help <module>\n"; $message .= "\n"; $message .= "Each module defines action(s) which are listed on the modules help page.\n"; $message .= "\n"; $message .= "\n"; $message .= "To get help about an action:\n"; $message .= "\n"; $message .= " {$this->command} [<flags>...] help <module> <action>\n"; $message .= "\n"; $message .= "\n"; $message .= "Available flags:"; return $this->printHelp($message); } // help for this module requested $module = array_shift($arguments); } $classname = "Loops\\Application\\LoopsAdmin\\" . Misc::camelize($module); if (!class_exists($classname)) { return $this->printError("Module not found: {$module}"); } $annotations = $this->getLoops()->getService("annotations")->get($classname); if (!$arguments) { if ($is_help) { if (!($help = $annotations->findFirst("Admin\\Help"))) { return $this->printError("Module '{$module}' does not define a help message."); } $actions = []; foreach ($annotations->methods as $method => $method_annotations) { if (!$method_annotations->findFirst("Admin\\Action")) { continue; } $actions[] = Misc::underscore($method); } $message = "Help for module '{$module}':\n"; $message .= "\n"; $message .= $this->ident($help->help) . "\n"; $message .= "\n"; $message .= "\n"; $message .= "Available actions:\n"; $message .= "\n"; $message .= $this->ident($actions ? implode(", ", $actions) : "No actions defined.") . "\n"; $message .= "\n"; $message .= "\n"; $message .= "To get help about an action:\n"; $message .= "\n"; $message .= " {$this->command} [<flags>...] help {$module} <action>\n"; $message .= "\n"; $message .= "\n"; $message .= "Available flags:"; return $this->printHelp($message); } else { return $this->printError("Action not specified for module '{$module}'."); } } $action = array_shift($arguments); $method = lcfirst(Misc::camelize($action)); if (empty($annotations->methods->{$method}) || !($action_annotation = $annotations->methods->{$method}->findFirst("Admin\\Action"))) { return $this->printError("Module '{$module}' does not support action: {$action}"); } $instance = new $classname($this->getLoops()); if ($action_annotation->init_flags) { if (!method_exists($instance, $action_annotation->init_flags)) { throw new Exception("Failed to init flags. Method '{$action_annotation->init}' is not defined."); } Misc::reflectionFunction([$instance, $action_annotation->init_flags], [$this->flags]); } if ($is_help) { if (!($help = $annotations->methods->{$method}->findFirst("Admin\\Help"))) { return $this->printError("Action '{$action}' of module '{$module}' does not define a help message."); } $message = "Usage:\n"; $message .= "\n"; $message .= " {$this->command} [<flags>...] {$module} {$action}" . ($action_annotation->arguments ? " {$action_annotation->arguments}" : "") . "\n"; $message .= "\n"; $message .= "\n"; $message .= "Help for action '{$module} {$action}':\n"; $message .= "\n"; $message .= $this->ident($help->help) . "\n"; $message .= "\n"; $message .= "\n"; $message .= "Available flags:"; return $this->printHelp($message); } try { $result = $this->flags->parse($this->arguments, !$strict, FALSE); } catch (Exception $e) { $message = $e->getMessage() . "\n"; $message .= "\n"; $message .= "To get help about this action:\n"; $message .= "\n"; $message .= " {$this->command} help {$module} {$action}"; return $this->printError($message); } $result = parent::parse(); if (is_integer($result)) { $this->printError("test"); return $result; } $result["__arguments"] = $arguments; $result["__module"] = $module; $result["__action"] = $action; $result["__instance"] = $instance; $result["__method"] = $method; return $result; }
public function testCamelize() { $this->assertEquals("Ucfirst", Misc::camelize("ucfirst")); $this->assertEquals("CamelCase", Misc::camelize("camel_case")); $this->assertEquals("ALongerExample", Misc::camelize("a_longer_example")); $this->assertEquals("Camel_Case", Misc::camelize("camel__case")); $this->assertEquals("CamelCase_", Misc::camelize("camel_case_")); }
public function __get($key) { if (!$this->swiftclassname) { $key = Misc::camelize($key); return new Swiftmailer($this->config, "Swift_{$key}"); } }