-
Notifications
You must be signed in to change notification settings - Fork 14
Add SchemaFactory #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Conversation
|
Actually you can do something like this in Aura.Di. $di->params['Aura\SqlSchema\MysqlSchema']['column_factory'] = $di->lazyNew('Aura\SqlSchema\ColumnFactory');
$di->params['Aura\SqlSchema\MysqlSchema']['pdo'] = $di->lazyGet('pdo');
$di->set('schema', $di->lazyNew('Aura\SqlSchema\MysqlSchema'));I am not in favor for Factory for the Di usage. |
|
Hm.. Regarding the Env may be you can do the same inside for params ? |
re: factories and di
Really? I'd love some further explanation on that. I initially found myself duplicating a lot of the config/Common code from the However, I thought using the factories and lazyGetCall, might be a better Also, I was under the impression, that the presence of the _Config/Common stuff re: knowing which object to createI could just do the translation of a string like "sqlite" to $drivers = [
'mysql' => 'Aura\SqlSchema\MysqlSchema',
'pgsql' => 'Aura\SqlSchema\PgsqlSchema',
'sqlite' => 'Aura\SqlSchema\SqliteSchema',
'sqlsrv' => 'Aura\SqlSchema\SqlsrvSchema'
];
$di->set('schema', $di->lazyNew($drivers[$_ENV['DB_DRIVER']]));
// or maybe something like this? but this seems really convoluted
$di->set('schema', $di->lazyValue('db_driver'));
$di->values['db_driver'] = $di->lazy(
function () use ($di, $drivers) {
$driver = $di->get('pdo')->getAttribute(PDO::ATTR_DRIVER_NAME);
return $drivers[$driver];
}
);Do people think these types of solutions are preferable? Thoughts? |
|
I am showing the examples in 2.x and sorry that it is not tested though. I haven't looked 3.x. My assumption is it will not be different. In Assuming you have already loaded the <?php
require __DIR__ . '/vendor/autoload.php';
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();Now in the $di->params[getenv('driver')] = $di->lazyNew('Aura\SqlSchema\ColumnFactory');
$di->params[getenv('driver')]['pdo'] = $di->lazyGet('pdo');
$di->set('schema', $di->lazyNew('Aura\SqlSchema\MysqlSchema'));Doesn't this was working for you ? |
|
Thanks for the response @harikt I assume you mean this as the last line? $di->set('schema', $di->lazyNew(getenv('driver')));Certainly that would work. However, I am hoping to reduce redundancy in the For example, this seems ideal to me: $di->set( // parse the DSN
'dsn',
$di->lazyNew(
'AD7six\Dsn\DbDsn',
['url' => getenv('DSN')]
)
);
$di->set( // pdo
'pdo',
$di->lazyNew(
'Aura\Sql\ExtendedPdo',
['dsn' => $di->lazyGetCall('dsn', 'toUrl')]
)
);
$di->set( // get the correct schema class based on the PDO
'schema',
$di->lazyGetCall(
'aura/schema:schema_factory',
'newSchema',
$di->lazyGet('pdo')
)
);
// get the right query factory based on parsing the dsn
$di->params['Aura\SqlQuery\QueryFactory']
['db'] = $di->lazyGetCall('dsn', 'getEngine');See, here the only thing I need to specify in my I can parse the dsn to determine the engine See, I can create an instance of My initial intention was just to make it so I could create a schema by using a Given the solution I think you are purposing, my Even, if we ignore the whole "parsing the DSN to get the engine" thing, and we Thoughts? Also, still curious about your objection to factory in the di in general. |
|
Thanks for your detailed answer . In my use I never switched between 2 different db, so I never needed something like this. If you really need I guess @pmjones will look and give you further thoughts / merge this. I am ok with the factory use case you have given.
One reason is in some cases we may need to make use of the same objects . When we use factories it may be creating a new instance of another object. It may not be a satifactory explanation though. Thank you. |
I think this should allow us to create a schema object without needing to know the
driver type by getting the information from the
pdoviaPDO::getAttribute(PDO::ATTR_DRIVER_NAME), or by passing a name.I think this makes it easier to get a schema object when the drive type is
defined in configuration. For example, defining a key in an
.envfile asDB_DRIVER=sqlite.