package installer

This commit is contained in:
tanch0
2024-06-11 12:16:24 +05:45
parent c569ea1d0c
commit 97f00e8172
88 changed files with 15037 additions and 3657 deletions

View File

@ -0,0 +1,95 @@
<?php
namespace App\Helpers\Installer;
use Exception;
use Illuminate\Database\SQLiteConnection;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Console\Output\BufferedOutput;
class DatabaseManager
{
/**
* Migrate and seed the database.
*
* @return array
*/
public function migrateAndSeed()
{
$outputLog = new BufferedOutput;
$this->sqlite($outputLog);
return $this->migrate($outputLog);
}
/**
* Run the migration and call the seeder.
*
* @param \Symfony\Component\Console\Output\BufferedOutput $outputLog
* @return array
*/
private function migrate(BufferedOutput $outputLog)
{
try {
Artisan::call('migrate', ['--force'=> true], $outputLog);
} catch (Exception $e) {
return $this->response($e->getMessage(), 'error', $outputLog);
}
return $this->seed($outputLog);
}
/**
* Seed the database.
*
* @param \Symfony\Component\Console\Output\BufferedOutput $outputLog
* @return array
*/
private function seed(BufferedOutput $outputLog)
{
try {
Artisan::call('db:seed', ['--force' => true], $outputLog);
} catch (Exception $e) {
return $this->response($e->getMessage(), 'error', $outputLog);
}
return $this->response(trans('installer_messages.final.finished'), 'success', $outputLog);
}
/**
* Return a formatted error messages.
*
* @param string $message
* @param string $status
* @param \Symfony\Component\Console\Output\BufferedOutput $outputLog
* @return array
*/
private function response($message, $status, BufferedOutput $outputLog)
{
return [
'status' => $status,
'message' => $message,
'dbOutputLog' => $outputLog->fetch(),
];
}
/**
* Check database type. If SQLite, then create the database file.
*
* @param \Symfony\Component\Console\Output\BufferedOutput $outputLog
*/
private function sqlite(BufferedOutput $outputLog)
{
if (DB::connection() instanceof SQLiteConnection) {
$database = DB::connection()->getDatabaseName();
if (! file_exists($database)) {
touch($database);
DB::reconnect(Config::get('database.default'));
}
$outputLog->write('Using SqlLite database: '.$database, 1);
}
}
}

View File

@ -0,0 +1,135 @@
<?php
namespace App\Helpers\Installer;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class EnvironmentManager
{
/**
* @var string
*/
private $envPath;
/**
* @var string
*/
private $envExamplePath;
/**
* Set the .env and .env.example paths.
*/
public function __construct()
{
$this->envPath = base_path('.env');
$this->envExamplePath = base_path('.env.example');
}
/**
* Get the content of the .env file.
*
* @return string
*/
public function getEnvContent()
{
if (! file_exists($this->envPath)) {
if (file_exists($this->envExamplePath)) {
copy($this->envExamplePath, $this->envPath);
} else {
touch($this->envPath);
}
}
return file_get_contents($this->envPath);
}
/**
* Get the the .env file path.
*
* @return string
*/
public function getEnvPath()
{
return $this->envPath;
}
/**
* Get the the .env.example file path.
*
* @return string
*/
public function getEnvExamplePath()
{
return $this->envExamplePath;
}
/**
* Save the edited content to the .env file.
*
* @param Request $input
* @return string
*/
public function saveFileClassic(Request $input)
{
$message = trans('installer_messages.environment.success');
try {
file_put_contents($this->envPath, $input->get('envConfig'));
} catch (Exception $e) {
$message = trans('installer_messages.environment.errors');
}
return $message;
}
/**
* Save the form content to the .env file.
*
* @param Request $request
* @return string
*/
public function saveFileWizard(Request $request)
{
$results = trans('installer_messages.environment.success');
$envFileData =
'APP_NAME=\''.$request->app_name."'\n".
'APP_ENV='.$request->environment."\n".
'APP_KEY='.'base64:'.base64_encode(Str::random(32))."\n".
'APP_DEBUG='.$request->app_debug."\n".
'APP_LOG_LEVEL='.$request->app_log_level."\n".
'APP_URL='.$request->app_url."\n\n".
'DB_CONNECTION='.$request->database_connection."\n".
'DB_HOST='.$request->database_hostname."\n".
'DB_PORT='.$request->database_port."\n".
'DB_DATABASE='.$request->database_name."\n".
'DB_USERNAME='.$request->database_username."\n".
'DB_PASSWORD='.$request->database_password."\n\n".
'BROADCAST_DRIVER='.$request->broadcast_driver."\n".
'CACHE_DRIVER='.$request->cache_driver."\n".
'SESSION_DRIVER='.$request->session_driver."\n".
'QUEUE_DRIVER='.$request->queue_driver."\n\n".
'REDIS_HOST='.$request->redis_hostname."\n".
'REDIS_PASSWORD='.$request->redis_password."\n".
'REDIS_PORT='.$request->redis_port."\n\n".
'MAIL_DRIVER='.$request->mail_driver."\n".
'MAIL_HOST='.$request->mail_host."\n".
'MAIL_PORT='.$request->mail_port."\n".
'MAIL_USERNAME='.$request->mail_username."\n".
'MAIL_PASSWORD='.$request->mail_password."\n".
'MAIL_ENCRYPTION='.$request->mail_encryption."\n\n".
'PUSHER_APP_ID='.$request->pusher_app_id."\n".
'PUSHER_APP_KEY='.$request->pusher_app_key."\n".
'PUSHER_APP_SECRET='.$request->pusher_app_secret;
try {
file_put_contents($this->envPath, $envFileData);
} catch (Exception $e) {
$results = trans('installer_messages.environment.errors');
}
return $results;
}
}

