Tôi đã tự hỏi tại sao không thể tạo plugin cho protectedcác phương thức. Có đoạn mã này trong Magento\Framework\Interception\Code\Generator\Interceptor:
protected function _getClassMethods()
{
$methods = [$this->_getDefaultConstructorDefinition()];
$reflectionClass = new \ReflectionClass($this->getSourceClassName());
$publicMethods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach ($publicMethods as $method) {
if ($this->isInterceptedMethod($method)) {
$methods[] = $this->_getMethodInfo($method);
}
}
return $methods;
}
Nó kiểm tra nếu phương thức là publictrước khi cho phép nó bị chặn. Nó có thể dễ dàng thay đổi bằng cách tạo ra một preferencetrong di.xmlcác mô-đun riêng, tất nhiên, như thế này:
<?xml version="1.0"?>
<config>
<preference for="Magento\Framework\Interception\Code\Generator\Interceptor" type="MyVendor\MyModule\Model\MyInterceptorModel" />
</config>
và viết lại _getClassMethodsvới sự \ReflectionMethod::IS_PUBLICthay đổi \ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTEDbên trong của phương thức.
Nhưng tôi tự hỏi tại sao không thể chặn các phương thức được bảo vệ trong định nghĩa phương thức ban đầu? Liệu nó có ảnh hưởng lớn đến hiệu suất, hoặc có một số lý do khác cho điều đó, như cho phép các mô-đun của bên thứ 3 làm cho logic Magento quá "lộn xộn"?