We have an old but very large Silex application using a fork of Silex 2.2.5 and Symfony 3.4. We intend to migrate away from Silex, but the application is still under heavy development from multiple engineers, so in the meantime, we are looking get onto PHP 8.1 before the 7.4 EOL in November '22. We discovered we were not actually using the final version of Silex (2.3.0) which requires Symfony 4, and Symfony 4.4 supports PHP 8.1.
We have upgraded our dependencies to suit Silex 2.3.0, no problem there. All Symfony components are upgraded to 4.4. But when we load a page in the application, we get this error:
PHP Fatal error: Uncaught Error: Call to a member function has() on null in /var/task/vendor/symfony/http-kernel/EventListener/SessionListener.php:41
This emanates from a method called onKernelRequest. Which was introduced in Symfony 4.4. Debugging the page load, it seems that $this->container is null. I also found that the constructor for SessionListener is not called prior to hitting this error.
class SessionListener extends AbstractSessionListener
{
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function onKernelRequest(GetResponseEvent $event)
{
parent::onKernelRequest($event);
// ERROR IS HIT ON THE FOLLOWING LINE
if (!$event->isMasterRequest() || !$this->container->has('session')) {
return;
}
if ($this->container->has('session_storage')
&& ($storage = $this->container->get('session_storage')) instanceof NativeSessionStorage
&& ($masterRequest = $this->container->get('request_stack')->getMasterRequest())
&& $masterRequest->isSecure()
) {
$storage->setOptions(['cookie_secure' => true]);
}
}
Edit: Interestingly, if I remove this onKernelRequest method from Symfony's SessionListener, the site seems to work just fine. Not that this is a solution since it's in a Symonfy lib.
I've scoured the docs left behind on Silex to see if 2.3.0 required something more to work with Symfony 4, but since Symfony 4.3 and below didn't have this method, perhaps they had Silex working with an earlier version of Symfony 4.
Googling the error, I do see a bit of chatter about doing something with services.yml to inject the container dependency. But this project doesn't have a services.yml file so I'm unsure what the starting point would be for that.
Not expecting anyone to have answers to this issue, but ideas? Did anyone ever have Silex 2.3.0 working with Symfony 4.4 perhaps?
Full stacktrace of the error:
result = {array} [9]
0 = {array} [7]
file = "/var/task/vendor/symfony/event-dispatcher/EventDispatcher.php"
line = {int} 264
function = "onKernelRequest"
class = "Symfony\Component\HttpKernel\EventListener\SessionListener"
object = {OurCompany\Session\SessionListener} [3]
type = "->"
args = {array} [3]
1 = {array} [7]
file = "/var/task/vendor/symfony/event-dispatcher/EventDispatcher.php"
line = {int} 239
function = "doDispatch"
class = "Symfony\Component\EventDispatcher\EventDispatcher"
object = {Symfony\Component\EventDispatcher\EventDispatcher} [3]
type = "->"
args = {array} [3]
2 = {array} [7]
file = "/var/task/vendor/symfony/event-dispatcher/EventDispatcher.php"
line = {int} 73
function = "callListeners"
class = "Symfony\Component\EventDispatcher\EventDispatcher"
object = {Symfony\Component\EventDispatcher\EventDispatcher} [3]
type = "->"
args = {array} [3]
3 = {array} [7]
file = "/var/task/vendor/symfony/http-kernel/HttpKernel.php"
line = {int} 135
function = "dispatch"
class = "Symfony\Component\EventDispatcher\EventDispatcher"
object = {Symfony\Component\EventDispatcher\EventDispatcher} [3]
type = "->"
args = {array} [2]
4 = {array} [7]
file = "/var/task/vendor/symfony/http-kernel/HttpKernel.php"
line = {int} 81
function = "handleRaw"
class = "Symfony\Component\HttpKernel\HttpKernel"
object = {Symfony\Component\HttpKernel\HttpKernel} [4]
type = "->"
args = {array} [2]
5 = {array} [7]
file = "/var/task/vendor/silex/silex/src/Silex/Application.php"
line = {int} 496
function = "handle"
class = "Symfony\Component\HttpKernel\HttpKernel"
object = {Symfony\Component\HttpKernel\HttpKernel} [4]
type = "->"
args = {array} [3]
6 = {array} [7]
file = "/var/task/vendor/silex/silex/src/Silex/Application.php"
line = {int} 477
function = "handle"
class = "Silex\Application"
object = {OurCompany\Application} [9]
type = "->"
args = {array} [1]
7 = {array} [7]
file = "/var/task/lib/Application.php"
line = {int} 37
function = "run"
class = "Silex\Application"
object = {OurCompany\Application} [9]
type = "->"
args = {array} [1]
8 = {array} [7]
file = "/var/task/web/index.php"
line = {int} 43
function = "run"
class = "OurCompany\Application"
object = {OurCompany\Application} [9]
type = "->"
args = {array} [1]
You can't inject ContainerInterface anymore. As I can see, you want to read the session data. To accomplish this, you can inject SessionInterface(for Symfony 4) or RequestStack (for Symfony 5+) instead of ContainerInterface.
Symfony 4 : https://symfony.com/doc/4.4/session.html Symfony 5+ : https://symfony.com/doc/5.4/session.html