Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- * CV Manager - File to create a new project, like laravel artisan but for simple structure project
- *
- * @author Vinícius Cordeiro <[email protected]>
- * @version 1.0.0
- * @copyright 2025 Vinícius Cordeiro
- */
- ini_set('display_errors', 1);
- ini_set('display_startup_errors', 1);
- error_reporting(E_ERROR | E_WARNING | E_PARSE);
- // Increase the memory limit
- ini_set('memory_limit', '-1');
- // Set the maximum execution time
- set_time_limit(0);
- $files = [
- 'app' => [
- 'Core' => [
- 'Controller.php',
- 'Middleware.php',
- 'Request.php',
- 'Api.php',
- 'Auth.php',
- 'Cookie.php',
- 'Session.php',
- 'Tests.php',
- ],
- 'Controllers' => [
- 'HomeController.php',
- 'LoginController.php',
- 'RegisterController.php',
- 'UserController.php',
- 'ErrorController.php',
- 'ExampleController.php'
- ],
- 'Models' => [
- 'Model.php',
- ],
- 'Views' => [
- 'View.php',
- ],
- 'Helpers' => [
- 'Helper.php',
- ],
- 'bootstrap.php',
- 'functions.php',
- ],
- 'config' => [
- 'app.config.php',
- 'database.config.php',
- 'errors.config.php',
- 'types.config.php',
- 'version.config.php',
- 'session.config.php',
- ],
- 'libs' => [
- 'autoload.php',
- ],
- 'storage' => [
- 'logs' => [
- 'debug.log',
- ],
- 'sessions' => [],
- 'uploads' => [
- 'images' => [],
- 'files' => [],
- 'avatars' => [],
- ]
- ],
- 'routes' => [
- 'base.php',
- 'web.php'
- ],
- 'tests' => [
- 'debug' => [
- 'showVersion.php',
- ]
- ],
- 'resources' => [
- 'css' => [
- 'app.css',
- ],
- 'js' => [
- 'app.js',
- 'home/' => [
- 'app.js'
- ],
- 'example/' => [
- 'app.js'
- ]
- ],
- 'images' => [],
- 'fonts' => [],
- 'views' => [
- 'layouts' => [
- 'guest.tpl',
- 'admin.tpl',
- 'error.tpl',
- ],
- 'components' => [
- 'menu.tpl',
- 'header.tpl',
- 'footer.tpl'
- ],
- 'errors' => [
- '404.tpl',
- '500.tpl',
- '403.tpl'
- ],
- ]
- ],
- '/' => [
- 'index.php',
- 'README.md',
- 'composer.json',
- '.gitignore',
- 'favicon.ico',
- 'LICENSE',
- 'tailwind.config.js',
- ],
- ];
- // Ask if the user want to give a name to the project
- echo 'Please enter the name of the project: ';
- $project_name = trim(fgets(STDIN));
- // Do not allow the user to give an empty name
- if ($project_name == '') {
- echo 'Please enter a name for the project, it cannot be empty.' . PHP_EOL . 'Exiting...' . PHP_EOL;
- exit;
- }
- // Ask if the user want to set the author of the project
- echo 'Please enter the author of the project: ';
- if (strtolower($author = trim(fgets(STDIN))) == 'y') {
- // Ask for the name of the project
- echo 'Enter the author of the project: ';
- $author = trim(fgets(STDIN));
- }
- // Do not allow the user to give an empty name
- if ($author == '') {
- echo 'Please enter an author for the project, it cannot be empty.' . PHP_EOL . 'Exiting...' . PHP_EOL;
- exit;
- }
- // Create the root folder
- mkdir($project_name, 0777, true);
- // Create icon with the initials of the project name
- $icon = strtoupper(substr($project_name, 0, 4));
- // using Imagick
- $imagick = new Imagick();
- $imagick->newImage(32, 32, new ImagickPixel('transparent'));
- $draw = new ImagickDraw();
- $draw->setFillColor(new ImagickPixel('black'));
- $draw->setFontSize(18);
- $draw->setTextAlignment(Imagick::ALIGN_CENTER);
- $draw->setTextKerning(0);
- $draw->setStrokeColor(new ImagickPixel('white'));
- $draw->setStrokeWidth(2);
- $draw->setGravity(Imagick::GRAVITY_CENTER);
- $imagick->annotateImage($draw, 16, 16, 0, $icon);
- $imagick->setImageFormat('ico');
- $imagick->writeImage($project_name . '/favicon.ico');
- $imagick->clear();
- mkdir($project_name . '/resources/images/', 0777, true);
- // Create icon with the initials of the project name
- $icon = strtoupper(substr($project_name, 0, 4));
- // using Imagick
- $imagick = new Imagick();
- $imagick->newImage(64, 64, new ImagickPixel('purple'));
- $draw = new ImagickDraw();
- $draw->setFillColor(new ImagickPixel('black'));
- $draw->setFontSize(16);
- $draw->setTextAlignment(Imagick::ALIGN_CENTER);
- $draw->setTextKerning(0);
- $draw->setStrokeColor(new ImagickPixel('white'));
- $draw->setStrokeWidth(2);
- $draw->setGravity(Imagick::GRAVITY_CENTER);
- $imagick->annotateImage($draw, 24, 24, 0, $icon);
- $imagick->setImageFormat('png');
- $imagick->writeImage($project_name . '/resources/images/' . '/logo.png');
- $imagick->clear();
- // Ask if the user want to give a description to the project
- echo 'Enter the description to the project: [default is empty] ';
- $project_description = trim(fgets(STDIN));
- // Ask if the user want to set the Keywords of the project
- echo 'Enter the keywords of the project: [separated by commas, default is empty] ';
- $keywords = trim(fgets(STDIN));
- $project_version = "1.0.0";
- // Ask if the user want to give a description to the project
- echo 'Enter the version to the project? [default is 1.0.0] ';
- $project_version = trim(fgets(STDIN));
- // Get the email from the author, between the < and >
- $authorNameWithoutEmail = preg_replace("/<(.*)>/", "", $author);
- $startFile = "<?php
- /**
- * " . $project_name . "
- * " . $project_description . "
- * @file [file]
- * @package cvmanager
- * @author $author
- * @version " . $project_version . "
- * @copyright " . date('Y') . " " . $authorNameWithoutEmail . "
- */";
- // Get the Root folder parameter passed on the command line if not specified use the project name on the current directory of the script
- $root = $project_name && $project_name != '' ? $project_name : '/var/www/html/';
- $errors = [];
- function createFolderStructure($array, $rootFolder, $startFile = "") {
- foreach ($array as $key => $value) {
- $path = $rootFolder . DIRECTORY_SEPARATOR . $key;
- if (is_array($value)) {
- if (!is_dir($path)) {
- mkdir($path, 0777, true);
- }
- createFolderStructure($value, $path, $startFile); // recursive call
- } else {
- $filePath = $rootFolder . DIRECTORY_SEPARATOR . $value;
- if (!file_exists($filePath)) {
- // Check if the file it's a php file by chekcing
- if (!file_put_contents($filePath, '')) { // create empty file
- $errors[] = [
- 'error' => 'Failed to create file ' . $filePath,
- 'folder' => $rootFolder,
- 'file' => $value
- ];
- }
- echo $filePath . PHP_EOL;
- if (pathinfo($filePath, PATHINFO_EXTENSION) == 'php') {
- file_put_contents($filePath, str_replace('[file]', $value, $startFile));
- }
- // chmod the file
- if (!chmod($filePath, 0777)) {
- $errors[] = [
- 'error' => 'Failed to change permission of file ' . $filePath,
- 'folder' => $rootFolder,
- 'file' => $value,
- ];
- }
- }
- }
- }
- }
- $rootFolder = $root;
- createFolderStructure($files, $rootFolder, $startFile);
- // Populate the app.config.php with the template app.config.php
- if (file_exists($root . '/config/' . 'app.config.php')) {
- $appConfig = "
- <?php
- /**
- * " . $project_name . "
- * " . $project_description . "
- * @file app.config.php
- * @package cvmanager
- * @author $author
- * @version " . $project_version . "
- * @copyright " . date('Y') . " $author
- */
- return [
- 'name' => 'CV Manager',
- 'debug' => 1,
- 'url' => 'http://localhost/',
- 'path' => '/var/www/cvmanager.com',
- 'timezone' => 'America/Sao_Paulo',
- 'locale' => 'pt-BR',
- 'default_locale' => 'pt-BR',
- 'default_timezone' => 'America/Sao_Paulo',
- 'app_namespace' => 'App',
- 'resources' => '/resources',
- 'css' => '/resources/css',
- 'js' => '/resources/js',
- 'views' => '/resources/views',
- 'libs' => '/libs',
- 'images' => '/resources/images',
- 'fonts' => '/resources/fonts',
- 'js' => '/resources/js',
- 'config' => '/config',
- 'routes' => '/routes',
- ];";
- file_put_contents($root . '/config/' . 'app.config.php', $appConfig);
- chmod($root . '/' . 'app.config.php', 0777);
- }
- // Populate the version.config.php
- if (file_exists($root . '/config/' . 'version.config.php')) {
- $versionConfig = "
- <?php
- /**
- * " . $project_name . "
- * " . $project_description . "
- * @file version.config.php
- * @package cvmanager
- * @author $author
- * @version " . $project_version . "
- * @copyright " . date('Y') . " $author
- */
- return [
- 'current' => '1.0.0',
- 'next' => '1.0.1',
- 'last' => '1.0.0',
- 'js' => '1.0.0',
- 'css' => '1.0.0',
- 'app' => '1.0.0',
- ];";
- file_put_contents($root . '/config/' . 'version.config.php', $versionConfig);
- chmod($root . '/' . 'version.config.php', 0777);
- }
- // Populate the smtp.config.php with the template smtp.config.php
- if (file_exists($root . '/config/' . 'smtp.config.php')) {
- $smtpConfig = "
- <?php
- /**
- * " . $project_name . "
- * " . $project_description . "
- * @file smtp.config.php
- * @package cvmanager
- * @author $author
- * @version " . $project_version . "
- * @copyright " . date('Y') . " $author
- */
- return [
- 'host' => 'smtp.gmail.com',
- 'port' => 587,
- 'encryption' => 'tls',
- 'username' => 'vinicordeirogo',
- 'password' => 'password',
- 'from' => 'vinicordeirogo',
- 'from_name' => 'Vinícius Cordeiro',
- ];";
- file_put_contents($root . '/config/' . 'smtp.config.php', $smtpConfig);
- chmod($root . '/' . 'smtp.config.php', 0777);
- }
- // Populate the types.config.php with the template types.config.php
- if (file_exists($root . '/config/' . 'types.config.php')) {
- $typesConfig = " <?php
- /**
- * " . $project_name . "
- * " . $project_description . "
- * @file types.config.php
- * @package cvmanager
- * @author $author
- * @version " . $project_version . "
- * @copyright " . date('Y') . " $author
- */
- namespace Config;
- enum Method : string {
- case GET = 'GET';
- case POST = 'POST';
- case PUT = 'PUT';
- case PATCH = 'PATCH';
- case DELETE = 'DELETE';
- }
- enum Role : string {
- case ADMIN = 'ADMIN';
- case USER = 'USER';
- }
- enum Status : string {
- case ACTIVE = 'ACTIVE';
- case INACTIVE = 'INACTIVE';
- }
- ";
- file_put_contents($root . '/config/' . 'types.config.php', $typesConfig);
- chmod($root . '/' . 'types.config.php', 0777);
- }
- // Populate the errors.config.php with the template errors.config.php
- if (file_exists($root . '/config/' . 'errors.config.php')) {
- $errorsConfig = "<?php
- /**
- * " . $project_name . "
- * " . $project_description . "
- * @file errors.config.php
- * @package cvmanager
- * @author $author
- * @version " . $project_version . "
- * @copyright " . date('Y') . " $author
- */
- return [
- '404' => [
- 'title' => 'Page not found',
- 'message' => 'The page you are looking for was not found.',
- 'code' => 404,
- ],
- '403' => [
- 'title' => 'Forbidden',
- 'message' => 'You do not have permission to access this page.',
- 'code' => 403,
- ],
- '500' => [
- 'title' => 'Internal Server Error',
- 'message' => 'An error occurred on the server.',
- 'code' => 500,
- ],
- '401' => [
- 'title' => 'Unauthorized',
- 'message' => 'You are not authorized to access this page.',
- 'code' => 401,
- ],
- '400' => [
- 'title' => 'Bad Request',
- 'message' => 'The request was invalid.',
- 'code' => 400,
- ],
- '409' => [
- 'title' => 'Conflict',
- 'message' => 'The request could not be completed due to a conflict.',
- 'code' => 409,
- ],
- '422' => [
- 'title' => 'Unprocessable Entity',
- 'message' => 'The request was valid, but the server was unable to process it.',
- 'code' => 422,
- ],
- '429' => [
- 'title' => 'Too Many Requests',
- 'message' => 'The user has made too many requests in a given amount of time.',
- 'code' => 429,
- ],
- '503' => [
- 'title' => 'Service Unavailable',
- 'message' => 'The server is currently unable to handle the request.',
- 'code' => 503,
- ],
- ];";
- file_put_contents($root . '/config/' . 'errors.config.php', $errorsConfig);
- chmod($root . '/' . 'errors.config.php', 0777);
- }
- if (file_exists($root . '/app/' . 'functions.php')) {
- // Populate the functions.php with the template functions.php
- $functions = "" . str_replace('[file]', 'functions.php', $startFile) . "
- function config(\$key) {
- // Get the configuration file, if app.\$key then get from the app.config.php file
- \$prefix = explode('.', \$key);
- if(!file_exists(__DIR__ . '/../config/' . \$prefix[0] . '.config.php')) {
- throw new Exception('Config file not found: ' . \$prefix[0] . '.config.php');
- }
- // Get the config file
- \$config = require __DIR__ . '/../config/' . \$prefix[0] . '.config.php';
- \$file = __DIR__ . '/../config/' . \$prefix[0] . '.config.php';
- // Check if the key exists in the config file if not throw an exception
- if(array_key_exists(\$prefix[1] , \$config)) {
- return \$config[\$prefix[1]];
- } else {
- throw new Exception('Config key not found: ' . \$key . ' in file: ' . \$file);
- }
- }";
- file_put_contents($root . '/app/' . 'functions.php', $functions);
- }
- if (file_exists($root . '/app/' . 'bootstrap.php')) {
- // Populate the bootstrap.php with the template bootstrap.php
- $bootstrap = "" . str_replace('[file]', 'bootstrap.php', $startFile) . "
- // Load the configuration file of the application
- include __DIR__ . '/../config/app.config.php';
- // Load the types file of the application
- include __DIR__ . '/../config/types.config.php';
- // Load the errors file of the application
- include __DIR__ . '/../config/errors.config.php';
- // Load the database file of the application
- include __DIR__ . '/../config/database.config.php';
- // Load the helpers file of the application
- include __DIR__ . '/../app/functions.php';
- ";
- file_put_contents($root . '/app/' . 'bootstrap.php', $bootstrap);
- // change the permission of the file and chown
- chmod($root . '/app/' . 'bootstrap.php', 0777);
- }
- // Populate the index.php with the template index.php
- if (file_exists($root . '/' . 'index.php')) {
- $index = "" . str_replace('[file]', 'index.php', $startFile) . "
- // Load the bootstrap file of the application
- require_once 'app/bootstrap.php';
- // Enable error reporting based on the debug mode specified on the app.config file
- if (config('app.debug')) {
- error_reporting(E_WARNING | E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR | E_PARSE);
- ini_set('display_errors', 1);
- } else {
- error_reporting(0);
- ini_set('display_errors', 0);
- }
- try {
- phpinfo();
- } catch (\Exception \$e) {
- }
- exit;
- ";
- file_put_contents($root . '/' . 'index.php', $index);
- // change the permission of the file and chown
- chmod($root . '/' . 'index.php', 0777);
- }
- // Create the composer.json file
- $composer = "{
- \"name\": \"vinicordeirogo/" . strtolower(str_replace(' ', '-', $project_name)) . "\",
- \"description\": \"" . $project_description . "\",
- \"type\": \"project\",
- \"readme\": \"README.md\",
- \"authors\": [
- {
- \"name\": \"Vinícius Gonçalves Cordeiro\",
- \"email\": \"[email protected]\"
- }
- ],
- \"require\": {
- \"guzzlehttp/guzzle\": \"^7.9\",
- \"adodb/adodb-php\": \"^5.22\",
- \"smarty/smarty\": \"^5.4\",
- \"phpmailer/phpmailer\": \"^6.9\",
- \"phpoffice/common\": \"^1.0\",
- \"phpoffice/phpspreadsheet\": \"^4.1\",
- \"phpoffice/phpword\": \"^1.3\",
- \"phpoffice/phppresentation\": \"^0.2.0\"
- },
- \"autoload\": {
- \"psr-4\": {
- \"App\\\\\": \"app/\",
- \"Config\\\\\": \"config/\",
- \"Routes\\\\\": \"routes/\"
- },
- \"classmap\": [
- \"app/Core/\",
- \"app/Controllers/\",
- \"app/Helpers/\",
- \"app/Models/\",
- \"app/Views/\",
- \"config/\",
- \"routes/\"
- ]
- },
- \"config\": {
- \"vendor-dir\": \"libs/\"
- }
- }
- ";
- // Create the composer.json file
- file_put_contents($root . '/' . 'composer.json', $composer);
- // Create the tailwind.config.js
- if (file_exists($root . '/tailwind.config.js')) {
- $tailwindConfig = "
- module.exports = {
- theme: {
- fontFamily: {
- sans: ['Nunito', 'sans-serif'],
- mono: ['Fira Code', 'monospace'],
- },
- colors: {
- transparent: 'transparent',
- current: 'currentColor',
- black: '#000',
- white: '#fff',
- primary: {
- 50: '#eff6ff',
- 100: '#dbeafe',
- 200: '#bfdbfe',
- 300: '#93c5fd',
- 400: '#60a5fa',
- 500: '#3b82f6',
- 600: '#2563eb',
- 700: '#1d4ed8',
- 800: '#1e40af',
- 900: '#1e3a8a',
- },
- secondary: {
- 50: '#f9fafb',
- 100: '#f3f4f6',
- 200: '#e5e7eb',
- 300: '#d1d5db',
- 400: '#9ca3af',
- 500: '#6b7280',
- 600: '#4b5563',
- 700: '#374151',
- 800: '#1f2937',
- 900: '#111827',
- }
- }
- extend: {
- },
- },
- darkMode: 'class',
- plugins: [
- require('@tailwindcss/typography'),
- require('@tailwindcss/forms'),
- require('@tailwindcss/aspect-ratio'),
- ],
- };
- ";
- file_put_contents($root . '/' . 'tailwind.config.js', $tailwindConfig);
- }
- // Ask if the user wants to create a git repository
- echo 'Do you want to create a git repository? (Y/n) ';
- $git = trim(fgets(STDIN));
- if (strtolower($git) == 'y') {
- // Create the git repository on main branch and Initial commit message
- exec(' cd /var/www/' . $project_name . ' && git init && git add . && git commit -m "Initial commit"');
- }
- // Ask if the user wants to create a database
- echo 'Do you want to create a database? (Y/n) ';
- $database = trim(fgets(STDIN));
- if (strtolower($database) == 'y') {
- // Create the database
- echo 'Enter the name of the database: ';
- $database_name = trim(fgets(STDIN));
- echo 'Enter the username of the database: ';
- $database_username = trim(fgets(STDIN));
- echo 'Enter the password of the database: ';
- $database_password = trim(fgets(STDIN));
- // Create the database
- exec('mysql -u ' . $database_username . ' -p' . $database_password . ' -e "CREATE DATABASE ' . $database_name . ';"');
- $adodbSessionSQL = "
- CREATE TABLE IF NOT EXISTS sessions2 (
- sesskey VARCHAR( 64 ) COLLATE utf8mb4_bin NOT NULL DEFAULT '',
- expiry DATETIME NOT NULL ,
- expireref VARCHAR( 250 ) DEFAULT '',
- created DATETIME NOT NULL ,
- modified DATETIME NOT NULL ,
- sessdata LONGTEXT,
- PRIMARY KEY ( sesskey ) ,
- INDEX sess2_expiry( expiry ),
- INDEX sess2_expireref( expireref )
- ) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
- ";
- // Create the database
- exec('mysql -u ' . $database_username . ' -p' . $database_password . ' ' . $database_name . ' -e "' . $adodbSessionSQL . '"');
- }
- // Run the composer update and dump-autoload command
- exec('cd /var/www/' . $project_name . ' && composer update ');
- // Run the composer dump-autoload command
- exec('composer dump-autoload -o');
- try {
- exec('php -S 0.0.0.0:8000 -t /var/www/' . $project_name . ' &');
- exec('firefox http://127.0.0.1:8000/');
- } catch (Exception $e) {
- $errors[] = [
- 'error' => 'Error on other runtime',
- 'message' => $e->getMessage(),
- 'file' => $e->getFile(),
- 'line' => $e->getLine(),
- ];
- }
- if (count($errors) > 0) {
- echo 'Errors: ' . PHP_EOL;
- foreach ($errors as $error) {
- echo $error['folder'] . ' - ' . $error['file'] . ' - ' . $error['message'] . PHP_EOL;
- }
- // Dump the errors on a file on the root folder
- file_put_contents($root . '/' . 'errors.log', json_encode($errors));
- }
- return true;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement