nbasnet / php-codewriter
Helper classes to generate code for writing to a file
Installs: 448
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/nbasnet/php-codewriter
Requires (Dev)
- phpunit/phpunit: 5.5.*
This package is not auto-updated.
Last update: 2025-12-21 02:18:23 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'; } }