Overview

Namespaces

  • GLFramework
    • Cache
    • Controller
    • DaMa
      • Manipulators
    • Database
    • HTTP
    • Mail
    • Middleware
    • Model
    • Module
    • Tests
    • Upload
    • Utils
  • None

Classes

  • GLFramework\Bootstrap
  • GLFramework\Cache\Cache
  • GLFramework\Cache\MemcachedCache
  • GLFramework\Cache\MemoryCache
  • GLFramework\Controller
  • GLFramework\Controller\AdminController
  • GLFramework\Controller\APIController
  • GLFramework\Controller\AuthController
  • GLFramework\Controller\ErrorController
  • GLFramework\Controller\ExceptionController
  • GLFramework\DaMa\Association
  • GLFramework\DaMa\DataManipulation
  • GLFramework\DaMa\Manipulator
  • GLFramework\DaMa\Manipulators\CSVManipulator
  • GLFramework\DaMa\Manipulators\ManipulatorCore
  • GLFramework\DaMa\Manipulators\XLSManipulator
  • GLFramework\DaMa\Manipulators\XLSXManipulator
  • GLFramework\Database\Connection
  • GLFramework\Database\MySQLConnection
  • GLFramework\DatabaseManager
  • GLFramework\DBStructure
  • GLFramework\Events
  • GLFramework\Filesystem
  • GLFramework\HTTP\API
  • GLFramework\Log
  • GLFramework\Mail
  • GLFramework\Mail\Mail
  • GLFramework\Mail\MailSystem
  • GLFramework\Mail\PHPMailer
  • GLFramework\MailView
  • GLFramework\Middleware\APIAuthorizationMiddleware
  • GLFramework\Middleware\ControllerMiddleware
  • GLFramework\Model
  • GLFramework\Model\Page
  • GLFramework\Model\User
  • GLFramework\Model\UserPage
  • GLFramework\ModelResult
  • GLFramework\Module\Module
  • GLFramework\Module\ModuleManager
  • GLFramework\Request
  • GLFramework\Response
  • GLFramework\Tests\DatabaseTestCase
  • GLFramework\Tests\TestCase
  • GLFramework\Upload\Upload
  • GLFramework\Upload\Uploads
  • GLFramework\Utils\DataTablesManager
  • GLFramework\Versions
  • GLFramework\View

Interfaces

  • GLFramework\Middleware

