Saya bertanya-tanya mengapa tidak mungkin membuat plugin untuk protectedmetode. Ada bagian kode ini di 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;
}
Ia memeriksa apakah metode publicsebelum memungkinkannya dicegat. Hal ini dapat dengan mudah diubah dengan menciptakan preferencedalam di.xmlmodul sendiri, tentu saja, seperti ini:
<?xml version="1.0"?>
<config>
<preference for="Magento\Framework\Interception\Code\Generator\Interceptor" type="MyVendor\MyModule\Model\MyInterceptorModel" />
</config>
dan menulis ulang _getClassMethodsdengan bagian dalam metode yang \ReflectionMethod::IS_PUBLICdiubah \ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED.
Tapi saya bertanya-tanya mengapa tidak mungkin untuk mencegat metode yang dilindungi dalam definisi metode asli? Apakah itu berdampak besar pada kinerja, atau ada beberapa alasan lain untuk itu, seperti mengizinkan modul pihak ke-3 untuk membuat logika Magento terlalu "berantakan"?