18 #include "dispatchtypepath_p.h"
21 #include "controller.h"
25 #include <QRegularExpression>
32 d_ptr(new DispatchTypePathPrivate)
36 DispatchTypePath::~DispatchTypePath()
45 QRegularExpression multipleSlashes(QLatin1String(
"/{1,}"));
47 QVector<QStringList> table;
49 QStringList keys = d->paths.keys();
50 keys.sort(Qt::CaseInsensitive);
51 for (
const QString &path : keys) {
52 const auto paths = d->paths.value(path);
53 for (
Action *action : paths) {
54 QString _path = QLatin1Char(
'/') + path;
55 if (action->attribute(QLatin1String(
"Args")).isEmpty()) {
56 _path.append(QLatin1String(
"/..."));
58 for (
int i = 0; i < action->numberOfArgs(); ++i) {
59 _path.append(QLatin1String(
"/*"));
62 _path.replace(multipleSlashes, QLatin1String(
"/"));
64 QString privateName = action->reverse();
65 if (!privateName.startsWith(QLatin1Char(
'/'))) {
66 privateName.prepend(QLatin1Char(
'/'));
69 table.append({ _path, privateName });
73 return Utils::buildTable(table, { QLatin1String(
"Path"), QLatin1String(
"Private") },
74 QLatin1String(
"Loaded Path actions:"));
82 if (_path.isEmpty()) {
83 _path = QStringLiteral(
"/");
86 const auto it = d->paths.constFind(_path);
87 if (it == d->paths.constEnd()) {
92 int numberOfArgs = args.size();
93 for (
Action *action : it.value()) {
97 if (action->numberOfArgs() == numberOfArgs) {
98 Request *request = c->request();
103 }
else if (action->numberOfArgs() == -1 &&
107 Request *request = c->request();
123 const auto range = attributes.equal_range(QLatin1String(
"Path"));
124 for (
auto i = range.first; i != range.second; ++i) {
125 if (d->registerPath(*i, action)) {
137 return !d->paths.isEmpty();
143 if (captures.isEmpty()) {
145 auto it = attributes.constFind(QStringLiteral(
"Path"));
146 if (it != attributes.constEnd()) {
147 const QString &path = it.value();
148 if (path.isEmpty()) {
149 ret = QStringLiteral(
"/");
150 }
else if (!path.startsWith(QLatin1Char(
'/'))) {
151 ret = QLatin1Char(
'/') + path;
160 bool DispatchTypePathPrivate::registerPath(
const QString &path,
Action *action)
162 QString _path = path;
163 if (_path.startsWith(QLatin1Char(
'/')) && !_path.isEmpty()) {
166 if (_path.isEmpty()) {
167 _path = QStringLiteral(
"/");
170 auto it = paths.find(_path);
171 if (it != paths.end()) {
173 for (
const Action *regAction : it.value()) {
174 if (regAction->numberOfArgs() == actionNumberOfArgs) {
175 qCCritical(CUTELYST_DISPATCHER_PATH) <<
"Not registering Action"
179 <<
"because it conflicts with"
182 << regAction->controller()->objectName();
187 it.value().push_back(action);
188 std::sort(it.value().begin(), it.value().end(), [](
Action *a,
Action *b) ->
bool {
189 return a->numberOfArgs() < b->numberOfArgs();
192 paths.insert(_path, { action });
197 #include "moc_dispatchtypepath.cpp"