@@ -63,6 +63,30 @@ public static function useRouter($routeProvider) {
6363 self ::$ config ['routeProvider ' ] = $ routeProvider ;
6464 }
6565
66+ /**
67+ * Sets response HTTP headers.
68+ *
69+ * @param array $headers The HTTP headers collection.
70+ * For example: array('X-Powered-By' => 'PHP', 'X-PhpVersion' => '7.0')
71+ *
72+ * @return void
73+ */
74+ public static function useHeaders ($ headers ) {
75+ self ::$ config ['httpHeaders ' ] = $ headers ;
76+ }
77+
78+ /**
79+ * Start new or resume existing session.
80+ *
81+ * @param array $opions If provided, this is an associative array of options that will override the currently set session configuration directives.
82+ * http://php.net/manual/en/session.configuration.php
83+ *
84+ * @return void
85+ */
86+ public static function useSession ($ opions = null ) {
87+ self ::$ config ['session ' ] = (!empty ($ opions ) ? $ opions : true );
88+ }
89+
6690 /**
6791 * Registers routes.
6892 *
@@ -108,8 +132,12 @@ public static function build() {
108132 * @return void
109133 */
110134 private static function headers () {
135+ if (empty (self ::$ config ['httpHeaders ' ])) {
136+ return ;
137+ }
138+
111139 $ response = self ::$ config ['httpContext ' ]->getResponse ();
112- $ response ->addHeader ( ' X-Powered-By ' , Info:: XPOWEREDBY );
140+ $ response ->setHeaders ( self :: $ config [ ' httpHeaders ' ] );
113141 }
114142
115143 /**
@@ -126,13 +154,33 @@ private static function init() {
126154 self ::$ config ['basePath ' ] = getcwd ();
127155 }
128156
157+ if (!defined ('PHPMVC_DS ' )) { define ('PHPMVC_DS ' , DIRECTORY_SEPARATOR ); }
158+ if (!defined ('PHPMVC_ROOT_PATH ' )) { define ('PHPMVC_ROOT_PATH ' , self ::$ config ['basePath ' ] . PHPMVC_DS ); }
159+ if (!defined ('PHPMVC_CORE_PATH ' )) { define ('PHPMVC_CORE_PATH ' , __DIR__ .PHPMVC_DS ); }
160+ if (!defined ('PHPMVC_CONFIG_PATH ' )) { define ('PHPMVC_CONFIG_PATH ' , PHPMVC_ROOT_PATH . 'config ' . PHPMVC_DS ); }
161+ if (!defined ('PHPMVC_FILTER_PATH ' )) { define ('PHPMVC_FILTER_PATH ' , PHPMVC_ROOT_PATH . 'filters ' . PHPMVC_DS ); }
162+ if (!defined ('PHPMVC_CONTROLLER_PATH ' )) { define ('PHPMVC_CONTROLLER_PATH ' , PHPMVC_ROOT_PATH . 'controllers ' . PHPMVC_DS ); }
163+ if (!defined ('PHPMVC_MODEL_PATH ' )) { define ('PHPMVC_MODEL_PATH ' , PHPMVC_ROOT_PATH . 'models ' . PHPMVC_DS ); }
164+ if (!defined ('PHPMVC_VIEW_PATH ' )) { define ('PHPMVC_VIEW_PATH ' , PHPMVC_ROOT_PATH . 'views ' . PHPMVC_DS ); }
165+ if (!defined ('PHPMVC_SHARED_PATH ' )) { define ('PHPMVC_SHARED_PATH ' , PHPMVC_VIEW_PATH . 'shared ' . PHPMVC_DS ); }
166+ if (!defined ('PHPMVC_UPLOAD_PATH ' )) { define ('PHPMVC_UPLOAD_PATH ' , PHPMVC_ROOT_PATH . 'upload ' . PHPMVC_DS ); }
167+
168+ if (!defined ('PHPMVC_APP_NAMESPACE ' )) {
169+ define ('PHPMVC_APP_NAMESPACE ' , self ::$ config ['appNamespace ' ]);
170+ }
171+ elseif (PHPMVC_APP_NAMESPACE !== self ::$ config ['appNamespace ' ]) {
172+ throw new \Exception ('Constant PHPMVC_CONTROLLER already defined. Re-define with other value is not possible. ' );
173+ }
174+
129175 if (empty (self ::$ config ['routeProvider ' ])) {
130176 self ::$ config ['routeProvider ' ] = new DefaultRouteProvider ();
131177 }
132178 elseif (!self ::$ config ['routeProvider ' ] instanceof RouteProvider) {
133179 throw new \Exception ('The routeProvider type must be the base of "\PhpMvc\RouteProvider". ' );
134180 }
135181
182+ self ::$ config ['routeProvider ' ]->init ();
183+
136184 if (isset (self ::$ config ['routes ' ])) {
137185 if (is_callable (self ::$ config ['routes ' ])) {
138186 self ::$ config ['routes ' ](self ::$ config ['routeProvider ' ]);
@@ -166,6 +214,8 @@ private static function init() {
166214 throw new \Exception ('The $cacheProvider type must be the base of "\PhpMvc\CacheProvider". ' );
167215 }
168216
217+ self ::$ config ['cacheProvider ' ]->init ();
218+
169219 if (empty (self ::$ config ['httpContext ' ])) {
170220 $ info = new HttpContextInfo ();
171221 $ info ->routeProvider = self ::$ config ['routeProvider ' ];
@@ -179,22 +229,14 @@ private static function init() {
179229 throw new \Exception ('The httpContext type must be the base of "\PhpMvc\HttpContextBase". ' );
180230 }
181231
182- if (!defined ('PHPMVC_DS ' )) { define ('PHPMVC_DS ' , DIRECTORY_SEPARATOR ); }
183- if (!defined ('PHPMVC_ROOT_PATH ' )) { define ('PHPMVC_ROOT_PATH ' , self ::$ config ['basePath ' ] . PHPMVC_DS ); }
184- if (!defined ('PHPMVC_CORE_PATH ' )) { define ('PHPMVC_CORE_PATH ' , __DIR__ .PHPMVC_DS ); }
185- if (!defined ('PHPMVC_CONFIG_PATH ' )) { define ('PHPMVC_CONFIG_PATH ' , PHPMVC_ROOT_PATH . 'config ' . PHPMVC_DS ); }
186- if (!defined ('PHPMVC_FILTER_PATH ' )) { define ('PHPMVC_FILTER_PATH ' , PHPMVC_ROOT_PATH . 'filters ' . PHPMVC_DS ); }
187- if (!defined ('PHPMVC_CONTROLLER_PATH ' )) { define ('PHPMVC_CONTROLLER_PATH ' , PHPMVC_ROOT_PATH . 'controllers ' . PHPMVC_DS ); }
188- if (!defined ('PHPMVC_MODEL_PATH ' )) { define ('PHPMVC_MODEL_PATH ' , PHPMVC_ROOT_PATH . 'models ' . PHPMVC_DS ); }
189- if (!defined ('PHPMVC_VIEW_PATH ' )) { define ('PHPMVC_VIEW_PATH ' , PHPMVC_ROOT_PATH . 'views ' . PHPMVC_DS ); }
190- if (!defined ('PHPMVC_SHARED_PATH ' )) { define ('PHPMVC_SHARED_PATH ' , PHPMVC_VIEW_PATH . 'shared ' . PHPMVC_DS ); }
191- if (!defined ('PHPMVC_UPLOAD_PATH ' )) { define ('PHPMVC_UPLOAD_PATH ' , PHPMVC_ROOT_PATH . 'upload ' . PHPMVC_DS ); }
192-
193- if (!defined ('PHPMVC_APP_NAMESPACE ' )) {
194- define ('PHPMVC_APP_NAMESPACE ' , self ::$ config ['appNamespace ' ]);
195- }
196- elseif (PHPMVC_APP_NAMESPACE !== self ::$ config ['appNamespace ' ]) {
197- throw new \Exception ('Constant PHPMVC_CONTROLLER already defined. Re-define with other value is not possible. ' );
232+ if (isset (self ::$ config ['session ' ])) {
233+ // TODO: session provider
234+ if (is_array (self ::$ config ['session ' ])) {
235+ session_start (self ::$ config ['session ' ]);
236+ }
237+ else {
238+ session_start ();
239+ }
198240 }
199241
200242 InternalHelper::setStaticPropertyValue ('\\PhpMvc \\HttpContext ' , 'current ' , self ::$ config ['httpContext ' ]);
0 commit comments