7#include "dispatchtypepath_p.h"
12#include <QRegularExpression>
18 , d_ptr(new DispatchTypePathPrivate)
22DispatchTypePath::~DispatchTypePath()
31 QRegularExpression multipleSlashes(QLatin1String(
"/{1,}"));
33 QVector<QStringList> table;
35 QStringList keys = d->paths.keys();
36 keys.sort(Qt::CaseInsensitive);
37 for (
const QString &path : keys) {
38 const auto paths = d->paths.value(path);
39 for (
Action *action : paths) {
40 QString _path = QLatin1Char(
'/') + path;
41 if (action->attribute(QLatin1String(
"Args")).isEmpty()) {
42 _path.append(QLatin1String(
"/..."));
44 for (
int i = 0; i < action->numberOfArgs(); ++i) {
45 _path.append(QLatin1String(
"/*"));
48 _path.replace(multipleSlashes, QLatin1String(
"/"));
50 QString privateName = action->reverse();
51 if (!privateName.startsWith(QLatin1Char(
'/'))) {
52 privateName.prepend(QLatin1Char(
'/'));
55 table.append({_path, privateName});
59 return Utils::buildTable(table,
60 {QLatin1String(
"Path"), QLatin1String(
"Private")},
61 QLatin1String(
"Loaded Path actions:"));
70 if (_path.isEmpty()) {
71 _path = QStringLiteral(
"/");
74 const auto it = d->paths.constFind(_path);
75 if (it == d->paths.constEnd()) {
80 int numberOfArgs = args.size();
81 for (
Action *action : it.value()) {
85 if (action->numberOfArgs() == numberOfArgs) {
86 Request *request = c->request();
91 }
else if (action->numberOfArgs() == -1 && !c->action()) {
94 Request *request = c->request();
110 const auto range = attributes.equal_range(QLatin1String(
"Path"));
111 for (
auto i = range.first; i != range.second; ++i) {
112 if (d->registerPath(*i, action)) {
124 return !d->paths.isEmpty();
130 if (captures.isEmpty()) {
132 auto it = attributes.constFind(QStringLiteral(
"Path"));
133 if (it != attributes.constEnd()) {
134 const QString &path = it.value();
135 if (path.isEmpty()) {
136 ret = QStringLiteral(
"/");
137 }
else if (!path.startsWith(QLatin1Char(
'/'))) {
138 ret = QLatin1Char(
'/') + path;
147bool DispatchTypePathPrivate::registerPath(
const QString &path,
Action *action)
149 QString _path = path;
150 if (_path.startsWith(QLatin1Char(
'/')) && !_path.isEmpty()) {
153 if (_path.isEmpty()) {
154 _path = QStringLiteral(
"/");
157 auto it = paths.find(_path);
158 if (it != paths.end()) {
160 for (
const Action *regAction : it.value()) {
161 if (regAction->numberOfArgs() == actionNumberOfArgs) {
162 qCCritical(CUTELYST_DISPATCHER_PATH)
163 <<
"Not registering Action" << action->
name() <<
"of controller"
164 << action->
controller()->objectName() <<
"because it conflicts with"
165 << regAction->name() <<
"of controller"
166 << regAction->controller()->objectName();
171 it.value().push_back(action);
172 std::sort(it.value().begin(), it.value().end(), [](
Action *a,
Action *b) ->
bool {
173 return a->numberOfArgs() < b->numberOfArgs();
176 paths.insert(_path, {action});
181#include "moc_dispatchtypepath.cpp"
This class represents a Cutelyst Action.
virtual qint8 numberOfArgs() const noexcept
ParamsMultiMap attributes() const noexcept
Controller * controller() const
virtual MatchType match(Context *c, const QString &path, const QStringList &args) const override
DispatchTypePath(QObject *parent=nullptr)
virtual QByteArray list() const override
list the registered actions To be implemented by subclasses
virtual bool inUse() override
virtual QString uriForAction(Action *action, const QStringList &captures) const override
virtual bool registerAction(Action *action) override
registerAction
DispatchType(QObject *parent=nullptr)
void setupMatchedAction(Context *c, Action *action) const
void setArguments(const QStringList &arguments)
void setMatch(const QString &match)
The Cutelyst namespace holds all public Cutelyst API.