Skip to content
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

FIX Handle calling Deprecation::notice() before manifests are available #10556

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Core/Injector/InjectorLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
class InjectorLoader
{
public const NO_MANIFESTS_AVAILABLE = 'No injector manifests available';

/**
* @internal
* @var self
Expand Down Expand Up @@ -42,7 +44,7 @@ public function getManifest()
);
}
if (empty($this->manifests)) {
throw new BadMethodCallException("No injector manifests available");
throw new BadMethodCallException(self::NO_MANIFESTS_AVAILABLE);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this would be a good place to throw a custom exception. Maybe, we can look at doing something like in CMS5/CMS6.

}
return $this->manifests[count($this->manifests) - 1];
}
Expand Down
11 changes: 11 additions & 0 deletions src/Dev/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace SilverStripe\Dev;

use BadMethodCallException;
use Exception;
use SilverStripe\Control\Director;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\InjectorLoader;
use SilverStripe\Core\Manifest\Module;

/**
Expand Down Expand Up @@ -234,6 +237,14 @@ public static function notice($atVersion, $string = '', $scope = Deprecation::SC
} else {
user_error($string, self::$notice_level);
}
} catch (BadMethodCallException $e) {
if ($e->getMessage() === InjectorLoader::NO_MANIFESTS_AVAILABLE) {
// noop
// this can happen when calling Deprecation::notice() before manifests are available, i.e.
// some of the code involved in creating the manifests calls Deprecation::notice()
} else {
throw $e;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this, but I'm assuming that every exception rethrown here, will appear to be coming from Deprecation::notice().

Copy link
Member Author

@emteknetnz emteknetnz Oct 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe so, I think it just throws the original $e unmodified

php -r '
  class A {
    function f() {
      throw new \Exception();
    }
  }
  class B {
    function f() {
      try {
        (new A)->f();
      } catch (\Exception $e) {
        throw $e;
      }
    }
  }
  class C {
    function f() {
      try {
        (new B)->f();
      } catch (\Exception $e) {
        var_dump($e->getLine());
        var_dump($e->getTraceAsString());
      }
    }
  }
  (new C)->f();
'
int(4)
string(108) "#0 Command line code(10): A->f()
#1 Command line code(19): B->f()
#2 Command line code(26): C->f()
#3 {main}"

}
} finally {
static::$inside_notice = false;
}
Expand Down