ฉันสงสัยว่าทำไมมันเป็นไปไม่ได้ที่จะสร้างปลั๊กอินสำหรับprotectedวิธีการ มีรหัสชิ้นนี้ใน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;
}
มันจะตรวจสอบว่าวิธีการpublicก่อนที่จะอนุญาตให้มีการดัก มันสามารถเปลี่ยนแปลงได้อย่างง่ายดายโดยการสร้างpreferenceในdi.xmlโมดูลเองแน่นอนเช่นนี้
<?xml version="1.0"?>
<config>
<preference for="Magento\Framework\Interception\Code\Generator\Interceptor" type="MyVendor\MyModule\Model\MyInterceptorModel" />
</config>
และเขียนใหม่_getClassMethodsด้วยการ\ReflectionMethod::IS_PUBLICเปลี่ยนเป็น\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTEDภายในของวิธีการ
แต่ฉันสงสัยว่าทำไมมันเป็นไปไม่ได้ที่จะสกัดกั้นวิธีการป้องกันในคำนิยามวิธีการเดิม? มันมีผลกระทบสำคัญกับประสิทธิภาพหรือมีเหตุผลอื่น ๆ เช่นการอนุญาตให้โมดูลของบุคคลที่สามสร้างตรรกะของวีโอไอพีด้วยเช่นกัน "ยุ่ง"?