6#include "application.h"
10#include "controller_p.h"
11#include "dispatcher_p.h"
12#include "dispatchtypechained.h"
13#include "dispatchtypepath.h"
25 , d_ptr(new DispatcherPrivate(this))
46 bool instanceUsed =
false;
48 for (
Action *action : actions) {
49 bool registered =
false;
50 if (!d->actions.contains(action->reverse())) {
51 if (!action->attributes().contains(QStringLiteral(
"Private"))) {
54 if (
dispatch->registerAction(action)) {
69 d->actions.insert(name, {name, action});
70 auto it = d->actionContainer.find(action->ns());
71 if (it != d->actionContainer.end()) {
72 it->actions << action;
74 d->actionContainer.insert(action->ns(), {action->ns(), {action}});
77 registeredActions.
append(action);
80 qCDebug(CUTELYST_DISPATCHER)
81 <<
"The action" << action->name() <<
"of" << action->controller()->objectName()
82 <<
"controller was not registered in any dispatcher."
83 " If you still want to access it internally (via actionFor())"
84 " you may make it's method private.";
98 d->rootActions = d->actionContainer.value(u
"").actions;
101 controller->d_ptr->setupFinished();
106 while (i < d->dispatchers.size()) {
108 if (!type->
inUse()) {
109 d->dispatchers.removeAt(i);
118 qCDebug(CUTELYST_DISPATCHER) << dispatch->
list().
constData();
153 Action *action = d->command2Action(c, opname, c->
request()->args());
158 qCCritical(CUTELYST_DISPATCHER) <<
"Action not found" << opname << c->
request()->args();
167 d->prepareAction(c, request->path());
169 static const bool log = CUTELYST_DISPATCHER().isDebugEnabled();
171 if (!request->match().
isEmpty()) {
172 qCDebug(CUTELYST_DISPATCHER) <<
"Path is" << request->match();
175 if (!request->args().
isEmpty()) {
176 qCDebug(CUTELYST_DISPATCHER) <<
"Arguments are" << request->args().
join(u
'/');
194 if (type->
match(c, path, args) == DispatchType::ExactMatch) {
225 const QString normName = u
'/' + name;
226 return d->actions.value(normName).action;
236 int slashes = path.
count(u
'/');
238 return d->actions.value(
QString{u
'/' + path}).action;
239 }
else if (path.
startsWith(u
'/') && slashes != 1) {
240 return d->actions.value(path.
mid(1)).action;
242 return d->actions.value(path).action;
255 const ActionList containers = d->getContainers(nameSpace);
256 auto rIt = containers.
rbegin();
257 while (rIt != containers.
rend()) {
258 if ((*rIt)->name() == name) {
269 return d->controllers.value(name).controller;
276 for (
const auto &value : d->controllers) {
277 ret.
append(value.controller);
286 if (Q_UNLIKELY(action ==
nullptr)) {
287 qCCritical(CUTELYST_DISPATCHER) <<
"Dispatcher::uriForAction called with null action";
291 ret =
dispatch->uriForAction(action, captures);
308 if (expandedAction) {
309 return expandedAction;
318 return d->dispatchers;
321void DispatcherPrivate::printActions()
const
325 auto keys = actions.keys();
326 std::sort(keys.begin(), keys.end());
327 for (
const auto &key : keys) {
328 Action *action = actions.value(key).action;
341 qCDebug(CUTELYST_DISPATCHER) << Utils::buildTable(table,
358 ret.
append(actionContainer.value(ns.
mid(0, pos)).actions);
372 auto it = actions.constFind(command);
373 if (it != actions.constEnd()) {
374 return it.value().action;
377 return invokeAsPath(c, command, args);
388 const QString path = DispatcherPrivate::actionRel2Abs(c, relativePath);
392 int lastPos = pathView.size();
395 ret = q->getAction(pathView);
400 const auto name = pathView.mid(pos + 1, lastPos);
401 pathView = pathView.mid(0, pos);
402 ret = q->getAction(name, pathView);
409 pos = pathView.indexOf(u
'/', pos - 1);
423 const QString ns = qobject_cast<Action *>(c->
stack().constLast())->ns();
432#include "moc_dispatcher.cpp"
This class represents a Cutelyst Action.
bool dispatch(Context *c)
QString className() const noexcept
Controller * controller() const noexcept
The Cutelyst Component base class.
QString name() const noexcept
QStack< Component * > stack() const noexcept
QString qtTrId(const char *id, int n=-1) const
bool execute(Component *code)
void appendError(const QString &error)
Cutelyst Controller base class.
ActionList actions() const noexcept
bool _DISPATCH(Context *c)
Describes a chained dispatch type.
Describes a path dispatch type.
Abstract class to described a dispatch type.
virtual QByteArray list() const =0
virtual MatchType match(Context *c, QStringView path, const QStringList &args) const =0
Action * getAction(QStringView name, QStringView nameSpace={}) const
void setupActions(const QVector< Controller * > &controllers, const QVector< DispatchType * > &dispatchers, bool printActions)
QVector< DispatchType * > dispatchers() const
QString uriForAction(Action *action, const QStringList &captures) const
ActionList getActions(QStringView name, QStringView nameSpace) const
bool forward(Context *c, Component *component)
Controller * controller(QStringView name) const
Dispatcher(QObject *parent=nullptr)
Action * getActionByPath(QStringView path) const
QList< Controller * > controllers() 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.
const char * constData() const const
void append(QList::parameter_type value)
bool isEmpty() const const
QList::reverse_iterator rbegin()
QList::reverse_iterator rend()
QObject * parent() const const
QString arg(Args &&... args) const const
bool isEmpty() const const
bool isNull() const const
qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs) const const
QString & prepend(QChar ch)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString join(QChar separator) const const
QStringView mid(qsizetype start, qsizetype length) const const
int compare(QChar ch) const const
qsizetype count(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs) const const
qsizetype length() const const
qsizetype size() const const
bool startsWith(QChar ch) const const
QString toString() const const
void truncate(qsizetype length)