我正在创建一个PHP框架,允许PHP开发人员仅使用PHP类创建ExtJS前端,例如创建网格如下所示:
$grid_import = new Backend_Layout_Grid('smart_worksheets');
$grid_import->set_width(1300);
$grid_import->set_rows_selectable(true);
$grid_import->set_title(__('backend.application.import.grid.title'));
$grid_import->set_margin('10px'); //CSS syntax, e.g. also "10px 0 0 0"
$grid_import->add_column(array('id_code'=>'name', 'label'=> __('backend.application.import.worksheetstoimport'), 'width'=>'300'));
$grid_import->add_column(array('id_code'=>'kind', 'label'=> __('backend.application.import.kind'), 'width'=>'50'));
$grid_import->add_column(array('id_code'=>'file_size', 'label'=> __('backend.application.import.sizebyte'), 'datatype' => 'int'));
$grid_import->add_column(array('id_code'=>'when_file_copied', 'label'=> __('backend.application.import.whenfilecopied'), 'datatype' => 'datetime', 'width'=>'150'));
$grid_import->add_column(array('id_code'=>'table_name', 'label'=> __('backend.application.import.mysqltablename'), 'width'=>'300'));
$grid_import->add_column(array('id_code'=>'when_table_created', 'label'=> __('backend.application.import.whentablecreated'), 'width'=>'160'));
$grid_import->add_column(array('id_code'=>'status', 'label'=> __('backend.application.import.status'), 'width'=>'300'));
$grid_import->set_doubleclick_target_uri('backend/application/importmanager/single', 0);
if (count($smart_worksheets) > 0)
{
$row_index = 0;
foreach ($smart_worksheets as $smart_worksheet)
{
$show_row = array(
'name' => $smart_worksheet['name'],
'kind' => $smart_worksheet['kind'],
'file_size' => $smart_worksheet['file_size'],
'when_file_copied' => $smart_worksheet['when_file_copied'],
'table_name' => $smart_worksheet['table_name'],
'when_table_created' => __($smart_worksheet['when_table_created']),
'status' => __($smart_worksheet['status'])
);
$grid_import->add_row($show_row);
if(in_array($smart_worksheet['status'], array('backend.application.import.status.needtoimport', 'backend.application.import.status.needtoreimport'))) {
$grid_import->add_row_format($row_index, Backend_Layout_Grid::ROW_FORMAT_RED);
}
if(in_array($smart_worksheet['status'], array('backend.application.import.status.isuptodate'))) {
$grid_import->add_row_format($row_index, Backend_Layout_Grid::ROW_FORMAT_GREEN);
}
if(intval($smart_worksheet['file_size']) > 4000000 AND (in_array($smart_worksheet['kind'], array('XLS','XLSX'))))
{
$grid_import->add_row_format($row_index, Backend_Layout_Grid::ROW_FORMAT_GRAY);
}
$row_index++;
}
}
Backend_Layout_Window::instance()->add_item($grid_import);
到目前为止,它表现良好,但由于我仅逐行输出javascript代码,因此随着我在类中构建更多功能,if/then逻辑在构建原始Javascript文本时变得越来越复杂。下面是一个典型的方法,可以生成Javascript代码:
public function render_main_code_block()
{
$retval = '';
$retval .= $this->render_data_variable();
$retval .= $this->render_array_reader_block();
$retval .= $this->render_grid_panel_block();
if($this->rows_selectable)
{
$retval .= self::render_line("````}),");
$retval .= self::render_line("````sm: sm,");
}
$retval .= self::render_line("````viewConfig: {");
if ($this->percentage_columns)
{
$retval .= self::render_line("``````forceFit: true,"); // true = percentage column width (add up to 100)
}
else
{
$retval .= self::render_line("``````forceFit: false,");
}
$retval .= self::render_line("``````getRowClass: function(record, rowIndex, rp, ds){");
if (count($this->row_formats) > 0)
{
foreach ($this->row_formats as $row_index => $row_format)
{
$retval .= self::render_line("````````if(rowIndex == ".$row_index."){");
$retval .= self::render_line("``````````return '".$row_format."';");
$retval .= self::render_line("````````}");
}
}
$retval .= self::render_line("````````return '';");
$retval .= self::render_line("``````}");
$retval .= self::render_line("````},");
$retval .= self::render_line("````title: '$this->title',");
if ( ! is_null($this->width))
{
$retval .= self::render_line("````width: $this->width,");
}
$retval .= $this->render_double_click_handler();
$retval .= self::render_line("````autoHeight: true,");
$retval .= self::render_line("````frame: true");
$retval .= self::render_line("``});");
$retval .= self::render_line("");
$retval .= self::render_line("``replaceComponentContent(targetRegion, ".$this->script_variable_name.");");
$retval .= self::render_line("``".$this->script_variable_name.".getSelectionModel().selectFirstRow();");
// for word wrapping in columns
$retval .= self::render_line("``function columnWrap(val){");
$retval .= self::render_line("````return '<div style=\"white-space:normal !important;\">'+ val +'</div>';");
$retval .= self::render_line("``}");
return $retval;
}
目前,有一些问题使得这段代码变得过于复杂难以维护:
- 属性列表中,只有最后一个项目没有逗号,这使得列表的处理变得困难。到目前为止,我已经将必需的属性放在列表的末尾,以确保不带逗号的项始终被包含。
- 某些功能(例如将复选框添加到网格中或否)需要在javascript代码的四个不同位置插入代码,我可以想象还有其他功能需要重构现有代码,因此如果启用该功能,则周围的代码需要被包含在对象的另一个部分中。
因此,我希望在某个时候进行重构,改进我的Javascript创建方式。
目前我只看到了一个选择,那就是创建一个CodeFactory
类,其中包含ExtJsVariable
,我可以向其中添加包含递归对象本身的对象,这些对象可以是simpleArray
或anonymousFunction
等类型,然后通过输出$codeFactory->render()
来生成代码,该方法将基于其所有内部对象生成代码:
还有哪些选项或代码生成框架可以使PHP生成此Javascript/ExtJS代码更易于维护和简单明了?