######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)
$variable = VariableComponent::create("var")->setValue("Is Name", "string")->writeComponent();
OUTPUT:
$var = 'Is Name';
$constant = VariableComponent::create("TEST")->setValue("VALUE 1")->makeConstant()->writeComponent();
OUTPUT:
const TEST = 'VALUE 1';
$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,
];
$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';
}
$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';
}
}