Pop\Project\Install\Base::install PHP Метод

install() публичный статический Метод

Install the base folder and file structure
public static install ( Config $install ) : void
$install Pop\Config
Результат void
    public static function install($install)
    {
        echo \Pop\I18n\I18n::factory()->__('Creating base folder and file structure...') . PHP_EOL;
        // Define folders to create
        $folders = array($install->project->base, $install->project->base . '/config', $install->project->base . '/module', $install->project->base . '/module/' . $install->project->name, $install->project->base . '/module/' . $install->project->name . '/config', $install->project->base . '/module/' . $install->project->name . '/data', $install->project->base . '/module/' . $install->project->name . '/src', $install->project->base . '/module/' . $install->project->name . '/src/' . $install->project->name, $install->project->base . '/module/' . $install->project->name . '/view', $install->project->docroot);
        // Create the folders
        foreach ($folders as $folder) {
            if (!file_exists($folder)) {
                mkdir($folder);
            }
        }
        // Make the '/data' folder writable
        chmod($install->project->base . '/module/' . $install->project->name . '/data', 0777);
        // Figure out the relative base and docroot
        $base = str_replace("\\", '/', realpath($install->project->base));
        $docroot = str_replace("\\", '/', realpath($install->project->docroot));
        $base = substr($base, -1) == '/' ? substr($base, 0, -1) : $base;
        $docroot = substr($docroot, -1) == '/' ? substr($docroot, 0, -1) : $docroot;
        // If the base and docroot are the same
        if (strlen($base) == strlen($docroot)) {
            $base = "__DIR__ . '/../'";
            $docroot = "__DIR__ . '/../'";
            // If the docroot is under the base
        } else {
            if (strlen($base) < strlen($docroot)) {
                $relDocroot = str_replace($base, '', $docroot);
                $base = "__DIR__ . '/../'";
                $docroot = "__DIR__ . '/.." . $relDocroot . "'";
                // If the base is under the docroot
            } else {
                if (strlen($base) > strlen($docroot)) {
                    // Calculate how many levels up the docroot is from the base
                    $diff = str_replace($docroot, '/', $base);
                    $levels = substr_count($diff, '/');
                    $dirs = null;
                    for ($i = 0; $i < $levels; $i++) {
                        $dirs .= '../';
                    }
                    $base = "__DIR__ . '/../'";
                    $docroot = "__DIR__ . '/" . $dirs . "'";
                }
            }
        }
        // Create project.php file
        $projectCfg = new \Pop\Code\Generator($install->project->base . '/config/project.php');
        $projectCfg->appendToBody('return new Pop\\Config(array(', true)->appendToBody("    'base'      => " . $base . ",")->appendToBody("    'docroot'   => " . $docroot, false);
        // Add the database config to it
        if (isset($install->databases)) {
            $projectCfg->appendToBody(",")->appendToBody("    'databases' => array(");
            $databases = $install->databases->asArray();
            $default = null;
            $i = 0;
            foreach ($databases as $dbname => $db) {
                $isPdo = stripos($db['type'], 'pdo') !== false ? true : false;
                $isSqlite = stripos($db['type'], 'sqlite') !== false ? true : false;
                if ($isPdo) {
                    $pdoType = strtolower(substr($db['type'], strpos($db['type'], '_') + 1));
                    $realDbType = 'Pdo';
                } else {
                    $pdoType = null;
                    $realDbType = $db['type'];
                }
                $projectCfg->appendToBody("        '" . $dbname . "' => Pop\\Db\\Db::factory('" . $realDbType . "', array (");
                $j = 0;
                $default = $db['default'] ? $dbname : null;
                $dbCreds = $db;
                unset($dbCreds['type']);
                unset($dbCreds['prefix']);
                unset($dbCreds['default']);
                foreach ($dbCreds as $key => $value) {
                    $j++;
                    if ($isSqlite) {
                        $dbFile = "__DIR__ . '/../module/" . $install->project->name . "/data/" . basename($value) . "'";
                        $ary = "            '{$key}' => {$dbFile}";
                    } else {
                        $ary = "            '{$key}' => '{$value}'";
                    }
                    if ($isPdo) {
                        $ary .= "," . PHP_EOL . "            'type' => '{$pdoType}'";
                    }
                    if ($j < count($dbCreds)) {
                        $ary .= ',';
                    }
                    $projectCfg->appendToBody($ary);
                }
                $i++;
                $end = $i < count($databases) ? '        )),' : '        ))';
                $projectCfg->appendToBody($end);
            }
            $projectCfg->appendToBody('    )', false);
            if (null !== $default) {
                $projectCfg->appendToBody("," . PHP_EOL . "    'defaultDb' => '" . $default . "'", false);
            }
        }
        // Save project config
        $projectCfg->appendToBody(PHP_EOL . '));', false);
        $projectCfg->save();
        // Create the module config file
        $moduleCfg = new \Pop\Code\Generator($install->project->base . '/module/' . $install->project->name . '/config/module.php');
        $moduleCfg->appendToBody('return array(')->appendToBody("    '{$install->project->name}' => new Pop\\Config(array(")->appendToBody("        'base'   => __DIR__ . '/../',")->appendToBody("        'config' => __DIR__ . '/../config',")->appendToBody("        'data'   => __DIR__ . '/../data',")->appendToBody("        'src'    => __DIR__ . '/../src',")->appendToBody("        'view'   => __DIR__ . '/../view'")->appendToBody("    ))")->appendToBody(");", false)->save();
    }

