Sistema web para crear proyecto web nuevo

This commit is contained in:
2020-07-03 17:21:55 -04:00
parent af3507bda5
commit ad3952501f
53 changed files with 2437 additions and 0 deletions

733
common/Service/Projects.php Normal file
View File

@ -0,0 +1,733 @@
<?php
namespace ProVM\Projects\Common\Service;
use Cz\Git\GitRepository;
use ProVM\Projects\Common\Factory\Project as Factory;
class Projects {
protected $folder;
public function __construct(string $projects_folder) {
$this->folder = $projects_folder;
}
public function getFolder(string $project = ''): string {
if (trim($project) == '') {
return $this->folder;
}
return implode(DIRECTORY_SEPARATOR, [
$this->folder,
$project
]);
}
protected $projects;
public function list(): array {
if ($this->projects === null) {
$folders = [];
$files = new \DirectoryIterator($this->folder);
foreach ($files as $file) {
if (!$file->isDir() or $file->isDot()) {
continue;
}
$folders []= (object) [
'name' => $file->getBasename(),
'folder' => $file->getRealPath()
];
}
$this->projects = $folders;
}
return $this->projects;
}
public function exists(string $project): bool {
$folder = $this->getFolder($project);
return file_exists($folder);
}
public function get(string $project) {
$folder = $this->getFolder($project);
$factory = new Factory();
return $factory
->find($folder)
->getAll()
->build();
}
public function create(string $project) {
$this->createFolder($project);
}
public function createFolder(string $project): bool {
if ($this->exists($project)) {
return false;
}
$folder = $this->getFolder($project);
$status = mkdir($folder);
$status &= file_exists($folder);
return $status;
}
public function createSubfolders(string $project): bool {
if (!$this->exists($project)) {
return false;
}
$folders = [
'cache',
'common',
'common/Controller',
'common/Controller/Web',
'public',
'resources',
'resources/routes',
'resources/routes/web',
'resources/views',
'resources/views/layout',
'setup',
'setup/env',
'setup/common',
'setup/web'
];
$status = false;
foreach ($folders as $folder) {
$f = implode(DIRECTORY_SEPARATOR, [
$this->folder,
$project,
$folder
]);
if (file_exists($f)) {
continue;
}
$status |= mkdir($f);
}
return $status;
}
public function gitInit(string $project): bool {
if (!$this->exists($project)) {
return false;
}
$content = [
'# Composer',
'/vendor/',
'composer.lock',
'',
'# Blade',
'/cache/',
'',
'# Env',
'/setup/env/'
];
$folder = implode(DIRECTORY_SEPARATOR, [
$this->folder,
$project
]);
$filename = implode(DIRECTORY_SEPARATOR, [
$folder,
'.gitignore'
]);
$status = (file_put_contents($filename, implode(PHP_EOL, $content)) !== false);
$repo = GitRepository::init($folder);
$repo->execute([
'config',
'--global',
'user.name',
'Aldarien'
]);
$repo->execute([
'config',
'--global',
'user.email',
'aldarien85@gmail.com'
]);
$repo->addFile('.gitignore');
$repo->commit('Inicio de Git');
$repo->createBranch('develop', true);
return $status;
}
public function addComposer(string $project, string $json): bool {
if (!$this->exists($project)) {
return false;
}
$folder = $this->getFolder($project);
$filename = implode(DIRECTORY_SEPARATOR, [
$folder,
'composer.json'
]);
$status = (file_put_contents($filename, $json) !== false);
$current = getcwd();
$s = chdir($folder);
if ($s) {
exec('composer install');
$status &= (file_exists(implode(DIRECTORY_SEPARATOR, [$folder, 'vendor'])));
chdir($current);
}
return $status;
}
public function addFiles(string $project): bool {
if (!$this->exists($project)) {
return false;
}
$folder = $this->getFolder($project);
$methods = [
'SetupApp',
'SetupComposer',
'SetupCommonConfig',
'SetupWebConfig',
'SetupWebSetup',
'PublicIndex',
'PublicHtaccess',
'RoutesRouter',
'RoutesWeb',
'LayoutBase',
'LayoutBody',
'LayoutFooter',
'LayoutHead',
'LayoutHeader',
'LayoutMenu',
'LayoutScripts',
'LayoutStyles'
];
$status = true;
foreach ($methods as $method) {
$m = 'add' . $method . 'File';
$status &= $this->$m($folder);
}
return $status;
}
protected function createFile(string $folder, string $file_name, array $content): bool {
$filename = implode(DIRECTORY_SEPARATOR, [
$folder,
$file_name
]);
$status = (file_put_contents($filename, implode(PHP_EOL, $content)) !== false);
$status &= file_exists($filename);
return $status;
}
protected function addSetupAppFile(string $folder): bool {
$content = [
"<?php",
"use DI\ContainerBuilder as Builder;",
"use DI\Bridge\Slim\Bridge;",
'',
"if (!isset(\$__environment)) {",
" throw new Exception('Missing __environment variable');",
"}",
'',
"include_once 'composer.php';",
'',
"\$builder = new Builder();",
'',
"\$files = [",
" 'config',",
" 'setup'",
"];",
"\$folders = [",
" 'common',",
" \$__environment",
"];",
'',
"foreach (\$files as \$file) {",
" foreach (\$folders as \$folder) {",
" \$filename = implode(DIRECTORY_SEPARATOR, [",
" __DIR__,",
" \$folder,",
" \$file . '.php'",
" ]);",
" if (file_exists(\$filename)) {",
" \$builder->addDefinitions(\$filename);",
" }",
" }",
"}",
'',
"\$container = \$builder->build();",
"\$app = Bridge::create(\$container);",
"try {",
" \$app->setBasePath(\$container->get('base_url'));",
"} catch (Exception \$e) {",
"}",
'',
"foreach (\$folders as \$folder) {",
" \$filename = implode(DIRECTORY_SEPARATOR, [",
" __DIR__,",
" \$folder,",
" 'middleware.php'",
" ]);",
" if (file_exists(\$filename)) {",
" include_once \$filename;",
" }",
"}",
'',
"\$filename = implode(DIRECTORY_SEPARATOR, [",
" \$container->get('folders')->routes,",
" 'router.php'",
"]);",
"if (!file_exists(\$filename)) {",
" throw new Exception('Missing router file.');",
"}",
"include_once \$filename;"
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'setup'
]);
return $this->createFile($folder, 'app.php', $content);
}
protected function addSetupComposerFile(string $folder): bool {
$content = [
'<?php',
"\$filename = implode(DIRECTORY_SEPARATOR, [",
" dirname(__DIR__)",
" 'vendor'",
" 'autoload.php'",
"])",
"if (!file_exists(\$filename)) {",
" throw new Exception('Missing composer install.');",
"}",
"include_once \$filename;"
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'setup'
]);
return $this->createFile($folder, 'composer.php', $content);
}
protected function addSetupCommonConfigFile(string $folder): bool {
$project = explode(DIRECTORY_SEPARATOR, $folder);
array_pop($project); // common
array_pop($project); // setup
$project = array_pop($project);
$content = [
'<?php',
'return [',
" 'base_url' => dirname(__DIR__),",
" 'urls' => function() {",
" \$arr = [];",
" \$arr['base'] = '/provm/demos/{$project}';",
" return (object) \$arr;",
" },",
" 'folders' => function() {",
" \$arr = [];",
" \$arr['base'] = dirname(__DIR__, 2);",
" \$arr['resources'] = implode(DIRECTORY_SEPARATOR, [",
" \$arr['base'],",
" 'resources'",
" ]);",
" \$arr['routes'] = implode(DIRECTORY_SEPARATOR, [",
" \$arr['resources'],",
" 'routes'",
" ]);",
" return (object) \$arr;",
" }",
'];'
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'setup',
'common'
]);
return $this->createFile($folder, 'config.php', $content);
}
protected function addSetupWebConfigFile(string $folder): bool {
$content = [
"<?php",
"use Psr\Container\ContainerInterface as Container;",
'',
"return [",
" 'folders' => DI\decorate(function(\$prev, Container \$c) {",
" \$arr = (array) \$prev;",
" \$arr['templates'] = implode(DIRECTORY_SEPARATOR, [",
" \$prev->resources,",
" 'views'",
" ]);",
" \$arr['cache'] = implode(DIRECTORY_SEPARATOR, [",
" \$prev->base,",
" 'cache'",
" ]);",
" return (object) \$arr;",
" }),",
" 'assets' => (object) [",
" 'styles' => [",
" 'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.6/semantic.min.css'",
" ],",
" 'scripts' => [",
" 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js',",
" 'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.6/semantic.min.js'",
" ],",
" 'fonts' => [",
" 'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.6/themes/default/assets/fonts/brand-icons.woff',",
" 'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.6/themes/default/assets/fonts/brand-icons.woff2',",
" 'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.6/themes/default/assets/fonts/icons.woff',",
" 'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.6/themes/default/assets/fonts/icons.woff2',",
" 'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.6/themes/default/assets/fonts/outline-icons.woff',",
" 'https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.8.6/themes/default/assets/fonts/outline-icons.woff2'",
" ]",
" ]",
"];"
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'setup',
'web'
]);
return $this->createFile($folder, 'config.app', $content);
}
protected function addSetupWebSetupFile(string $folder): bool {
$content = [
"<?php",
"use Psr\Container\ContainerInterface as Container;",
'',
"return [",
" ProVM\Common\Alias\View::class => function(Container \$c) {",
" return new ProVM\Common\Define\View(",
" \$c->get('folders')->templates,",
" \$c->get('folders')->cache,",
" null,",
" [",
" 'urls' => \$c->get('urls'),",
" 'assets' => \$c->get('assets')",
" ]",
" );",
" }",
"];"
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'setup',
'web'
]);
return $this->createFile($folder, 'setup.php', $content);
}
protected function addPublicIndexFile(string $folder): bool {
$content = [
"<?php",
"\$__environment = 'web';",
"include_once implode(DIRECTORY_SEPARATOR, [",
" dirname(__DIR__),",
" 'setup',",
" 'app.php'",
"]);",
"\$app->run();",
""
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'public'
]);
return $this->createFile($folder, 'index.php', $content);
}
protected function addPublicHtaccessFile(string $folder): bool {
$content = [
"RewriteEngine On",
"RewriteCond %{REQUEST_FILENAME} !-f",
"RewriteCond %{REQUEST_FILENAME} !-d",
"RewriteRule ^ index.php [QSA,L]"
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'public'
]);
return $this->createFile($folder, '.htaccess', $content);
}
protected function addRoutesRouterFile(string $folder): bool {
$content = [
"<?php",
"include_once $__environment . '.php';",
""
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'resources',
'routes'
]);
return $this->createFile($folder, 'router.php', $content);
}
protected function addRoutesWebFile(string $folder): bool {
$content = [
"<?php",
"\$folder = implode(DIRECTORY_SEPARATOR, [",
" __DIR__,",
" 'web'",
"]);",
"if (file_exists(\$folder)) {",
" \$files = new DirectoryIterator(\$folder);",
" foreach (\$files as \$file) {",
" if (\$file->isDir()) {",
" continue;",
" }",
" include_once \$file->getRealPath();",
" }",
"}"
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'resources',
'routes'
]);
return $this->createFile($folder, 'web.php', $content);
}
protected function addLayoutBaseFile(string $folder): bool {
$content = [
"<!DOCTYPE html>",
"<html lang=\"es\">",
"@include('layout.head')",
"@include('layout.body')",
"</html>",
""
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'resources',
'views',
'layout'
]);
return $this->createFile($folder, 'base.blade.php', $content);
}
protected function addLayoutBodyFile(string $folder): bool {
$content = [
"<body>",
" @include('layout.header')",
" <div class=\"ui container\">",
" <div class=\"ui basic segment\">",
" @yield('page_content')",
" </div>",
" </div>",
" @include('layout.footer')",
"</body>",
""
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'resources',
'views',
'layout'
]);
return $this->createFile($folder, 'body.blade.php', $content);
}
protected function addLayoutFooterFile(string $folder): bool {
$content = ["@include('layout.scripts')"];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'resources',
'views',
'layout'
]);
return $this->createFile($folder, 'footer.blade.php', $content);
}
protected function addLayoutHeadFile(string $folder): bool {
$content = [
"<head>",
" <meta charset=\"utf8\" />",
" <title>@yield('page_title')</title>",
" @include('layout.styles')",
"</head>",
""
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'resources',
'views',
'layout'
]);
return $this->createFile($folder, 'head.blade.php', $content);
}
protected function addLayoutHeaderFile(string $folder): bool {
$content = [
"<header class=\"ui container\">",
" @include('layout.menu')",
"</header>",
""
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'resources',
'views',
'layout'
]);
return $this->createFile($folder, 'header.blade.php', $content);
}
protected function addLayoutMenuFile(string $folder): bool {
$content = [
"<nav class=\"ui menu\">",
" <a class=\"item\" href=\"{{$urls->base}}\">Inicio</a>",
" <a class=\"item\" href=\"{{$urls->base}}/create\">Crear</a>",
"</nav>",
""
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'resources',
'views',
'layout'
]);
return $this->createFile($folder, 'menu.blade.php', $content);
}
protected function addLayoutScriptsFile(string $folder): bool {
$content = [
"@if (isset(\$assets->scripts))",
" @foreach (\$assets->scripts as \$script)",
" <script type=\"text/javascript\" src=\"{{\$script}}\"></script>",
" @endforeach",
"@endif",
"",
"<script type=\"text/javascript\">",
" \$(document).ready(() => {",
" @stack('global_script')",
" })",
"</script>",
"",
"@stack('scripts')",
""
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'resources',
'views',
'layout'
]);
return $this->createFile($folder, 'scripts.blade.php', $content);
}
protected function addLayoutStylesFile(string $folder): bool {
$content = [
"@if (isset(\$assets->styles))",
" @foreach (\$assets->styles as \$style)",
" <link rel=\"stylesheet\" type=\"text/css\" href=\"{{\$style}}\" />",
" @endforeach",
"@endif",
"@if (isset(\$assets->fonts))",
" @foreach (\$assets->fonts as \$font)",
" <link href=\"{{\$font}}\" />",
" @endforeach",
"@endif",
"",
"@stack('styles')",
"",
"<style type=\"text/css\">",
" @stack('global_style')",
"</style>",
""
];
$folder = implode(DIRECTORY_SEPARATOR, [
$folder,
'resources',
'views',
'layout'
]);
return $this->createFile($folder, 'styles.blade.php', $content);
}
public function gitPush(string $project): bool {
if (!$this->exists($project)) {
return false;
}
$folder = implode(DIRECTORY_SEPARATOR, [
$this->folder,
$project
]);
$repo = new GitRepository($folder);
$repo->execute([
'config',
'--global',
'user.name',
'Aldarien'
]);
$repo->execute([
'config',
'--global',
'user.email',
'aldarien85@gmail.com'
]);
$repo->addAllChanges();
$repo->commit('Seteo inicial');
$repo->addRemote('provm', 'http://git.provm.cl/ProVM/' . $project . '.git');
$repo->push('provm', ['develop', '-u']);
return true;
}
public function addView(string $project, string $name, string $location = '', array $options = []): bool {
$content = [];
if (isset($options['extends'])) {
$content []= '@extends("' . $options['extends'] . '")';
}
if (isset($options['section'])) {
$content []= '@section("' . $options['section'] . '")';
$content []= '@endsection';
}
$folder = [
$this->folder,
$project,
'resources',
'views'
];
if (trim($location) != '') {
$folder []= trim($location);
}
$folder = implode(DIRECTORY_SEPARATOR, $folder);
if (!file_exists($folder)) {
mkdir($folder);
}
$filename = $name . '.blade.php';
return $this->createFile($folder, $filename, $content);
}
public function addRoute(string $project, string $space, string $file_name, string $route, $callback, string $method = 'get'): bool {
$folder = implode(DIRECTORY_SEPARATOR, [
$this->folder,
$project,
'resources',
'routes',
$space
]);
$content = [
'<?php'
];
$filename = implode(DIRECTORY_SEPARATOR, [$folder, $file_name . '.php']);
if (file_exists($filename)) {
$content = trim(file_get_contents($filename));
if (strpos($content, $route) !== false) {
return false;
}
$content = explode(PHP_EOL, $content);
}
$content []= "\$app->{$method}('{$route}', {$callback});";
return $this->createFile($folder, $file_name . '.php', $content);
}
public function addController(string $project, string $space, string $name, string $template): bool {
$name = str_replace(' ', '', ucwords($name));
$content = [
'<?php',
'namespace ' . implode("\\", [
'ProVM',
ucwords($project),
'Common',
'Controller',
ucwords($space)
]) . ';',
'',
'use ' . implode("\\", [
'Psr',
'Http',
'Message',
'RequestInterface'
]) . ' as Request;',
'use ' . implode("\\", [
'Psr',
'Http',
'Message',
'ResponseInterface'
]) . ' as Response;',
'use ' . implode("\\", [
'ProVM',
'Common',
'Define',
'View'
]) . ';',
'',
"class {$name} {",
" public function __invoke(Request \$request, Response \$response, View \$view): Response {",
" return \$view->render(\$response, '{$template}');",
' }',
'}',
''
];
$folder = implode(DIRECTORY_SEPARATOR, [
$this->folder,
$project,
'common',
'Controller',
ucwords($space)
]);
return $this->createFile($folder, $name . '.php', $content);
}
}