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

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

Install the form class files
public static install ( Config $install ) : void
$install Pop\Config
Результат void
    public static function install($install)
    {
        echo \Pop\I18n\I18n::factory()->__('Creating form class files...') . PHP_EOL;
        // Create form class folder
        $formDir = $install->project->base . '/module/' . $install->project->name . '/src/' . $install->project->name . '/Form';
        if (!file_exists($formDir)) {
            mkdir($formDir);
        }
        $forms = $install->forms->asArray();
        foreach ($forms as $name => $form) {
            $formName = ucfirst(\Pop\Filter\String::underscoreToCamelcase($name));
            // Define namespace
            $ns = new NamespaceGenerator($install->project->name . '\\Form');
            $ns->setUse('Pop\\Form\\Form')->setUse('Pop\\Form\\Element')->setUse('Pop\\Validator');
            // Create the constructor
            $construct = new MethodGenerator('__construct');
            $construct->setDesc('Constructor method to instantiate the form object');
            $construct->getDocblock()->setReturn('self');
            $construct->addArguments(array(array('name' => 'action', 'value' => 'null', 'type' => 'string'), array('name' => 'method', 'value' => "'post'", 'type' => 'string'), array('name' => 'fields', 'value' => 'null', 'type' => 'array'), array('name' => 'indent', 'value' => 'null', 'type' => 'string')));
            // Create the init values array within the constructor
            if (is_array($form) && count($form) > 0) {
                $construct->appendToBody("\$this->initFieldsValues = array (");
                $i = 0;
                foreach ($form as $name => $field) {
                    $i++;
                    $construct->appendToBody("    '" . $name . "' => array (");
                    $j = 0;
                    foreach ($field as $key => $value) {
                        $j++;
                        $comma = $j < count($field) ? ',' : null;
                        if ($key == 'validators') {
                            $val = null;
                            if (is_array($value)) {
                                $val = 'array(' . PHP_EOL;
                                foreach ($value as $v) {
                                    $val .= '            new Validator\\' . $v . ',' . PHP_EOL;
                                }
                                $val .= '        )';
                            } else {
                                $val = 'new Validator\\' . $value;
                            }
                            $construct->appendToBody("        '{$key}' => {$val}{$comma}");
                        } else {
                            if ($key == 'value' || $key == 'marked' || $key == 'attributes' || $key == 'error') {
                                $val = var_export($value, true);
                                $val = str_replace(PHP_EOL, PHP_EOL . '        ', $val);
                                if (strpos($val, 'Select::') !== false) {
                                    $val = 'Element\\' . str_replace("'", '', $val);
                                }
                                $construct->appendToBody("        '{$key}' => {$val}{$comma}");
                            } else {
                                if (is_bool($value)) {
                                    $val = $value ? 'true' : 'false';
                                } else {
                                    $val = "'" . $value . "'";
                                }
                                $construct->appendToBody("        '{$key}' => {$val}{$comma}");
                            }
                        }
                    }
                    $end = $i < count($form) ? '    ),' : '    )';
                    $construct->appendToBody($end);
                }
                $construct->appendToBody(");");
            }
            $construct->appendToBody("parent::__construct(\$action, \$method, \$fields, \$indent);");
            // Create and save form class file
            $formCls = new Generator($formDir . '/' . $formName . '.php', Generator::CREATE_CLASS);
            $formCls->setNamespace($ns);
            $formCls->code()->setParent('Form')->addMethod($construct);
            $formCls->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;
     }
 }
Forms