nbasnet / php-codewriter
There is no license information available for the latest version (0.1.0) of this package.
Helper classes to generate code for writing to a file
0.1.0
2017-02-10 07:41 UTC
Requires (Dev)
- phpunit/phpunit: 5.5.*
This package is not auto-updated.
Last update: 2025-06-07 23:06:18 UTC
README
######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'; } }