2024-09-25 00:40:46 -04:00
< ? php
namespace Socialbox\Classes\CliCommands ;
use LogLib\Log ;
use PDOException ;
use Socialbox\Classes\Configuration ;
use Socialbox\Classes\Database ;
use Socialbox\Classes\Resources ;
use Socialbox\Enums\DatabaseObjects ;
use Socialbox\Interfaces\CliCommandInterface ;
class InitializeCommand implements CliCommandInterface
{
/**
2024-09-25 00:41:57 -04:00
* @ inheritDoc
2024-09-25 00:40:46 -04:00
*/
public static function execute ( array $args ) : int
{
if ( Configuration :: getConfiguration ()[ 'instance' ][ 'enabled' ] === false && ! isset ( $args [ 'force' ]))
{
Log :: info ( 'net.nosial.socialbox' , 'Socialbox is disabled. Use --force to initialize the instance or set `instance.enabled` to True in the configuration' );
return 1 ;
}
print ( " Initializing Socialbox... \n " );
foreach ( DatabaseObjects :: casesOrdered () as $object )
{
Log :: verbose ( 'net.nosial.socialbox' , " Initializing database object { $object -> value } " );
try
{
Database :: getConnection () -> exec ( file_get_contents ( Resources :: getDatabaseResource ( $object )));
}
catch ( PDOException $e )
{
// Check if the error code is for "table already exists"
if ( $e -> getCode () === '42S01' )
{
Log :: warning ( 'net.nosial.socialbox' , " Database object { $object -> value } already exists, skipping... " );
continue ;
}
else
{
Log :: error ( 'net.nosial.socialbox' , " Failed to initialize database object { $object -> value } : { $e -> getMessage () } " , $e );
return 1 ;
}
}
catch ( \Exception $e )
{
Log :: error ( 'net.nosial.socialbox' , " Failed to initialize database object { $object -> value } : { $e -> getMessage () } " , $e );
return 1 ;
}
}
Log :: info ( 'net.nosial.socialbox' , 'Socialbox has been initialized successfully' );
return 0 ;
}
/**
2024-09-25 00:41:57 -04:00
* @ inheritDoc
2024-09-25 00:40:46 -04:00
*/
public static function getHelpMessage () : string
{
return " Initialize Command - Initializes Socialbox for first-runs \n " .
" Usage: socialbox init [arguments] \n \n " .
" Arguments: \n " .
" --force - Forces the initialization process to run even the instance is disabled \n " ;
}
/**
2024-09-25 00:41:57 -04:00
* @ inheritDoc
2024-09-25 00:40:46 -04:00
*/
public static function getShortHelpMessage () : string
{
return " Initializes Socialbox for first-runs " ;
}
}