18 #include "dispatcher_p.h"
21 #include "application.h"
24 #include "controller.h"
25 #include "controller_p.h"
27 #include "request_p.h"
28 #include "dispatchtypepath.h"
29 #include "dispatchtypechained.h"
33 #include <QMetaMethod>
38 , d_ptr(new DispatcherPrivate(this))
44 Dispatcher::~Dispatcher()
57 bool instanceUsed =
false;
58 const auto actions = controller->actions();
59 for (
Action *action : actions) {
60 bool registered =
false;
61 if (!d->actions.contains(action->reverse())) {
62 if (!action->attributes().contains(QStringLiteral(
"Private"))) {
65 if (
dispatch->registerAction(action)) {
80 d->actions.insert(name, { name, action });
81 d->actionContainer[action->ns()] << action;
82 registeredActions.
append(action);
85 qCDebug(CUTELYST_DISPATCHER) <<
"The action" << action->name() <<
"of"
86 << action->controller()->objectName()
87 <<
"controller was not registered in any dispatcher."
88 " If you still want to access it internally (via actionFor())"
89 " you may make it's method private.";
94 d->controllers.insert(controller->objectName(), controller);
103 d->rootActions = d->actionContainer.value(
QLatin1String(
""));
106 controller->d_ptr->setupFinished();
113 if (!type->
inUse()) {
114 d->dispatchers.removeAt(i);
123 qCDebug(CUTELYST_DISPATCHER) <<
dispatch->list().constData();
130 Action *action = c->action();
134 const QString path = c->req()->path();
136 c->
error(c->
translate(
"Cutelyst::Dispatcher",
"No default action defined"));
138 c->
error(c->
translate(
"Cutelyst::Dispatcher",
"Unknown resource '%1'.").
arg(path));
156 Action *action = d->command2Action(c, opname, c->request()->args());
161 qCCritical(CUTELYST_DISPATCHER) <<
"Action not found" << opname << c->request()->args();
169 Request *request = c->request();
170 d->prepareAction(c, request->path());
172 static const auto &log = CUTELYST_DISPATCHER();
173 if (log.isDebugEnabled()) {
174 if (!request->match().
isEmpty()) {
175 qCDebug(log) <<
"Path is" << request->match();
178 if (!request->args().
isEmpty()) {
179 qCDebug(log) <<
"Arguments are" << request->args().
join(
QLatin1Char(
'/'));
184 void DispatcherPrivate::prepareAction(
Context *c,
const QString &requestPath)
const
186 QString path = normalizePath(requestPath);
197 if (type->match(c, path, args) == DispatchType::ExactMatch) {
226 return d->actions.value(normName).action;
229 const QString ns = DispatcherPrivate::cleanNamespace(nameSpace);
244 return d->actions.value(_path).action;
257 const QString ns = DispatcherPrivate::cleanNamespace(nameSpace);
258 const ActionList containers = d->getContainers(ns);
259 auto rIt = containers.
rbegin();
260 while (rIt != containers.
rend()) {
261 if ((*rIt)->name() == name) {
272 return d->controllers;
280 ret =
dispatch->uriForAction(action, captures);
283 ret = QStringLiteral(
"/");
296 if (expandedAction) {
297 return expandedAction;
306 return d->dispatchers;
312 bool lastWasSlash =
true;
313 int nsSize = ns.
size();
314 for (
int i = 0; i < nsSize; ++i) {
327 lastWasSlash =
false;
336 bool lastSlash =
true;
338 while (i < ret.
size()) {
357 void DispatcherPrivate::printActions()
const
361 auto keys = actions.keys();
362 std::sort(keys.begin(), keys.end());
363 for (
const auto &key : keys) {
364 Action *action = actions.value(key).action;
377 qCDebug(CUTELYST_DISPATCHER) << Utils::buildTable(table, {
394 ret.
append(actionContainer.value(ns.
mid(0, pos)));
406 auto it = actions.constFind(command);
407 if (it != actions.constEnd()) {
408 return it.value().action;
411 return invokeAsPath(c, command, args);
420 QString path = DispatcherPrivate::actionRel2Abs(c, relativePath);
423 int lastPos = path.
size();
426 ret = q->getAction(path,
QString());
431 const QString name = path.
mid(pos + 1, lastPos);
432 path = path.
mid(0, pos);
433 ret = q->getAction(name, path);
454 const QString ns = qobject_cast<Action *>(c->
stack().constLast())->ns();
463 #include "moc_dispatcher.cpp"
This class represents a Cutelyst Action.
bool dispatch(Context *c)
QString className() const
Controller * controller() const
The Cutelyst Component base class.
QStack< Component * > stack() const noexcept
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
bool execute(Component *code)
bool error() const noexcept
Returns true if an error was set.
Cutelyst Controller base class
bool _DISPATCH(Context *c)
QMap< QString, Controller * > controllers() const
void setupActions(const QVector< Controller * > &controllers, const QVector< DispatchType * > &dispatchers, bool printActions)
Action * getAction(const QString &name, const QString &nameSpace=QString()) const
QVector< DispatchType * > dispatchers() const
Action * getActionByPath(const QString &path) const
QString uriForAction(Action *action, const QStringList &captures) const
bool forward(Context *c, Component *component)
Dispatcher(QObject *parent=nullptr)
ActionList getActions(const QString &name, const QString &nameSpace) const
void prepareAction(Context *c)
bool dispatch(Context *c)
Action * expandAction(const Context *c, Action *action) const
The Cutelyst namespace holds all public Cutelyst API.
void append(const T &value)
bool isEmpty() const const
void prepend(const T &value)
QObject * parent() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
const QChar at(int position) const const
int compare(const QString &other, Qt::CaseSensitivity cs) const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
bool isNull() const const
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString mid(int position, int n) const const
QString & prepend(QChar ch)
QString & remove(int position, int n)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString join(const QString &separator) const const
void append(const T &value)
QVector::reverse_iterator rbegin()
QVector::reverse_iterator rend()