public function color($str, $cor = 'Blue') { $color = ['Black' => '0;30', 'Red' => '0;31', 'Green' => '0;32', 'Brown' => '0;33', 'Blue' => '0;34', 'Purple' => '0;35', 'Cyan' => '0;36']; $cor = Inflector::camelize($cor); if (empty($color[$cor])) { $cor = 'Black'; } $str = var_export($str, true); $this->out("[" . $color[$cor] . "m" . $str . "[0m"); }
public static function write($msg, $level = 'error') { $level = strtoupper(Inflector::camelize($level)); $diretorio = ROOT . 'src/tmp/logs/'; $filepath = $diretorio . 'log-' . $level . '-' . date('Y-m-d') . '.log'; $r = new Request(); $msg = var_export($msg, true); $message = ['Nivel: ' . $level, 'Time: ' . date('H:i:s'), 'Path: ' . implode('/', $r->path), 'Uri: ' . implode('/', $r->uri), 'Data: ' . print_r($r->data, true), 'Params: ' . implode('/', $r->params), 'Query: ' . print_r($r->query, true), $msg]; $message = implode("\n", $message) . "\n\n"; if (!($fp = @fopen($filepath, FOPEN_WRITE_CREATE))) { return FALSE; } flock($fp, LOCK_EX); fwrite($fp, $message); flock($fp, LOCK_UN); fclose($fp); @chmod($filepath, FILE_WRITE_MODE); return TRUE; }
/** * Executa as chamadas dos dados referente as informações vido da navegação. */ public function run() { $this->controller = $this->request->controller; $this->action = $this->request->action; if (!isset($this->uri)) { $this->uri = []; } else { $this->uri = array_slice($this->uri, 2); } $path = ''; if (count($this->path) > 0) { $path = implode('\\', $this->path); if (trim($path) != '') { $path = Inflector::camelize($path) . '\\'; } } $controller = 'App\\Controller\\' . $path . $this->controller . 'Controller'; $class_name = ROOT . str_replace('\\', DS, $controller) . '.php'; $class_name = str_replace(DS . 'App' . DS, DS . 'src' . DS, $class_name); if (!file_exists($class_name)) { $ex = new \Core\MyException(); $ex->layout = 'default'; $ex->show_404('pagina não localizada.'); } else { $controller = new $controller($this->request, new Session(), new Auth()); $action = $this->action; call_user_func_array([$controller, 'beforeController'], $this->uri); if (method_exists($controller, $action)) { call_user_func_array([$controller, $action], $this->uri); } else { call_user_func_array([$controller, '_error'], $this->uri); } call_user_func_array([$controller, 'afterController'], $this->uri); call_user_func_array([$controller, 'beforeRender'], $this->uri); $controller->render(); } }
/** * * Cria um input * * @param string $field * @param array $options * @return string */ public function input($field, $options = []) { $default = ['type' => 'text', 'name' => $this->getNameChave($field), 'id' => $this->getId($field), 'value' => $this->request->data($field), 'label' => '', 'autocomplete' => 'off', 'div' => []]; $options = \Core\Hash::merge($default, $options); if (isset($options['required'])) { if ((bool) $options['required'] === false) { unset($options['required']); } else { $options['required'] = 'required'; } } if (!in_array($options['type'], $this->types)) { $options['type'] = 'text'; } $label = ''; if ($options['label'] !== false) { if ($options['type'] != 'checkbox' and $options['type'] != 'radio') { $label = $this->label($options['label'], ['for' => $options['id']]); } } $classField = 'field'; if ($options['type'] == 'radio' or $options['type'] == 'checkbox') { $classField = ''; } $div = ['class' => $options['type'] . ' ' . $classField . ' ' . (isset($options['required']) ? 'required' : '')]; if (!empty($options['div'])) { if (isset($options['div']['class'])) { $options['div']['class'] .= ' ' . $div['class']; } $div = \Core\Hash::merge($div, $options['div']); } unset($options['div']); if ($options['type'] == 'hidden') { return $this->{\Core\Inflector::parameterize($options['type'], '_')}($options); } $error = ''; if (!empty($this->error[$field])) { $div['class'] .= ' has-error'; $error = '<div style="padding: 5px; color: red;">' . implode('<br />', $this->error[$field]) . '</div>'; unset($this->error[$field]); } if ($this->_label == 'left') { return $this->html->tags('div', $div, true, $label . $this->{\Core\Inflector::parameterize($options['type'], '_')}($options) . $error); } else { return $this->html->tags('div', $div, true, $this->{\Core\Inflector::parameterize($options['type'], '_')}($options) . $label . $error); } }
/** * * Cria uma tag HTML * * @param string $tag * @param array $options * @param boolean $close * @param string $label * @return string */ public function tags($tag, $options = [], $close = true, $label = null) { $tag = strtolower($tag); $return = '<' . $tag . ' '; if (count($options) > 0) { foreach ($options as $key => $value) { $key = (string) $key; $return .= \Core\Inflector::parameterize($key) . '="' . trim($value) . '" '; } } if ($close) { return $return . '>' . $label . '</' . $tag . '>'; } return $return . '/>'; }
/** * * Padroniza o id * * @param string $name * @param string|null $prefix * @return string */ public function getId($name, $prefix = null) { if (!is_null($prefix)) { $name = Inflector::parameterize($prefix . '-' . $name); } $name = Inflector::parameterize($name); return str_replace('_', '-', $name); }
/** * * função que faz um pre tratamento nas condições de consulta na tabela do banco de dados. * * @param string $campos * @param array $arguments * @return string */ private function _argumentos($campos, $arguments, $tipo = '=') { $type = 'AND'; if (stripos($campos, 'And') !== false) { $campos = explode('And', $campos); } elseif (stripos($campos, 'Or') !== false) { $campos = explode('Or', $campos); $type = 'OR'; } else { $campos = [$campos]; } foreach ($campos as $key => $value) { $this->where(strtolower(\Core\Inflector::underscore($value)), $arguments[$key], $tipo, $type); } }
public function prepareUrl($url) { $defautl = ['action' => $this->action, 'controller' => $this->controller, 'path' => $this->path, 'params' => '', 'query' => []]; $url = array_merge($defautl, $url); if (!empty($url['path'])) { if (is_array($url['path'])) { foreach ($url['path'] as $key => $value) { $url['path'][$key] = Inflector::underscore(Inflector::camelize($value)); } } else { $url['path'] = Inflector::underscore(Inflector::camelize($url['path'])); } } if (!empty($url['params'])) { foreach ($url['params'] as $key => $value) { $url['params'][$key] = Inflector::slug($value); } } $oldUrl = $url; unset($oldUrl['action'], $oldUrl['controller'], $oldUrl['path'], $oldUrl['params'], $oldUrl['query'], $oldUrl['?']); if (!empty($oldUrl)) { //$url['params'] = array_merge($url['params'], $oldUrl); foreach ($oldUrl as $key => $value) { unset($url[$key]); $url['params'][$key] = $value; } } if (!empty($url['?'])) { $url['query'] = Hash::merge($url['query'], $url['?']); unset($url['?']); } $_url = []; if (!empty($url['path'])) { if (is_array($url['path'])) { $_url['path'] = implode('/', $url['path']); } else { $_url['path'] = $url['path']; } } if (!empty($url['controller'])) { $_url['controller'] = Inflector::underscore($url['controller']); } if (!empty($url['action'])) { $_url['action'] = Inflector::underscore($url['action']); } if (!empty($url['params']) and is_array($url['params'])) { $_url['params'] = implode('/', $url['params']); } if (!empty($url['query']) and is_array($url['query'])) { $_url['query'] = '?' . http_build_query($url['query']); } return $_url; }