setUse() публичный Метод

Set a namespace to use
public setUse ( string $use, string $as = null ) : NamespaceGenerator
$use string
$as string
Результат NamespaceGenerator
Пример #1
0
 /**
  * Install the project class files
  *
  * @param \Pop\Config $install
  * @param string     $installDir
  * @return void
  */
 public static function install($install, $installDir)
 {
     // Create the project class file
     $projectCls = new Generator($install->project->base . '/module/' . $install->project->name . '/src/' . $install->project->name . '/Project.php', Generator::CREATE_CLASS);
     // Set namespace
     $ns = new NamespaceGenerator($install->project->name);
     $ns->setUse('Pop\\Project\\Project', 'P');
     // Create 'run' method
     $run = new MethodGenerator('run');
     $run->setDesc('Add any project specific code to this method for run-time use here.');
     $run->appendToBody('parent::run();', false);
     $run->getDocblock()->setReturn('void');
     // Finalize the project config file and save it
     $projectCls->setNamespace($ns);
     $projectCls->code()->setParent('P')->addMethod($run);
     $projectCls->save();
     $input = self::installWeb();
     // Install any web config and controller files
     if ($input != 'n') {
         if (file_exists(__DIR__ . '/Web/index.php')) {
             $index = new Generator(__DIR__ . '/Web/index.php');
             $contents = $index->read() . '// Run the project' . PHP_EOL . 'try {' . PHP_EOL . '    $project->run();' . PHP_EOL . '} catch (\\Exception $e) {' . PHP_EOL . '    echo $e->getMessage();' . PHP_EOL . '}' . PHP_EOL;
             file_put_contents($install->project->docroot . '/index.php', $contents);
         }
         if ($input == 'a') {
             if (file_exists(__DIR__ . '/Web/ht.access')) {
                 copy(__DIR__ . '/Web/ht.access', $install->project->docroot . '/.htaccess');
             }
         } else {
             if ($input == 'i') {
                 if (file_exists(__DIR__ . '/Web/web.config')) {
                     copy(__DIR__ . '/Web/web.config', $install->project->docroot . '/web.config');
                 }
             } else {
                 echo \Pop\I18n\I18n::factory()->__('You will have to install your web server rewrite configuration manually.') . PHP_EOL;
             }
         }
     }
 }
Пример #2
0
 /**
  * Install the table class files
  *
  * @param \Pop\Config $install
  * @param array  $dbTables
  * @return void
  */
 public static function install($install, $dbTables)
 {
     echo \Pop\I18n\I18n::factory()->__('Creating database table class files...') . PHP_EOL;
     // Create table class folder
     $tableDir = $install->project->base . '/module/' . $install->project->name . '/src/' . $install->project->name . '/Table';
     if (!file_exists($tableDir)) {
         mkdir($tableDir);
     }
     // Loop through the tables, creating the classes
     foreach ($dbTables as $table => $value) {
         $prefix = isset($value['prefix']) ? $value['prefix'] : null;
         $tableName = ucfirst(\Pop\Filter\String::underscoreToCamelcase(str_replace($prefix, '', $table)));
         $ns = new NamespaceGenerator($install->project->name . '\\Table');
         $ns->setUse('Pop\\Db\\Record');
         if (strpos($value['primaryId'], '|') !== false) {
             $pIdType = 'array';
             $pId = explode('|', $value['primaryId']);
         } else {
             $pIdType = 'string';
             $pId = $value['primaryId'];
         }
         if (null !== $prefix) {
             $prefix = new PropertyGenerator('prefix', 'string', $prefix, 'protected');
         }
         $propId = new PropertyGenerator('primaryId', $pIdType, $pId, 'protected');
         $propAuto = new PropertyGenerator('auto', 'boolean', $value['auto'], 'protected');
         // Create and save table class file
         $tableCls = new Generator($tableDir . '/' . $tableName . '.php', Generator::CREATE_CLASS);
         $tableCls->setNamespace($ns);
         $tableCls->code()->setParent('Record')->addProperty($propId)->addProperty($propAuto);
         if (null !== $prefix) {
             $tableCls->code()->addProperty($prefix);
         }
         $tableCls->save();
     }
 }
Пример #3
0
<?php

require_once '../../bootstrap.php';
use Pop\Code;
try {
    // Create the code generator object
    $code = new Code\Generator('MyClass.php', Code\Generator::CREATE_CLASS);
    $code->setDocblock(new Code\Generator\DocblockGenerator('This is my test class file'))->getDocblock()->setTag('category', 'Pop')->setTag('package', 'Pop_Code')->setTag('author', 'Joe Author');
    // Create namespace object
    $ns = new Code\Generator\NamespaceGenerator('Some\\Other');
    $ns->setUse('Some\\Other\\Thing')->setUse('Some\\Other\\Blah', 'B')->setUse('Some\\Other\\Another');
    // Create property object
    $prop = new Code\Generator\PropertyGenerator('_testProp', 'string', 'test', 'protected');
    $prop->setDesc('This is a test property');
    // Create a method object
    $method = new Code\Generator\MethodGenerator('__construct');
    $method->setDesc('This is a test method')->setBody("// Let's get some stuff to happen here." . PHP_EOL . "\$blah = 'Sounds like a good idea';")->appendToBody("echo \$blah;", false)->addArgument('test', "null", "Pop\\Filter\\String")->addArgument('other', "array()", 'array');
    // Add code pieces to the code file
    $code->setNamespace($ns);
    $code->code()->setDocblock(new Code\Generator\DocblockGenerator('This is my test class'))->getDocblock()->setTag('category', 'Pop')->setTag('package', 'Pop_Code')->setTag('author', 'Joe Author');
    $code->code()->addProperty($prop);
    $code->code()->addMethod($method);
    // Render and output the code
    $code->output();
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL . PHP_EOL;
}
Пример #4
0
 /**
  * Install the form class files
  *
  * @param \Pop\Config $install
  * @return 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();
     }
 }