コード例 #1
0
 public function __construct()
 {
     $this->app = \Framework\App::getInstance();
     $this->view = \Framework\View::getInstance();
     $this->config = $this->app->getConfig();
     $this->input = \Framework\InputData::getInstance();
 }
コード例 #2
0
ファイル: BaseController.php プロジェクト: Fearways/PhoneBook
 public function __construct()
 {
     $this->app = App::getInstance();
     $this->view = View::getInstance();
     $this->config = $this->app->getConfig();
     $this->input = InputData::getInstance();
     $this->session = $this->app->getSession();
     $this->db = new SimpleDB();
 }
コード例 #3
0
 public function dispatch()
 {
     $router = new \Framework\Routers\DefaultRouter();
     if ($this->router == null) {
         throw new \Exception("No valid router found", 500);
     }
     $uri = $this->router->getUri();
     $routes = \Framework\App::getInstance()->getConfig()->routes;
     $rc = null;
     if (is_array($routes) && count($routes) > 0) {
         foreach ($routes as $key => $value) {
             if (stripos($uri, $key) === 0 && ($uri == $key || stripos($uri, $key . '/') === 0) && $value['namespace']) {
                 $this->ns = $value['namespace'];
                 $uri = substr($uri, strlen($key) + 1);
                 $rc = $value;
                 break;
             }
         }
     } else {
         throw new \Exception("No routes.", 500);
     }
     if ($this->ns == null && $routes['*']['namespace']) {
         $this->ns = $routes['*']['namespace'];
         $rc = $routes['*'];
     } else {
         if ($this->ns == null && !$routes['*']['namespace']) {
             throw new \Exception("Default routes missing", 500);
         }
     }
     $input = \Framework\InputData::getInstance();
     $params = explode('/', $uri);
     if ($params[0]) {
         $this->controller = strtolower($params[0]);
         if ($params[1]) {
             $this->method = strtolower($params[1]);
             unset($params[0], $params[1]);
             $input->setGet(array_values($params));
         } else {
             $this->method = $this->getDefaultMethod();
         }
     } else {
         $this->controller = $this->getDefaultController();
         $this->method = $this->getDefaultMethod();
     }
     if (is_array($rc) && $rc['controllers']) {
         if ($rc['controllers'][$this->controller]['methods'][$this->method]) {
             $this->method = strtolower($rc['controllers'][$this->controller]['methods'][$this->method]);
         }
         if (isset($rc['controllers'][$this->controller]['to'])) {
             $this->controller = strtolower($rc['controllers'][$this->controller]['to']);
         }
     }
     $input->setPost($this->router->getPost());
     $controllerClass = $this->ns . '\\' . ucfirst($this->controller);
     $newController = new $controllerClass();
     $newController->{$this->method}();
 }
コード例 #4
0
 public function dispatch()
 {
     if ($this->_router == null) {
         throw new \Exception('No valid router found.', 500);
     }
     $uri = $this->_router->getURI();
     $routes = App::getInstance()->getConfig()->routes;
     $_rc = null;
     if (is_array($routes) && count($routes) > 0) {
         foreach ($routes as $route => $value) {
             if (stripos($uri, $route) === 0 && ($uri == $route || strpos($uri, $route . '/') === 0) && $value['namespace']) {
                 $this->_namespace = $value['namespace'];
                 $uri = substr($uri, strlen($route) + 1);
                 $_rc = $value;
                 break;
             }
         }
     } else {
         throw new \Exception('Default route missing', 500);
     }
     if ($this->_namespace == null && $routes['*']['namespace']) {
         $this->_namespace = $routes['*']['namespace'];
         $_rc = $routes['*'];
     } else {
         if ($this->_namespace == null && !$routes['*']['namespace']) {
             throw new \Exception('Default route missing', 500);
         }
     }
     $input = InputData::getInstance();
     $params = explode('/', $uri);
     if ($params[0]) {
         $this->_controller = strtolower($params[0]);
         if ($params[1]) {
             $this->_method = strtolower($params[1]);
             unset($params[0], $params[1]);
             $input->setGet(array_values($params));
         } else {
             $this->_method = $this->getDefaultMethod();
         }
     } else {
         $this->_controller = $this->getDefaultController();
         $this->_method = $this->getDefaultMethod();
     }
     if (is_array($_rc) && $_rc['controllers']) {
         if ($_rc['controllers'][$this->_controller]['methods'][$this->_method]) {
             $this->_method = strtolower($_rc['controllers'][$this->_controller]['methods'][$this->_method]);
         }
         if (isset($_rc['controllers'][$this->_controller]['to'])) {
             $this->_controller = strtolower($_rc['controllers'][$this->_controller]['to']);
         }
     }
     $input->setPost($this->_router->getPost());
     $contructed = $this->_namespace . '\\' . ucfirst($this->_controller);
     $newController = new $contructed();
     $newController->{$this->_method}();
 }
