Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
KaduAmaral committed Oct 8, 2017
2 parents 188e461 + 38576dd commit e204630
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 76 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion app/example/config.php → app/example/environments/dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
16 changes: 11 additions & 5 deletions base/cmd
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

include '../vendor/autoload.php';
include './config/constants.php';
include './config/autoload.php';
include dirname(__DIR__).'/vendor/autoload.php';
include __DIR__.'/config/loader.php';

$cmd = new CMP\Console();

Expand Down Expand Up @@ -66,6 +64,14 @@ function fatal_handler() {

register_shutdown_function( "fatal_handler" );

$cmd->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();

try {
$cmd->run();
} catch (\Exception $e) {
$cmd->writeln("Exception: <error>{$e->getMessage()}</error>");
}
45 changes: 44 additions & 1 deletion base/config/repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,47 @@ function is_class_method($type="public", $method, $class) {
} catch (Exception $e) {
exit('Erro: '.$e->getMessage());
}
}
}

/**
* 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);
}
}
78 changes: 20 additions & 58 deletions base/console/commands/build.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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("<error>Exception: {$e->getMessage()}</error>");
}

$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('<error></error>');
return false;
}

// try {
$this->config = \Core\Config::SetApplication($this->app);
// } catch (InvalidApplicationException $e) {
// $this->console->writeln('<error>'.$e->getMessage().'</error>');
// 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('<info>Dumping routes...</info>');
$annotation = new \Core\Routes\Annotation();
@unlink($this->appPath.'routes.cache.php');
$annotation->dump();
$this->console->writeln('<success>Routes dumped with success!</success>');
// } catch (\Exception $e) {
// $this->console->writeln("<error>{$e->getMessage()}</error>");
// }
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;
}
}
}
64 changes: 64 additions & 0 deletions base/console/commands/dumproutes.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
namespace Console\Commands;

class DumpRoutes extends \CMP\Command\Command {

private $console;
private $config;
private $app;
private $appPath;

public function execute(\CMP\Console $console, $args = []) {
$this->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('<error></error>');
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;
}


}
87 changes: 87 additions & 0 deletions base/console/commands/environment.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
namespace Console\Commands;

class Environment extends \CMP\Command\Command {

private $console;
private $config;
private $env;
private $app;
private $appPath;

public function execute(\CMP\Console $console, $args = []) {
$this->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;

}

private function copyConfig() {
$this->console->writeln('Setting config file...');
$path = "{$this->appPath}/environments/{$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: <info>{$path}</info>");
$this->console->writeln("To: <info>{$newPath}</info>");
if (file_exists($path))
copy($path, $newPath);
else
$this->console->writeln("<error>HTACCESS file does\'t exists for environment {$this->env}</error>");
// 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('<error></error>');
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|stage] (default: dev)', 'dev');

return $collection;
}

}
2 changes: 1 addition & 1 deletion base/core/routes/annotation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion base/core/routes/route.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Loading

0 comments on commit e204630

Please sign in to comment.