Usage Example

Пример #1
0
 /**
  * Install the project based on the available config files
  *
  * @param string $installFile
  * @return void
  */
 public static function install($installFile)
 {
     // Display instructions to continue
     $dbTables = array();
     self::instructions();
     $input = self::cliInput();
     if ($input == 'n') {
         echo I18n::factory()->__('Aborted.') . PHP_EOL . PHP_EOL;
         exit(0);
     }
     // Get the install config.
     $installDir = realpath(dirname($installFile));
     $install = (include $installFile);
     // Check if a project folder already exists.
     if (file_exists($install->project->name)) {
         echo wordwrap(I18n::factory()->__('Project folder exists. This may overwrite any project files you may already have under that project folder.'), 70, PHP_EOL) . PHP_EOL;
         $input = self::cliInput();
     } else {
         $input = 'y';
     }
     // If 'No', abort
     if ($input == 'n') {
         echo I18n::factory()->__('Aborted.') . PHP_EOL . PHP_EOL;
         exit(0);
         // Else, continue
     } else {
         $db = false;
         $databases = array();
         // Test for a database creds and schema, and ask
         // to test and install the database.
         if (isset($install->databases)) {
             $databases = $install->databases->asArray();
             echo I18n::factory()->__('Database credentials and schema detected.') . PHP_EOL;
             $input = self::cliInput(I18n::factory()->__('Test and install the database(s)?') . ' (Y/N) ');
             $db = $input == 'n' ? false : true;
         }
         // Handle any databases
         if ($db) {
             // Get current error reporting setting and set
             // error reporting to E_ERROR to suppress warnings
             $oldError = ini_get('error_reporting');
             error_reporting(E_ERROR);
             // Test the databases
             echo I18n::factory()->__('Testing the database(s)...') . PHP_EOL;
             foreach ($databases as $dbname => $db) {
                 echo I18n::factory()->__('Testing') . ' \'' . $dbname . '\'...' . PHP_EOL;
                 if (!isset($db['type']) || !isset($db['database'])) {
                     echo I18n::factory()->__('The database type and database name must be set for the database ') . '\'' . $dbname . '\'.' . PHP_EOL . PHP_EOL;
                     exit(0);
                 }
                 $check = Install\Dbs::check($db);
                 if (null !== $check) {
                     echo $check . PHP_EOL . PHP_EOL;
                     exit(0);
                 } else {
                     echo I18n::factory()->__('Database') . ' \'' . $dbname . '\' passed.' . PHP_EOL;
                     echo I18n::factory()->__('Installing database') . ' \'' . $dbname . '\'...' . PHP_EOL;
                     $tables = Install\Dbs::install($dbname, $db, $installDir, $install);
                     if (count($tables) > 0) {
                         $dbTables = array_merge($dbTables, $tables);
                     }
                 }
             }
             // Return error reporting to its original state
             error_reporting($oldError);
         }
         // Install base folder and file structure
         Install\Base::install($install);
         // Install project files
         Install\Projects::install($install, $installDir);
         // Install table class files
         if (count($dbTables) > 0) {
             Install\Tables::install($install, $dbTables);
         }
         // Install controller class files
         if (isset($install->controllers)) {
             Install\Controllers::install($install, $installDir);
         }
         // Install form class files
         if (isset($install->forms)) {
             Install\Forms::install($install);
         }
         // Install model class files
         if (isset($install->models)) {
             Install\Models::install($install);
         }
         // Create 'bootstrap.php' file
         Install\Bootstrap::install($install);
         echo I18n::factory()->__('Project install complete.') . PHP_EOL . PHP_EOL;
     }
 }