コード例 #5
0
 public function __construct()
 {
     $this->app = App::getInstance();
     $this->view = View::getInstance();
     $this->config = $this->app->getConfig();
     $this->input = InputData::getInstance();
     $this->db = new SimpleDatabase();
     $this->session = $this->app->getSession();
     $this->path = isset($this->config->app['default_path']) ? $this->config->app['default_path'] : null;
 }
コード例 #6
0
 public function __construct()
 {
     $this->app = App::getInstance();
     $this->view = View::getInstance();
     $this->config = $this->app->getConfig();
     $this->input = InputData::getInstance();
     if (is_null($this->session)) {
         $this->session = new NativeSession('session');
     }
 }
コード例 #7
0
 private function BindModel($annotations)
 {
     $bindingNamespace = null;
     $appConfig = App::getInstance()->getConfig()->app;
     $namespaces = $appConfig['namespaces'];
     foreach ($namespaces as $key => $value) {
         if (strpos($key, "BindingModels")) {
             $bindingNamespace = $key;
         }
     }
     $bindingModelName = null;
     foreach ($annotations as $annotation) {
         $bindingAnnotation = explode(' ', $annotation);
         if ($bindingAnnotation[0] === 'BingingModel') {
             $bindingModelName = $bindingAnnotation[1];
         }
     }
     $bindingModel = null;
     if ($bindingNamespace && $bindingModelName) {
         $bindingModelClass = $bindingNamespace . "\\" . $bindingModelName;
         $bindingModel = new $bindingModelClass();
         $reflectionModel = new ReflectionClass($bindingModel);
         $properties = $reflectionModel->getProperties();
         $post = $this->input->post();
         foreach ($properties as $property) {
             $propertyName = $property->getName();
             $propertyDoc = $property->getDocComment();
             $annotations = array();
             preg_match_all('#@(.*?)\\n#s', $propertyDoc, $annotations);
             $set = 'set' . $propertyName;
             if ($annotations[1][0] === "Required" && !$post[$propertyName]) {
                 throw new \Exception("Field " . $propertyName . " is required");
             }
             if (array_key_exists($propertyName, $post)) {
                 $bindingModel->{$set}($post[$propertyName]);
             } else {
                 throw new \Exception("Field " . $propertyName . " is not accepted");
             }
         }
     }
     return $bindingModel;
 }
コード例 #8
0
 private function processController()
 {
     $input = InputData::getInstance();
     $input->setGet($this->_params);
     $input->setPost($this->_router->GetPost());
     $file = ucfirst($this->_namespace) . '\\' . ucfirst($this->_controller);
     $this->_controller = $file;
     $realPath = str_replace('\\', DIRECTORY_SEPARATOR, '../' . $file . '.php');
     $realPath = realpath($realPath);
     if (file_exists($realPath) && is_readable($realPath)) {
         $calledController = new $file();
         if (method_exists($calledController, $this->_method)) {
             if ($this->isValidRequestMethod($calledController, $this->_method)) {
                 // Create binding model
                 $refMethod = new \ReflectionMethod($calledController, $this->_method);
                 $doc = $refMethod->getDocComment();
                 // Validate accessibility
                 $this->ValidateAuthorization($doc);
                 if (preg_match('/@param\\s+\\\\?([\\s\\S]+BindingModel)\\s+\\$/', $doc, $match)) {
                     $bindingModelName = $match[1];
                     $bindingModelsNamespace = App::getInstance()->getConfig()->app['namespaces']['Models'] . 'BindingModels/';
                     $bindingModelsNamespace = str_replace('../', '', $bindingModelsNamespace);
                     $bindingModelPath = str_replace('/', '\\', $bindingModelsNamespace . $bindingModelName);
                     $bindingReflection = new \ReflectionClass($bindingModelPath);
                     $properties = $bindingReflection->getProperties();
                     $params = array();
                     foreach ($properties as $property) {
                         $name = $property->getName();
                         $value = $input->postForDb($name);
                         if ($value === null) {
                             throw new \Exception("Invalid binding model! Property '{$name}' not found", 400);
                         } else {
                             $params[$name] = $value;
                         }
                     }
                     $bindingModel = new $bindingModelPath($params);
                     Injector::getInstance()->loadDependencies($calledController);
                     $calledController->{strtolower($this->_method)}($bindingModel);
                 } else {
                     Injector::getInstance()->loadDependencies($calledController);
                     $calledController->{strtolower($this->_method)}();
                 }
                 exit;
             } else {
                 throw new \Exception("Method does not allow '" . ucfirst($this->_requestMethod) . "' requests!", 500);
             }
         } else {
             throw new \Exception("'" . $this->_method . "' not found in '" . $file . '.php', 404);
         }
     } else {
         throw new \Exception("File '" . $file . '.php' . "' not found!", 404);
     }
 }