Functions

  • array_merge_recursive_ex
  • e
  • file_get_php_classes
  • fix_date
  • fix_date_format
  • fix_decimal
  • fix_url
  • get
  • get_php_classes
  • http_response_code
  • instance_method
  • is_module_enabled
  • now
  • post
  • print_array
  • print_brief_debug
  • print_debug
  • random_str
  • reArrayFiles
  • reArrayPost
  • today
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /**
  3:  * Created by PhpStorm.
  4:  * User: manus
  5:  * Date: 12/02/16
  6:  * Time: 11:58
  7:  */
  8: 
  9: namespace GLFramework\Module;
 10: 
 11: 
 12: use GLFramework\Controller;
 13: use GLFramework\Events;
 14: use GLFramework\Model\UserPage;
 15: use GLFramework\Request;
 16: 
 17: class Module
 18: {
 19: 
 20:     var $title;
 21:     var $description;
 22:     private $config;
 23:     private $directory;
 24: 
 25:     private $controllers = array();
 26:     private $controllers_map = array();
 27:     private $controllers_routes = array();
 28:     private $controllers_url_routes = array();
 29: 
 30:     /**
 31:      * Module constructor.
 32:      * @param $config
 33:      * @param $directory
 34:      */
 35:     public function __construct($config, $directory)
 36:     {
 37:         $this->config = $config;
 38:         $this->directory = $directory;
 39:         if(isset($this->config['title']))
 40:             $this->title = $this->config['title'];
 41:         if(isset($this->config['description']))
 42:             $this->description = $this->config['description'];
 43:     }
 44: 
 45:     public function init()
 46:     {
 47: //        $this->register_composer();
 48:         $controllers = $this->config['app']['controllers'];
 49:         if (!is_array($controllers)) $controllers = array($controllers);
 50:         foreach($controllers as $controllerFolder)
 51:         {
 52:             if($controllerFolder)
 53:                 $this->load_controllers($this->directory . "/" . $controllerFolder);
 54:         }
 55:         $this->register_autoload_controllers();
 56:         $this->register_autoload_model();
 57:         $this->register_events();
 58:     }
 59: 
 60:     public function register_autoload_model()
 61:     {
 62:         $models = $this->config['app']['model'];
 63:         if (!is_array($models)) $models = array($models);
 64:         $dir = $this->directory;
 65: 
 66:         spl_autoload_register(function ($class) use ($models, $dir) {
 67:             foreach ($models as $directory) {
 68:                 $filename = $dir . "/" . $directory . "/$class.php";
 69:                 if (file_exists($filename)) {
 70:                     include_once $filename;
 71:                     return true;
 72:                 }
 73:             }
 74:         });
 75:     }
 76: 
 77:     public function load_controllers($root, $folder = null)
 78:     {
 79: 
 80:         $current = $root . ($folder?"/" . $folder:"");
 81:         if(is_dir($current)) {
 82:             $files = scandir($current);
 83:             foreach($files as $file)
 84:             {
 85:                 if($file != "." && $file != "..")
 86:                 {
 87:                     $filename = $current . "/" . $file;
 88:                     $name = $folder . "/" . $file;
 89:                     $ext = substr($file, strrpos($file, "."));
 90:                     if($ext == ".php")
 91:                     {
 92:                         $class = file_get_php_classes($filename);
 93:                         $this->controllers[$class[0]] = $folder . "/" . $file;
 94:                         $this->controllers_map[$class[0]] = $root . "/" . $folder . "/" . $file;
 95:                     }
 96:                     else if(is_dir($filename))
 97:                     {
 98:                         $this->load_controllers($root, $name);
 99:                     }
100:                 }
101:             }
102:         }
103: 
104:     }
105: 
106:     public function register_autoload_controllers()
107:     {
108:         $map = $this->controllers_map;
109:         spl_autoload_register(function($class) use($map)
110:         {
111:            if(isset($map[$class]))
112:            {
113:                $file = $map[$class];
114:                require_once $file;
115:                return true;
116:            }
117:             return false;
118:         });
119:     }
120: 
121:     /**
122:      * @return array
123:      */
124:     public function getControllers()
125:     {
126:         return $this->controllers;
127:     }
128: 
129:     public function getModels()
130:     {
131:         $list = array();
132:         $models = $this->config['app']['model'];
133:         if (!is_array($models)) $models = array($models);
134:         foreach ($models as $model) {
135:             $folder = $this->directory . "/$model";
136:             $files = scandir($folder);
137:             foreach ($files as $file) {
138:                 if (strpos($file, ".php") !== FALSE) {
139:                     $list[] = substr($file, 0, -4);
140:                 }
141:             }
142:         }
143:         return $list;
144:     }
145: 
146:     /**
147:      * Return folder to find views
148:      */
149:     public function getViews()
150:     {
151:         $config = $this->config;
152:         $directories = array();
153:         $dir = $this->directory;
154:         if(isset($config['app']['views']))
155:         {
156:             $directoriesTmp = $config['app']['views'];
157:             if(!is_array($directoriesTmp)) $directoriesTmp = array($directoriesTmp);
158:             foreach($directoriesTmp as $directory)
159:             {
160:                 $this->addFolder($directories, $dir . "/" . $directory);
161:             }
162:         }
163:         // Add main module views
164:         $mainModule = ModuleManager::getInstance()->getMainModule();
165:         if($this != $mainModule) $this->addFolder($directories, $mainModule->getViews());
166:         // Add framework views
167:         $this->addFolder($directories, realpath(__DIR__ . "/../..") . "/views");
168:         $this->addFolder($directories, realpath(__DIR__ . "/../..") . "/modules");
169:         return $directories;
170:     }
171: 
172:     public function addFolder(&$array, $folder)
173:     {
174:         if(is_array($folder))
175:         {
176:             foreach($folder as $item)
177:             {
178:                 $this->addFolder($array, $item);
179:             }
180:         }
181:         else
182:         {
183:             if(is_dir($folder) && !in_array($folder, $array)) $array[] = $folder;
184:         }
185: 
186:     }
187: 
188:     /**
189:      * @return mixed
190:      */
191:     public function getDirectory()
192:     {
193:         return $this->directory;
194:     }
195: 
196: 
197:     /**
198:      * @param $router \AltoRouter
199:      * @return array
200:      */
201:     public function register_router($router)
202:     {
203:         $list = array();
204:         $controllers = $this->getControllers();
205:         foreach($controllers as $controller => $file)
206:         {
207:             $routes = $this->getControllerDefaultRoutes($controller, $file);
208:             $list[] = $routes;
209:             foreach($routes as $route)
210:             {
211:                 $this->register_router_controller($router, $route, $controller);
212:             }
213:         }
214:         return $list;
215:     }
216: 
217:     public function getControllerDefaultRoutes($controller, $file)
218:     {
219:         $array = array();
220: 
221:         if(isset($this->config['app']['routes']))
222:         {
223:             $routes = $this->config['app']['routes'];
224:             if(!is_integer(key($routes))) $routes = array($routes);
225:             foreach($routes as $item)
226:             {
227:                 if(isset($item[$controller]))
228:                 {
229:                     $array[] = $item[$controller];
230:                 }
231:             }
232:         }
233: 
234:         $index = $this->config['app']['index'];
235:         if(strpos($file, $index) !== FALSE)
236:         {
237:             $array[] = $this->cleanUrl(substr($file, 0, strpos($file, $index)));
238:         }
239:         $array[] = $this->cleanUrl($file);
240: 
241:         return $array;
242:     }
243: 
244:     private function cleanUrl($url)
245:     {
246:         if(strlen($url) > 1 && strrpos($url, "/") == strlen($url) - 1 )
247:             $url = substr($url, 0, -1);
248:         if(strrpos($url, ".php") == strlen($url) - 4)
249:             $url = substr($url, 0, -4);
250:         return $url;
251:     }
252: 
253:     /**
254:      * @param $router \AltoRouter
255:      * @param $params
256:      * @param $controller
257:      */
258:     public function register_router_controller($router, $params, $controller)
259:     {
260: 
261:         if(!is_array($params)) $params = array($params);
262:         $route = $params[0];
263:         $method = isset($params[1])?$params[1]:"GET|POST";
264:         $name = isset($params[2])?$params[2]:$controller;
265:         if(in_array($name, $this->controllers_routes)) $name = null;
266:         else $this->controllers_routes[] = $name;
267:         $this->controllers_url_routes[] = $controller . " " . $route . " [" . $method . "]";
268:         $router->map($method, $route, array($this, $controller), $name);
269:     }
270: 
271:     private function register_composer()
272:     {
273:         $composer = $this->getDirectory() . "/vendor/autoload.php";
274:         if(file_exists($composer))
275:         {
276:             include_once $composer;
277:         }
278:     }
279: 
280:     private function register_events()
281:     {
282:         $context = array();
283:         if(isset($this->config['app']['listeners']))
284:         {
285:             $events = $this->config['app']['listeners'];
286:             if(!is_array($events)) $events = array($events);
287:             foreach($events as $event => $listener)
288:             {
289:                 if(!is_array($listener)) $listener = array($listener);
290:                 foreach($listener as $fn)
291:                 {
292:                     Events::getInstance()->listen($event, instance_method($fn, $context, array($this)), $this);
293:                 }
294:             }
295:         }
296:     }
297: 
298:     /**
299:      * @param $controller
300:      * @return Controller
301:      */
302:     public function instanceController($controller)
303:     {
304:         $folder = $this->controllers[$controller];
305:         return new $controller($folder, $this);
306:     }
307:     /**
308:      * @param $controller
309:      * @param $request Request
310:      * @return bool|\GLFramework\Response
311:      * @throws \Exception
312:      */
313:     public function run($controller, $request)
314:     {
315:         if(!is_object($controller)) {
316:             $instance = $this->instanceController($controller);
317:         }
318:         else
319:         {
320:             $instance = $controller;
321:         }
322: 
323:         if($instance instanceof Controller)
324:         {
325:             if($instance instanceof Controller\AuthController)
326:             {
327:                 if($instance->user)
328:                 {
329:                     if(Events::anyFalse(Events::fire('isUserAllowed', array($instance, $instance->user))))
330:                     {
331:                         throw new \Exception('El usuario no tiene permisos para acceder a este sitio');
332:                     }
333:                 }
334:             }
335:             $response = $instance->call($request);
336:             return $response;
337:         }
338:         return false;
339:     }
340: 
341: 
342:     /**
343:      * @return mixed
344:      */
345:     public function getConfig()
346:     {
347:         return $this->config;
348:     }
349: 
350:     /**
351:      * @return array
352:      */
353:     public function getControllersUrlRoutes()
354:     {
355:         return $this->controllers_url_routes;
356:     }
357: 
358: 
359: 
360: 
361: }
API documentation generated by ApiGen