/** * Ler um model e gera um SQL referente * @param Model $model instância do model para gerar o SQL * @return string retorna o SQL do model */ protected function getTable(Model $model) { $annotation = Annotation::get(get_class($model)); $class = $annotation->getClass(); if ($class->Entity) { $table = new stdClass(); $table->Name = $class->Entity->Name ? $class->Entity->Name : get_class($model); $table->Columns = array(); $table->Keys = array(); foreach ($model as $field => $value) { $property = $annotation->getProperty($field); if ($this->isField($property)) { $column = new stdClass(); $column->Name = $this->getField($field, $property); if (!$column->Name) { $column->Name = $field; } $column->Type = $this->getType($property); $column->Nullable = $this->getNullable($property); $table->Columns[] = $column; if ($this->isKey($property)) { $table->Keys[] = $this->getKey($field, $property); } } } return $this->getSql($table); } }
/** * Identifica e retorna o nome da propriedade que é uma chave primária * @return string nome da propriedade */ protected function _getKey() { if ($this->_key) { return $this->_key; } $class = get_called_class(); $annotation = Annotation::get($class); foreach ($this as $p => $v) { if ($p != '_isNew') { $property = $annotation->getProperty($p); if ($property->Column && $property->Column->Key) { return $this->_key = $p; } } } }
/** * Monta a instrunção SQL a partir da operações chamadas e executa a instrução * @throws DatabaseException disparada caso ocorra algum erro na execução da operação * @return array retorna um array com instâncias do Model */ public function all() { if (func_num_args() > 0) { $reflectionMethod = new ReflectionMethod('MysqlDatasource', 'where'); $args = func_get_args(); $reflectionMethod->invokeArgs($this, $args); } $sql = $this->getSQL(); Debug::addSql($sql, $this->where_params); $stmt = $this->connection()->prepare($sql); $status = $stmt->execute($this->where_params); if (!$status) { $error = $stmt->errorInfo(); throw new DatabaseException($error[2]); } if ($stmt->rowCount() > 0) { $results = array(); $annotation = Annotation::get($this->clazz); $i = 0; while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) { if ($this->calc) { return $result['calc']; } $model = $this->clazz; $object = new $model(); $object->_setNew(); foreach ($result as $field => $value) { if ($this->config['validate'] || property_exists($object, $field)) { $property = $annotation->getProperty($field); $type = strtolower($property->Column->Type); $types = array('boolean', 'integer', 'float', 'string', 'array', 'object', 'null'); if ($type === 'double') { $type = 'float'; } elseif ($type === 'int') { $type = 'integer'; } elseif ($type === 'boolean') { $value = $value == 1; } elseif ($type === 'datetime') { $type = 'string'; } if (!in_array($type, $types)) { throw new DatabaseException('O tipo de dados ' . $type . ' é inválido'); } if ($type !== 'datetime') { settype($value, $type); } $object->{$field} = $value; } } $results[$i] = $object; ++$i; } return $results; } return array(); }
/** * Verifica e retorna o master page deve ser renderizada * @throws MethodNotFoundException disparado caso método referente ao nome da master não seja encontrado dentro da MasterController * @throws MethodVisibilityException disparado caso método referente ao nome da master não esteja público * @return string retorna o nome da master page */ private function master() { $annotation = Annotation::get(CONTROLLER); $reflection = new ReflectionClass(CONTROLLER); $tpl = null; if ($reflection->hasMethod('__construct')) { if (property_exists($annotation->getMethod('__construct'), 'Master')) { $tpl = $annotation->getMethod('__construct')->Master; } } $action = $annotation->getMethod(ACTION); $tpl_action = isset($action->Master) ? $action->Master : null; if ($tpl_action) { $tpl = $tpl_action; } if (!$tpl) { $tpl = Config::get('default_master'); } define('master', $tpl); define('MASTER', $tpl); return $tpl; }
/** * Verifica se o usuário pode acessar a página de acordo com sua autenticação e a anotação do controller. Se não tiver permissão * é redirecionado para a página de login defina nas configurações * @return void */ private function auth() { $annotation = Annotation::get(CONTROLLER); $roles = null; if (method_exists(CONTROLLER, '__construct')) { $method = new ReflectionMethod(CONTROLLER, '__construct'); if ($method->isPublic()) { $construct = $annotation->getMethod('__construct'); if (isset($construct->Auth)) { $roles = $construct->Auth; } } } $method = $annotation->getMethod(ACTION); $auth_action = isset($method->Auth) ? $method->Auth : null; if ($auth_action) { $roles = $auth_action; } if ($auth_action == '*' || is_array($auth_action) && in_array('*', $auth_action)) { $roles = null; } if ($roles && !is_array($roles)) { $roles = array($roles); } if ($roles) { call_user_func_array('Auth::allow', $roles); } }
protected final function _xml($data) { return $this->_return('xml', $data); } /** * Redireciona a requisição para outra página * @param string $param1 se for passado sozinho e inicioar "~/" define a URL (dentro da aplicação) na qual será redirecionada, caso seja um string, define o nome da action dentro do controller que será redicionada, se for passado junto com segundo parâmetro, define o nome do controller * @param string $param2 define o nome da action que será redirecionada dentro do controller informado no primeiro parâmetro * @return void */ protected final function _redirect($param1, $param2 = '', $params = null) { if (preg_match('@^~/(.*)@', $param1)) { $this->_location(ROOT_VIRTUAL . trim($param1, '~/')); } if ($param1 && !$param2) { $this->_location(ROOT_VIRTUAL . CONTROLLER . '/' . $param1); } if ($param1 && $param2) { $this->_location(ROOT_VIRTUAL . $param1 . '/' . $param2); } } /** * Método que executa a função de redirecionadomento * @param string $location local do redirecionamento * @return void */ private final function _location($location) {