It is a pity, but I have to admit, that Magento 2 has issues in layout generation process. You'll learn during the talk about the benefits of compiling XML structures into PHP code, and even use the compilation of PHP code into PHP code to speed up your modules complex logic.
7. COMPLEX DEPENDENCY TREE
class ClassOne
{
public function __construct(ClassTwo $dependency) {}
}
class ClassTwo
{
public function __construct(ClassThree $dependency) {}
}
class ClassThree
{
public function __construct(ClassFour $dependencyOne, ClassFive $dependen
}
// ..
12. BUT COMPILATION LOOKS UGLY...
You need to create PHP code within PHP code
You need to write it to external file
You need to include that file inside of your code
13. I WAS LOOKING FOR A LIBRARY
Didn't find one... So I wrote it myself.
14. ECOMDEVCOMPILER
Created to wrap writing PHP code within PHP
Automatically stores compiled code
Automatically validates source and re-compiles code
when needed
Provides easy to use API to create parsers, builders and
executors
15. INSTALLATION
1. Available as a composer dependency
2. Instantiate it directly or via DI container.
composer require "ecomdev/compiler"
19. PARSER
use EcomDevCompilerStatementBuilder;
class Parser implements EcomDevCompilerParserInterface
{
// .. constructor with builder as dependency
public function parse($value)
{
$xml = simplexml_load_string($value);
$info = $this->readXml($xml);
return $this->getPhpCode($info, $this->builder);
}
// .. other methods
}
20. PARSE XML DATA
private function readXml($xml)
{
$info = [];
foreach ($xml->children() as $node) {
if ($node->getName() === 'type') {
$info['types'][(string)$node->id] = (string)$node->class;
} elseif ($node->getName() === 'object') {
$info['objects'][(string)$node->id] = (string)$node->type;
}
}
return $info;
}
27. RESULT
return function ($item, $columns) {
return [
$columns['id1']->export($item),
$columns['id2']->export($item),
$columns['id3']->export($item),
// ...
];
};
28. MAIN COMPONENTS
CompilerInterface - Compiler instance
StorageInterface - Stores compiled files
SourceInterface - Provider of data (File, String,
StaticData)
ParserInterface - Parser of data
ObjectBuilderInterface - Bound builder for included files
30. EXPORTABLE OBJECTS
class SomeClass implements EcomDevCompilerExportableInterface
{
public function __construct($foo, $bar) { /* */ }
public function export() {
return [
'foo' => $this->foo,
'bar' => $this->bar
];
}
}
Will be automatically compiled into:
new SomeClass('fooValue', 'barValue');