1 | "use strict";
|
2 |
|
3 | var fs = require("fs"),
|
4 | path = require("path"),
|
5 | count = require("object-count"),
|
6 | merge = require("utils-merge"),
|
7 | exists = require('fs-exists-sync'),
|
8 | events = require('../events/index.js');
|
9 |
|
10 | module.exports = class {
|
11 |
|
12 | constructor(config, paths) {
|
13 | this.files = {};
|
14 | this.config = config;
|
15 | this.paths = paths;
|
16 | this.appControllerPath = path.join(paths.app.app, 'controllers');
|
17 | this.events = new events(config, paths);
|
18 | }
|
19 |
|
20 | find() {
|
21 | var url = this.request.params[0].split('/');
|
22 |
|
23 | var position = 0;
|
24 |
|
25 | if(!url[0]){
|
26 | ++position;
|
27 | }
|
28 |
|
29 | if (url[position]) {
|
30 | var controller = url[position],
|
31 | action = (url[++position]) ? url[position] : 'index',
|
32 | params = [];
|
33 |
|
34 | if (url[++position]) {
|
35 | for (var i = position; i < url.length; i++) {
|
36 | params.push(url[i]);
|
37 | }
|
38 | }
|
39 |
|
40 | if (this.exists(controller, action)) {
|
41 | return this.invoke(controller, action, params, true)
|
42 | }
|
43 |
|
44 | return this.error(controller, action, 404);
|
45 |
|
46 | }
|
47 | else {
|
48 | return this.invoke('index', 'index', [], true)
|
49 | }
|
50 | }
|
51 |
|
52 | exists(controller, action = false) {
|
53 | if (action) {
|
54 | if (this.files[controller]) {
|
55 | var control = require(path.join(this.appControllerPath, controller + 'Controller.js'))({});
|
56 | if (typeof control[action] == 'function') {
|
57 | return true
|
58 | }
|
59 | else if (this.request) {
|
60 | return (typeof control[this.request.method.toLocaleLowerCase() + '_' + action] == 'function');
|
61 | }
|
62 | else {
|
63 | return false
|
64 | }
|
65 | }
|
66 |
|
67 | return false;
|
68 | }
|
69 |
|
70 | return (this.files[controller]);
|
71 | }
|
72 |
|
73 | invoke(controller, action, params, scaffold) {
|
74 | if (!params) {
|
75 | if (this.request.params[0]) {
|
76 | params = this.request.params[0].split('/')
|
77 | }
|
78 | }
|
79 |
|
80 | var app = {
|
81 | controller, action, params,
|
82 | model: null,
|
83 | view: null,
|
84 | request: null,
|
85 | response: null,
|
86 | req: null,
|
87 | res: null,
|
88 | events: this.events,
|
89 | logger: this.logger
|
90 | };
|
91 |
|
92 | if (this.view) {
|
93 | app.view = this.view.controller(controller, action);
|
94 | }
|
95 |
|
96 | if (this.model) {
|
97 | app.model = this.model;
|
98 | }
|
99 |
|
100 | if (this.request) {
|
101 | app.request = this.request;
|
102 | app.req = this.request;
|
103 |
|
104 | app.method = this.request.method.toLocaleLowerCase();
|
105 | var methodAction = app.methodAction = app.method + '_' + action;
|
106 | }
|
107 |
|
108 | if (this.response) {
|
109 | app.response = this.response;
|
110 | app.res = this.response;
|
111 | }
|
112 |
|
113 | if (!this.files[controller]) {
|
114 | return this.error(controller, action, 404);
|
115 | }
|
116 |
|
117 | app = this.events.emit('afterLoadController', app)
|
118 |
|
119 | if(!app){
|
120 | return;
|
121 | }
|
122 |
|
123 | var defaultController;
|
124 |
|
125 | if(exists(path.join(this.appControllerPath, 'index.js'))){
|
126 | defaultController = require(path.join(this.appControllerPath, 'index.js'))(app);
|
127 | app.index = defaultController;
|
128 | }
|
129 | else{
|
130 | app.index = null;
|
131 | }
|
132 |
|
133 |
|
134 | var control = require(path.join(this.appControllerPath, controller + 'Controller.js'))(app);
|
135 |
|
136 | if(control.init){
|
137 | if(control.init() === false){
|
138 | return
|
139 | }
|
140 | }
|
141 |
|
142 | if (typeof control[methodAction] == 'function') {
|
143 | action = methodAction;
|
144 | }
|
145 |
|
146 | if ((scaffold && control.scaffold === false) || (typeof control[action] != 'function')) {
|
147 | return this.error(controller, action, 404);
|
148 | }
|
149 |
|
150 | if (count(this.request.body) && params.length == 0) {
|
151 | var body = this.request.body;
|
152 |
|
153 | if (!body.files && this.request.files) {
|
154 | body.files = this.request.files;
|
155 | }
|
156 |
|
157 | return this.out(control[action].apply(null, [body]))
|
158 | }
|
159 | else {
|
160 | if (count(this.request.query)) {
|
161 | params.push(this.request.query)
|
162 | }
|
163 |
|
164 | return this.out(control[action].apply(null, params))
|
165 | }
|
166 |
|
167 | }
|
168 |
|
169 | out(data) {
|
170 | if (data) {
|
171 | if (typeof data == 'object' && data.renderContent) {
|
172 | return this.response.send(data.renderContent);
|
173 | }
|
174 |
|
175 | return this.response.send(data);
|
176 | }
|
177 |
|
178 | return '';
|
179 | }
|
180 |
|
181 | http(request, response) {
|
182 | this.request = request;
|
183 | this.response = response;
|
184 | this.view.request = request
|
185 | this.view.response = response
|
186 | this.view.setConfig({request, response});
|
187 | return this;
|
188 | }
|
189 |
|
190 | load() {
|
191 | var files = [];
|
192 | if (exists(this.appControllerPath)) {
|
193 | fs.readdirSync(this.appControllerPath)
|
194 | .filter(function (file) {
|
195 | return (file.indexOf(".") !== 0) && (file !== "index.js") && (file.indexOf("Controller") > -1);
|
196 | })
|
197 | .forEach(function (file) {
|
198 | files[file.replace('Controller.js', '')] = file;
|
199 | });
|
200 |
|
201 | this.files = files;
|
202 | }
|
203 |
|
204 | return this;
|
205 | }
|
206 |
|
207 | error(controller, action, error) {
|
208 | var message = this.view.errors[error] ? this.view.errors[error] : '';
|
209 |
|
210 | if (this.exists(controller, 'error' + error)) {
|
211 | return this.invoke(controller, 'error' + error, [message], true)
|
212 | }
|
213 | else if (this.exists(controller, 'errors')) {
|
214 | return this.invoke(controller, 'errors', [error, message], true)
|
215 | }
|
216 | else if (this.exists('error' + error, 'index')) {
|
217 | return this.invoke('error' + error, 'index', [message], true)
|
218 | }
|
219 | else if (this.exists('errors', 'error' + error)) {
|
220 | return this.invoke('errors', 'error' + error, [error, message], true)
|
221 | }
|
222 | else if (this.exists('errors', 'index')) {
|
223 | return this.invoke('errors', 'index', [error, message], true)
|
224 | }
|
225 | else {
|
226 | return this.view.error(error, message)
|
227 | }
|
228 | }
|
229 | }
|