6#include "application.h"
9#include "controller_p.h"
10#include "dispatcher.h"
12#include <QMetaClassInfo>
13#include <QRegularExpression>
242 , d_ptr(new ControllerPrivate(this))
246Controller::~Controller()
249 qDeleteAll(d->actionList);
256 return d->pathPrefix;
262 auto it = d->actions.constFind(name);
263 if (it != d->actions.constEnd()) {
266 return d->dispatcher->getAction(name.toString(), d->pathPrefix);
272 return d->actionList;
277 return !qstrcmp(metaObject()->className(), className);
292ControllerPrivate::ControllerPrivate(
Controller *parent)
301 q->setObjectName(QString::fromLatin1(q->metaObject()->className()));
303 dispatcher = _dispatcher;
309 const QMetaObject *meta = q->metaObject();
310 const QString className = QString::fromLatin1(meta->className());
311 q->setObjectName(className);
313 bool namespaceFound =
false;
314 for (
int i = meta->classInfoCount() - 1; i >= 0; --i) {
315 if (qstrcmp(meta->classInfo(i).name(),
"Namespace") == 0) {
316 pathPrefix = QString::fromLatin1(meta->classInfo(i).value());
317 while (pathPrefix.startsWith(u
'/')) {
318 pathPrefix.remove(0, 1);
320 namespaceFound =
true;
325 if (!namespaceFound) {
327 bool lastWasUpper =
true;
329 for (
int i = 0; i < className.length(); ++i) {
330 const QChar c = className.at(i);
331 if (c.isLower() || c.isDigit()) {
332 controlerNS.append(c);
333 lastWasUpper =
false;
334 }
else if (c == u
'_') {
335 controlerNS.append(c);
339 controlerNS.append(u
'/');
342 controlerNS.append(c.toLower());
347 pathPrefix = controlerNS;
350 registerActionMethods(meta, q, app);
353void ControllerPrivate::setupFinished()
357 const ActionList beginList = dispatcher->getActions(QStringLiteral(
"Begin"), pathPrefix);
358 if (!beginList.isEmpty()) {
359 beginAutoList.append(beginList.last());
362 beginAutoList.append(dispatcher->getActions(QStringLiteral(
"Auto"), pathPrefix));
364 const ActionList endList = dispatcher->getActions(QStringLiteral(
"End"), pathPrefix);
365 if (!endList.isEmpty()) {
366 end = endList.last();
369 const auto actions = actionList;
370 for (
Action *action : actions) {
371 action->dispatcherReady(dispatcher, q);
374 q->preFork(qobject_cast<Application *>(q->parent()));
383 int &actionRefCount = c->d_ptr->actionRefCount;
386 const auto beginAutoList = d->beginAutoList;
387 for (
Action *action : beginAutoList) {
388 if (actionRefCount) {
389 c->d_ptr->pendingAsync.enqueue(action);
390 }
else if (!action->dispatch(c)) {
398 if (actionRefCount) {
399 c->d_ptr->pendingAsync.enqueue(c->
action());
407 if (actionRefCount) {
408 c->d_ptr->pendingAsync.enqueue(d->end);
409 }
else if (!d->end->dispatch(c)) {
414 if (actionRefCount) {
415 c->d_ptr->engineRequest->status |= EngineRequest::Async;
421Action *ControllerPrivate::actionClass(
const QVariantHash &args)
423 const auto attributes = args.value(QStringLiteral(
"attributes")).value<
ParamsMultiMap>();
424 const QString actionClass = attributes.value(QStringLiteral(
"ActionClass"));
426 QObject *
object = instantiateClass(actionClass,
"Cutelyst::Action");
428 Action *action = qobject_cast<Cutelyst::Action *>(
object);
432 qCWarning(CUTELYST_CONTROLLER) <<
"ActionClass" << actionClass <<
"is not an ActionClass"
433 <<
object->metaObject()->superClass()->
className();
440Action *ControllerPrivate::createAction(
const QVariantHash &args,
441 const QMetaMethod &method,
445 Action *action = actionClass(args);
450 QStack<Component *> roles = gatherActionRoles(args);
451 for (
int i = 0; i < roles.size(); ++i) {
453 code->
init(app, args);
454 code->setParent(action);
459 action->
setName(args.value(QStringLiteral(
"name")).toString());
460 action->
setReverse(args.value(QStringLiteral(
"reverse")).toString());
466void ControllerPrivate::registerActionMethods(
const QMetaObject *meta,
471 for (
int i = 0; i < meta->methodCount(); ++i) {
472 const QMetaMethod method = meta->method(i);
473 const QByteArray name = method.
name();
478 if (method.isValid() &&
479 (method.methodType() == QMetaMethod::Method ||
480 method.methodType() == QMetaMethod::Slot) &&
481 (method.parameterCount() &&
482 method.parameterType(0) == qMetaTypeId<Cutelyst::Context *>())) {
485 QByteArray attributeArray;
486 for (
int i2 = meta->classInfoCount() - 1; i2 >= 0; --i2) {
487 QMetaClassInfo classInfo = meta->classInfo(i2);
488 if (name == classInfo.name()) {
489 attributeArray.append(classInfo.value());
492 ParamsMultiMap attrs = parseAttributes(method, attributeArray, name);
495 if (controller->
ns().isEmpty()) {
496 reverse = QString::fromLatin1(name);
498 reverse = controller->
ns() + QLatin1Char(
'/') + QString::fromLatin1(name);
502 createAction({{QStringLiteral(
"name"), QVariant::fromValue(name)},
503 {QStringLiteral(
"reverse"), QVariant::fromValue(reverse)},
504 {QStringLiteral(
"namespace"), QVariant::fromValue(controller->
ns())},
505 {QStringLiteral(
"attributes"), QVariant::fromValue(attrs)}},
511 actionList.append(action);
516ParamsMultiMap ControllerPrivate::parseAttributes(
const QMetaMethod &method,
517 const QByteArray &str,
518 const QByteArray &name)
521 std::vector<std::pair<QString, QString>> attributes;
528 int size = str.size();
535 if (str.at(pos) ==
':') {
536 int keyStart = ++pos;
539 if (str.at(pos) ==
'(') {
541 int valueStart = ++pos;
543 if (str.at(pos) ==
')') {
546 if (++pos < size && str.at(pos) ==
':') {
550 QString::fromLatin1(str.mid(valueStart, valueEnd - valueStart));
552 }
else if (pos >= size) {
556 QString::fromLatin1(str.mid(valueStart, valueEnd - valueStart));
566 }
else if (str.at(pos) ==
':') {
575 key = QString::fromLatin1(str.mid(keyStart, keyLength));
578 if (!value.isEmpty()) {
579 if ((value.startsWith(u
'\'') && value.endsWith(u
'\'')) ||
580 (value.startsWith(u
'"') && value.endsWith(u
'"'))) {
582 value.remove(value.size() - 1, 1);
587 attributes.emplace_back(std::make_pair(key, value));
593 const static auto digitRE = QRegularExpression(u
"\\D"_qs);
597 for (
const auto &pair : attributes) {
598 QString key = pair.first;
599 QString value = pair.second;
600 if (key.compare(u
"Global") == 0) {
601 key = QStringLiteral(
"Path");
602 value = parsePathAttr(QLatin1Char(
'/') + QString::fromLatin1(name));
603 }
else if (key.compare(u
"Local") == 0) {
604 key = QStringLiteral(
"Path");
605 value = parsePathAttr(QString::fromLatin1(name));
606 }
else if (key.compare(u
"Path") == 0) {
607 value = parsePathAttr(value);
608 }
else if (key.compare(u
"Args") == 0) {
609 QString args = value;
610 if (!args.isEmpty()) {
611 value = args.remove(digitRE);
613 }
else if (key.compare(u
"CaptureArgs") == 0) {
614 QString captureArgs = value;
615 value = captureArgs.remove(digitRE);
616 }
else if (key.compare(u
"Chained") == 0) {
617 value = parseChainedAttr(value);
620 ret.insert(key, value);
624 if (!ret.contains(QStringLiteral(
"Args")) && !ret.contains(QStringLiteral(
"CaptureArgs")) &&
625 (ret.contains(QStringLiteral(
"AutoArgs")) ||
626 ret.contains(QStringLiteral(
"AutoCaptureArgs")))) {
627 if (ret.contains(QStringLiteral(
"AutoArgs")) &&
628 ret.contains(QStringLiteral(
"AutoCaptureArgs"))) {
629 qFatal(
"Action '%s' has both AutoArgs and AutoCaptureArgs, which is not allowed",
632 QString parameterName;
633 if (ret.contains(QStringLiteral(
"AutoArgs"))) {
634 ret.remove(QStringLiteral(
"AutoArgs"));
635 parameterName = QStringLiteral(
"Args");
637 ret.remove(QStringLiteral(
"AutoCaptureArgs"));
638 parameterName = QStringLiteral(
"CaptureArgs");
642 if (!(method.parameterCount() == 2 &&
643 method.parameterType(1) == QMetaType::QStringList)) {
644 int parameterCount = 0;
645 for (
int i2 = 1; i2 < method.parameterCount(); ++i2) {
646 int typeId = method.parameterType(i2);
647 if (typeId == QMetaType::QString) {
651 ret.replace(parameterName, QString::number(parameterCount));
657 if (!ret.contains(QStringLiteral(
"Private")) && method.access() == QMetaMethod::Private) {
658 ret.replace(QStringLiteral(
"Private"), QString());
664QStack<Component *> ControllerPrivate::gatherActionRoles(
const QVariantHash &args)
666 QStack<Component *> roles;
667 const auto attributes = args.value(QStringLiteral(
"attributes")).value<
ParamsMultiMap>();
668 auto doesIt = attributes.constFind(QStringLiteral(
"Does"));
669 while (doesIt != attributes.constEnd() && doesIt.key().compare(u
"Does") == 0) {
671 instantiateClass(doesIt.value(), QByteArrayLiteral(
"Cutelyst::Component"));
673 roles.push(qobject_cast<Component *>(
object));
680QString ControllerPrivate::parsePathAttr(
const QString &value)
682 QString ret = pathPrefix;
683 if (value.startsWith(u
'/')) {
685 }
else if (!value.isEmpty()) {
686 ret = pathPrefix + u
'/' + value;
691QString ControllerPrivate::parseChainedAttr(
const QString &attr)
693 QString ret = QStringLiteral(
"/");
694 if (attr.isEmpty()) {
698 if (attr.compare(u
".") == 0) {
699 ret.append(pathPrefix);
700 }
else if (!attr.startsWith(u
'/')) {
701 if (!pathPrefix.isEmpty()) {
702 ret.append(pathPrefix + u
'/' + attr);
714QObject *ControllerPrivate::instantiateClass(
const QString &name,
const QByteArray &super)
716 QString instanceName = name;
717 if (!instanceName.isEmpty()) {
718 const static QRegularExpression nonWordsRE(u
"\\W"_qs);
719 instanceName.remove(nonWordsRE);
721 QMetaType
id = QMetaType::fromName(instanceName.toLatin1().data());
723 if (!instanceName.endsWith(QLatin1Char(
'*'))) {
724 instanceName.append(QLatin1Char(
'*'));
727 id = QMetaType::fromName(instanceName.toLatin1().data());
728 if (!
id.isValid() && !instanceName.startsWith(u
"Cutelyst::")) {
729 instanceName = QLatin1String(
"Cutelyst::") + instanceName;
730 id = QMetaType::fromName(instanceName.toLatin1().data());
735 const QMetaObject *metaObj =
id.metaObject();
737 if (!superIsClassName(metaObj->superClass(), super)) {
738 qCWarning(CUTELYST_CONTROLLER)
739 <<
"Class name" << instanceName <<
"is not a derived class of" << super;
742 QObject *
object = metaObj->newInstance();
744 qCWarning(CUTELYST_CONTROLLER)
745 <<
"Could create a new instance of" << instanceName
746 <<
"make sure it's default constructor is "
747 "marked with the Q_INVOKABLE macro";
753 Component *component = application->createComponentPlugin(name);
758 component = application->createComponentPlugin(instanceName);
765 qCCritical(CUTELYST_CONTROLLER,
766 "Could not create component '%s', you can register it with "
767 "qRegisterMetaType<%s>(); or set a proper CUTELYST_PLUGINS_DIR",
768 qPrintable(instanceName),
769 qPrintable(instanceName));
776bool ControllerPrivate::superIsClassName(
const QMetaObject *super,
const QByteArray &className)
779 if (super->className() == className) {
782 return superIsClassName(super->superClass(), className);
787#include "moc_controller.cpp"
This class represents a Cutelyst Action.
void setupAction(const QVariantHash &args, Application *app)
bool dispatch(Context *c)
void setMethod(const QMetaMethod &method)
QString className() const noexcept
void setController(Controller *controller)
The Cutelyst application.
The Cutelyst Component base class.
void setReverse(const QString &reverse)
virtual bool init(Application *application, const QVariantHash &args)
void applyRoles(const QStack< Component * > &roles)
QString reverse() const noexcept
QString name() const noexcept
void setName(const QString &name)
Cutelyst Controller base class.
virtual bool preFork(Application *app)
virtual bool postFork(Application *app)
bool operator==(const char *className)
QString ns() const noexcept
ActionList actions() const noexcept
bool _DISPATCH(Context *c)
Controller(QObject *parent=nullptr)
Action * actionFor(QStringView name) const
QMultiMap< QString, QString > ParamsMultiMap
The Cutelyst namespace holds all public Cutelyst API.
QVector< Action * > ActionList