189 lines
6.9 KiB
PHP
189 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace WizdomNetworks\WizeWeb\Utilities;
|
|
|
|
use WizdomNetworks\WizeWeb\Utilities\Logger;
|
|
use WizdomNetworks\WizeWeb\Utilities\ErrorHandler;
|
|
|
|
/**
|
|
* StructureGenerator Utility
|
|
*
|
|
* Handles the creation of file and directory structures for the application.
|
|
*/
|
|
class StructureGenerator
|
|
{
|
|
private array $structure = [
|
|
'app/Controllers/',
|
|
'app/Core/',
|
|
'app/Models/',
|
|
'app/Services/',
|
|
'app/Utils/',
|
|
'app/Interfaces/',
|
|
'config/',
|
|
'logs/',
|
|
'public/assets/css/',
|
|
'public/assets/js/',
|
|
'resources/views/layouts/',
|
|
'resources/views/pages/services/',
|
|
'resources/views/partials/',
|
|
'resources/emails/',
|
|
'scripts/',
|
|
'storage/cache/',
|
|
'storage/uploads/',
|
|
'tests/Controllers/',
|
|
'tests/Models/',
|
|
'tests/Utils/',
|
|
'vendor/'
|
|
];
|
|
|
|
private array $files = [
|
|
'.env',
|
|
'.gitignore',
|
|
'composer.json',
|
|
'composer.lock',
|
|
'README.md',
|
|
'config/config.php',
|
|
'logs/app.log',
|
|
'public/index.php',
|
|
'public/assets/css/styles.css',
|
|
'public/assets/js/main.js',
|
|
'resources/views/layouts/header.php',
|
|
'resources/views/layouts/footer.php',
|
|
'resources/views/layouts/main.php',
|
|
'resources/views/layouts/head.php',
|
|
'resources/views/pages/home.php',
|
|
'resources/views/pages/services/it_consulting.php',
|
|
'resources/views/pages/services/emergency_support.php',
|
|
'resources/views/pages/services/managed_services.php',
|
|
'resources/views/pages/services/online_brand_management.php',
|
|
'resources/views/pages/services/project_management.php',
|
|
'resources/views/pages/about.php',
|
|
'resources/views/pages/contact.php',
|
|
'resources/views/pages/testimonials.php',
|
|
'resources/views/pages/error.php',
|
|
'resources/views/partials/navbar.php',
|
|
'resources/views/partials/sidebar.php',
|
|
'resources/emails/contact_confirmation.php',
|
|
'scripts/generate_structure.php',
|
|
'app/Controllers/HomeController.php',
|
|
'app/Controllers/ServicesController.php',
|
|
'app/Controllers/HelpDeskController.php',
|
|
'app/Controllers/EmergencySupportController.php',
|
|
'app/Controllers/ClientsController.php',
|
|
'app/Controllers/ContactController.php',
|
|
'app/Controllers/AboutController.php',
|
|
'app/Controllers/TestimonialsController.php',
|
|
'app/Core/Router.php',
|
|
'app/Core/View.php',
|
|
'app/Core/BaseController.php',
|
|
'app/Models/ServiceModel.php',
|
|
'app/Models/ContactModel.php',
|
|
'app/Models/TestimonialModel.php',
|
|
'app/Models/ClientModel.php',
|
|
'app/Services/ITConsultingService.php',
|
|
'app/Services/EmergencySupportService.php',
|
|
'app/Services/OnlineBrandManagementService.php',
|
|
'app/Services/ManagedHostingService.php',
|
|
'app/Services/HelpDeskService.php',
|
|
'app/Services/ProjectManagementService.php',
|
|
'app/Utils/Logger.php',
|
|
'app/Utils/Validator.php',
|
|
'app/Utils/Mailer.php',
|
|
'app/Interfaces/ServiceInterface.php',
|
|
'app/Interfaces/LoggableInterface.php'
|
|
];
|
|
|
|
public function createStructure(string $basePath): void
|
|
{
|
|
foreach ($this->structure as $folder) {
|
|
try {
|
|
$path = rtrim($basePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $folder;
|
|
if (!is_dir($path)) {
|
|
mkdir($path, 0755, true);
|
|
Logger::info("Created directory: $path");
|
|
} else {
|
|
Logger::info("Directory already exists: $path");
|
|
}
|
|
} catch (\Throwable $e) {
|
|
Logger::error("Failed to create directory: $folder");
|
|
ErrorHandler::exception($e);
|
|
}
|
|
}
|
|
|
|
foreach ($this->files as $file) {
|
|
try {
|
|
$filePath = rtrim($basePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file;
|
|
if (!file_exists($filePath)) {
|
|
file_put_contents($filePath, $this->getDefaultContent($file));
|
|
Logger::info("Created file with default content: $filePath");
|
|
} else {
|
|
$currentContent = file_get_contents($filePath);
|
|
$defaultContent = $this->getDefaultContent($file);
|
|
if (trim($currentContent) === '') {
|
|
file_put_contents($filePath, $defaultContent);
|
|
Logger::info("Updated empty file with default content: $filePath");
|
|
} elseif (trim($currentContent) === trim($defaultContent)) {
|
|
Logger::info("File already has default content, no changes made: $filePath");
|
|
} else {
|
|
Logger::info("File has additional content, no changes made: $filePath");
|
|
}
|
|
}
|
|
} catch (\Throwable $e) {
|
|
Logger::error("Failed to create or update file: $file");
|
|
ErrorHandler::exception($e);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function getDefaultContent(string $file): string
|
|
{
|
|
if (preg_match('/app\/(Controllers|Core|Models|Services|Utils|Interfaces)\//', $file)) {
|
|
$namespace = preg_replace('/app\/(.*?)\//', 'WizdomNetworks\\WizeWeb\\$1', dirname($file));
|
|
$namespace = str_replace('/', '\\', $namespace);
|
|
$className = pathinfo($file, PATHINFO_FILENAME);
|
|
return "<?php\n\nnamespace $namespace;\n\nclass $className\n{\n // Placeholder for future implementation\n}\n";
|
|
}
|
|
|
|
if ($file === '.gitignore') {
|
|
return "/vendor/\n/logs/\n/storage/\n.env\n";
|
|
}
|
|
|
|
if ($file === '.env') {
|
|
return "# Application Environment\n" .
|
|
"# Set the application environment (e.g., development, production)\n" .
|
|
"APP_ENV=development\n\n" .
|
|
"# Database Configuration\n" .
|
|
"# Configure the database connection\n" .
|
|
"DB_HOST=localhost\n" .
|
|
"DB_PORT=3306\n" .
|
|
"DB_NAME=\n" .
|
|
"DB_USER=\n" .
|
|
"DB_PASSWORD=\n\n" .
|
|
"# Mailer Configuration\n" .
|
|
"# Configure email sending\n" .
|
|
"MAIL_HOST=\n" .
|
|
"MAIL_PORT=\n" .
|
|
"MAIL_USER=\n" .
|
|
"MAIL_PASSWORD=\n";
|
|
}
|
|
|
|
if ($file === 'README.md') {
|
|
return "# Project Documentation\n\nThis file will contain project documentation.\n";
|
|
}
|
|
|
|
return "";
|
|
}
|
|
}
|
|
|
|
if (PHP_SAPI === 'cli') {
|
|
global $argc, $argv;
|
|
if ($argc !== 2) {
|
|
echo "Usage: php generate_structure.php <path>\n";
|
|
exit(1);
|
|
}
|
|
|
|
$path = $argv[1];
|
|
$generator = new StructureGenerator();
|
|
$generator->createStructure($path);
|
|
}
|