Esempio n. 1
0
File: model.php Progetto: soanni/mvc
 public function getColumns()
 {
     if (empty($this->_columns)) {
         $primaries = 0;
         $columns = array();
         $class = get_class($this);
         $types = $this->_types;
         $inspector = new Inspector($this);
         $properties = $inspector->getClassProperties();
         $first = function ($array, $key) {
             if (!empty($array[$key]) && count($array[$key]) == 1) {
                 return $array[$key][0];
             }
             return null;
         };
         foreach ($properties as $property) {
             $propertyMeta = $inspector->getPropertyMeta($property);
             if (!empty($propertyMeta['@column'])) {
                 $name = preg_replace("#^_#", '', $property);
                 $primary = !empty($propertyMeta['@primary']);
                 $type = $first($propertyMeta, '@type');
                 $length = $first($propertyMeta, '@length');
                 $index = !empty($propertyMeta['@index']);
                 $readwrite = !empty($propertyMeta['@readwrite']);
                 $read = !empty($propertyMeta['@read']) || $readwrite;
                 $write = !empty($propertyMeta['@write']) || $readwrite;
                 $validate = !empty($propertyMeta['@validate']) ? $propertyMeta['@validate'] : false;
                 $label = $first($propertyMeta, '@label');
                 if (!in_array($type, $types)) {
                     throw new Exception\Type("{$type} is not a valid type");
                 }
                 if ($primary) {
                     $primaries++;
                 }
                 $columns[$name] = array('raw' => $property, 'name' => $name, 'primary' => $primary, 'type' => $type, 'length' => $length, 'index' => $index, 'read' => $read, 'write' => $write, 'validate' => $validate, 'label' => $label);
             }
         }
         if ($primaries !== 1) {
             throw new Exception\Primary("{$class} must have exactly one @primary column");
         }
         $this->_columns = $columns;
     }
     return $this->_columns;
 }
Esempio n. 2
0
 /**
  * Returns an associative array of column data.
  * @return type
  * @throws Exception\Type
  * @throws Exception\Primary
  */
 public function getColumns()
 {
     if (empty($_columns)) {
         $primaries = 0;
         $columns = array();
         $class = get_class($this);
         $types = $this->types;
         $inspector = new Inspector($this);
         $properties = $inspector->getClassProperties();
         $first = function ($array, $key) {
             if (!empty($array[$key]) && sizeof($array[$key]) == 1) {
                 return $array[$key][0];
             }
             return null;
         };
         $decimal = function ($array, $key) {
             if (!empty($array[$key]) && sizeof($array[$key]) == 2) {
                 return implode(",", $array[$key]);
             }
             return null;
         };
         foreach ($properties as $property) {
             $propertyMeta = $inspector->getPropertyMeta($property);
             if (!empty($propertyMeta["@column"])) {
                 $name = preg_replace("#^_#", "", $property);
                 $primary = !empty($propertyMeta["@primary"]);
                 $type = $first($propertyMeta, "@type");
                 switch ($type) {
                     case "decimal":
                         $length = $decimal($propertyMeta, "@length");
                         break;
                     default:
                         $length = $first($propertyMeta, "@length");
                         break;
                 }
                 $index = !empty($propertyMeta["@index"]);
                 $uindex = !empty($propertyMeta["@uindex"]);
                 $readwrite = !empty($propertyMeta["@readwrite"]);
                 $read = !empty($propertyMeta["@read"]) || $readwrite;
                 $write = !empty($propertyMeta["@write"]) || $readwrite;
                 $validate = !empty($propertyMeta["@validate"]) ? $propertyMeta["@validate"] : false;
                 $label = $first($propertyMeta, "@label");
                 if (!in_array($type, $types)) {
                     throw new Exception\Type("{$type} is not a valid type");
                 }
                 if ($primary) {
                     $primaries++;
                 }
                 $columns[$name] = array("raw" => $property, "name" => $name, "primary" => $primary, "type" => $type, "length" => $length, "index" => $index, "uindex" => $uindex, "read" => $read, "write" => $write, "validate" => $validate, "label" => $label);
             }
         }
         if ($primaries !== 1) {
             throw new Exception\Primary("{$class} must have exactly one @primary column");
         }
         $this->_columns = $columns;
     }
     return $this->_columns;
 }
Esempio n. 3
0
 protected function _pass($controller, $action, $parameters = array())
 {
     $name = ucfirst($controller);
     $this->_controller = $controller;
     $this->_action = $action;
     try {
         $instance = new $name(array("parameters" => $parameters));
         Registry::set('controller', $instance);
     } catch (\Exception $e) {
         throw new Exception\Controller("Controller {$name} is not found");
     }
     if (!method_exists($instance, $action)) {
         $instance->willRenderLayoutView = false;
         $instance->willRenderActionView = false;
         throw new Exception\Action("Action {$action} is not found");
     }
     $inspector = new Inspector($instance);
     $methodMeta = $inspector->getMethodMeta($action);
     if (!empty($methodMeta['@protected']) || !empty($methodMeta['@private'])) {
         throw new Exception\Action("Action {$action} is not found");
     }
     $hooks = function ($meta, $type) use($inspector, $instance) {
         if (isset($meta[$type])) {
             $run = array();
             foreach ($meta[$type] as $method) {
                 $hookMeta = $inspector->getMethodMeta($method);
                 if (in_array($method, $run) && !empty($hookMeta["@once"])) {
                     continue;
                 }
                 $instance->{$method}();
                 $run[] = $method;
             }
         }
     };
     $hooks($methodMeta, "@before");
     call_user_func_array(array($instance, $action), is_array($parameters) ? $parameters : array());
     $hooks($methodMeta, "@after");
     Registry::erase("controller");
 }
Esempio n. 4
0
 /**
  * Load a controller
  * @return boolean|string
  */
 private function _loadController($method, $controller, $action, $parameters = array())
 {
     $name = ucfirst($controller);
     $this->_controller = $controller;
     $this->_action = $action;
     Event::fire("framework.router.controller.before", array($controller, $parameters));
     try {
         $instance = new $name(array("parameters" => $parameters));
         Registry::set("controller", $instance);
     } catch (\Exception $e) {
         throw new \Exception("Controller {$name} not found", 404);
     }
     Event::fire("framework.router.controller.after", array($controller, $parameters));
     if (!empty($action)) {
         // If request method valid append to action
         if ($this->_validateRequestMethod($method)) {
             $action = strtolower($method) . ucfirst($action);
         }
         // Check if called method exists
         if (!method_exists($instance, $action)) {
             throw new \Exception("Action {$action} not found", 404);
         }
         // Check if method is callable (checking for @protected or @private)
         $inspector = new Inspector($instance);
         $methodMeta = $inspector->getMethodMeta($action);
         if (!empty($methodMeta["@protected"]) || !empty($methodMeta["@private"])) {
             throw new \Exception("Action {$action} not found", 404);
         }
     }
     $hooks = function ($meta, $type) use($inspector, $instance) {
         if (isset($meta[$type])) {
             $run = array();
             foreach ($meta[$type] as $method) {
                 $hookMeta = $inspector->getMethodMeta($method);
                 if (in_array($method, $run) && !empty($hookMeta["@once"])) {
                     contiue;
                 }
                 $instance->{$method}();
                 $run[] = $method;
             }
         }
     };
     Event::fire("framework.router.beforehooks.before", array($action, $parameters));
     $hooks($methodMeta, "@before");
     Event::fire("framework.router.beforehooks.after", array($action, $parameters));
     Event::fire("framework.router.action.before", array($action, $parameters));
     // Call Method or resort to default
     switch (true) {
         case !empty($action):
             call_user_func_array(array($instance, $action), is_array($parameters) ? $parameters : array());
             break;
         default:
             call_user_func_array(array($instance, $this->_defaultAction), is_array($parameters) ? $parameters : array());
             break;
     }
     Event::fire("framework.router.action.after", array($action, $parameters));
     Event::fire("framework.router.afterhooks.before", array($action, $parameters));
     $hooks($methodMeta, "@after");
     Event::fire("framework.router.afterhooks.after", array($action, $parameters));
     // unset controller
     Registry::erase("controller");
 }
Esempio n. 5
0
 /**
  * Return property metadata for any designated 
  * with the @column flag. Includes a test for valid type
  * @return array Column metadata
  */
 public function getColumns()
 {
     if (empty($_columns)) {
         $primaries = 0;
         $columns = array();
         $class = get_class($this);
         $types = $this->types;
         // Create Inspector instance to allow reading of metadata
         $inspector = new Inspector($this);
         $properties = $inspector->getClassProperties();
         // Utility function to return the first item in a metadata array,
         // i.e., The value of the length flag
         $first = function ($array, $key) {
             if (!empty($array[$key]) && count($array[$key]) == 1) {
                 return $array[$key][0];
             }
             return null;
         };
         // Loop through all properties in the model
         foreach ($properties as $property) {
             $propertyMeta = $inspector->getPropertyMeta($property);
             // Select only those with the @column flag and
             // retrieve metadata
             if (!empty($propertyMeta["@column"])) {
                 $name = preg_replace("#^_#", "", $property);
                 $primary = !empty($propertyMeta["@primary"]);
                 $type = $first($propertyMeta, "@type");
                 $length = $first($propertyMeta, "@length");
                 $index = !empty($propertyMeta["@index"]);
                 $readwrite = !empty($propertyMeta["@readwrite"]);
                 $read = !empty($propertyMeta["@read"]) || $readwrite;
                 $write = !empty($propertyMeta["@write"]) || $readwrite;
                 // Not a valid property type so throw an error
                 if (!in_array($type, $types)) {
                     throw new \Exception("{$type} is not a valid column type", 500);
                 }
                 if ($primary) {
                     $primaries++;
                 }
                 $columns[$name] = array("raw" => $property, "name" => $name, "primary" => $primary, "type" => $type, "length" => $length, "index" => $index, "read" => $read, "write" => $write);
             }
         }
         // More than one primary column so throw an error
         if ($primaries !== 1) {
             throw new \Exception("{$class} must have exactly one @primary column", 500);
         }
         $this->_columns = $columns;
     }
     return $this->_columns;
 }