Skip to content

Instantly share code, notes, and snippets.

@harikt
Created December 3, 2014 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harikt/1a2a2b4d3a0e6a00bf57 to your computer and use it in GitHub Desktop.
Save harikt/1a2a2b4d3a0e6a00bf57 to your computer and use it in GitHub Desktop.
Fastest way to create di configuration for Aura.Di . Make sure the classes are auto-loadable.
{
"require": {
"crodas/class-info": "~0.1"
}
}
<?php
namespace HariKT;
require __DIR__ . '/vendor/autoload.php';
use crodas\ClassInfo\Definition\TFunction;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use crodas\ClassInfo\ClassInfo;
use ReflectionClass;
use Exception;
class Dumper
{
public function load($path)
{
$dir = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator($dir);
$parser = new ClassInfo;
$text = '';
foreach ($iterator as $file) {
$path = $file->getRealPath();
$parser->parse($path);
foreach ($parser->getClasses() as $class) {
try {
$reflection = new ReflectionClass((string) $class);
$method = $reflection->getConstructor();
if ($method) {
$parameters = $method->getParameters();
foreach($parameters as $param) {
$text .= '$di->params[\'' . (string)$class . '\'][\'' . $param->name . '\'] = $di->lazyNew(\'' . $param->getClass()->name . '\');' . PHP_EOL;
}
}
} catch (Exception $e) {
echo $e->getMessage();
}
}
}
echo PHP_EOL;
echo $text;
}
}
$dumper = new Dumper();
$dumper->load(__DIR__ . '/src');
@harikt
Copy link
Author

harikt commented Dec 3, 2014

Eg :

php dump.php

and in src folder you have

<?php
class Hello
{
    public function __construct(Aura\Web\Request $request)
    {
        $this->request = $request;
    }
}

will output something like

$di->params['Hello']['request'] = $di->lazyNew('Aura\Web\Request');

and yes all are lazyNew. Change to lazyGet or something if you need shared instance. But much simpler I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment