phinx
phinx copied to clipboard
Conflict with env function
Hi.
My site stopped working because I have a env function to return environment variables and I just realized this library use CakePHP, that defines another env function before, so the site was using the cake's function that have a different behaviour.
Is there any way to have phinx installed without having these functions defined by default? (Maybe using a phar file or something like that).
Thanks!
Is there any way to have phinx installed without having these functions defined by default? (Maybe using a phar file or something like that).
We don't currently create phar files. I'm not too familiar with the tooling to do so either, perhaps another contributor is.
The same trick might also work here: https://github.com/cakephp/phinx/issues/1401#issuecomment-435210854
The same trick might also work here: #1401 (comment)
Yes and no
I'm using oscarotero/env library to get environment variables and it provides a env() function (https://github.com/oscarotero/env#the-env-function)
The library does not expose that function by default (it is not autoloaded by composer), you have to run Env::init() so it's not posible to execute this funcion before include composer autoload php file.
The only workaround is to don't use the env function (but Env::get() instead). Other option is include manually the function before include composer (https://github.com/oscarotero/env/blob/master/src/env_function.php).
Anyway, I've started to using this library before it was converted to a cake component and would like it to remain a standalone framework-agnostic tool, so IMO, polluting the global with these functions is not a good idea. Global functions should be defined/loaded and used by final users, but not by dependencies. Maybe cake could expose its funcions under its own namespace, or using a class with static methods.
@markstory Any way we can do this?
The env() function in Cake is conditionally defined. If your application defines the function first the Cake one won't be created.
Not autoloading the env() function entirely could be possible, but runs a high risk of breaking compatibility unless we can come up with a way to ensure env() exists everywhere but in phinx's usage.
We should add a docs example how to load custom one before the core. That would solve it for n ow.
A library that has some helper functions but pollutes the global namespace is really not very nice. Move the global methods to the Cake specific namespace, and heavily document a polyfiller project that reinstates the global pollution with a strong suggestion that the consumer's code should be upgraded to use the Cake specific namespace if they want to continue using the global function.
I'm unsure if we should be using a static class, as there is a small semantic difference between a static method and a namespaced class (so small I can hardly see it).
This is one of my biggest gripes with another well known and popular framework that starts with L.
In short why does ANY framework think it should be able to pollute the global namespace.
I just with PHP had function autoloading
https://wiki.php.net/rfc/function_autoloading
I use psysh REPL library which includes autoload.php in runtime. So I can't define my own env function before that.