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()
49 void Dispatcher::setupActions(
const QVector<Controller*> &controllers,
const QVector<Cutelyst::DispatchType *> &dispatchers,
bool printActions)
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)) {
79 d->actions.insert(action->ns() +
QLatin1Char(
'/') + action->name(), 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();
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();
134 if (path.isEmpty()) {
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) {
202 if (path.isEmpty()) {
208 const QString arg = path.mid(pos + 1);
219 if (name.isEmpty()) {
223 if (nameSpace.isEmpty()) {
227 const QString ns = DispatcherPrivate::cleanNamespace(nameSpace);
235 QString _path = path;
239 }
else if (_path.startsWith(
QLatin1Char(
'/')) && slashes != 1) {
242 return d->actions.value(_path);
251 if (name.isEmpty()) {
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;
278 ret =
dispatch->uriForAction(action, captures);
281 ret = QStringLiteral(
"/");
294 if (expandedAction) {
295 return expandedAction;
304 return d->dispatchers;
307 QString DispatcherPrivate::cleanNamespace(
const QString &ns)
310 bool lastWasSlash =
true;
311 int nsSize = ns.size();
312 for (
int i = 0; i < nsSize; ++i) {
325 lastWasSlash =
false;
331 QString DispatcherPrivate::normalizePath(
const QString &path)
334 bool lastSlash =
true;
336 while (i < ret.size()) {
350 ret.resize(ret.size() - 1);
355 void DispatcherPrivate::printActions()
const 357 QVector<QStringList> table;
360 keys.
sort(Qt::CaseInsensitive);
361 for (
const QString &key : keys) {
362 Action *action = actions.value(key);
371 row.append(action->
name());
375 qCDebug(CUTELYST_DISPATCHER) << Utils::buildTable(table, {
383 ActionList DispatcherPrivate::getContainers(
const QString &ns)
const 392 ret.append(actionContainer.value(ns.mid(0, pos)));
397 ret.append(rootActions);
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);
443 QString DispatcherPrivate::actionRel2Abs(
Context *c,
const QString &path)
451 const QString ns = qobject_cast<Action *>(c->
stack().constLast())->ns();
460 #include "moc_dispatcher.cpp" bool dispatch(Context *c)
QString uriForAction(Action *action, const QStringList &captures) const
QVector< Action * > ActionList
bool dispatch(Context *c)
bool forward(Context *c, Component *component)
QString join(const QString &sep) const const
void prepareAction(Context *c)
QVector< DispatchType * > dispatchers() const
The Cutelyst Component base class.
ActionList getActions(const QString &name, const QString &nameSpace) const
This class represents a Cutelyst Action.
QStack< Component * > stack() const
Cutelyst Controller base class
QMap< QString, Controller * > controllers() const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
bool _DISPATCH(Context *c)
Controller * controller() const
The Cutelyst namespace holds all public Cutelyst API.
bool execute(Component *code)
Action * expandAction(const Context *c, Action *action) const
int lastIndexOf(const int &rx, int from) const const
Action * getAction(const QString &name, const QString &nameSpace=QString()) const
Action * getActionByPath(const QString &path) const
Dispatcher(QObject *parent=nullptr)
bool error() const
Returns true if an error was set.
void setupActions(const QVector< Controller * > &controllers, const QVector< DispatchType * > &dispatchers, bool printActions)
QString className() const