18 #include "controller_p.h" 20 #include "application.h" 21 #include "dispatcher.h" 25 #include "context_p.h" 27 #include <QMetaClassInfo> 28 #include <QRegularExpression> 33 , d_ptr(new ControllerPrivate(this))
37 Controller::~Controller()
40 qDeleteAll(d->actionList);
53 Action *ret = d->actions.value(name);
57 return d->dispatcher->getAction(name, d->pathPrefix);
68 return !qstrcmp(metaObject()->className(), className);
83 ControllerPrivate::ControllerPrivate(
Controller *parent) :
92 dispatcher = _dispatcher;
98 const QMetaObject *meta = q->metaObject();
99 const QString className = QString::fromLatin1(meta->className());
100 q->setObjectName(className);
102 bool namespaceFound =
false;
103 for (
int i = meta->classInfoCount() - 1; i >= 0; --i) {
104 if (qstrcmp(meta->classInfo(i).name(),
"Namespace") == 0) {
105 pathPrefix = QString::fromLatin1(meta->classInfo(i).value());
107 pathPrefix.remove(0, 1);
109 namespaceFound =
true;
114 if (!namespaceFound) {
116 bool lastWasUpper =
true;
118 for (
int i = 0; i < className.length(); ++i) {
119 const QChar c = className.at(i);
120 if (c.isLower() || c.isDigit()) {
121 controlerNS.append(c);
122 lastWasUpper =
false;
124 controlerNS.append(c);
130 controlerNS.append(c.toLower());
134 pathPrefix = controlerNS;
137 registerActionMethods(meta, q, app);
140 void ControllerPrivate::setupFinished()
144 const ActionList beginList = dispatcher->getActions(QStringLiteral(
"Begin"), pathPrefix);
145 if (!beginList.isEmpty()) {
146 beginAutoList.append(beginList.last());
149 beginAutoList.append(dispatcher->getActions(QStringLiteral(
"Auto"), pathPrefix));
151 const ActionList endList = dispatcher->getActions(QStringLiteral(
"End"), pathPrefix);
152 if (!endList.isEmpty()) {
153 end = endList.last();
156 const auto actions = actionList;
157 for (
Action *action : actions) {
158 action->dispatcherReady(dispatcher, q);
161 q->preFork(qobject_cast<Application *>(q->parent()));
170 int &asyncDetached = c->d_ptr->asyncDetached;
173 const auto beginAutoList = d->beginAutoList;
174 for (
Action *action : beginAutoList) {
176 c->d_ptr->pendingAsync.append(action);
177 }
else if (!action->dispatch(c)) {
186 c->d_ptr->pendingAsync.append(c->action());
187 }
else if (!c->action()->
dispatch(c)) {
195 c->d_ptr->pendingAsync.append(d->end);
196 }
else if (!d->end->dispatch(c)) {
204 Action *ControllerPrivate::actionClass(
const QVariantHash &args)
207 const QString actionClass = attributes.
value(QStringLiteral(
"ActionClass"));
209 QObject *
object = instantiateClass(actionClass,
"Cutelyst::Action");
211 Action *action = qobject_cast<Action*>(
object);
215 qCWarning(CUTELYST_CONTROLLER) <<
"ActionClass" 217 <<
"is not an ActionClass";
224 Action *ControllerPrivate::createAction(
const QVariantHash &args,
const QMetaMethod &method,
Controller *controller,
Application *app)
226 Action *action = actionClass(args);
231 QStack<Component *> roles = gatherActionRoles(args);
232 for (
int i = 0; i < roles.size(); ++i) {
234 code->
init(app, args);
235 code->setParent(action);
240 action->
setName(args.value(QStringLiteral(
"name")).toString());
241 action->
setReverse(args.value(QStringLiteral(
"reverse")).toString());
247 void ControllerPrivate::registerActionMethods(
const QMetaObject *meta,
Controller *controller,
Application *app)
250 for (
int i = 0; i < meta->methodCount(); ++i) {
251 const QMetaMethod method = meta->method(i);
252 const QByteArray name = method.
name();
257 if (method.isValid() &&
258 (method.methodType() == QMetaMethod::Method || method.methodType() == QMetaMethod::Slot) &&
259 (method.parameterCount() && method.parameterType(0) == qMetaTypeId<Cutelyst::Context *>())) {
262 QByteArray attributeArray;
263 for (
int i2 = meta->classInfoCount() - 1; i2 >= 0; --i2) {
264 QMetaClassInfo classInfo = meta->classInfo(i2);
265 if (name == classInfo.name()) {
266 attributeArray.append(classInfo.value());
272 if (controller->
ns().isEmpty()) {
273 reverse = QString::fromLatin1(name);
275 reverse = controller->
ns() +
QLatin1Char(
'/') + QString::fromLatin1(name);
278 Action *action = createAction({
279 {QStringLiteral(
"name"), QVariant::fromValue(name)},
280 {QStringLiteral(
"reverse"), QVariant::fromValue(reverse)},
281 {QStringLiteral(
"namespace"), QVariant::fromValue(controller->
ns())},
282 {QStringLiteral(
"attributes"), QVariant::fromValue(attrs)}
288 actions.insertMulti(action->
reverse(), action);
289 actionList.append(action);
294 QMap<QString, QString> ControllerPrivate::parseAttributes(
const QMetaMethod &method,
const QByteArray &str,
const QByteArray &name)
297 std::vector<std::pair<QString, QString> > attributes;
304 int size = str.
size();
311 if (str.at(pos) ==
':') {
312 int keyStart = ++pos;
315 if (str.at(pos) ==
'(') {
317 int valueStart = ++pos;
319 if (str.at(pos) ==
')') {
322 if (++pos < size && str.at(pos) ==
':') {
325 value = QString::fromLatin1(str.mid(valueStart, valueEnd - valueStart));
327 }
else if (pos >= size) {
330 value = QString::fromLatin1(str.mid(valueStart, valueEnd - valueStart));
340 }
else if (str.at(pos) ==
':') {
349 key = QString::fromLatin1(str.mid(keyStart, keyLength));
352 if (!value.isEmpty()) {
356 value.remove(value.size() - 1, 1);
361 attributes.push_back({ key, value });
369 auto i = attributes.crbegin();
370 const auto end = attributes.crend();
372 QString key = i->first;
373 QString value = i->second;
375 key = QStringLiteral(
"Path");
376 value = parsePathAttr(
QLatin1Char(
'/') + QString::fromLatin1(name));
378 key = QStringLiteral(
"Path");
379 value = parsePathAttr(QString::fromLatin1(name));
381 value = parsePathAttr(value);
383 QString args = value;
384 if (!args.isEmpty()) {
385 value = args.remove(QRegularExpression(QStringLiteral(
"\\D")));
388 QString captureArgs = value;
389 value = captureArgs.remove(QRegularExpression(QStringLiteral(
"\\D")));
391 value = parseChainedAttr(value);
399 if (!ret.
contains(QStringLiteral(
"Args")) && !ret.
contains(QStringLiteral(
"CaptureArgs")) &&
400 (ret.
contains(QStringLiteral(
"AutoArgs")) || ret.
contains(QStringLiteral(
"AutoCaptureArgs")))) {
401 if (ret.
contains(QStringLiteral(
"AutoArgs")) && ret.
contains(QStringLiteral(
"AutoCaptureArgs"))) {
402 qFatal(
"Action '%s' has both AutoArgs and AutoCaptureArgs, which is not allowed", name.constData());
404 QString parameterName;
405 if (ret.
contains(QStringLiteral(
"AutoArgs"))) {
406 ret.
remove(QStringLiteral(
"AutoArgs"));
407 parameterName = QStringLiteral(
"Args");
409 ret.
remove(QStringLiteral(
"AutoCaptureArgs"));
410 parameterName = QStringLiteral(
"CaptureArgs");
414 if (!(method.parameterCount() == 2 && method.parameterType(1) == QMetaType::QStringList)) {
415 int parameterCount = 0;
416 for (
int i2 = 1; i2 < method.parameterCount(); ++i2) {
417 int typeId = method.parameterType(i2);
418 if (typeId == QMetaType::QString) {
422 ret.
insert(parameterName, QString::number(parameterCount));
429 if (!ret.
contains(QStringLiteral(
"Private")) && method.access() == QMetaMethod::Private) {
430 ret.
insert(QStringLiteral(
"Private"), QString());
436 QStack<Component *> ControllerPrivate::gatherActionRoles(
const QVariantHash &args)
438 QStack<Component *> roles;
440 auto doesIt = attributes.
constFind(QStringLiteral(
"Does"));
441 while (doesIt != attributes.constEnd() && doesIt.
key() ==
QLatin1String(
"Does")) {
442 QObject *
object = instantiateClass(doesIt.
value(), QByteArrayLiteral(
"Cutelyst::Component"));
444 roles.push(qobject_cast<Component *>(
object));
451 QString ControllerPrivate::parsePathAttr(
const QString &value)
453 QString ret = pathPrefix;
456 }
else if (!value.isEmpty()) {
462 QString ControllerPrivate::parseChainedAttr(
const QString &attr)
464 QString ret = QStringLiteral(
"/");
465 if (attr.isEmpty()) {
469 if (attr == QStringLiteral(
".")) {
470 ret.append(pathPrefix);
472 if (!pathPrefix.isEmpty()) {
485 QObject *ControllerPrivate::instantiateClass(
const QString &name,
const QByteArray &super)
487 QString instanceName = name;
488 if (!instanceName.isEmpty()) {
489 instanceName.remove(QRegularExpression(QStringLiteral(
"\\W")));
491 int id = QMetaType::type(instanceName.toLatin1().data());
497 id = QMetaType::type(instanceName.toLatin1().data());
498 if (!
id && !instanceName.startsWith(QStringLiteral(
"Cutelyst::"))) {
500 id = QMetaType::type(instanceName.toLatin1().data());
505 const QMetaObject *metaObj = QMetaType::metaObjectForType(
id);
507 if (!superIsClassName(metaObj->superClass(), super)) {
508 qCWarning(CUTELYST_CONTROLLER)
511 <<
"is not a derived class of" 515 QObject *
object = metaObj->newInstance();
517 qCWarning(CUTELYST_CONTROLLER)
518 <<
"Could create a new instance of" 520 <<
"make sure it's default constructor is " 521 "marked with the Q_INVOKABLE macro";
527 Component *component = application->createComponentPlugin(name);
532 component = application->createComponentPlugin(instanceName);
539 qFatal(
"Could not create component '%s', you can register it with qRegisterMetaType<%s>(); or set a proper CUTELYST_PLUGINS_DIR",
540 qPrintable(instanceName), qPrintable(instanceName));
546 bool ControllerPrivate::superIsClassName(
const QMetaObject *super,
const QByteArray &className)
549 if (super->className() == className) {
552 return superIsClassName(super->superClass(), className);
557 #include "moc_controller.cpp" void setName(const QString &name)
QVector< Action * > ActionList
bool dispatch(Context *c)
bool contains(const Key &key) const const
void setReverse(const QString &reverse)
virtual bool preFork(Application *app)
QMap::const_iterator constFind(const Key &key) const const
Action * actionFor(const QString &name) const
The Cutelyst Component base class.
This class represents a Cutelyst Action.
Cutelyst Controller base class
QMap::iterator insertMulti(const Key &key, const T &value)
virtual bool init(Application *application, const QVariantHash &args)
bool _DISPATCH(Context *c)
The Cutelyst namespace holds all public Cutelyst API.
void applyRoles(const QStack< Component * > &roles)
void setController(Controller *controller)
virtual bool postFork(Application *app)
const Key key(const T &value, const Key &defaultKey) const const
void setupAction(const QVariantHash &args, Application *app)
bool operator==(const char *className)
The Cutelyst Application.
void setMethod(const QMetaMethod &method)
QMap::iterator insert(const Key &key, const T &value)
ActionList actions() const
const T value(const Key &key, const T &defaultValue) const const
int remove(const Key &key)
Controller(QObject *parent=nullptr)