6 #include "application.h" 9 #include "controller.h" 10 #include "controller_p.h" 11 #include "dispatcher_p.h" 12 #include "dispatchtypechained.h" 13 #include "dispatchtypepath.h" 15 #include "request_p.h" 18 #include <QMetaMethod> 25 , d_ptr(new DispatcherPrivate(this))
31 Dispatcher::~Dispatcher()
46 bool instanceUsed =
false;
47 const auto actions = controller->actions();
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 d->actionContainer[action->ns()] << action;
71 registeredActions.
append(action);
74 qCDebug(CUTELYST_DISPATCHER)
75 <<
"The action" << action->name() <<
"of" << action->controller()->objectName()
76 <<
"controller was not registered in any dispatcher." 77 " If you still want to access it internally (via actionFor())" 78 " you may make it's method private.";
83 d->controllers.insert(controller->objectName(), controller);
95 controller->d_ptr->setupFinished();
102 if (!type->
inUse()) {
103 d->dispatchers.removeAt(i);
112 qCDebug(CUTELYST_DISPATCHER) <<
dispatch->list().constData();
119 Action *action = c->action();
123 const QString path = c->req()->path();
125 c->
error(c->
translate(
"Cutelyst::Dispatcher",
"No default action defined"));
127 c->
error(c->
translate(
"Cutelyst::Dispatcher",
"Unknown resource '%1'.").
arg(path));
145 Action *action = d->command2Action(c, opname, c->request()->args());
150 qCCritical(CUTELYST_DISPATCHER) <<
"Action not found" << opname << c->request()->args();
158 Request *request = c->request();
159 d->prepareAction(c, request->path());
161 static const auto &log = CUTELYST_DISPATCHER();
162 if (log.isDebugEnabled()) {
163 if (!request->match().
isEmpty()) {
164 qCDebug(log) <<
"Path is" << request->match();
167 if (!request->args().
isEmpty()) {
168 qCDebug(log) <<
"Arguments are" << request->args().
join(
QLatin1Char(
'/'));
173 void DispatcherPrivate::prepareAction(
Context *c,
const QString &requestPath)
const 175 QString path = normalizePath(requestPath);
187 if (type->match(c, path, args) == DispatchType::ExactMatch) {
215 const QString normName = u
'/' + name;
216 return d->actions.value(normName).action;
219 const QString ns = DispatcherPrivate::cleanNamespace(nameSpace);
228 int slashes = _path.
count(u
'/');
231 }
else if (_path.
startsWith(u
'/') && slashes != 1) {
234 return d->actions.value(_path).action;
247 const QString ns = DispatcherPrivate::cleanNamespace(nameSpace);
248 const ActionList containers = d->getContainers(ns);
249 auto rIt = containers.
rbegin();
250 while (rIt != containers.
rend()) {
251 if ((*rIt)->name() == name) {
262 return d->controllers;
270 ret =
dispatch->uriForAction(action, captures);
273 ret = QStringLiteral(
"/");
286 if (expandedAction) {
287 return expandedAction;
296 return d->dispatchers;
302 bool lastWasSlash =
true;
303 int nsSize = ns.
size();
304 for (
int i = 0; i < nsSize; ++i) {
309 if (ret.
at(i) == u
'/') {
317 lastWasSlash =
false;
326 bool lastSlash =
true;
328 while (i < ret.
size()) {
329 if (ret.
at(i) == u
'/') {
347 void DispatcherPrivate::printActions()
const 351 auto keys = actions.keys();
352 std::sort(keys.begin(), keys.end());
353 for (
const auto &key : keys) {
354 Action *action = actions.value(key).action;
367 qCDebug(CUTELYST_DISPATCHER) << Utils::buildTable(table,
384 ret.
append(actionContainer.value(ns.
mid(0, pos)));
398 auto it = actions.constFind(command);
399 if (it != actions.constEnd()) {
400 return it.value().action;
403 return invokeAsPath(c, command, args);
414 QString path = DispatcherPrivate::actionRel2Abs(c, relativePath);
417 int lastPos = path.
size();
420 ret = q->getAction(path,
QString());
425 const QString name = path.
mid(pos + 1, lastPos);
426 path = path.
mid(0, pos);
427 ret = q->getAction(name, path);
457 #include "moc_dispatcher.cpp" bool dispatch(Context *c)
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString uriForAction(Action *action, const QStringList &captures) const
bool dispatch(Context *c)
void setupActions(const QVector< Controller *> &controllers, const QVector< DispatchType *> &dispatchers, bool printActions)
bool error() const noexcept
Returns true if an error was set.
void append(const T &value)
QString & prepend(QChar ch)
QVector::reverse_iterator rend()
bool forward(Context *c, Component *component)
QString join(const QString &separator) const const
void prepareAction(Context *c)
QString & remove(int position, int n)
QVector< DispatchType * > dispatchers() const
The Cutelyst Component base class.
ActionList getActions(const QString &name, const QString &nameSpace) const
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
bool isNull() const const
This class represents a Cutelyst Action.
Cutelyst Controller base class
void append(const T &value)
QMap< QString, Controller * > controllers() const
bool isEmpty() const const
bool isEmpty() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
bool _DISPATCH(Context *c)
Controller * controller() const
The Cutelyst namespace holds all public Cutelyst API.
bool execute(Component *code)
QStack< Component * > stack() const noexcept
QString mid(int position, int n) const const
Action * expandAction(const Context *c, Action *action) const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
const QChar at(int position) const const
Action * getAction(const QString &name, const QString &nameSpace=QString()) const
void prepend(const T &value)
QVector::reverse_iterator rbegin()
Action * getActionByPath(const QString &path) const
QObject * parent() const const
Dispatcher(QObject *parent=nullptr)
int compare(const QString &other, Qt::CaseSensitivity cs) const const
QString className() const