<?php
if ( version_compare( PHP_VERSION, '7.3.0', '<' ) ) {
  die( 'Requires PHP 7.3.0 or higher.' );
}

// error_reporting( E_ALL );
// ini_set( 'log_errors', '1' );
// ini_set( 'display_errors', '1' );

error_reporting( 0 );
ini_set( 'log_errors', '1' );
ini_set( 'display_errors', '0' );

function dump( $value, $exit = false ) {
  echo '<pre>';
  print_r( $value );
  echo '</pre>';
  if ( $exit ) {
    exit();
  }
}

spl_autoload_register( function( $className ) {
  $filename = strtolower( str_replace( '\\', DS, $className ) ) . '.class.php';
  if ( stream_resolve_include_path( $filename ) ) {
    include( $filename );
  }
});

define( 'DEVELOPMENT_ENVIRONMENT', false );

define( 'DS', DIRECTORY_SEPARATOR );
define( 'PS', PATH_SEPARATOR );

/* define the root directory */
define( 'ROOT_DIR', dirname( __FILE__, 2 ) . DS );

/* define the library directory */
define( 'LIB_DIR', ROOT_DIR . 'library' . DS );

/* define the configuration files directory */
define( 'CONFIG_DIR', ROOT_DIR . 'config' . DS );

/* define the application directory */
define( 'APP_DIR', ROOT_DIR . 'application' . DS );

/* define the variable data directory */
define( 'VAR_DIR', ROOT_DIR . 'var' . DS );

/* define the templates directory */
define( 'TEMPLATES_DIR', 'templates' . DS );

/* define the log directory */
define( 'LOG_DIR', ROOT_DIR . 'log' . DS );

set_include_path( get_include_path() . PS . LIB_DIR . 'core' . DS . PS . ROOT_DIR . PS . APP_DIR );

require ROOT_DIR . 'vendor' . DS . 'autoload.php';

include( CONFIG_DIR . 'dependencies.php' );

$config = IoC::resolve( 'config' );

define( 'BASE_URL', $config['base_url'] );

\IoC::resolve('eventmediator')->attach( 'upgrade_succeeded', function() {
  $GLOBALS['error'] = false;
} );
\IoC::resolve('eventmediator')->attach( 'upgrade_failed', function( $error ) {
  $GLOBALS['error'] = $error;
} );

use upgrade\services\WriteconfigFile;
use models\Template;

$writeConfigFile = new WriteconfigFile();
$writeConfigFile->perform();

if ( !$GLOBALS['error'] ) {
    $templates = Template::findMany( '*', ['content[~]' => 'Registry::get'] );

    foreach( $templates as $template ) {
        $template->content = str_replace( 'Registry::get', '\IoC::resolve', $template->content );
        $template->save();
    }
}

?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" media="screen" href="../public/css/ink.min.css" />
    <link rel="icon" type="image/png" href="../public/images/lemon-24.png">
    <title>LemonCMS | Upgrade</title>
  </head>
  <body>
    <?php if ( $GLOBALS['error'] ): ?>
      <div class="space">
	<div class="ink-alert basic error" role="alert">
	  <p><strong>Error: </strong><?=$GLOBALS['error']?></p>
	</div>
      </div>
    <?php else: ?>
      <div class="space">
	<div class="ink-alert block success" role="alert">
	  <h4>LemonCMS successfully upgraded.</h4>
	  <p>To enhance security you should now delete the <em>upgrade</em> folder.</p>
	</div>
      </div>
    <?php endif; ?>
  </body>
</html>
