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>
35 using namespace Cutelyst;
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(
QLatin1String(
"Private"))) {
65 if (
dispatch->registerAction(action)) {
80 d->actionContainer[action->ns()] << action;
81 registeredActions.
append(action);
84 qCDebug(CUTELYST_DISPATCHER) <<
"The action" << action->name() <<
"of"
85 << action->controller()->objectName()
86 <<
"controller was not registered in any dispatcher."
87 " If you still want to access it internally (via actionFor())"
88 " you may make it's method private.";
93 d->controllers.insert(controller->objectName(), controller);
102 d->rootActions = d->actionContainer.value(
QLatin1String(
""));
105 controller->d_ptr->setupFinished();
110 while (i < d->dispatchers.
size()) {
112 if (!type->
inUse()) {
113 d->dispatchers.removeAt(i);
122 qCDebug(CUTELYST_DISPATCHER) <<
dispatch->list().constData();
129 Action *action = c->action();
133 const QString path = c->req()->path();
135 c->
error(c->
translate(
"Cutelyst::Dispatcher",
"No default action defined"));
137 c->
error(c->
translate(
"Cutelyst::Dispatcher",
"Unknown resource '%1'.").
arg(path));
155 Action *action = d->command2Action(c, opname, c->request()->args());
160 qCCritical(CUTELYST_DISPATCHER) <<
"Action not found" << opname << c->request()->args();
168 Request *request = c->request();
169 d->prepareAction(c, request->path());
171 static const auto &log = CUTELYST_DISPATCHER();
172 if (log.isDebugEnabled()) {
173 if (!request->match().
isEmpty()) {
174 qCDebug(log) <<
"Path is" << request->match();
177 if (!request->args().
isEmpty()) {
178 qCDebug(log) <<
"Arguments are" << request->args().
join(
QLatin1Char(
'/'));
183 void DispatcherPrivate::prepareAction(
Context *c,
const QString &requestPath)
const
185 QString path = normalizePath(requestPath);
196 if (type->match(c, path, args) == DispatchType::ExactMatch) {
227 const QString ns = DispatcherPrivate::cleanNamespace(nameSpace);
242 return d->actions.value(_path);
255 const QString ns = DispatcherPrivate::cleanNamespace(nameSpace);
256 const ActionList containers = d->getContainers(ns);
257 auto rIt = containers.
rbegin();
258 while (rIt != containers.
rend()) {
259 if ((*rIt)->name() == name) {
270 return d->controllers;
281 ret = QStringLiteral(
"/");
294 if (expandedAction) {
295 return expandedAction;
304 return d->dispatchers;
310 bool lastWasSlash =
true;
311 int nsSize = ns.
size();
312 for (
int i = 0; i < nsSize; ++i) {
325 lastWasSlash =
false;
334 bool lastSlash =
true;
336 while (i < ret.
size()) {
355 void DispatcherPrivate::printActions()
const
360 keys.
sort(Qt::CaseInsensitive);
361 for (
const QString &key : keys) {
362 Action *action = actions.value(key);
375 qCDebug(CUTELYST_DISPATCHER) << Utils::buildTable(table, {
392 ret.
append(actionContainer.value(ns.
mid(0, pos)));
404 auto it = actions.constFind(command);
405 if (it != actions.constEnd()) {
409 return invokeAsPath(c, command, args);
417 QString path = DispatcherPrivate::actionRel2Abs(c, relativePath);
420 int lastPos = path.
size();
423 ret = q->getAction(path,
QString());
428 const QString name = path.
mid(pos + 1, lastPos);
429 path = path.
mid(0, pos);
430 ret = q->getAction(name, path);
460 #include "moc_dispatcher.cpp"
bool dispatch(Context *c)
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QMap< QString, Controller * > controllers() const
bool dispatch(Context *c)
void append(const T &value)
QString & prepend(QChar ch)
void insert(int i, const T &value)
bool forward(Context *c, Component *component)
Controller * controller() const
QString join(const QString &separator) const
void prepareAction(Context *c)
QString & remove(int position, int n)
bool error() const
Returns true if an error was set.
QStack< Component * > stack() const
Action * getActionByPath(const QString &path) const
The Cutelyst Component base class.
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
This class represents a Cutelyst Action.
Cutelyst Controller base class
void append(const T &value)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
ActionList getActions(const QString &name, const QString &nameSpace) const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
bool _DISPATCH(Context *c)
bool execute(Component *code)
Action * getAction(const QString &name, const QString &nameSpace=QString()) const
QString mid(int position, int n) const
const QChar at(int position) const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Action * expandAction(const Context *c, Action *action) const
void sort(Qt::CaseSensitivity cs)
void prepend(const T &value)
QVector< DispatchType * > dispatchers() const
reverse_iterator rbegin()
virtual Action * expandAction(const Context *c, Action *action) const
Dispatcher(QObject *parent=nullptr)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
QString className() const
QString uriForAction(Action *action, const QStringList &captures) const
virtual QString uriForAction(Action *action, const QStringList &captures) const =0
void setupActions(const QVector< Controller * > &controllers, const QVector< DispatchType * > &dispatchers, bool printActions)