6#include "application.h"
10#include "controller_p.h"
11#include "dispatcher_p.h"
12#include "dispatchtypechained.h"
13#include "dispatchtypepath.h"
22using namespace Qt::Literals::StringLiterals;
26 , d_ptr(new DispatcherPrivate(this))
38 const QVector<Cutelyst::DispatchType *> &
dispatchers,
47 bool instanceUsed =
false;
48 const auto actions = controllerItem->actions();
49 for (
Action *action : actions) {
50 bool registered =
false;
51 if (!d->actions.contains(action->reverse())) {
52 if (!action->attributes().contains(u
"Private"_s)) {
55 if (dispatcher->registerAction(action)) {
69 const QString name = action->ns() + u
'/' + action->name();
70 d->actions.insert(name, {name, action});
71 auto it = d->actionContainer.find(action->ns());
72 if (it != d->actionContainer.end()) {
73 it->actions << action;
75 d->actionContainer.insert(action->ns(), {action->ns(), {action}});
78 registeredActions.append(action);
81 qCDebug(CUTELYST_DISPATCHER)
82 <<
"The action" << action->name() <<
"of" << action->controller()->objectName()
83 <<
"controller was not registered in any dispatcher."
84 " If you still want to access it internally (via actionFor())"
85 " you may make it's method private.";
90 d->controllers.insert(controllerItem->objectName(),
91 {controllerItem->objectName(), controllerItem});
100 d->rootActions = d->actionContainer.value(u
"").actions;
102 for (
const Controller *controllerItem : controllers) {
103 controllerItem->d_ptr->setupFinished();
108 while (i < d->dispatchers.size()) {
110 if (!type->
inUse()) {
111 d->dispatchers.removeAt(i);
120 qCDebug(CUTELYST_DISPATCHER) << dispatcher->list().constData();
131 const QString path = c->
req()->path();
132 if (path.isEmpty()) {
155 Action *action = d->command2Action(c, opname, c->
request()->args());
160 qCCritical(CUTELYST_DISPATCHER) <<
"Action not found" << opname << c->
request()->args();
169 d->prepareAction(c, request->path());
171 static const bool log = CUTELYST_DISPATCHER().isDebugEnabled();
173 if (!request->match().isEmpty()) {
174 qCDebug(CUTELYST_DISPATCHER) <<
"Path is" << request->match();
177 if (!request->args().isEmpty()) {
178 qCDebug(CUTELYST_DISPATCHER) <<
"Arguments are" << request->args().join(u
'/');
183void DispatcherPrivate::prepareAction(
Context *c, QStringView path)
const
195 bool matched = std::ranges::any_of(dispatchers, [&](
const DispatchType *type) {
196 return type->
match(c, path, args) == DispatchType::ExactMatch;
203 if (path.length() == 1) {
207 int pos = path.lastIndexOf(u
'/');
209 args.emplaceFront(path.mid(pos + 1).toString());
212 path.truncate(pos + 1);
223 if (name.isEmpty()) {
227 if (nameSpace.isEmpty()) {
228 const QString normName = u
'/' + name;
229 return d->actions.value(normName).action;
239 int slashes = path.count(u
'/');
241 return d->actions.value(QString{u
'/' + path}).action;
242 }
else if (path.startsWith(u
'/') && slashes != 1) {
243 return d->actions.value(path.mid(1)).action;
245 return d->actions.value(path).action;
254 if (name.isEmpty()) {
258 const ActionList containers = d->getContainers(nameSpace);
259 auto rIt = containers.rbegin();
260 while (rIt != containers.rend()) {
261 if ((*rIt)->name() == name) {
272 return d->controllers.value(name).controller;
278 QList<Controller *> ret;
279 for (
const auto &value : d->controllers) {
280 ret.append(value.controller);
289 if (Q_UNLIKELY(action ==
nullptr)) {
290 qCCritical(CUTELYST_DISPATCHER) <<
"Dispatcher::uriForAction called with null action";
311 if (expandedAction) {
312 return expandedAction;
321 return d->dispatchers;
324void DispatcherPrivate::printActions()
const
326 QVector<QStringList> table;
328 auto keys = actions.keys();
329 std::ranges::sort(keys);
330 for (
const auto &key : std::as_const(keys)) {
331 const Action *action = actions.value(key).action;
332 QString path = key.toString();
333 if (!path.startsWith(u
'/')) {
340 row.append(action->
name());
344 qCDebug(CUTELYST_DISPATCHER) << Utils::buildTable(table,
350 u
"Loaded Private actions:"_s)
354ActionList DispatcherPrivate::getContainers(QStringView ns)
const
358 if (ns.compare(u
"/") != 0) {
363 ret.append(actionContainer.value(ns.mid(0, pos)).actions);
364 pos = ns.lastIndexOf(u
'/', pos - 1);
368 ret.append(rootActions);
375 const QStringList &args)
const
377 auto it = actions.constFind(command);
378 if (it != actions.constEnd()) {
379 return it.value().action;
382 return invokeAsPath(c, command, args);
386 QStringView relativePath,
387 const QStringList &args)
const
393 const QString path = DispatcherPrivate::actionRel2Abs(c, relativePath);
394 QStringView pathView{path};
396 int pos = pathView.lastIndexOf(u
'/');
397 int lastPos = pathView.size();
400 ret = q->getAction(pathView);
405 const auto name = pathView.mid(pos + 1, lastPos);
406 pathView = pathView.mid(0, pos);
407 ret = q->getAction(name, pathView);
414 pos = pathView.indexOf(u
'/', pos - 1);
420QString DispatcherPrivate::actionRel2Abs(
const Context *c, QStringView path)
423 if (path.startsWith(u
'/')) {
424 ret = path.mid(1).toString();
428 const QString ns = qobject_cast<Action *>(c->
stack().constLast())->
ns();
430 ret = path.toString();
432 ret = ns + u
'/' + path;
437#include "moc_dispatcher.cpp"
This class represents a Cutelyst Action.
QString ns() const noexcept
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.
bool _DISPATCH(Context *c)
Describes a chained dispatch type.
Describes a path dispatch type.
Abstract class to described a dispatch type.
virtual QString uriForAction(Action *action, const QStringList &captures) const =0
virtual MatchType match(Context *c, QStringView path, const QStringList &args) const =0
virtual Action * expandAction(const Context *c, Action *action) const
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.
QVector< Action * > ActionList