5 #include "dispatcher_p.h" 8 #include "application.h" 11 #include "controller.h" 12 #include "controller_p.h" 14 #include "request_p.h" 15 #include "dispatchtypepath.h" 16 #include "dispatchtypechained.h" 20 #include <QMetaMethod> 25 , d_ptr(new DispatcherPrivate(this))
31 Dispatcher::~Dispatcher()
44 bool instanceUsed =
false;
45 const auto actions = controller->actions();
46 for (
Action *action : actions) {
47 bool registered =
false;
48 if (!d->actions.contains(action->reverse())) {
49 if (!action->attributes().contains(QStringLiteral(
"Private"))) {
52 if (
dispatch->registerAction(action)) {
67 d->actions.insert(name, { name, action });
68 d->actionContainer[action->ns()] << action;
69 registeredActions.
append(action);
72 qCDebug(CUTELYST_DISPATCHER) <<
"The action" << action->name() <<
"of" 73 << action->controller()->objectName()
74 <<
"controller was not registered in any dispatcher." 75 " If you still want to access it internally (via actionFor())" 76 " you may make it's method private.";
81 d->controllers.insert(controller->objectName(), controller);
93 controller->d_ptr->setupFinished();
100 if (!type->
inUse()) {
101 d->dispatchers.removeAt(i);
110 qCDebug(CUTELYST_DISPATCHER) <<
dispatch->list().constData();
117 Action *action = c->action();
121 const QString path = c->req()->path();
123 c->
error(c->
translate(
"Cutelyst::Dispatcher",
"No default action defined"));
125 c->
error(c->
translate(
"Cutelyst::Dispatcher",
"Unknown resource '%1'.").
arg(path));
143 Action *action = d->command2Action(c, opname, c->request()->args());
148 qCCritical(CUTELYST_DISPATCHER) <<
"Action not found" << opname << c->request()->args();
156 Request *request = c->request();
157 d->prepareAction(c, request->path());
159 static const auto &log = CUTELYST_DISPATCHER();
160 if (log.isDebugEnabled()) {
161 if (!request->match().
isEmpty()) {
162 qCDebug(log) <<
"Path is" << request->match();
165 if (!request->args().
isEmpty()) {
166 qCDebug(log) <<
"Arguments are" << request->args().
join(
QLatin1Char(
'/'));
171 void DispatcherPrivate::prepareAction(
Context *c,
const QString &requestPath)
const 173 QString path = normalizePath(requestPath);
184 if (type->match(c, path, args) == DispatchType::ExactMatch) {
213 return d->actions.value(normName).action;
216 const QString ns = DispatcherPrivate::cleanNamespace(nameSpace);
231 return d->actions.value(_path).action;
244 const QString ns = DispatcherPrivate::cleanNamespace(nameSpace);
245 const ActionList containers = d->getContainers(ns);
246 auto rIt = containers.
rbegin();
247 while (rIt != containers.
rend()) {
248 if ((*rIt)->name() == name) {
259 return d->controllers;
267 ret =
dispatch->uriForAction(action, captures);
270 ret = QStringLiteral(
"/");
283 if (expandedAction) {
284 return expandedAction;
293 return d->dispatchers;
299 bool lastWasSlash =
true;
300 int nsSize = ns.
size();
301 for (
int i = 0; i < nsSize; ++i) {
314 lastWasSlash =
false;
323 bool lastSlash =
true;
325 while (i < ret.
size()) {
344 void DispatcherPrivate::printActions()
const 348 auto keys = actions.keys();
349 std::sort(keys.begin(), keys.end());
350 for (
const auto &key : keys) {
351 Action *action = actions.value(key).action;
364 qCDebug(CUTELYST_DISPATCHER) << Utils::buildTable(table, {
381 ret.
append(actionContainer.value(ns.
mid(0, pos)));
393 auto it = actions.constFind(command);
394 if (it != actions.constEnd()) {
395 return it.value().action;
398 return invokeAsPath(c, command, args);
407 QString path = DispatcherPrivate::actionRel2Abs(c, relativePath);
410 int lastPos = path.
size();
413 ret = q->getAction(path,
QString());
418 const QString name = path.
mid(pos + 1, lastPos);
419 path = path.
mid(0, pos);
420 ret = q->getAction(name, path);
450 #include "moc_dispatcher.cpp" bool dispatch(Context *c)
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString uriForAction(Action *action, const QStringList &captures) const
bool dispatch(Context *c)
void setupActions(const QVector< Controller *> &controllers, const QVector< DispatchType *> &dispatchers, bool printActions)
bool error() const noexcept
Returns true if an error was set.
void append(const T &value)
QString & prepend(QChar ch)
QVector::reverse_iterator rend()
bool forward(Context *c, Component *component)
QString join(const QString &separator) const const
void prepareAction(Context *c)
QString & remove(int position, int n)
QVector< DispatchType * > dispatchers() const
The Cutelyst Component base class.
ActionList getActions(const QString &name, const QString &nameSpace) const
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
bool isNull() const const
This class represents a Cutelyst Action.
Cutelyst Controller base class
void append(const T &value)
QMap< QString, Controller * > controllers() const
bool isEmpty() const const
bool isEmpty() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
bool _DISPATCH(Context *c)
Controller * controller() const
The Cutelyst namespace holds all public Cutelyst API.
bool execute(Component *code)
QStack< Component * > stack() const noexcept
QString mid(int position, int n) const const
Action * expandAction(const Context *c, Action *action) const
const QChar at(int position) const const
Action * getAction(const QString &name, const QString &nameSpace=QString()) const
void prepend(const T &value)
QVector::reverse_iterator rbegin()
Action * getActionByPath(const QString &path) const
QObject * parent() const const
Dispatcher(QObject *parent=nullptr)
int compare(const QString &other, Qt::CaseSensitivity cs) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString className() const