Exemplo n.º 1
0
 /**
  * Loads the given configs into \Pvik\Core\Config.
  * @param array $configPaths
  * @return \Pvik\Core\Core
  */
 public function loadConfig(array $configPaths)
 {
     foreach ($configPaths as $configPath) {
         Config::load(Path::realPath($configPath));
     }
     Log::writeLine('[Info] Loaded: ' . implode(",", $configPaths));
     return $this;
 }
Exemplo n.º 2
0
 /**
  * Executes a statement.
  * @param string $queryString
  * @return mixed 
  */
 public function execute($queryString)
 {
     Log::writeLine('Executing querystring: ' . $queryString);
     $result = mysql_query($queryString);
     if (!$result) {
         throw new \Exception('Could not execute following statement: ' . $queryString . "\n" . 'MySQLError: ' . mysql_error());
     }
     return $result;
 }
Exemplo n.º 3
0
 /**
  *
  * @param string $viewPath
  * @param Controller $controller 
  */
 public function __construct($viewPath, Controller $controller)
 {
     $this->contents = new KeyValueArray();
     $this->viewPath = $viewPath;
     $this->controller = $controller;
     $this->viewData = $this->controller->getViewData();
     $this->helper = new \Pvik\Web\ViewHelpers\HtmlHelper();
     $this->executePartialCode($this->viewPath);
     if ($this->masterPagePath != null) {
         Log::writeLine('Executing masterpage: ' . $this->masterPagePath);
         $baseMasterPage = new MasterPage($this->realPath($this->masterPagePath), $this);
     }
 }
Exemplo n.º 4
0
 /**
  * Execute a action from a controller.
  * @param string $controllerName
  * @param string $actionName
  * @param KeyValueArray $parameters 
  */
 public static function executeController($controllerName, $actionName, Request $request)
 {
     $controllerClassName = $controllerName;
     if ($controllerClassName[0] !== '\\') {
         $controllerClassName = Config::$config['DefaultNamespace'] . Config::$config['DefaultNamespaceControllers'] . '\\' . $controllerClassName;
     }
     $controllerInstance = new $controllerClassName($request, $controllerName);
     /* @var $controllerInstance \Pvik\Web\Controller */
     $actionFunctionName = $actionName . 'Action';
     if (method_exists($controllerInstance, $actionFunctionName)) {
         Log::writeLine('Executing action: ' . $actionFunctionName);
         $controllerInstance->setCurrentActionName($actionName);
         // execute action
         $controllerInstance->{$actionFunctionName}();
     } else {
         throw new \Exception('Action doesn\'t exists: ' . $controllerClassName . '->' . $actionFunctionName);
     }
 }
Exemplo n.º 5
0
 /**
  * Tries to load a class.
  * @param String $class  
  */
 protected function loadClass($class)
 {
     if ($class[0] !== '\\') {
         $class = '\\' . $class;
     }
     $name = $class;
     foreach ($this->getNamespaceAssociationList() as $namespace => $path) {
         if (strpos($name, $namespace . '\\') === 0) {
             // starts with
             $name = str_replace($namespace, $path, $name);
             break;
         }
     }
     $path = str_replace('\\', '/', $name);
     $path = str_replace('//', '/', $path);
     $path = Path::realPath($path . '.php');
     if (file_exists($path)) {
         require $path;
         if (class_exists('\\Pvik\\Core\\Log')) {
             Log::writeLine('[Include] ' . $path);
         }
         return true;
     }
 }
Exemplo n.º 6
0
 /**
  * Checks if a url matches with an route. 
  * @param string $orignalUrl
  * @param array $route
  * @return bool
  */
 protected function urlIsMatching($orignalUrl, $route)
 {
     $routeUrl = $route['Url'];
     if ($routeUrl == '*' || strtolower($orignalUrl) == $routeUrl) {
         $request = new Request();
         $request->setRoute($route);
         $request->setUrl($orignalUrl);
         return $request;
     } elseif (strpos($routeUrl, '{') !== false && strpos($routeUrl, '}') !== false) {
         // contains a variable
         $routeUrlParts = explode('/', $routeUrl);
         $orignalUrlParts = explode('/', $orignalUrl);
         // have the same part length
         if (count($routeUrlParts) == count($orignalUrlParts)) {
             for ($index = 0; $index < count($routeUrlParts); $index++) {
                 if (strlen($routeUrlParts[$index]) >= 3 && $routeUrlParts[$index][0] == '{') {
                     // it's a variable
                     $key = substr($routeUrlParts[$index], 1, -1);
                     if (isset($route['Parameters'][$key]) && !preg_match($route['Parameters'][$key], $orignalUrlParts[$index])) {
                         return null;
                     }
                 } else {
                     if (strtolower($routeUrlParts[$index]) != $orignalUrlParts[$index]) {
                         // not matching
                         return null;
                     }
                 }
             }
             Log::writeLine('Route matching: ' . $route['Url']);
             $request = new Request();
             $request->setRoute($route);
             $request->setUrl($orignalUrl);
             // matching successfull
             // save url parameter
             for ($index = 0; $index < count($routeUrlParts); $index++) {
                 if (strlen($routeUrlParts[$index]) >= 3 && $routeUrlParts[$index][0] == '{') {
                     // it's a variable
                     // the key is the name between the brakets
                     $key = substr($routeUrlParts[$index], 1, -1);
                     // add to url parameters
                     $request->getParameters()->add($key, $orignalUrlParts[$index]);
                     if (isset($route['Parameters'][$key])) {
                         Log::writeLine('Url parameter: ' . $key . ' -> ' . $route['Parameters'][$key] . ' -> ' . $orignalUrlParts[$index]);
                     } else {
                         Log::writeLine('Url parameter: ' . $key . ' -> ' . $orignalUrlParts[$index]);
                     }
                 }
             }
             return $request;
         }
     }
     return null;
 }
Exemplo n.º 7
0
 /**
  * Redirects to another controllers action with passing the original parameters.
  * @param string $controllerName
  * @param string $actionName 
  */
 protected function redirectToController($controllerName, $actionName, Request $request = null)
 {
     if ($request == null) {
         $request = $this->request;
     }
     Log::writeLine('Redirecting to controller: ' . $controllerName);
     Log::writeLine('Redirecting to action: ' . $actionName);
     ControllerManager::executeController($controllerName, $actionName, $request);
 }