2017 © Pedro Peláez
 

library php-codewriter

Helper classes to generate code for writing to a file

image

nbasnet/php-codewriter

Helper classes to generate code for writing to a file

  • Thursday, June 8, 2017
  • by nbasnet
  • Repository
  • 1 Watchers
  • 0 Stars
  • 102 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 11 % Grown

The README.md

php-codewriter

Generate code for writing to file using different components
Need to pass instance of CodeWriterSettings to the top most component using setSettings() [inherited from BaseComponent Class] before calling writeComponent() method. Exception SettingsNotSet is thrown if writeComponent() is called before settings is set.
      CodeWriterSettings::create(ISyntaxGrammar::PHP, $indent = 0)

1 . Variable Component

      $variable = VariableComponent::create("var")->setValue("Is Name", "string")->writeComponent();

OUTPUT: 
      $var = 'Is Name';
Constants:
      $constant = VariableComponent::create("TEST")->setValue("VALUE 1")->makeConstant()->writeComponent();

OUTPUT: 
      const TEST = 'VALUE 1';

2 . Array Component

      $array    = ArrayComponent::create("what_is_this", TRUE)
        ->setValue([
            "string" => "is game",
            "number" => 2,
            "bool"   => FALSE,
        ])
        ->writeComponent();

OUTPUT:   
      $what_is_this = [
            'string' => 'is game',
            'number' => 2,
            'bool' => false,
        ];

3 . Function Component

      $function = FunctionComponent::create("myFunction")
        ->setAccessIdentifier(BaseComponent::ACCESS_PUBLIC)
        ->setParameters([ArrayComponent::create("my_array"), $variable])
        ->appendComponent($array)
        ->appendComponent($variable)
        ->writeComponent();

OUTPUT:
      /**
       * @param array $my_array
       * @param string $val
       */
      public function myFunction(array $my_array, $val = 'Is Name')
      {
          $what_is_this = [
              'string' => 'is game',
              'number' => 2,
              'bool' => false,
          ];
          $nischal = 'Is Name';
      }

4 . Class Component

      $class = ClassComponent::create('TestController')
        ->setExtends("Controller")
        ->appendComponent($variable)
        ->appendComponent($constant)
        ->appendBlankLine()
        ->appendComponent($array)
        ->appendBlankLine()
        ->appendComponent($function)
        ->writeComponent();

OUTPUT:
      /**
       * Class TestController
       */
      class TestController extends Controller
      {
           $var = 'Is Name';
           const TEST = 'VALUE 1';

           $what_is_this = [
              'string' => 'is game',
              'number' => 2,
              'bool' => false,
           ];

           /**
            * @param array $my_array
            * @param string $val
            */
           public function myFunction(array $my_array, $val = 'Is Name')
           {
               $what_is_this = [
                   'string' => 'is game',
                   'number' => 2,
                   'bool' => false,
               ];
               $nischal = 'Is Name';
           }
      }

The Versions

08/06 2017

dev-master

9999999-dev

Helper classes to generate code for writing to a file

  Sources   Download

The Development Requires

by Nischal Basnet

17/02 2017

v0.1.0.x-dev

0.1.0.9999999-dev

Helper classes to generate code for writing to a file

  Sources   Download

The Development Requires

by Nischal Basnet

10/02 2017

0.1.0

0.1.0.0

Helper classes to generate code for writing to a file

  Sources   Download

The Development Requires

by Nischal Basnet