From a09f7cf94c4af759c9f03e92771baae316ea6f44 Mon Sep 17 00:00:00 2001 From: Carlos Eduardo Date: Sat, 23 Sep 2017 23:55:36 -0300 Subject: [PATCH 1/2] Fixies --- base/cmd | 16 ++-- base/config/repository.php | 45 ++++++++++- base/console/commands/build.class.php | 78 +++++-------------- base/console/commands/dumproutes.class.php | 64 +++++++++++++++ base/console/commands/environment.class.php | 86 +++++++++++++++++++++ base/core/routes/annotation.class.php | 2 +- base/core/routes/route.class.php | 2 +- 7 files changed, 227 insertions(+), 66 deletions(-) create mode 100644 base/console/commands/dumproutes.class.php create mode 100644 base/console/commands/environment.class.php diff --git a/base/cmd b/base/cmd index 654d8b6..5fbdf27 100644 --- a/base/cmd +++ b/base/cmd @@ -1,8 +1,6 @@ register('environment', new \Console\Commands\Environment); +$cmd->register('dump-routes', new \Console\Commands\DumpRoutes); +$cmd->alias('environment', 'env'); $cmd->register('build', new \Console\Commands\Build); -$cmd->run(); \ No newline at end of file + +try { + $cmd->run(); +} catch (\Exception $e) { + $cmd->writeln("Exception: {$e->getMessage()}"); +} diff --git a/base/config/repository.php b/base/config/repository.php index 3d01ff1..1f45a33 100644 --- a/base/config/repository.php +++ b/base/config/repository.php @@ -108,4 +108,47 @@ function is_class_method($type="public", $method, $class) { } catch (Exception $e) { exit('Erro: '.$e->getMessage()); } -} \ No newline at end of file +} + +/** + * Return a max size for upload + * + * @return void + */ +function file_upload_max_size() { + static $max_size = -1; + + if ($max_size < 0) { + // Start with post_max_size. + $post_max_size = parse_size(ini_get('post_max_size')); + if ($post_max_size > 0) { + $max_size = $post_max_size; + } + + // If upload_max_size is less, then reduce. Except if upload_max_size is + // zero, which indicates no limit. + $upload_max = parse_size(ini_get('upload_max_filesize')); + if ($upload_max > 0 && $upload_max < $max_size) { + $max_size = $upload_max; + } + } + return $max_size; + } + + /** + * Parser for max upload + * + * @param string $size + * @return void + */ + function parse_size($size) { + $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size. + $size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size. + if ($unit) { + // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by. + return round($size * pow(1024, stripos('bkmgtpezy', $unit[0]))); + } + else { + return round($size); + } + } \ No newline at end of file diff --git a/base/console/commands/build.class.php b/base/console/commands/build.class.php index a35b0be..12b4504 100644 --- a/base/console/commands/build.class.php +++ b/base/console/commands/build.class.php @@ -10,78 +10,40 @@ class Build extends \CMP\Command\Command { public function execute(\CMP\Console $console, $args = []) { $this->console = $console; - - try { - $this->setArgs($args); - - $this->copyConfig($args['--env']); - $this->configure(); - - $this->dumpRoutesCache(); - } catch (\Exception $e) { - $this->console->writeln("Exception: {$e->getMessage()}"); - } - + $this->setArgs($args); + + $this->build(); return TRUE; } private function setArgs($args) { $this->app = $args['--app']; + $this->env = $args['--env']; $this->appPath = APPS.$this->app.DS; } - private function configure() { - if (empty($this->app) || !is_dir($this->appPath)) { - throw new \Exception('Informe um app válido!'); - // $this->console->writeln(''); - return false; - } - - // try { - $this->config = \Core\Config::SetApplication($this->app); - // } catch (InvalidApplicationException $e) { - // $this->console->writeln(''.$e->getMessage().''); - // return FALSE; - // } - - $this->loadFile($this->appPath.'config.php'); - - return TRUE; - } + public function getOptionCollection() { + $collection = new \CMP\Command\OptionCollection(); + $collection->add('a|app:', 'Application folder name'); + $collection->add('e|env:?', 'Environment [prod|dev] (default: dev)', 'dev'); - private function dumpRoutesCache() { - // try { - $this->console->writeln('Dumping routes...'); - $annotation = new \Core\Routes\Annotation(); - @unlink($this->appPath.'routes.cache.php'); - $annotation->dump(); - $this->console->writeln('Routes dumped with success!'); - // } catch (\Exception $e) { - // $this->console->writeln("{$e->getMessage()}"); - // } + return $collection; } - private function copyConfig($env) { - if (file_exists($this->appPath.'config.php.'.$env)) - copy($this->appPath.'config.php.'.$env, $this->appPath.'config.php'); - else - throw new \Exception('Config file does\'t exists for environment '.$env); - } + private function build() { + $setEnv = $this->console->getCommand('environment'); + $setEnv->execute($this->console, [ + '--env' => $this->env, + '--app' => $this->app + ]); - private function loadFile($file) { - if (file_exists($file)) - require $file; - else - throw new \Exception("Can't load file: {$file}"); - } + $dumpRoutes = $this->console->getCommand('dump-routes'); + $dumpRoutes->execute($this->console, [ + '--app' => $this->app + ]); - public function getOptionCollection() { - $collection = new \CMP\Command\OptionCollection(); - $collection->add('a|app:', 'Application folder name'); - $collection->add('env:?', 'Environment [prod|dev] (default: dev)', 'dev'); - return $collection; } -} \ No newline at end of file +} diff --git a/base/console/commands/dumproutes.class.php b/base/console/commands/dumproutes.class.php new file mode 100644 index 0000000..2c6f196 --- /dev/null +++ b/base/console/commands/dumproutes.class.php @@ -0,0 +1,64 @@ +console = $console; + + $this->setArgs($args); + + $this->configure(); + + $this->dumpRoutesCache(); + } + + + private function setArgs($args) { + $this->app = $args['--app']; + $this->appPath = APPS.$this->app.DS; + } + + private function dumpRoutesCache() { + $this->console->writeln('Dumping routes...'); + $annotation = new \Core\Routes\Annotation(); + @unlink($this->appPath.'routes.cache.php'); + $annotation->dump(); + } + + private function configure() { + if (empty($this->app) || !is_dir($this->appPath)) { + throw new \Exception('Informe um app válido!'); + // $this->console->writeln(''); + return false; + } + + if (is_null($this->config = $this->console->share('config'))) { + $this->config = $this->console->share('config', \Core\Config::SetApplication($this->appPath)); + $this->loadFile($this->appPath.'config.php'); + } + + return TRUE; + } + + private function loadFile($file) { + if (file_exists($file)) + require $file; + else + throw new \Exception("Can't load file: {$file}"); + } + + public function getOptionCollection() { + $collection = new \CMP\Command\OptionCollection(); + $collection->add('a|app:', 'Application folder name'); + + return $collection; + } + + +} diff --git a/base/console/commands/environment.class.php b/base/console/commands/environment.class.php new file mode 100644 index 0000000..42d53f1 --- /dev/null +++ b/base/console/commands/environment.class.php @@ -0,0 +1,86 @@ +console = $console; + + $this->setArgs($args); + + $this->console->writeln('Setting environment: '.$this->env); + + $this->copyConfig(); + + $this->configure(); + + $this->copyHtaccess(); + + return TRUE; + } + + private function setArgs($args) { + $this->app = $args['--app']; + $this->env = $args['--env']; + $this->appPath = APPS.$this->app.DS; + + } + + private function copyConfig() { + $this->console->writeln('Setting config file...'); + $path = "{$this->appPath}config.{$this->env}.php"; + if (file_exists($path)) + copy($path, "{$this->appPath}config.php"); + else + throw new \Exception('Config file does\'t exists for environment '.$this->env); + } + + private function copyHtaccess() { + $path = "{$this->config->public}/{$this->env}.htaccess"; + $newPath = "{$this->config->public}/.htaccess"; + $this->console->writeln("Setting htaccess file"); + $this->console->writeln("From: {$path}"); + $this->console->writeln("To: {$newPath}"); + if (file_exists($path)) + copy($path, $newPath); + else + throw new \Exception('HTACCESS file does\'t exists for environment '.$this->env); + } + + private function configure() { + if (empty($this->app) || !is_dir($this->appPath)) { + throw new \Exception('Informe um app válido!'); + // $this->console->writeln(''); + return false; + } + + if (is_null($this->config = $this->console->share('config'))) { + $this->config = $this->console->share('config', \Core\Config::SetApplication($this->appPath)); + $this->loadFile($this->appPath.'config.php'); + } + + return TRUE; + } + + private function loadFile($file) { + if (file_exists($file)) + require $file; + else + throw new \Exception("Can't load file: {$file}"); + } + + public function getOptionCollection() { + $collection = new \CMP\Command\OptionCollection(); + $collection->add('a|app:', 'Application folder name'); + $collection->add('e|env:', 'Environment [prod|dev] (default: dev)', 'dev'); + + return $collection; + } + +} diff --git a/base/core/routes/annotation.class.php b/base/core/routes/annotation.class.php index 34be000..3c95d02 100644 --- a/base/core/routes/annotation.class.php +++ b/base/core/routes/annotation.class.php @@ -55,7 +55,7 @@ public function dump() { $controllers = rtrim($this->config->controllers ?: ($this->config->dir . 'controller'), '/ ') . '/'; if (!is_dir($controllers)) - throw new \InvalidArgumentException('O diretório de Controllers não foi configurado corretamente.'); + throw new \InvalidArgumentException("O diretório de Controllers não foi configurado corretamente `{$controllers}`."); $files = scandir($controllers); diff --git a/base/core/routes/route.class.php b/base/core/routes/route.class.php index 76e52cf..0483c39 100644 --- a/base/core/routes/route.class.php +++ b/base/core/routes/route.class.php @@ -302,7 +302,7 @@ public function controller($controller) { $controller = str_replace(':', '\\', $controller); $this->setImutableProperty('controller', $controller); if (!$this->checkControllerIsValid()) - throw new InvalidPropertyException('O controller informado é inválido.'); + throw new InvalidPropertyException('O controller informado é inválido: '.$controller); return $this; } From 38576dd439f8078c2269f611980cd3cbce26ca96 Mon Sep 17 00:00:00 2001 From: Carlos Eduardo Date: Sun, 8 Oct 2017 18:16:44 -0300 Subject: [PATCH 2/2] Fixies and Updates --- .gitignore | 5 ++++- .../{config.php => environments/dev.php} | 3 ++- .../{config.php.dev => environments/prod.php} | 3 ++- .../stage.php} | 3 ++- base/console/commands/environment.class.php | 13 ++++++----- composer.json | 2 +- public_html/{.htaccess => dev.htaccess} | 2 +- public_html/index.php | 5 +---- public_html/prod.htaccess | 22 +++++++++++++++++++ public_html/stage.htaccess | 22 +++++++++++++++++++ 10 files changed, 64 insertions(+), 16 deletions(-) rename app/example/{config.php => environments/dev.php} (79%) rename app/example/{config.php.dev => environments/prod.php} (79%) rename app/example/{config.php.prod => environments/stage.php} (79%) rename public_html/{.htaccess => dev.htaccess} (93%) create mode 100644 public_html/prod.htaccess create mode 100644 public_html/stage.htaccess diff --git a/.gitignore b/.gitignore index 778538d..5fb6eb0 100644 --- a/.gitignore +++ b/.gitignore @@ -70,4 +70,7 @@ Temporary Items /.idea/markdown-navigator /.idea/base.iml /.idea/blade.xml -/app/example/routes.cache.php + +# Application Files +/app/*/routes.cache.php +/app/*/config.php \ No newline at end of file diff --git a/app/example/config.php b/app/example/environments/dev.php similarity index 79% rename from app/example/config.php rename to app/example/environments/dev.php index d867f5c..191db07 100644 --- a/app/example/config.php +++ b/app/example/environments/dev.php @@ -19,6 +19,7 @@ Config::Set([ - 'url' => 'http://localhost/Base/public_html/', + 'url' => 'http://localhost/base/public_html/', + 'public' => dirname(dirname(__DIR__)).'/public_html', 'name' => 'example', ]); \ No newline at end of file diff --git a/app/example/config.php.dev b/app/example/environments/prod.php similarity index 79% rename from app/example/config.php.dev rename to app/example/environments/prod.php index d867f5c..191db07 100644 --- a/app/example/config.php.dev +++ b/app/example/environments/prod.php @@ -19,6 +19,7 @@ Config::Set([ - 'url' => 'http://localhost/Base/public_html/', + 'url' => 'http://localhost/base/public_html/', + 'public' => dirname(dirname(__DIR__)).'/public_html', 'name' => 'example', ]); \ No newline at end of file diff --git a/app/example/config.php.prod b/app/example/environments/stage.php similarity index 79% rename from app/example/config.php.prod rename to app/example/environments/stage.php index d867f5c..191db07 100644 --- a/app/example/config.php.prod +++ b/app/example/environments/stage.php @@ -19,6 +19,7 @@ Config::Set([ - 'url' => 'http://localhost/Base/public_html/', + 'url' => 'http://localhost/base/public_html/', + 'public' => dirname(dirname(__DIR__)).'/public_html', 'name' => 'example', ]); \ No newline at end of file diff --git a/base/console/commands/environment.class.php b/base/console/commands/environment.class.php index 42d53f1..1ec6ae6 100644 --- a/base/console/commands/environment.class.php +++ b/base/console/commands/environment.class.php @@ -28,15 +28,15 @@ public function execute(\CMP\Console $console, $args = []) { private function setArgs($args) { $this->app = $args['--app']; $this->env = $args['--env']; - $this->appPath = APPS.$this->app.DS; + $this->appPath = APPS.$this->app; } private function copyConfig() { $this->console->writeln('Setting config file...'); - $path = "{$this->appPath}config.{$this->env}.php"; + $path = "{$this->appPath}/environments/{$this->env}.php"; if (file_exists($path)) - copy($path, "{$this->appPath}config.php"); + copy($path, "{$this->appPath}/config.php"); else throw new \Exception('Config file does\'t exists for environment '.$this->env); } @@ -50,7 +50,8 @@ private function copyHtaccess() { if (file_exists($path)) copy($path, $newPath); else - throw new \Exception('HTACCESS file does\'t exists for environment '.$this->env); + $this->console->writeln("HTACCESS file does\'t exists for environment {$this->env}"); + // throw new \Exception('HTACCESS file does\'t exists for environment '.$this->env); } private function configure() { @@ -62,7 +63,7 @@ private function configure() { if (is_null($this->config = $this->console->share('config'))) { $this->config = $this->console->share('config', \Core\Config::SetApplication($this->appPath)); - $this->loadFile($this->appPath.'config.php'); + $this->loadFile($this->appPath.'/config.php'); } return TRUE; @@ -78,7 +79,7 @@ private function loadFile($file) { public function getOptionCollection() { $collection = new \CMP\Command\OptionCollection(); $collection->add('a|app:', 'Application folder name'); - $collection->add('e|env:', 'Environment [prod|dev] (default: dev)', 'dev'); + $collection->add('e|env:', 'Environment [prod|dev|stage] (default: dev)', 'dev'); return $collection; } diff --git a/composer.json b/composer.json index 59935bb..b551b3a 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,6 @@ "kaduamaral/connectionpdo": "~1.1", "phpmailer/phpmailer": "~5.2", "mobiledetect/mobiledetectlib": "~2.8", - "kaduamaral/cmp": "^1.1" + "kaduamaral/cmp": "^1.1.4" } } \ No newline at end of file diff --git a/public_html/.htaccess b/public_html/dev.htaccess similarity index 93% rename from public_html/.htaccess rename to public_html/dev.htaccess index e05845e..1db5c55 100644 --- a/public_html/.htaccess +++ b/public_html/dev.htaccess @@ -11,7 +11,7 @@ Options -Indexes RewriteEngine On -RewriteBase /Base/public_html/ +RewriteBase /base/public_html/ RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d diff --git a/public_html/index.php b/public_html/index.php index 588d903..66fe9a2 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -1,8 +1,5 @@ + Order deny,allow + Deny from all + + +RewriteEngine On + +RewriteBase /base/public_html/ + +RewriteCond %{SCRIPT_FILENAME} !-f +RewriteCond %{SCRIPT_FILENAME} !-d +RewriteRule ^$ index.php?URI=$1 [L,QSA] + +RewriteCond %{SCRIPT_FILENAME} !-f +RewriteCond %{SCRIPT_FILENAME} !-d +RewriteRule ^(.*)/? index.php?URI=$1 [L,QSA] \ No newline at end of file diff --git a/public_html/stage.htaccess b/public_html/stage.htaccess new file mode 100644 index 0000000..1db5c55 --- /dev/null +++ b/public_html/stage.htaccess @@ -0,0 +1,22 @@ +Options +FollowSymlinks + +# Prevent Directoy listing +Options -Indexes + +# Prevent Direct Access to files + + Order deny,allow + Deny from all + + +RewriteEngine On + +RewriteBase /base/public_html/ + +RewriteCond %{SCRIPT_FILENAME} !-f +RewriteCond %{SCRIPT_FILENAME} !-d +RewriteRule ^$ index.php?URI=$1 [L,QSA] + +RewriteCond %{SCRIPT_FILENAME} !-f +RewriteCond %{SCRIPT_FILENAME} !-d +RewriteRule ^(.*)/? index.php?URI=$1 [L,QSA] \ No newline at end of file