doctrine / orm

Doctrine Object Relational Mapper (ORM)
https://www.doctrine-project.org/projects/orm.html
MIT License
9.92k stars 2.51k forks source link

Automatic proxy generation does not work for AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED #11418

Open dlebech opened 5 months ago

dlebech commented 5 months ago

Bug Report

Q A
BC Break don't know
Version 3.1.1

Summary

The AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED setting for setAutoGenerateProxyClasses does not update the proxy files.

The bug is here.

The bug is due to the order of the switch cases in the loadProxyClass function in the ProxyFactory.php file.

When the the file has changed, the code falls through to the second case which just checks if the file exists and then breaks, so it never reached the final case.

I have annotated the troublesome code below with "[BUG Explanation]":

switch ($this->autoGenerate) {
    case self::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED:
        if (file_exists($fileName) && filemtime($fileName) >= filemtime($class->getReflectionClass()->getFileName())) {
            break;
        }
        // [BUG Explanation]: It correctly falls through here to the next case, when the file has changed
    case self::AUTOGENERATE_FILE_NOT_EXISTS:
        if (file_exists($fileName)) {
            // [BUG Explanation]: It reaches this point, because the files exists
            break;
        }
    case self::AUTOGENERATE_ALWAYS:
        // [BUG explanation] It never reaches this point and so the proxy is not generated.
        $this->generateProxyClass($class, $fileName, $proxyClassName);
        break;
}

Again, the code is here.

Current behavior

The proxy classes are not updated automatically, even when their Entity class files are updated, due to the error outlined above.

How to reproduce

  1. Create basic config $config
  2. Set AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED
    • $config->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED)
  3. Change a model file.
  4. Observe the file does not change.

Expected behavior

The proxy file is updated when the entity class file has changed.

Thank you.