7#include "dispatchtypepath_p.h"
12#include <QRegularExpression>
15using namespace Qt::Literals::StringLiterals;
19 , d_ptr(new DispatchTypePathPrivate)
32 const static QRegularExpression multipleSlashes(u
"/{1,}"_s);
34 QVector<QStringList> table;
36 auto keys = d->paths.keys();
38 std::sort(keys.begin(), keys.end(), [](QStringView a, QStringView b) {
39 return a.compare(b, Qt::CaseInsensitive) < 0;
41 for (
const auto &path : keys) {
42 const auto paths = d->paths.value(path);
43 for (
Action *action : paths.actions) {
44 QString _path = u
'/' + path;
45 if (action->attribute(u
"Args"_s).isEmpty()) {
46 _path.append(u
"/...");
48 for (
int i = 0; i < action->numberOfArgs(); ++i) {
52 _path.replace(multipleSlashes, u
"/"_s);
54 QString privateName = action->reverse();
55 if (!privateName.startsWith(u
'/')) {
56 privateName.prepend(u
'/');
59 table.append({_path, privateName});
63 return Utils::buildTable(table, {u
"Path"_s, u
"Private"_s}, u
"Loaded Path actions:"_s);
71 auto it = d->paths.constFind(path);
72 if (it == d->paths.constEnd()) {
77 int numberOfArgs = args.size();
78 for (
Action *action : it->actions) {
82 if (action->numberOfArgs() == numberOfArgs) {
88 }
else if (action->numberOfArgs() == -1 && !c->
action()) {
107 const auto range = attributes.equal_range(u
"Path"_s);
108 for (
auto i = range.first; i != range.second; ++i) {
109 if (d->registerPath(*i, action)) {
121 return !d->paths.isEmpty();
127 if (captures.isEmpty()) {
129 auto it = attributes.constFind(u
"Path"_s);
130 if (it != attributes.constEnd()) {
131 const QString &path = it.value();
132 if (path.isEmpty()) {
134 }
else if (!path.startsWith(u
'/')) {
144bool DispatchTypePathPrivate::registerPath(
const QString &path,
Action *action)
146 QString _path = path;
148 if (_path.isEmpty()) {
150 }
else if (!_path.startsWith(u
'/')) {
154 auto it = paths.find(_path);
155 if (it != paths.end()) {
157 auto &actions = it->actions;
158 for (
const Action *regAction : actions) {
159 if (regAction->numberOfArgs() == actionNumberOfArgs) {
160 qCCritical(CUTELYST_DISPATCHER_PATH)
161 <<
"Not registering Action" << action->
name() <<
"of controller"
162 << action->
controller()->objectName() <<
"because it conflicts with"
163 << regAction->name() <<
"of controller"
164 << regAction->controller()->objectName();
169 actions.push_back(action);
170 std::sort(actions.begin(), actions.end(), [](
Action *a,
Action *b) ->
bool {
171 return a->numberOfArgs() < b->numberOfArgs();
174 paths.insert(_path, DispatchTypePathReplacement{_path, {action}});
179#include "moc_dispatchtypepath.cpp"
This class represents a Cutelyst Action.
virtual qint8 numberOfArgs() const
ParamsMultiMap attributes() const noexcept
Controller * controller() const noexcept
QString name() const noexcept
Describes a path dispatch type.
~DispatchTypePath() override
DispatchTypePath(QObject *parent=nullptr)
QByteArray list() const override
MatchType match(Context *c, QStringView path, const QStringList &args) const override
QString uriForAction(Action *action, const QStringList &captures) const override
bool registerAction(Action *action) override
Abstract class to described a dispatch type.
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.