Tôi đã tự hỏi tại sao không thể tạo plugin cho protected
cá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à public
trướ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 preference
trong di.xml
cá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 _getClassMethods
với sự \ReflectionMethod::IS_PUBLIC
thay đổi \ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED
bê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"?