Example #1
0
 public function testInitializerSkipsGenerationInProductionMode()
 {
     $this->maam->init($this->loader);
     $classMap = $this->loader->getClassMap();
     $this->assertArrayHasKey('someclass', $classMap);
     $this->assertSame('somefile', $classMap['someclass']);
 }
Example #2
0
 /**
  * Construct
  *
  * @param ClassLoader $loader Loader
  *
  * @throws Exception
  */
 public function __construct(ClassLoader $loader)
 {
     if (empty($loader->getClassMap())) {
         throw new Exception('You are required to run: composer dump-autoload -o', Http::STATUS_CODE_404);
     }
     if (session_status() == PHP_SESSION_NONE) {
         session_start();
     }
     $this->directoryClassLoader($loader, self::getAppDir() . DS . 'modules');
     $this->setModules(Loader::load($loader));
     $this->directoryClassLoader($loader, self::getAppDir() . DS . 'libraries');
     $routing = array();
     foreach ($this->getModules() as $mod) {
         // Get routing from menus
         if (method_exists($mod, 'menus')) {
             $menus = array();
             foreach ($mod->menus() as $name => $menu) {
                 foreach ($menu as $key => $value) {
                     $routing['GET'] = array_merge_recursive($value->getRoutesRecursive(), $routing);
                     if ($value instanceof Menu) {
                         $menus[$name][$key] = $value->toArray();
                     }
                 }
             }
             $this->menus = array_replace_recursive($menus, $this->menus);
         }
         // Get additional routing
         if (method_exists($mod, 'routes')) {
             foreach ($mod->routes() as $method => $route) {
                 foreach ($route as $key => $value) {
                     if (key_exists(strtoupper($method), $routing) && key_exists($value, $routing[strtoupper($method)])) {
                         $routing[strtoupper($method)][$value] = array_merge_recursive($routing[strtoupper($method)][$value], array($key));
                     } else {
                         $routing[strtoupper($method)][$value] = array($key);
                     }
                 }
             }
         }
         self::callGlobalEvent($mod, 'bootstrap');
     }
     // Add extra routes to module routing
     foreach ($routing as $method => $routes) {
         foreach ($routes as $action => $route) {
             if (!empty($route)) {
                 foreach ($this->getModules() as $module) {
                     foreach ($module->getRoutes() as &$urls) {
                         if ($urls->getRequestMethod() === $method) {
                             if (in_array($action, $urls->getUrls())) {
                                 $urls->setUrls(array_merge($urls->getUrls(), $route));
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Example #3
0
 /**
  * Construct
  *
  * @param ClassLoader $loader Loader
  *
  * @throws Exception
  */
 public function __construct(ClassLoader $loader)
 {
     if (empty($loader->getClassMap())) {
         throw new Exception('You are required to run: composer dump-autoload -o', Http::STATUS_CODE_404);
     }
     $this->directoryClassLoader($loader, self::getAppDir() . DS . 'modules');
     $this->setModules(Loader::load($loader));
     $this->directoryClassLoader($loader, self::getAppDir() . DS . 'libraries');
     if (session_status() == PHP_SESSION_NONE) {
         session_start();
     }
     $this->buildRoutes();
 }
Example #4
0
 /**
  * Loader
  *
  * @param ClassLoader $loader Loader
  *
  * @return array
  */
 public static function load(ClassLoader $loader)
 {
     $modules = array();
     $classmap = $loader->getClassMap();
     foreach ($classmap as $class => $file) {
         // The following is required as class_exists, is_subclass_of and ReflectionClass will throw a fatal error if class extends a non-existent class
         // @todo allow custom namespaces
         if (!preg_match('/^main|phpforge|module/i', $class) && !preg_match('/^' . str_replace('/', '\\/', self::getModDir()) . '/i', $file)) {
             continue;
         }
         if (class_exists($class)) {
             if (is_subclass_of($class, 'Forge\\Application\\Module')) {
                 $ref = new \ReflectionClass($class);
                 if ($ref->IsInstantiable()) {
                     $mod = $ref->newInstanceWithoutConstructor();
                     $acls = array();
                     if (method_exists($mod, 'acl')) {
                         $acls = $mod->acl();
                     }
                     $routemap = array();
                     $methods = $ref->getMethods(\ReflectionMethod::IS_PUBLIC);
                     foreach ($methods as $method) {
                         if (preg_match('/Post|Get|Put|Delete$/', $method->name)) {
                             $methodName = preg_replace('/Post|Get|Put|Delete$/', '', $method->name);
                             $methodType = strtoupper(preg_replace('/.*(Post|Get|Put|Delete)$/', '$1', $method->name));
                             $urls = array();
                             if (strtolower($methodName) == strtolower($ref->getShortName())) {
                                 if (strtolower($method->class) == strtolower(self::$defaultModule) && $methodType == 'GET') {
                                     $urls[] = '/';
                                 }
                                 $urls[] = '/' . strtolower(preg_replace('/\\\\/', '/', $class));
                             } else {
                                 $urls[] = '/' . strtolower(preg_replace('/\\\\/', '/', $class) . '/' . $methodName);
                             }
                             $route = new Route();
                             $route->setClass($class)->setMethod($method->name)->setRequestMethod($methodType)->setUrls($urls);
                             if (key_exists($method->name, $acls)) {
                                 $route->setAcls($acls[$method->name]);
                             }
                             $routemap[] = $route;
                         }
                     }
                     $mod->setRoutes($routemap);
                     $modules[] = $mod;
                 }
             }
         }
     }
     return $modules;
 }
Example #5
0
 /**
  * Loader
  *
  * @param ClassLoader $loader Loader
  *
  * @return array
  */
 public static function load(ClassLoader $loader)
 {
     $modules = array();
     $classmap = $loader->getClassMap();
     foreach ($classmap as $class => $file) {
         if (preg_match('/^phpforge|forge|devforge|module/i', $class) || preg_match('/^' . str_replace('/', '\\/', self::getModDir()) . '/i', $file)) {
             if (class_exists($class)) {
                 $ref = new \ReflectionClass($class);
                 if ($ref->IsInstantiable()) {
                     $mod = $ref->newInstanceWithoutConstructor();
                     if ($mod instanceof Module) {
                         $routemap = array();
                         $methods = $ref->getMethods(\ReflectionMethod::IS_PUBLIC);
                         foreach ($methods as $method) {
                             if (preg_match('/Post|Get|Put|Delete$/', $method->name)) {
                                 $methodName = preg_replace('/Post|Get|Put|Delete$/', '', $method->name);
                                 $methodType = strtoupper(preg_replace('/.*(Post|Get|Put|Delete)$/', '$1', $method->name));
                                 $urls = array();
                                 if (strtolower($methodName) == strtolower($ref->getShortName())) {
                                     if (strtolower($method->class) == strtolower(self::$defaultModule) && $methodType == 'GET') {
                                         $urls[] = '/';
                                     }
                                     $urls[] = '/' . strtolower(preg_replace('/\\\\/', '/', $class));
                                 } else {
                                     $urls[] = '/' . strtolower(preg_replace('/\\\\/', '/', $class) . '/' . $methodName);
                                 }
                                 if (!preg_match('/^(event|global|hook)/', $methodName)) {
                                     $route = new Route();
                                     $route->setClass($class)->setMethod($method->name)->setRequestMethod($methodType)->setUrls($urls);
                                     $routemap[] = $route;
                                 }
                             }
                         }
                         $mod->setRoutes($routemap);
                         $modules[] = $mod;
                     }
                 }
             }
         }
     }
     return $modules;
 }