View File

@ -0,0 +1,79 @@
<?php
namespace App\Helpers\Installer;
use Exception;
use Illuminate\Support\Facades\Artisan;
use Symfony\Component\Console\Output\BufferedOutput;
class FinalInstallManager
{
/**
* Run final commands.
*
* @return string
*/
public function runFinal()
{
$outputLog = new BufferedOutput;
$this->generateKey($outputLog);
$this->publishVendorAssets($outputLog);
return $outputLog->fetch();
}
/**
* Generate New Application Key.
*
* @param \Symfony\Component\Console\Output\BufferedOutput $outputLog
* @return \Symfony\Component\Console\Output\BufferedOutput|array
*/
private static function generateKey(BufferedOutput $outputLog)
{
try {
if (config('installer.final.key')) {
Artisan::call('key:generate', ['--force'=> true], $outputLog);
}
} catch (Exception $e) {
return static::response($e->getMessage(), $outputLog);
}
return $outputLog;
}
/**
* Publish vendor assets.
*
* @param \Symfony\Component\Console\Output\BufferedOutput $outputLog
* @return \Symfony\Component\Console\Output\BufferedOutput|array
*/
private static function publishVendorAssets(BufferedOutput $outputLog)
{
try {
if (config('installer.final.publish')) {
Artisan::call('vendor:publish', ['--all' => true], $outputLog);
}
} catch (Exception $e) {
return static::response($e->getMessage(), $outputLog);
}
return $outputLog;
}
/**
* Return a formatted error messages.
*
* @param $message
* @param \Symfony\Component\Console\Output\BufferedOutput $outputLog
* @return array
*/
private static function response($message, BufferedOutput $outputLog)
{
return [
'status' => 'error',
'message' => $message,
'dbOutputLog' => $outputLog->fetch(),
];
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace App\Helpers\Installer;
class InstalledFileManager
{
/**
* Create installed file.
*
* @return int
*/
public function create()
{
$installedLogFile = storage_path('installed');
$dateStamp = date('Y/m/d h:i:sa');
if (! file_exists($installedLogFile)) {
$message = trans('installer_messages.installed.success_log_message').$dateStamp."\n";
file_put_contents($installedLogFile, $message);
} else {
$message = trans('installer_messages.updater.log.success_message').$dateStamp;
file_put_contents($installedLogFile, $message.PHP_EOL, FILE_APPEND | LOCK_EX);
}
return $message;
}
/**
* Update installed file.
*
* @return int
*/
public function update()
{
return $this->create();
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Helpers\Installer;
use Illuminate\Support\Facades\DB;
trait MigrationsHelper
{
/**
* Get the migrations in /database/migrations.
*
* @return array Array of migrations name, empty if no migrations are existing
*/
public function getMigrations()
{
$migrations = glob(database_path().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR.'*.php');
return str_replace('.php', '', $migrations);
}
/**
* Get the migrations that have already been ran.
*
* @return \Illuminate\Support\Collection List of migrations
*/
public function getExecutedMigrations()
{
// migrations table should exist, if not, user will receive an error.
return DB::table('migrations')->get()->pluck('migration');
}
}

View File

@ -0,0 +1,83 @@
<?php
namespace App\Helpers\Installer;
class PermissionsChecker
{
/**
* @var array
*/
protected $results = [];
/**
* Set the result array permissions and errors.
*
* @return mixed
*/
public function __construct()
{
$this->results['permissions'] = [];
$this->results['errors'] = null;
}
/**
* Check for the folders permissions.
*
* @param array $folders
* @return array
*/
public function check(array $folders)
{
foreach ($folders as $folder => $permission) {
if (! ($this->getPermission($folder) >= $permission)) {
$this->addFileAndSetErrors($folder, $permission, false);
} else {
$this->addFile($folder, $permission, true);
}
}
return $this->results;
}
/**
* Get a folder permission.
*
* @param $folder
* @return string
*/
private function getPermission($folder)
{
return substr(sprintf('%o', fileperms(base_path($folder))), -4);
}
/**
* Add the file to the list of results.
*
* @param $folder
* @param $permission
* @param $isSet
*/
private function addFile($folder, $permission, $isSet)
{
array_push($this->results['permissions'], [
'folder' => $folder,
'permission' => $permission,
'isSet' => $isSet,
]);
}
/**
* Add the file and set the errors.
*
* @param $folder
* @param $permission
* @param $isSet
*/
private function addFileAndSetErrors($folder, $permission, $isSet)
{
$this->addFile($folder, $permission, $isSet);
$this->results['errors'] = true;
}
}

View File

@ -0,0 +1,114 @@
<?php
namespace App\Helpers\Installer;
class RequirementsChecker
{
/**
* Minimum PHP Version Supported (Override is in installer.php config file).
*
* @var _minPhpVersion
*/
private $_minPhpVersion = '7.0.0';
/**
* Check for the server requirements.
*
* @param array $requirements
* @return array
*/
public function check(array $requirements)
{
$results = [];
foreach ($requirements as $type => $requirement) {
switch ($type) {
// check php requirements
case 'php':
foreach ($requirements[$type] as $requirement) {
$results['requirements'][$type][$requirement] = true;
if (! extension_loaded($requirement)) {
$results['requirements'][$type][$requirement] = false;
$results['errors'] = true;
}
}
break;
// check apache requirements
case 'apache':
foreach ($requirements[$type] as $requirement) {
// if function doesn't exist we can't check apache modules
if (function_exists('apache_get_modules')) {
$results['requirements'][$type][$requirement] = true;
if (! in_array($requirement, apache_get_modules())) {
$results['requirements'][$type][$requirement] = false;
$results['errors'] = true;
}
}
}
break;
}
}
return $results;
}
/**
* Check PHP version requirement.
*
* @return array
*/
public function checkPHPversion(string $minPhpVersion = null)
{
$minVersionPhp = $minPhpVersion;
$currentPhpVersion = $this->getPhpVersionInfo();
$supported = false;
if ($minPhpVersion == null) {
$minVersionPhp = $this->getMinPhpVersion();
}
if (version_compare($currentPhpVersion['version'], $minVersionPhp) >= 0) {
$supported = true;
}
$phpStatus = [
'full' => $currentPhpVersion['full'],
'current' => $currentPhpVersion['version'],
'minimum' => $minVersionPhp,
'supported' => $supported,
];
return $phpStatus;
}
/**
* Get current Php version information.
*
* @return array
*/
private static function getPhpVersionInfo()
{
$currentVersionFull = PHP_VERSION;
preg_match("#^\d+(\.\d+)*#", $currentVersionFull, $filtered);
$currentVersion = $filtered[0];
return [
'full' => $currentVersionFull,
'version' => $currentVersion,
];
}
/**
* Get minimum PHP version ID.
*
* @return string _minPhpVersion
*/
protected function getMinPhpVersion()
{
return $this->_minPhpVersion;
}
}

View File

@ -0,0 +1,23 @@
<?php
if (! function_exists('isActive')) {
/**
* Set the active class to the current opened menu.
*
* @param string|array $route
* @param string $className
* @return string
*/
function isActive($route, $className = 'active')
{
if (is_array($route)) {
return in_array(Route::currentRouteName(), $route) ? $className : '';
}
if (Route::currentRouteName() == $route) {
return $className;
}
if (strpos(URL::current(), $route)) {
return $className;
}
}
}