Esempio n. 1
0
 /**
  * Create the bootstrap file
  *
  * @param \Pop\Config $install
  * @return void
  */
 public static function install($install)
 {
     // Define full paths of the autoloader and config files
     $autoload = realpath(__DIR__ . '/../../Loader/Autoloader.php');
     $moduleSrc = realpath($install->project->base . '/module/' . $install->project->name . '/src');
     $projectCfg = realpath($install->project->base . '/config/project.php');
     $moduleCfg = realpath($install->project->base . '/module/' . $install->project->name . '/config/module.php');
     // 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)) {
         $autoload = "__DIR__ . '/vendor/PopPHPFramework/src/Pop/Loader/Autoloader.php'";
         $moduleSrc = "__DIR__ . '/module/" . $install->project->name . "/src'";
         $projectCfg = "__DIR__ . '/config/project.php'";
         $moduleCfg = "__DIR__ . '/module/" . $install->project->name . "/config/module.php'";
         // If the docroot is under the base
     } else {
         if (strlen($base) < strlen($docroot)) {
             // Calculate how many levels up the base is from the docroot
             $diff = str_replace($base, '', $docroot);
             $levels = substr_count($diff, '/');
             $dirs = '/';
             for ($i = 0; $i < $levels; $i++) {
                 $dirs .= '../';
             }
             $autoload = "__DIR__ . '" . $dirs . "vendor/PopPHPFramework/src/Pop/Loader/Autoloader.php'";
             $moduleSrc = "__DIR__ . '" . $dirs . "module/" . $install->project->name . "/src'";
             $projectCfg = "__DIR__ . '" . $dirs . "config/project.php'";
             $moduleCfg = "__DIR__ . '" . $dirs . "module/" . $install->project->name . "/config/module.php'";
             // If the base is under the docroot
         } else {
             if (strlen($base) > strlen($docroot)) {
                 $dir = str_replace($docroot, '', $base);
                 $autoload = "__DIR__ . '" . $dir . "/vendor/PopPHPFramework/src/Pop/Loader/Autoloader.php'";
                 $moduleSrc = "__DIR__ . '" . $dir . "/module/" . $install->project->name . "/src'";
                 $projectCfg = "__DIR__ . '" . $dir . "/config/project.php'";
                 $moduleCfg = "__DIR__ . '" . $dir . "/module/" . $install->project->name . "/config/module.php'";
             }
         }
     }
     // Create new Code file object
     $bootstrap = new \Pop\Code\Generator($install->project->docroot . '/bootstrap.php');
     // Create new bootstrap file
     if (!file_exists($install->project->docroot . '/bootstrap.php')) {
         $bootstrap->appendToBody("// Require the Autoloader class file" . PHP_EOL . "require_once {$autoload};" . PHP_EOL)->appendToBody("// Instantiate the autoloader object" . PHP_EOL . "\$autoloader = Pop\\Loader\\Autoloader::factory();" . PHP_EOL . "\$autoloader->splAutoloadRegister();");
     }
     // Else, just append to the existing bootstrap file
     $bootstrap->appendToBody("\$autoloader->register('{$install->project->name}', {$moduleSrc});" . PHP_EOL)->appendToBody("// Create a project object")->appendToBody("\$project = {$install->project->name}\\Project::factory(")->appendToBody("    include {$projectCfg},")->appendToBody("    include {$moduleCfg},");
     // Set up any controllers via a router object
     if (isset($install->controllers)) {
         $controllers = $install->controllers->asArray();
         $ctrls = array();
         foreach ($controllers as $key => $value) {
             $subs = array();
             foreach ($value as $k => $v) {
                 if (is_array($v)) {
                     $subs[] = $k;
                 }
             }
             if (count($subs) > 0) {
                 $ctls = "'{$key}' => array(" . PHP_EOL;
                 if (array_key_exists('index', $value)) {
                     $ctls .= "            '/' => '{$install->project->name}\\Controller\\" . ucfirst(String::underscoreToCamelcase(substr($key, 1))) . "\\IndexController'," . PHP_EOL;
                 }
                 foreach ($subs as $sub) {
                     $ctls .= "            '{$sub}' => '{$install->project->name}\\Controller\\" . ucfirst(String::underscoreToCamelcase(substr($key, 1))) . "\\" . ucfirst(String::underscoreToCamelcase(substr($sub, 1))) . "Controller'," . PHP_EOL;
                 }
                 $ctls .= '        )';
                 $ctrls[] = $ctls;
             } else {
                 if ($key == '/') {
                     $ctrls[] = "'{$key}' => '{$install->project->name}\\Controller\\IndexController'";
                 } else {
                     $controllerName = substr($key, 1);
                     if (array_key_exists('index', $value)) {
                         $ctrls[] = "'{$key}' => '{$install->project->name}\\Controller\\" . ucfirst(String::underscoreToCamelcase($controllerName)) . "\\IndexController'";
                     } else {
                         $ctrls[] = "'{$key}' => '{$install->project->name}\\Controller\\" . ucfirst(String::underscoreToCamelcase($controllerName)) . "Controller'";
                     }
                 }
             }
         }
         $bootstrap->appendToBody("    new Pop\\Mvc\\Router(array(");
         $i = 1;
         foreach ($ctrls as $c) {
             $end = $i < count($ctrls) ? ',' : null;
             $bootstrap->appendToBody("        " . $c . $end);
             $i++;
         }
         $bootstrap->appendToBody("    ))");
     }
     // Finalize and save the bootstrap file
     $bootstrap->appendToBody(");")->save();
 }
Esempio n. 2
0
 /**
  * Install the base folder and file structure
  *
  * @param \Pop\Config $install
  * @return 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();
 }