From 53709eb344ce0cf24717b2a424b0c5c783151bbf Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Fri, 10 May 2019 23:25:42 +0200 Subject: [PATCH 01/52] add creatuity compiled interceptors module --- .../Generator/CompiledInterceptor.php | 319 +++++++++++++++ .../Generator/CompiledPluginList.php | 381 ++++++++++++++++++ .../Framework/CompiledInterception/README.md | 91 +++++ .../CompiledInterceptorTest.php | 92 +++++ .../_out_interceptors/ComplexItem.txt | 137 +++++++ .../_out_interceptors/ComplexItemTyped.txt | 142 +++++++ .../_out_interceptors/Item.txt | 54 +++ .../CompiledPluginListTest.php | 156 +++++++ .../Unit/Custom/Module/Model/ComplexItem.php | 55 +++ .../Custom/Module/Model/ComplexItemTyped.php | 67 +++ .../Test/Unit/Custom/Module/Model/Item.php | 17 + .../Custom/Module/Model/Item/Enhanced.php | 17 + .../Module/Model/ItemPlugin/Advanced.php | 46 +++ .../Module/Model/ItemPlugin/Complex.php | 57 +++ .../Custom/Module/Model/ItemPlugin/Simple.php | 21 + .../Test/Unit/_files/reader_mock_map.php | 102 +++++ 16 files changed, 1754 insertions(+) create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/README.md create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item/Enhanced.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Advanced.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Complex.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Simple.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php new file mode 100644 index 0000000000000..c6b8fa406a261 --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -0,0 +1,319 @@ +plugins = $plugins; + } else { + $this->plugins = []; + foreach (['primary', 'frontend', 'adminhtml', 'crontab', 'webapi_rest', 'webapi_soap'] as $scope) { + $this->plugins[$scope] = new CompiledPluginList($scope); + } + } + } + + public function setInterceptedMethods($interceptedMethods) + { + //DUMMY + } + + protected function _getClassProperties() + { + return []; + } + + protected function _generateCode() + { + $typeName = $this->getSourceClassName(); + $reflection = new \ReflectionClass($typeName); + + if ($reflection->isInterface()) { + $this->_classGenerator->setImplementedInterfaces([$typeName]); + } else { + $this->_classGenerator->setExtendedClass($typeName); + } + + $this->_classGenerator->addUse(ObjectManager::class); + $this->_classGenerator->addUse(\Magento\Framework\Config\Scope::class); + //return parent::_generateCode(); + return EntityAbstract::_generateCode(); + } + + protected function _getClassMethods() + { + $reflectionClass = new \ReflectionClass($this->getSourceClassName()); + $publicMethods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC); + + $methods = []; + $allPlugins = []; + foreach ($publicMethods as $method) { + if ($this->isInterceptedMethod($method)) { + $config = $this->_getPluginsConfig($method, $allPlugins); + if (!empty($config)) { + $methods[] = $this->_getCompiledMethodInfo($method, $config); + } + } + } + if (!empty($methods) && !empty($allPlugins)) { + foreach ($allPlugins as $key => $plugins) { + foreach ($plugins as $plugin) { + $methods[] = $this->_getPluginGetterInfo($plugin); + } + } + } + + return $methods; + } + + private function addCodeSubBlock(&$body, $sub, $indent = 1) + { + foreach ($sub as $line) { + $body[] = str_repeat("\t", $indent) . $line; + } + } + + /** + * @param \ReflectionMethod $method + * @param $conf + * @param $parameters + * @return array + */ + protected function _getMethodSourceFromConfig(\ReflectionMethod $method, $conf, $parameters, $returnVoid) + { + $body = []; + $first = true; + $capName = ucfirst($method->getName()); + $extraParams = empty($parameters) ? '' : (', ' . $this->_getParameterList($parameters)); + + if (isset($conf[DefinitionInterface::LISTENER_BEFORE])) { + foreach ($conf[DefinitionInterface::LISTENER_BEFORE] as $plugin) { + if ($first) $first = false; else $body[] = ""; + //$body[] = "/** @var \\" . "{$plugin['class']} \$plugin {$plugin['code']} */"; + //$body[] = "\$plugin = \$this->" . $this->getGetterName($plugin) . "();"; + + $call = "\$this->" . $this->getGetterName($plugin) . "()->before$capName(\$this$extraParams);"; + + if (!empty($parameters)) { + $body[] = "\$beforeResult = " . $call; + $body[] = "if (\$beforeResult !== null) list({$this->_getParameterList($parameters)}) = (array)\$beforeResult;"; + } else { + $body[] = $call; + } + } + } + + + $chain = []; + $main = []; + if (isset($conf[DefinitionInterface::LISTENER_AROUND])) { + $plugin = $conf[DefinitionInterface::LISTENER_AROUND]; + //$body[] = "/** @var \\" . "{$plugin['class']} \$plugin {$plugin['code']} */"; + //$body[] = "\$plugin = \$this->" . $this->getGetterName($plugin) . "();"; + $main[] = "\$this->" . $this->getGetterName($plugin) . "()->around$capName(\$this, function({$this->_getParameterListForNextCallback($parameters)}){"; + $this->addCodeSubBlock($main, $this->_getMethodSourceFromConfig($method, $plugin['next'] ?: [], $parameters, $returnVoid)); + //$body[] = "\treturn \$result;"; + $main[] = "}$extraParams);"; + } else { + $main[] = "parent::{$method->getName()}({$this->_getParameterList($parameters)});"; + } + $chain[] = $main; + + if (isset($conf[DefinitionInterface::LISTENER_AFTER])) { + foreach ($conf[DefinitionInterface::LISTENER_AFTER] as $plugin) { + //$body[] = "/** @var \\" . "{$plugin['class']} \$plugin {$plugin['code']} */"; + //$body[] = "\$plugin = \$this->" . $this->getGetterName($plugin) . "();"; + if ($returnVoid) { + $chain[] = ["((\$tmp = \$this->" . $this->getGetterName($plugin) . "()->after$capName(\$this, \$result$extraParams)) !== null) ? \$tmp : \$result;"]; + } else { + $chain[] = ["\$this->" . $this->getGetterName($plugin) . "()->after$capName(\$this, \$result$extraParams);"]; + } + } + } + foreach ($chain as $lp => $piece) { + //if ($first) $first = false; else $body[] = ""; + if (!$returnVoid) { + $piece[0] = (($lp + 1 == count($chain)) ? "return " : "\$result = ") . $piece[0]; + } + foreach ($piece as $line) { + $body[] = $line; + } + } + + return $body; + } + + /** + * @param \ReflectionParameter[] $parameters + * @return string + */ + protected function _getParameterListForNextCallback(array $parameters) + { + $ret = []; + foreach ($parameters as $parameter) { + $ret [] = + ($parameter->isPassedByReference() ? '&' : '') . + "\${$parameter->getName()}" . + ($parameter->isDefaultValueAvailable() ? + ' = ' . ($parameter->isDefaultValueConstant() ? + $parameter->getDefaultValueConstantName() : + str_replace("\n", '', var_export($parameter->getDefaultValue(), true))) : + ''); + } + return implode(', ', $ret); + } + + /** + * @param \ReflectionParameter[] $parameters + * @return string + */ + protected function _getParameterList(array $parameters) + { + $ret = []; + foreach ($parameters as $parameter) { + $ret [] = "\${$parameter->getName()}"; + } + return implode(', ', $ret); + } + + protected function getGetterName($plugin) + { + return '_get_plugin_' . preg_replace("/[^A-Za-z0-9_]/", '_', $plugin['code'] . $plugin['suffix']); + } + + protected function _getPluginGetterInfo($plugin) + { + $body = []; + + $body[] = "static \$cache = null;"; + $body[] = "if (\$cache === null) \$cache = ObjectManager::getInstance()->get(\\" . "{$plugin['class']}::class);"; + $body[] = "return \$cache;"; + + return [ + 'name' => $this->getGetterName($plugin), + 'parameters' => [], + 'body' => implode("\n", $body), + //'returnType' => $class, + 'docblock' => [ + 'shortDescription' => 'plugin "' . $plugin['code'] . '"' . "\n" . '@return \\' . $plugin['class'] + ], + ]; + } + + protected function _getCompiledMethodInfo(\ReflectionMethod $method, $config) + { + $parameters = $method->getParameters(); + $returnsVoid = ($method->hasReturnType() && $method->getReturnType()->getName() == 'void'); + + $body = [ + 'switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){' + ]; + + $cases = []; + //group cases by config + foreach ($config as $scope => $conf) { + $key = md5(serialize($conf)); + if (!isset($cases[$key])) $cases[$key] = ['cases'=>[], 'conf'=>$conf]; + $cases[$key]['cases'][] = "\tcase '$scope':"; + } + //call parent method for scopes with no plugins (or when no scope is set) + $cases[] = ['cases'=>["\tdefault:"], 'conf'=>[]]; + + foreach($cases as $case) { + $body = array_merge($body, $case['cases']); + $this->addCodeSubBlock($body, $this->_getMethodSourceFromConfig($method, $case['conf'], $parameters, $returnsVoid), 2); + //$body[] = "\t\tbreak;"; + } + + $body[] = "}"; + + return [ + 'name' => ($method->returnsReference() ? '& ' : '') . $method->getName(), + 'parameters' =>array_map(array($this, '_getMethodParameterInfo'), $parameters), + 'body' => implode("\n", $body), + 'returnType' => $method->getReturnType(), + 'docblock' => ['shortDescription' => '{@inheritdoc}'], + ]; + } + + protected function _getPluginInfo(CompiledPluginList $plugins, $code, $className, &$allPlugins, $next = null) + { + $className = $plugins->getPluginType($className, $code); + if (!isset($allPlugins[$code])) $allPlugins[$code] = []; + if (empty($allPlugins[$code][$className])) { + $allPlugins[$code][$className] = [ + 'code' => $code, + 'class' => $className, + 'suffix' => count($allPlugins[$code]) ? count($allPlugins[$code]) + 1 : '' + ]; + } + $ret = $allPlugins[$code][$className]; + $ret['next'] = $next; + return $ret; + + } + + protected function _getPluginsChain(CompiledPluginList $plugins, $className, $method, &$allPlugins, $next = '__self') + { + $ret = $plugins->getNext($className, $method, $next); + if(!empty($ret[DefinitionInterface::LISTENER_BEFORE])) { + foreach ($ret[DefinitionInterface::LISTENER_BEFORE] as $k => $code) { + $ret[DefinitionInterface::LISTENER_BEFORE][$k] = $this->_getPluginInfo($plugins, $code, $className, $allPlugins); + } + } + if(!empty($ret[DefinitionInterface::LISTENER_AFTER])) { + foreach ($ret[DefinitionInterface::LISTENER_AFTER] as $k => $code) { + $ret[DefinitionInterface::LISTENER_AFTER][$k] = $this->_getPluginInfo($plugins, $code, $className, $allPlugins); + } + } + if (isset($ret[DefinitionInterface::LISTENER_AROUND])) { + $ret[DefinitionInterface::LISTENER_AROUND] = $this->_getPluginInfo($plugins, $ret[DefinitionInterface::LISTENER_AROUND], $className, $allPlugins, + $this->_getPluginsChain($plugins, $className, $method, $allPlugins, $ret[DefinitionInterface::LISTENER_AROUND])); + } + return $ret; + } + + protected function _getPluginsConfig(\ReflectionMethod $method, &$allPlugins) + { + $className = ltrim($this->getSourceClassName(), '\\'); + + $ret = array(); + foreach ($this->plugins as $scope => $pluginsList) { + $p = $this->_getPluginsChain($pluginsList, $className, $method->getName(), $allPlugins); + if ($p) { + $ret[$scope] = $p; + } + + } + return $ret; + } + +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php new file mode 100644 index 0000000000000..fae1ad4f144dd --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php @@ -0,0 +1,381 @@ +currentScope = $scope; + + if ($reader && $omConfig) { + $this->_reader = $reader; + $this->_omConfig = $omConfig; + } else { + $objectManager = ObjectManager::getInstance(); + $this->_reader = $objectManager->get(Proxy::class); + $this->_omConfig = $objectManager->get(ConfigInterface::class); + } + $this->_relations = new \Magento\Framework\ObjectManager\Relations\Runtime(); + $this->_definitions = new \Magento\Framework\Interception\Definition\Runtime(); + $this->_classDefinitions = new \Magento\Framework\ObjectManager\Definition\Runtime(); + $this->_scopePriorityScheme = ['first' => 'global']; + $this->_cachePath = ($cachePath === null ? BP . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'cache' : $cachePath); + } + + /** + * Collect parent types configuration for requested type + * + * @param string $type + * @return array + * @throws \InvalidArgumentException + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) + */ + protected function _inheritPlugins($type) + { + $type = ltrim($type, '\\'); + if (!array_key_exists($type, $this->_inherited)) { + $realType = $this->_omConfig->getOriginalInstanceType($type); + + if ($realType !== $type) { + $plugins = $this->_inheritPlugins($realType); + } elseif ($this->_relations->has($type)) { + $relations = $this->_relations->getParents($type); + $plugins = []; + foreach ($relations as $relation) { + if ($relation) { + $relationPlugins = $this->_inheritPlugins($relation); + if ($relationPlugins) { + $plugins = array_replace_recursive($plugins, $relationPlugins); + } + } + } + } else { + $plugins = []; + } + if (isset($this->_data[$type])) { + if (!$plugins) { + $plugins = $this->_data[$type]; + } else { + $plugins = array_replace_recursive($plugins, $this->_data[$type]); + } + } + $this->_inherited[$type] = null; + if (is_array($plugins) && count($plugins)) { + $this->filterPlugins($plugins); + uasort($plugins, [$this, '_sort']); + $this->trimInstanceStartingBackslash($plugins); + $this->_inherited[$type] = $plugins; + $lastPerMethod = []; + foreach ($plugins as $key => $plugin) { + // skip disabled plugins + if (isset($plugin['disabled']) && $plugin['disabled']) { + unset($plugins[$key]); + continue; + } + $pluginType = $this->_omConfig->getOriginalInstanceType($plugin['instance']); + if (!class_exists($pluginType)) { + throw new \InvalidArgumentException('Plugin class ' . $pluginType . ' doesn\'t exist'); + } + foreach ($this->_definitions->getMethodList($pluginType) as $pluginMethod => $methodTypes) { + $current = isset($lastPerMethod[$pluginMethod]) ? $lastPerMethod[$pluginMethod] : '__self'; + $currentKey = $type . '_' . $pluginMethod . '_' . $current; + if ($methodTypes & DefinitionInterface::LISTENER_AROUND) { + $this->_processed[$currentKey][DefinitionInterface::LISTENER_AROUND] = $key; + $lastPerMethod[$pluginMethod] = $key; + } + if ($methodTypes & DefinitionInterface::LISTENER_BEFORE) { + $this->_processed[$currentKey][DefinitionInterface::LISTENER_BEFORE][] = $key; + } + if ($methodTypes & DefinitionInterface::LISTENER_AFTER) { + $this->_processed[$currentKey][DefinitionInterface::LISTENER_AFTER][] = $key; + } + } + } + } + return $plugins; + } + return $this->_inherited[$type]; + } + + /** + * Trims starting backslash from plugin instance name + * + * @param array $plugins + * @return void + */ + private function trimInstanceStartingBackslash(&$plugins) + { + foreach ($plugins as &$plugin) { + $plugin['instance'] = ltrim($plugin['instance'], '\\'); + } + } + + /** + * Sort items + * + * @param array $itemA + * @param array $itemB + * @return int + */ + protected function _sort($itemA, $itemB) + { + if (isset($itemA['sortOrder'])) { + if (isset($itemB['sortOrder'])) { + return $itemA['sortOrder'] - $itemB['sortOrder']; + } + return $itemA['sortOrder']; + } elseif (isset($itemB['sortOrder'])) { + return (0 - (int)$itemB['sortOrder']); + } else { + return 0; + } + } + + /** + * Retrieve plugin Instance + * + * @param string $type + * @param string $code + * @return mixed + */ + public function getPlugin($type, $code) + { + return null; + } + + /** + * Retrieve next plugins in chain + * + * @param string $type + * @param string $method + * @param string $code + * @return array + */ + public function getNext($type, $method, $code = '__self') + { + $this->_loadScopedData(); + if (!isset($this->_inherited[$type]) && !array_key_exists($type, $this->_inherited)) { + $this->_inheritPlugins($type); + } + $key = $type . '_' . lcfirst($method) . '_' . $code; + return isset($this->_processed[$key]) ? $this->_processed[$key] : null; + } + + protected function _loadScopedData() + { + if (false == isset($this->_loadedScopes[$this->currentScope])) { + if (false == in_array($this->currentScope, $this->_scopePriorityScheme)) { + $this->_scopePriorityScheme[] = $this->currentScope; + } + if ($this->_cachePath) { + $cacheId = $this->_cachePath . DIRECTORY_SEPARATOR . 'compiled-plugins_' . implode('_', $this->_scopePriorityScheme) . '.php'; + $data = @include($cacheId); + } else { + $data = null; + } + + if ($data) { + list($this->_data, $this->_inherited, $this->_processed) = $data; + foreach ($this->_scopePriorityScheme as $scopeCode) { + $this->_loadedScopes[$scopeCode] = true; + } + } else { + $virtualTypes = []; + foreach ($this->_scopePriorityScheme as $scopeCode) { + if (false == isset($this->_loadedScopes[$scopeCode])) { + $data = $this->_reader->read($scopeCode); + unset($data['preferences']); + if (count($data) > 0) { + $this->_inherited = []; + $this->_processed = []; + $this->merge($data); + foreach ($data as $class => $config) { + if (isset($config['type'])) { + $virtualTypes[] = $class; + } + } + } + $this->_loadedScopes[$scopeCode] = true; + } + if ($scopeCode == $this->currentScope) { + break; + } + } + foreach ($virtualTypes as $class) { + $this->_inheritPlugins($class); + } + foreach ($this->getClassDefinitions() as $class) { + $this->_inheritPlugins($class); + } + if ($this->_cachePath) { + file_put_contents( + $cacheId, + '_data, $this->_inherited, $this->_processed], true) . '?>' + ); + } + } + $this->_pluginInstances = []; + } + } + + /** + * Returns class definitions + * + * @return array + */ + protected function getClassDefinitions() + { + return $this->_classDefinitions->getClasses(); + } + + /** + * Merge configuration + * + * @param array $config + * @return void + */ + public function merge(array $config) + { + foreach ($config as $type => $typeConfig) { + if (isset($typeConfig['plugins'])) { + $type = ltrim($type, '\\'); + if (isset($this->_data[$type])) { + $this->_data[$type] = array_replace_recursive($this->_data[$type], $typeConfig['plugins']); + } else { + $this->_data[$type] = $typeConfig['plugins']; + } + } + } + } + + public function getPluginType($type, $code) + { + return $this->_inherited[$type][$code]['instance']; + } + + /** + * Remove from list not existing plugins + * + * @param array $plugins + * @return void + */ + private function filterPlugins(array &$plugins) + { + foreach ($plugins as $name => $plugin) { + if (empty($plugin['instance'])) { + unset($plugins[$name]); + //$this->getLogger()->info("Reference to undeclared plugin with name '{$name}'."); + } + } + } +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/README.md b/lib/internal/Magento/Framework/CompiledInterception/README.md new file mode 100644 index 0000000000000..fb5c19d3770ee --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/README.md @@ -0,0 +1,91 @@ +### ABOUT + +This component changes the way Magento 2 generates Interceptor classes (a mechanism that allows plugins to work together). + +Instead of generating boilerplate code it compiles the Interceptor using information from source code. +This makes plugins slightly faster and as Magento uses a lot of plugins even at its core, it lowers request time by ~10%. +This is important in places where there is a lot of non-cached PHP logic going (for example admin panel is noticeably faster). + +The dafault method uses code that is called on nearly eatch method to see if there are any plugins connected, in generated code this is not required and call-stack is reduced. +Having plugins called directly also makes code easier to debug and bugs easier to find. + +Interceptors generated by this plugin are 100% compatible with ones generated by Magento by default so there is no need to change anything in your plugins. + +### ENABLING + +to use in developer mode in `app/etc/di.xml` replace: +``` +\Magento\Framework\Interception\Code\Generator\Interceptor +``` +with: +``` +\Magento\Framework\CompiledInterception\Generator\CompiledInterceptor +``` + +to use compiled interceptors in production mode please add following preference to your di.xml +``` + +``` + +clear generated files and cache: + +`rm -rf generated/* && rm -rf var/cache/* && bin/magento cache:clean` + +### DISABLING + +Replace back the lines in `app/etc/di.xml`, remove module and clear cache and generated files. + +### TECHNICAL DETAILS + +Instead of interceptors that read plugins config at runtime like this: + +``` +public function methodX($arg) { + $pluginInfo = $this->pluginList->getNext($this->subjectType, 'methodX'); + if (!$pluginInfo) { + return parent::methodX($arg); + } else { + return $this->___callPlugins('methodX', func_get_args(), $pluginInfo); + } +} +``` + +This generator generates static interceptors like: + + +``` +public function methodX($arg) { + switch(getCurrentScope()){ + case 'frontend': + $this->_get_example_plugin()->beforeMethodX($this, $arg); + $this->_get_another_plugin()->beforeMethodX($this, $arg); + $result = $this->_get_around_plugin()->aroundMethodX($this, function($arg){ + return parent::methodX($arg); + }); + return $this->_get_after_plugin()->afterMethodX($this, $result); + case 'adminhtml': + // ... + default: + return parent::methodX($arg); + } +} +``` + + +#### PROS + +* easier debugging. + * If you ever stumbled upon `___callPlugins` when debugging you should know how painful it is to debug issues inside plugins. + * generated code is decorated with PHPDoc for easier debugging in IDE + +* fastest response time (5%-15% faster in developer and production mode) + * no redundant calls to `___callPlugins` in call stack. + * methods with no plugins are not overriden in parent at all. + +* implemented as a module and can be easily reverted to default `Generator\Interceptor` + +#### CONS + +* each time after making change in etc plugins config, `generated/code/*` needs to be purged +* tiny longer code generation step when run with no cache +* as this does not load plugins at runtime might not work in an edge case of plugging into core Magento classes like PluginsList etc. diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php new file mode 100644 index 0000000000000..9ada33c5de8b8 --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php @@ -0,0 +1,92 @@ +ioGenerator = $this->getMockBuilder(Io::class) + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * Checks a test case when interceptor generates code for the specified class. + * + * @param string $className + * @param string $resultClassName + * @param string $fileName + * @dataProvider interceptorDataProvider + */ + public function testGenerate($className, $resultClassName, $fileName) + { + /** @var CompiledInterceptor|MockObject $interceptor */ + $interceptor = $this->getMockBuilder(CompiledInterceptor::class) + ->setMethods(['_validateData']) + ->setConstructorArgs([ + $className, + $resultClassName, + $this->ioGenerator, + null, + null, + (new CompiledPluginListTest())->createScopeReaders() + ]) + ->getMock(); + + $this->ioGenerator->method('generateResultFileName')->with('\\' . $resultClassName)->willReturn($fileName . '.php'); + + $code = file_get_contents(__DIR__ . '/_out_interceptors/' . $fileName . '.txt'); + + $this->ioGenerator->method('writeResultFile')->with($fileName . '.php', $code); + $interceptor->method('_validateData')->willReturn(true); + + $generated = $interceptor->generate(); + + $this->assertEquals($fileName . '.php', $generated, 'Generated interceptor is invalid.'); + } + + /** + * Gets list of interceptor samples. + * + * @return array + */ + public function interceptorDataProvider() + { + return [ + [ + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item\Interceptor::class, + 'Item' + ], + [ + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem::class, + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem\Interceptor::class, + 'ComplexItem' + ], + [ + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped::class, + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped\Interceptor::class, + 'ComplexItemTyped' + ], + ]; + } +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt new file mode 100644 index 0000000000000..e37b0f1fd18d5 --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt @@ -0,0 +1,137 @@ +namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem; + +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Config\Scope; + +/** + * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem + */ +class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem +{ + /** + * {@inheritdoc} + */ + public function getName() + { + switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + case 'global': + case 'frontend': + case 'emptyscope': + $this->_get_plugin_advanced_plugin()->beforeGetName($this); + $result = $this->_get_plugin_advanced_plugin()->aroundGetName($this, function(){ + return parent::getName(); + }); + return $this->_get_plugin_advanced_plugin()->afterGetName($this, $result); + case 'backend': + $this->_get_plugin_advanced_plugin()->beforeGetName($this); + $result = $this->_get_plugin_advanced_plugin()->aroundGetName($this, function(){ + $result = $this->_get_plugin_complex_plugin()->aroundGetName($this, function(){ + return parent::getName(); + }); + return $this->_get_plugin_complex_plugin()->afterGetName($this, $result); + }); + return $this->_get_plugin_advanced_plugin()->afterGetName($this, $result); + default: + return parent::getName(); + } + } + + /** + * {@inheritdoc} + */ + public function setValue($value) + { + switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + case 'backend': + $beforeResult = $this->_get_plugin_complex_plugin()->beforeSetValue($this, $value); + if ($beforeResult !== null) list($value) = (array)$beforeResult; + return $this->_get_plugin_complex_plugin()->aroundSetValue($this, function($value){ + return parent::setValue($value); + }, $value); + default: + return parent::setValue($value); + } + } + + /** + * {@inheritdoc} + */ + public function & getReference() + { + switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + case 'backend': + return $this->_get_plugin_complex_plugin()->aroundGetReference($this, function(){ + return parent::getReference(); + }); + default: + return parent::getReference(); + } + } + + /** + * {@inheritdoc} + */ + public function firstVariadicParameter(... $variadicValue) + { + switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + case 'backend': + return $this->_get_plugin_complex_plugin()->aroundFirstVariadicParameter($this, function($variadicValue){ + return parent::firstVariadicParameter($variadicValue); + }, $variadicValue); + default: + return parent::firstVariadicParameter($variadicValue); + } + } + + /** + * {@inheritdoc} + */ + public function secondVariadicParameter($value, ... $variadicValue) + { + switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + case 'backend': + return $this->_get_plugin_complex_plugin()->aroundSecondVariadicParameter($this, function($value, $variadicValue){ + return parent::secondVariadicParameter($value, $variadicValue); + }, $value, $variadicValue); + default: + return parent::secondVariadicParameter($value, $variadicValue); + } + } + + /** + * {@inheritdoc} + */ + public function byRefVariadic(&... $variadicValue) + { + switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + case 'backend': + return $this->_get_plugin_complex_plugin()->aroundByRefVariadic($this, function(&$variadicValue){ + return parent::byRefVariadic($variadicValue); + }, $variadicValue); + default: + return parent::byRefVariadic($variadicValue); + } + } + + /** + * plugin "advanced_plugin" + * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + */ + public function _get_plugin_advanced_plugin() + { + static $cache = null; + if ($cache === null) $cache = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + return $cache; + } + + /** + * plugin "complex_plugin" + * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + */ + public function _get_plugin_complex_plugin() + { + static $cache = null; + if ($cache === null) $cache = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); + return $cache; + } +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt new file mode 100644 index 0000000000000..4526d1311bf8d --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -0,0 +1,142 @@ +namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped; + +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Config\Scope; + +/** + * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped + */ +class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped +{ + /** + * {@inheritdoc} + */ + public function returnVoid() : void + { + switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + case 'backend': + parent::returnVoid(); + (($tmp = $this->_get_plugin_complex_plugin()->afterReturnVoid($this, $result)) !== null) ? $tmp : $result; + default: + parent::returnVoid(); + } + } + + /** + * {@inheritdoc} + */ + public function getNullableValue() : string + { + switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + case 'backend': + $result = parent::getNullableValue(); + return $this->_get_plugin_complex_plugin()->afterGetNullableValue($this, $result); + default: + return parent::getNullableValue(); + } + } + + /** + * {@inheritdoc} + */ + public function getName() : string + { + switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + case 'backend': + $this->_get_plugin_advanced_plugin()->beforeGetName($this); + $result = $this->_get_plugin_advanced_plugin()->aroundGetName($this, function(){ + $result = $this->_get_plugin_complex_plugin()->aroundGetName($this, function(){ + return parent::getName(); + }); + return $this->_get_plugin_complex_plugin()->afterGetName($this, $result); + }); + return $this->_get_plugin_advanced_plugin()->afterGetName($this, $result); + default: + return parent::getName(); + } + } + + /** + * {@inheritdoc} + */ + public function setValue(string $value) + { + switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + case 'backend': + $beforeResult = $this->_get_plugin_complex_plugin()->beforeSetValue($this, $value); + if ($beforeResult !== null) list($value) = (array)$beforeResult; + return $this->_get_plugin_complex_plugin()->aroundSetValue($this, function($value){ + return parent::setValue($value); + }, $value); + default: + return parent::setValue($value); + } + } + + /** + * {@inheritdoc} + */ + public function firstVariadicParameter(string ... $variadicValue) + { + switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + case 'backend': + return $this->_get_plugin_complex_plugin()->aroundFirstVariadicParameter($this, function($variadicValue){ + return parent::firstVariadicParameter($variadicValue); + }, $variadicValue); + default: + return parent::firstVariadicParameter($variadicValue); + } + } + + /** + * {@inheritdoc} + */ + public function secondVariadicParameter(string $value, string ... $variadicValue) + { + switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + case 'backend': + return $this->_get_plugin_complex_plugin()->aroundSecondVariadicParameter($this, function($value, $variadicValue){ + return parent::secondVariadicParameter($value, $variadicValue); + }, $value, $variadicValue); + default: + return parent::secondVariadicParameter($value, $variadicValue); + } + } + + /** + * {@inheritdoc} + */ + public function byRefVariadic(string &... $variadicValue) + { + switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + case 'backend': + return $this->_get_plugin_complex_plugin()->aroundByRefVariadic($this, function(&$variadicValue){ + return parent::byRefVariadic($variadicValue); + }, $variadicValue); + default: + return parent::byRefVariadic($variadicValue); + } + } + + /** + * plugin "complex_plugin" + * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + */ + public function _get_plugin_complex_plugin() + { + static $cache = null; + if ($cache === null) $cache = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); + return $cache; + } + + /** + * plugin "advanced_plugin" + * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + */ + public function _get_plugin_advanced_plugin() + { + static $cache = null; + if ($cache === null) $cache = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + return $cache; + } +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt new file mode 100644 index 0000000000000..06c8f20787a66 --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt @@ -0,0 +1,54 @@ +namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; + +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Config\Scope; + +/** + * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item + */ +class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item +{ + /** + * {@inheritdoc} + */ + public function getName() + { + switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + case 'global': + case 'emptyscope': + $result = parent::getName(); + return $this->_get_plugin_simple_plugin()->afterGetName($this, $result); + case 'backend': + $this->_get_plugin_advanced_plugin()->beforeGetName($this); + $result = $this->_get_plugin_advanced_plugin()->aroundGetName($this, function(){ + $result = parent::getName(); + return $this->_get_plugin_simple_plugin()->afterGetName($this, $result); + }); + return $this->_get_plugin_advanced_plugin()->afterGetName($this, $result); + default: + return parent::getName(); + } + } + + /** + * plugin "simple_plugin" + * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple + */ + public function _get_plugin_simple_plugin() + { + static $cache = null; + if ($cache === null) $cache = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class); + return $cache; + } + + /** + * plugin "advanced_plugin" + * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + */ + public function _get_plugin_advanced_plugin() + { + static $cache = null; + if ($cache === null) $cache = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + return $cache; + } +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php new file mode 100644 index 0000000000000..eb56e44e62ef9 --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php @@ -0,0 +1,156 @@ +objects = $this->createScopeReaders(); + } + + public function createScopeReaders() + { + $readerMap = include __DIR__ . '/../_files/reader_mock_map.php'; + $readerMock = $this->createMock(\Magento\Framework\ObjectManager\Config\Reader\Dom::class); + $readerMock->expects($this->any())->method('read')->will($this->returnValueMap($readerMap)); + + $omConfigMock = $this->getMockForAbstractClass( + \Magento\Framework\Interception\ObjectManager\ConfigInterface::class + ); + + $omConfigMock->expects($this->any())->method('getOriginalInstanceType')->will($this->returnArgument(0)); + $ret = []; + $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + foreach($readerMap as $readerLine) { + $ret[$readerLine[0]] = $objectManagerHelper->getObject( + CompiledPluginList::class, + [ + 'scope' => $readerLine[0], + 'reader' => $readerMock, + 'omConfig' => $omConfigMock, + 'cachePath' => false + ] + ); + } + return $ret; + } + + public function testGetPlugin() + { + + $this->objects['backend']->getNext(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, 'getName'); + $this->assertEquals( + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class, + $this->objects['backend']->getPluginType( + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, + 'simple_plugin' + ) + ); + $this->assertEquals( + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class, + $this->objects['backend']->getPluginType( + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, + 'advanced_plugin' + ) + ); + } + + /** + * @param $expectedResult + * @param $type + * @param $method + * @param $scopeCode + * @param string $code + * @dataProvider getPluginsDataProvider + */ + public function testGetPlugins($expectedResult, $type, $method, $scopeCode, $code = '__self') + { + $this->assertEquals($expectedResult, $this->objects[$scopeCode]->getNext($type, $method, $code)); + } + + /** + * @return array + */ + public function getPluginsDataProvider() + { + return [ + [ + [4 => ['simple_plugin']], \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, + 'getName', + 'global', + ], + [ + // advanced plugin has lower sort order + [2 => 'advanced_plugin', 4 => ['advanced_plugin'], 1 => ['advanced_plugin']], + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, + 'getName', + 'backend' + ], + [ + // advanced plugin has lower sort order + [4 => ['simple_plugin']], + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, + 'getName', + 'backend', + 'advanced_plugin' + ], + // simple plugin is disabled in configuration for + // \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item in frontend + [null, \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, 'getName', 'frontend'], + // test plugin inheritance + [ + [4 => ['simple_plugin']], + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item\Enhanced::class, + 'getName', + 'global' + ], + [ + // simple plugin is disabled in configuration for parent + [2 => 'advanced_plugin', 4 => ['advanced_plugin'], 1 => ['advanced_plugin']], + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item\Enhanced::class, + 'getName', + 'frontend' + ] + ]; + } + + /** + * @expectedException \InvalidArgumentException + * @covers \Magento\Framework\Interception\PluginList\PluginList::getNext + * @covers \Magento\Framework\Interception\PluginList\PluginList::_inheritPlugins + */ + public function testInheritPluginsWithNonExistingClass() + { + $this->objects['frontend']->getNext('SomeType', 'someMethod'); + } + + /** + * @covers \Magento\Framework\Interception\PluginList\PluginList::getNext + * @covers \Magento\Framework\Interception\PluginList\PluginList::_inheritPlugins + */ + public function testInheritPluginsWithNotExistingPlugin() + { + $this->assertNull($this->objects['frontend']->getNext('typeWithoutInstance', 'someMethod')); + } + +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php new file mode 100644 index 0000000000000..7a51943607ee8 --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php @@ -0,0 +1,55 @@ +attribute; + } + + /** + * @param $value + */ + public function setValue($value) + { + $this->attribute = $value; + } + + public function & getReference() + { + } + + /** + * @param mixed ...$variadicValue + */ + public function firstVariadicParameter(...$variadicValue) + { + $this->variadicAttribute = $variadicValue; + } + + /** + * @param $value + * @param mixed ...$variadicValue + */ + public function secondVariadicParameter($value, ...$variadicValue) + { + $this->attribute = $value; + $this->variadicAttribute = $variadicValue; + } + + /** + * @param mixed ...$variadicValue + */ + public function byRefVariadic(& ...$variadicValue) + { + $this->variadicAttribute = $variadicValue; + } +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php new file mode 100644 index 0000000000000..7d962c5c106fd --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php @@ -0,0 +1,67 @@ +value; + } + + /** + * @param string $value + */ + public function setValue(string $value) + { + $this->value = $value; + } + + /** + * @param string ...$variadicValue + */ + public function firstVariadicParameter(string ...$variadicValue) + { + $this->variadicValue = $variadicValue; + } + + /** + * @param string $value + * @param string ...$variadicValue + */ + public function secondVariadicParameter(string $value, string ...$variadicValue) + { + $this->value = $value; + $this->variadicValue = $variadicValue; + } + + /** + * @param string ...$variadicValue + */ + public function byRefVariadic(string & ...$variadicValue) + { + $this->variadicValue = $variadicValue; + } +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item.php new file mode 100644 index 0000000000000..f127bdb8dbd62 --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item.php @@ -0,0 +1,17 @@ + [ + 'plugins' => [ + 'simple_plugin' => [ + 'sortOrder' => 10, + 'instance' => + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class, + ], + ], + ], + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem::class => [ + 'plugins' => [ + 'advanced_plugin' => [ + 'sortOrder' => 5, + 'instance' => + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class, + ], + ], + ], + ], + ], + [ + 'backend', + [ + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class => [ + 'plugins' => [ + 'advanced_plugin' => [ + 'sortOrder' => 5, + 'instance' => + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class, + ], + ], + ], + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem::class => [ + 'plugins' => [ + 'complex_plugin' => [ + 'sortOrder' => 15, + 'instance' => + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class, + ], + 'advanced_plugin' => [ + 'sortOrder' => 5, + 'instance' => + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class, + ], + ], + ], + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped::class => [ + 'plugins' => [ + 'complex_plugin' => [ + 'sortOrder' => 25, + 'instance' => + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class, + ], + 'advanced_plugin' => [ + 'sortOrder' => 5, + 'instance' => + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class, + ], + ], + ], + ] + ], + [ + 'frontend', + [\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class => [ + 'plugins' => ['simple_plugin' => ['disabled' => true]], + ], \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item\Enhanced::class => [ + 'plugins' => [ + 'advanced_plugin' => [ + 'sortOrder' => 5, + 'instance' => + \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class, + ], + ], + ], + 'SomeType' => [ + 'plugins' => [ + 'simple_plugin' => [ + 'instance' => 'NonExistingPluginClass', + ], + ], + ], + 'typeWithoutInstance' => [ + 'plugins' => [ + 'simple_plugin' => [], + ], + ] + ] + ], + [ + 'emptyscope', + [ + + ] + ] +]; From 7f499e130e667ff4bd533fe41ef6e7c4ab52b3b5 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Mon, 13 May 2019 21:03:28 +0200 Subject: [PATCH 02/52] added scope as constructor parameter and properties for plugins cache --- .../Generator/CompiledInterceptor.php | 130 +++++++++++++----- .../Framework/CompiledInterception/README.md | 15 +- .../CompiledInterceptorTest.php | 1 - .../_out_interceptors/ComplexItem.txt | 81 +++++++---- .../_out_interceptors/ComplexItemTyped.txt | 79 +++++++---- .../_out_interceptors/Item.txt | 53 +++++-- .../Unit/Custom/Module/Model/ComplexItem.php | 2 + .../Custom/Module/Model/ComplexItemTyped.php | 2 + .../Test/Unit/Custom/Module/Model/Item.php | 2 + .../Custom/Module/Model/Item/Enhanced.php | 1 + .../Module/Model/ItemPlugin/Advanced.php | 2 + .../Module/Model/ItemPlugin/Complex.php | 5 + .../Custom/Module/Model/ItemPlugin/Simple.php | 2 + 13 files changed, 261 insertions(+), 114 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index c6b8fa406a261..389e6f77961ed 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -8,6 +8,7 @@ use Magento\Framework\App\ObjectManager; use Magento\Framework\Code\Generator\EntityAbstract; +use Magento\Framework\Config\Scope; use Magento\Framework\Interception\Code\Generator\Interceptor; use Magento\Framework\Interception\DefinitionInterface; @@ -16,6 +17,9 @@ class CompiledInterceptor extends Interceptor protected $plugins; + protected $classMethods = []; + protected $classProperties = []; + public function __construct( $sourceClassName = null, $resultClassName = null, @@ -41,14 +45,12 @@ public function __construct( } } + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ public function setInterceptedMethods($interceptedMethods) { - //DUMMY - } - - protected function _getClassProperties() - { - return []; + //NOOP } protected function _generateCode() @@ -57,41 +59,98 @@ protected function _generateCode() $reflection = new \ReflectionClass($typeName); if ($reflection->isInterface()) { - $this->_classGenerator->setImplementedInterfaces([$typeName]); + return false; } else { $this->_classGenerator->setExtendedClass($typeName); } $this->_classGenerator->addUse(ObjectManager::class); - $this->_classGenerator->addUse(\Magento\Framework\Config\Scope::class); + $this->_classGenerator->addUse(Scope::class); + + $this->classMethods = []; + $this->classProperties = []; + $this->overrideMethodsAndGeneratePluginGetters($reflection); + + array_unshift($this->classMethods, $this->_getConstructorInfo($reflection->getConstructor())); + array_unshift($this->classProperties, [ + 'name' => '____scope', + 'visibility' => 'private', + 'docblock' => [ + 'tags' => [['name' => 'var', 'description' => 'Scope']], + ] + ]); //return parent::_generateCode(); return EntityAbstract::_generateCode(); } - protected function _getClassMethods() + protected function overrideMethodsAndGeneratePluginGetters(\ReflectionClass $reflection) { - $reflectionClass = new \ReflectionClass($this->getSourceClassName()); - $publicMethods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC); + $publicMethods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); - $methods = []; $allPlugins = []; foreach ($publicMethods as $method) { if ($this->isInterceptedMethod($method)) { $config = $this->_getPluginsConfig($method, $allPlugins); if (!empty($config)) { - $methods[] = $this->_getCompiledMethodInfo($method, $config); + $this->classMethods[] = $this->_getCompiledMethodInfo($method, $config); } } } - if (!empty($methods) && !empty($allPlugins)) { - foreach ($allPlugins as $key => $plugins) { - foreach ($plugins as $plugin) { - $methods[] = $this->_getPluginGetterInfo($plugin); + foreach ($allPlugins as $plugins) { + foreach ($plugins as $plugin) { + $this->classMethods[] = $this->_getPluginGetterInfo($plugin); + $this->classProperties[] = $this->_getPluginPropertyInfo($plugin); + } + } + } + + protected function _getClassMethods() + { + return $this->classMethods; + } + + protected function _getClassProperties() + { + return $this->classProperties; + } + + protected function _getConstructorInfo(\ReflectionMethod $parentConstructor = null) + { + if ($parentConstructor == null) { + $parameters = [[ + 'name' => 'scope', + 'type' => Scope::class + ]]; + $body = ["\$this->____scope = \$scope;"]; + } else { + $parameters = $parentConstructor->getParameters(); + $addScopeParam = true; + $scopeParamName = '____scope'; + foreach ($parameters as $parameter) { + $parentCallParams[] = '$' . $parameter->getName(); + if ($parameter->getType() == Scope::class) { + $scopeParamName = $parameter->getName(); + $addScopeParam = false; } } + + $parameters = array_map(array($this, '_getMethodParameterInfo'), $parameters); + $addScopeParam && array_unshift($parameters, [ + 'name' => $scopeParamName, + 'type' => Scope::class + ]); + $body = [ + "\$this->____scope = \$$scopeParamName;", + "parent::__construct(" . implode(', ', $parentCallParams) .");" + ]; } - return $methods; + return [ + 'name' => '__construct', + 'parameters' => $parameters, + 'body' => implode("\n", $body), + 'docblock' => ['shortDescription' => '{@inheritdoc}'], + ]; } private function addCodeSubBlock(&$body, $sub, $indent = 1) @@ -117,8 +176,6 @@ protected function _getMethodSourceFromConfig(\ReflectionMethod $method, $conf, if (isset($conf[DefinitionInterface::LISTENER_BEFORE])) { foreach ($conf[DefinitionInterface::LISTENER_BEFORE] as $plugin) { if ($first) $first = false; else $body[] = ""; - //$body[] = "/** @var \\" . "{$plugin['class']} \$plugin {$plugin['code']} */"; - //$body[] = "\$plugin = \$this->" . $this->getGetterName($plugin) . "();"; $call = "\$this->" . $this->getGetterName($plugin) . "()->before$capName(\$this$extraParams);"; @@ -136,11 +193,8 @@ protected function _getMethodSourceFromConfig(\ReflectionMethod $method, $conf, $main = []; if (isset($conf[DefinitionInterface::LISTENER_AROUND])) { $plugin = $conf[DefinitionInterface::LISTENER_AROUND]; - //$body[] = "/** @var \\" . "{$plugin['class']} \$plugin {$plugin['code']} */"; - //$body[] = "\$plugin = \$this->" . $this->getGetterName($plugin) . "();"; $main[] = "\$this->" . $this->getGetterName($plugin) . "()->around$capName(\$this, function({$this->_getParameterListForNextCallback($parameters)}){"; $this->addCodeSubBlock($main, $this->_getMethodSourceFromConfig($method, $plugin['next'] ?: [], $parameters, $returnVoid)); - //$body[] = "\treturn \$result;"; $main[] = "}$extraParams);"; } else { $main[] = "parent::{$method->getName()}({$this->_getParameterList($parameters)});"; @@ -149,8 +203,6 @@ protected function _getMethodSourceFromConfig(\ReflectionMethod $method, $conf, if (isset($conf[DefinitionInterface::LISTENER_AFTER])) { foreach ($conf[DefinitionInterface::LISTENER_AFTER] as $plugin) { - //$body[] = "/** @var \\" . "{$plugin['class']} \$plugin {$plugin['code']} */"; - //$body[] = "\$plugin = \$this->" . $this->getGetterName($plugin) . "();"; if ($returnVoid) { $chain[] = ["((\$tmp = \$this->" . $this->getGetterName($plugin) . "()->after$capName(\$this, \$result$extraParams)) !== null) ? \$tmp : \$result;"]; } else { @@ -206,16 +258,29 @@ protected function _getParameterList(array $parameters) protected function getGetterName($plugin) { - return '_get_plugin_' . preg_replace("/[^A-Za-z0-9_]/", '_', $plugin['code'] . $plugin['suffix']); + return '____plugin_' . $plugin['clean_name']; + } + + protected function _getPluginPropertyInfo($plugin) + { + return [ + 'name' => '____plugin_' . $plugin['clean_name'], + 'visibility' => 'private', + 'docblock' => [ + 'tags' => [['name' => 'var', 'description' => '\\' . $plugin['class']]], + ] + ]; } protected function _getPluginGetterInfo($plugin) { $body = []; + $varName = "\$this->____plugin_" . $plugin['clean_name']; - $body[] = "static \$cache = null;"; - $body[] = "if (\$cache === null) \$cache = ObjectManager::getInstance()->get(\\" . "{$plugin['class']}::class);"; - $body[] = "return \$cache;"; + $body[] = "if ($varName === null) {"; + $body[] = "\t$varName = ObjectManager::getInstance()->get(\\" . "{$plugin['class']}::class);"; + $body[] = "}"; + $body[] = "return $varName;"; return [ 'name' => $this->getGetterName($plugin), @@ -234,7 +299,7 @@ protected function _getCompiledMethodInfo(\ReflectionMethod $method, $config) $returnsVoid = ($method->hasReturnType() && $method->getReturnType()->getName() == 'void'); $body = [ - 'switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){' + 'switch($this->____scope->getCurrentScope()){' ]; $cases = []; @@ -247,7 +312,7 @@ protected function _getCompiledMethodInfo(\ReflectionMethod $method, $config) //call parent method for scopes with no plugins (or when no scope is set) $cases[] = ['cases'=>["\tdefault:"], 'conf'=>[]]; - foreach($cases as $case) { + foreach ($cases as $case) { $body = array_merge($body, $case['cases']); $this->addCodeSubBlock($body, $this->_getMethodSourceFromConfig($method, $case['conf'], $parameters, $returnsVoid), 2); //$body[] = "\t\tbreak;"; @@ -269,10 +334,11 @@ protected function _getPluginInfo(CompiledPluginList $plugins, $code, $className $className = $plugins->getPluginType($className, $code); if (!isset($allPlugins[$code])) $allPlugins[$code] = []; if (empty($allPlugins[$code][$className])) { + $suffix = count($allPlugins[$code]) ? count($allPlugins[$code]) + 1 : ''; $allPlugins[$code][$className] = [ 'code' => $code, 'class' => $className, - 'suffix' => count($allPlugins[$code]) ? count($allPlugins[$code]) + 1 : '' + 'clean_name' => preg_replace("/[^A-Za-z0-9_]/", '_', $code . $suffix) ]; } $ret = $allPlugins[$code][$className]; diff --git a/lib/internal/Magento/Framework/CompiledInterception/README.md b/lib/internal/Magento/Framework/CompiledInterception/README.md index fb5c19d3770ee..c0ca3711245f9 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/README.md +++ b/lib/internal/Magento/Framework/CompiledInterception/README.md @@ -13,16 +13,7 @@ Interceptors generated by this plugin are 100% compatible with ones generated by ### ENABLING -to use in developer mode in `app/etc/di.xml` replace: -``` -\Magento\Framework\Interception\Code\Generator\Interceptor -``` -with: -``` -\Magento\Framework\CompiledInterception\Generator\CompiledInterceptor -``` - -to use compiled interceptors in production mode please add following preference to your di.xml +to use compiled interceptors in developer and production mode please add following preference to your di.xml ``` ``` @@ -33,7 +24,7 @@ clear generated files and cache: ### DISABLING -Replace back the lines in `app/etc/di.xml`, remove module and clear cache and generated files. +Replace remove preference from `app/etc/di.xml`, remove module and clear cache and generated files. ### TECHNICAL DETAILS @@ -80,7 +71,7 @@ public function methodX($arg) { * fastest response time (5%-15% faster in developer and production mode) * no redundant calls to `___callPlugins` in call stack. - * methods with no plugins are not overriden in parent at all. + * methods with no plugins are not overridden in parent at all. * implemented as a module and can be easily reverted to default `Generator\Interceptor` diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php index 9ada33c5de8b8..a0c8cd7c64812 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php @@ -60,7 +60,6 @@ public function testGenerate($className, $resultClassName, $fileName) $interceptor->method('_validateData')->willReturn(true); $generated = $interceptor->generate(); - $this->assertEquals($fileName . '.php', $generated, 'Generated interceptor is invalid.'); } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt index e37b0f1fd18d5..a205f447422f6 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt @@ -8,29 +8,52 @@ use Magento\Framework\Config\Scope; */ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem { + /** + * @var Scope + */ + private $____scope = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + */ + private $____plugin_advanced_plugin = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + */ + private $____plugin_complex_plugin = null; + + /** + * {@inheritdoc} + */ + public function __construct(\Magento\Framework\Config\Scope $scope) + { + $this->____scope = $scope; + } + /** * {@inheritdoc} */ public function getName() { - switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + switch($this->____scope->getCurrentScope()){ case 'global': case 'frontend': case 'emptyscope': - $this->_get_plugin_advanced_plugin()->beforeGetName($this); - $result = $this->_get_plugin_advanced_plugin()->aroundGetName($this, function(){ + $this->____plugin_advanced_plugin()->beforeGetName($this); + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ return parent::getName(); }); - return $this->_get_plugin_advanced_plugin()->afterGetName($this, $result); + return $this->____plugin_advanced_plugin()->afterGetName($this, $result); case 'backend': - $this->_get_plugin_advanced_plugin()->beforeGetName($this); - $result = $this->_get_plugin_advanced_plugin()->aroundGetName($this, function(){ - $result = $this->_get_plugin_complex_plugin()->aroundGetName($this, function(){ + $this->____plugin_advanced_plugin()->beforeGetName($this); + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ + $result = $this->____plugin_complex_plugin()->aroundGetName($this, function(){ return parent::getName(); }); - return $this->_get_plugin_complex_plugin()->afterGetName($this, $result); + return $this->____plugin_complex_plugin()->afterGetName($this, $result); }); - return $this->_get_plugin_advanced_plugin()->afterGetName($this, $result); + return $this->____plugin_advanced_plugin()->afterGetName($this, $result); default: return parent::getName(); } @@ -41,11 +64,11 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function setValue($value) { - switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + switch($this->____scope->getCurrentScope()){ case 'backend': - $beforeResult = $this->_get_plugin_complex_plugin()->beforeSetValue($this, $value); + $beforeResult = $this->____plugin_complex_plugin()->beforeSetValue($this, $value); if ($beforeResult !== null) list($value) = (array)$beforeResult; - return $this->_get_plugin_complex_plugin()->aroundSetValue($this, function($value){ + return $this->____plugin_complex_plugin()->aroundSetValue($this, function($value){ return parent::setValue($value); }, $value); default: @@ -58,9 +81,9 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function & getReference() { - switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + switch($this->____scope->getCurrentScope()){ case 'backend': - return $this->_get_plugin_complex_plugin()->aroundGetReference($this, function(){ + return $this->____plugin_complex_plugin()->aroundGetReference($this, function(){ return parent::getReference(); }); default: @@ -73,9 +96,9 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function firstVariadicParameter(... $variadicValue) { - switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + switch($this->____scope->getCurrentScope()){ case 'backend': - return $this->_get_plugin_complex_plugin()->aroundFirstVariadicParameter($this, function($variadicValue){ + return $this->____plugin_complex_plugin()->aroundFirstVariadicParameter($this, function($variadicValue){ return parent::firstVariadicParameter($variadicValue); }, $variadicValue); default: @@ -88,9 +111,9 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function secondVariadicParameter($value, ... $variadicValue) { - switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + switch($this->____scope->getCurrentScope()){ case 'backend': - return $this->_get_plugin_complex_plugin()->aroundSecondVariadicParameter($this, function($value, $variadicValue){ + return $this->____plugin_complex_plugin()->aroundSecondVariadicParameter($this, function($value, $variadicValue){ return parent::secondVariadicParameter($value, $variadicValue); }, $value, $variadicValue); default: @@ -103,9 +126,9 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function byRefVariadic(&... $variadicValue) { - switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + switch($this->____scope->getCurrentScope()){ case 'backend': - return $this->_get_plugin_complex_plugin()->aroundByRefVariadic($this, function(&$variadicValue){ + return $this->____plugin_complex_plugin()->aroundByRefVariadic($this, function(&$variadicValue){ return parent::byRefVariadic($variadicValue); }, $variadicValue); default: @@ -117,21 +140,23 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust * plugin "advanced_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced */ - public function _get_plugin_advanced_plugin() + public function ____plugin_advanced_plugin() { - static $cache = null; - if ($cache === null) $cache = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); - return $cache; + if ($this->____plugin_advanced_plugin === null) { + $this->____plugin_advanced_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + } + return $this->____plugin_advanced_plugin; } /** * plugin "complex_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex */ - public function _get_plugin_complex_plugin() + public function ____plugin_complex_plugin() { - static $cache = null; - if ($cache === null) $cache = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); - return $cache; + if ($this->____plugin_complex_plugin === null) { + $this->____plugin_complex_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); + } + return $this->____plugin_complex_plugin; } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt index 4526d1311bf8d..791cd66456189 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -8,15 +8,38 @@ use Magento\Framework\Config\Scope; */ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped { + /** + * @var Scope + */ + private $____scope = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + */ + private $____plugin_complex_plugin = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + */ + private $____plugin_advanced_plugin = null; + + /** + * {@inheritdoc} + */ + public function __construct(\Magento\Framework\Config\Scope $scope) + { + $this->____scope = $scope; + } + /** * {@inheritdoc} */ public function returnVoid() : void { - switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + switch($this->____scope->getCurrentScope()){ case 'backend': parent::returnVoid(); - (($tmp = $this->_get_plugin_complex_plugin()->afterReturnVoid($this, $result)) !== null) ? $tmp : $result; + (($tmp = $this->____plugin_complex_plugin()->afterReturnVoid($this, $result)) !== null) ? $tmp : $result; default: parent::returnVoid(); } @@ -27,10 +50,10 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function getNullableValue() : string { - switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + switch($this->____scope->getCurrentScope()){ case 'backend': $result = parent::getNullableValue(); - return $this->_get_plugin_complex_plugin()->afterGetNullableValue($this, $result); + return $this->____plugin_complex_plugin()->afterGetNullableValue($this, $result); default: return parent::getNullableValue(); } @@ -41,16 +64,16 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function getName() : string { - switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + switch($this->____scope->getCurrentScope()){ case 'backend': - $this->_get_plugin_advanced_plugin()->beforeGetName($this); - $result = $this->_get_plugin_advanced_plugin()->aroundGetName($this, function(){ - $result = $this->_get_plugin_complex_plugin()->aroundGetName($this, function(){ + $this->____plugin_advanced_plugin()->beforeGetName($this); + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ + $result = $this->____plugin_complex_plugin()->aroundGetName($this, function(){ return parent::getName(); }); - return $this->_get_plugin_complex_plugin()->afterGetName($this, $result); + return $this->____plugin_complex_plugin()->afterGetName($this, $result); }); - return $this->_get_plugin_advanced_plugin()->afterGetName($this, $result); + return $this->____plugin_advanced_plugin()->afterGetName($this, $result); default: return parent::getName(); } @@ -61,11 +84,11 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function setValue(string $value) { - switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + switch($this->____scope->getCurrentScope()){ case 'backend': - $beforeResult = $this->_get_plugin_complex_plugin()->beforeSetValue($this, $value); + $beforeResult = $this->____plugin_complex_plugin()->beforeSetValue($this, $value); if ($beforeResult !== null) list($value) = (array)$beforeResult; - return $this->_get_plugin_complex_plugin()->aroundSetValue($this, function($value){ + return $this->____plugin_complex_plugin()->aroundSetValue($this, function($value){ return parent::setValue($value); }, $value); default: @@ -78,9 +101,9 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function firstVariadicParameter(string ... $variadicValue) { - switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + switch($this->____scope->getCurrentScope()){ case 'backend': - return $this->_get_plugin_complex_plugin()->aroundFirstVariadicParameter($this, function($variadicValue){ + return $this->____plugin_complex_plugin()->aroundFirstVariadicParameter($this, function($variadicValue){ return parent::firstVariadicParameter($variadicValue); }, $variadicValue); default: @@ -93,9 +116,9 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function secondVariadicParameter(string $value, string ... $variadicValue) { - switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + switch($this->____scope->getCurrentScope()){ case 'backend': - return $this->_get_plugin_complex_plugin()->aroundSecondVariadicParameter($this, function($value, $variadicValue){ + return $this->____plugin_complex_plugin()->aroundSecondVariadicParameter($this, function($value, $variadicValue){ return parent::secondVariadicParameter($value, $variadicValue); }, $value, $variadicValue); default: @@ -108,9 +131,9 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function byRefVariadic(string &... $variadicValue) { - switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + switch($this->____scope->getCurrentScope()){ case 'backend': - return $this->_get_plugin_complex_plugin()->aroundByRefVariadic($this, function(&$variadicValue){ + return $this->____plugin_complex_plugin()->aroundByRefVariadic($this, function(&$variadicValue){ return parent::byRefVariadic($variadicValue); }, $variadicValue); default: @@ -122,21 +145,23 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust * plugin "complex_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex */ - public function _get_plugin_complex_plugin() + public function ____plugin_complex_plugin() { - static $cache = null; - if ($cache === null) $cache = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); - return $cache; + if ($this->____plugin_complex_plugin === null) { + $this->____plugin_complex_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); + } + return $this->____plugin_complex_plugin; } /** * plugin "advanced_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced */ - public function _get_plugin_advanced_plugin() + public function ____plugin_advanced_plugin() { - static $cache = null; - if ($cache === null) $cache = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); - return $cache; + if ($this->____plugin_advanced_plugin === null) { + $this->____plugin_advanced_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + } + return $this->____plugin_advanced_plugin; } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt index 06c8f20787a66..416ab0de7f875 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt @@ -8,23 +8,46 @@ use Magento\Framework\Config\Scope; */ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item { + /** + * @var Scope + */ + private $____scope = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple + */ + private $____plugin_simple_plugin = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + */ + private $____plugin_advanced_plugin = null; + + /** + * {@inheritdoc} + */ + public function __construct(\Magento\Framework\Config\Scope $scope) + { + $this->____scope = $scope; + } + /** * {@inheritdoc} */ public function getName() { - switch(ObjectManager::getInstance()->get(Scope::class)->getCurrentScope()){ + switch($this->____scope->getCurrentScope()){ case 'global': case 'emptyscope': $result = parent::getName(); - return $this->_get_plugin_simple_plugin()->afterGetName($this, $result); + return $this->____plugin_simple_plugin()->afterGetName($this, $result); case 'backend': - $this->_get_plugin_advanced_plugin()->beforeGetName($this); - $result = $this->_get_plugin_advanced_plugin()->aroundGetName($this, function(){ + $this->____plugin_advanced_plugin()->beforeGetName($this); + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ $result = parent::getName(); - return $this->_get_plugin_simple_plugin()->afterGetName($this, $result); + return $this->____plugin_simple_plugin()->afterGetName($this, $result); }); - return $this->_get_plugin_advanced_plugin()->afterGetName($this, $result); + return $this->____plugin_advanced_plugin()->afterGetName($this, $result); default: return parent::getName(); } @@ -34,21 +57,23 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust * plugin "simple_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple */ - public function _get_plugin_simple_plugin() + public function ____plugin_simple_plugin() { - static $cache = null; - if ($cache === null) $cache = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class); - return $cache; + if ($this->____plugin_simple_plugin === null) { + $this->____plugin_simple_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class); + } + return $this->____plugin_simple_plugin; } /** * plugin "advanced_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced */ - public function _get_plugin_advanced_plugin() + public function ____plugin_advanced_plugin() { - static $cache = null; - if ($cache === null) $cache = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); - return $cache; + if ($this->____plugin_advanced_plugin === null) { + $this->____plugin_advanced_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + } + return $this->____plugin_advanced_plugin; } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php index 7a51943607ee8..6b44fb700358d 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php @@ -3,6 +3,8 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +// @codingStandardsIgnoreFile + namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model; class ComplexItem diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php index 7d962c5c106fd..e26e51ba6022d 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php @@ -3,6 +3,8 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +// @codingStandardsIgnoreFile + namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model; class ComplexItemTyped diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item.php index f127bdb8dbd62..705d37face0fa 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item.php @@ -3,6 +3,8 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +// @codingStandardsIgnoreFile + namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model; class Item diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item/Enhanced.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item/Enhanced.php index e53b4ed830c8e..8fa8590d677b8 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item/Enhanced.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item/Enhanced.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +// @codingStandardsIgnoreFile namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; class Enhanced extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Advanced.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Advanced.php index 5a4967dd722b5..65388c2735918 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Advanced.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Advanced.php @@ -3,6 +3,8 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +// @codingStandardsIgnoreFile + namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin; class Advanced diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Complex.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Complex.php index 5be5f1ff901a5..8081236687983 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Complex.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Complex.php @@ -1,4 +1,9 @@ Date: Tue, 14 May 2019 16:22:48 +0200 Subject: [PATCH 03/52] get object manager from DI --- .../Generator/CompiledInterceptor.php | 97 ++++++++++--------- .../Generator/CompiledPluginList.php | 4 +- .../CompiledInterceptorTest.php | 2 +- .../_out_interceptors/ComplexItem.txt | 22 ++++- .../_out_interceptors/ComplexItemTyped.txt | 22 ++++- .../_out_interceptors/Item.txt | 20 +++- .../CompiledPluginListTest.php | 2 +- .../Test/Unit/_files/reader_mock_map.php | 4 + 8 files changed, 109 insertions(+), 64 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 389e6f77961ed..5ca327cbeacb6 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -6,7 +6,7 @@ namespace Magento\Framework\CompiledInterception\Generator; -use Magento\Framework\App\ObjectManager; +use Magento\Framework\ObjectManagerInterface; use Magento\Framework\Code\Generator\EntityAbstract; use Magento\Framework\Config\Scope; use Magento\Framework\Interception\Code\Generator\Interceptor; @@ -64,21 +64,14 @@ protected function _generateCode() $this->_classGenerator->setExtendedClass($typeName); } - $this->_classGenerator->addUse(ObjectManager::class); - $this->_classGenerator->addUse(Scope::class); - $this->classMethods = []; $this->classProperties = []; + $this->injectPropertiesSettersToConstructor($reflection->getConstructor(), [ + Scope::class => '____scope', + ObjectManagerInterface::class => '____om', + ]); $this->overrideMethodsAndGeneratePluginGetters($reflection); - array_unshift($this->classMethods, $this->_getConstructorInfo($reflection->getConstructor())); - array_unshift($this->classProperties, [ - 'name' => '____scope', - 'visibility' => 'private', - 'docblock' => [ - 'tags' => [['name' => 'var', 'description' => 'Scope']], - ] - ]); //return parent::_generateCode(); return EntityAbstract::_generateCode(); } @@ -104,53 +97,67 @@ protected function overrideMethodsAndGeneratePluginGetters(\ReflectionClass $ref } } - protected function _getClassMethods() - { - return $this->classMethods; - } - - protected function _getClassProperties() - { - return $this->classProperties; - } - - protected function _getConstructorInfo(\ReflectionMethod $parentConstructor = null) + protected function injectPropertiesSettersToConstructor(\ReflectionMethod $parentConstructor = null, $properties = []) { if ($parentConstructor == null) { - $parameters = [[ - 'name' => 'scope', - 'type' => Scope::class - ]]; - $body = ["\$this->____scope = \$scope;"]; + $parameters = []; + $body = []; } else { $parameters = $parentConstructor->getParameters(); - $addScopeParam = true; - $scopeParamName = '____scope'; foreach ($parameters as $parameter) { $parentCallParams[] = '$' . $parameter->getName(); - if ($parameter->getType() == Scope::class) { - $scopeParamName = $parameter->getName(); - $addScopeParam = false; + } + $body = ["parent::__construct(" . implode(', ', $parentCallParams) .");"]; + } + foreach ($properties as $type => $name) { + $this->_classGenerator->addUse($type); + $this->classProperties[] = [ + 'name' => $name, + 'visibility' => 'private', + 'docblock' => [ + 'tags' => [['name' => 'var', 'description' => substr(strrchr($type, "\\"), 1)]], + ] + ]; + } + $extraParams = $properties; + $extraSetters = array_combine($properties, $properties); + foreach ($parameters as $parameter) { + if ($parameter->getType()) { + $type = $parameter->getType()->getName(); + if (isset($properties[$type])) { + $extraSetters[$properties[$type]] = $parameter->getName(); + unset($extraParams[$type]); } } - - $parameters = array_map(array($this, '_getMethodParameterInfo'), $parameters); - $addScopeParam && array_unshift($parameters, [ - 'name' => $scopeParamName, - 'type' => Scope::class + } + $parameters = array_map(array($this, '_getMethodParameterInfo'), $parameters); + foreach ($extraParams as $type => $name) { + array_unshift($parameters, [ + 'name' => $name, + 'type' => $type ]); - $body = [ - "\$this->____scope = \$$scopeParamName;", - "parent::__construct(" . implode(', ', $parentCallParams) .");" - ]; + } + foreach ($extraSetters as $name => $paramName) { + array_unshift($body, "\$this->$name = \$$paramName;"); } - return [ + $this->classMethods[] = [ 'name' => '__construct', 'parameters' => $parameters, 'body' => implode("\n", $body), 'docblock' => ['shortDescription' => '{@inheritdoc}'], ]; + + } + + protected function _getClassMethods() + { + return $this->classMethods; + } + + protected function _getClassProperties() + { + return $this->classProperties; } private function addCodeSubBlock(&$body, $sub, $indent = 1) @@ -211,7 +218,7 @@ protected function _getMethodSourceFromConfig(\ReflectionMethod $method, $conf, } } foreach ($chain as $lp => $piece) { - //if ($first) $first = false; else $body[] = ""; + if ($first) $first = false; else $body[] = ""; if (!$returnVoid) { $piece[0] = (($lp + 1 == count($chain)) ? "return " : "\$result = ") . $piece[0]; } @@ -278,7 +285,7 @@ protected function _getPluginGetterInfo($plugin) $varName = "\$this->____plugin_" . $plugin['clean_name']; $body[] = "if ($varName === null) {"; - $body[] = "\t$varName = ObjectManager::getInstance()->get(\\" . "{$plugin['class']}::class);"; + $body[] = "\t$varName = \$this->____om->get(\\" . "{$plugin['class']}::class);"; $body[] = "}"; $body[] = "return $varName;"; diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php index fae1ad4f144dd..62ace69bb192f 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php @@ -10,7 +10,7 @@ use Magento\Framework\Interception\DefinitionInterface; use Magento\Framework\Interception\PluginListInterface as InterceptionPluginList; use Magento\Framework\Interception\ObjectManager\ConfigInterface; -use Magento\Framework\ObjectManager\Config\Reader\Dom\Proxy; +use Magento\Framework\ObjectManager\Config\Reader\Dom; use Magento\Framework\ObjectManager\RelationsInterface; use Magento\Framework\ObjectManager\DefinitionInterface as ClassDefinitions; @@ -121,7 +121,7 @@ public function __construct( $this->_omConfig = $omConfig; } else { $objectManager = ObjectManager::getInstance(); - $this->_reader = $objectManager->get(Proxy::class); + $this->_reader = $objectManager->get(Dom::class); $this->_omConfig = $objectManager->get(ConfigInterface::class); } $this->_relations = new \Magento\Framework\ObjectManager\Relations\Runtime(); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php index a0c8cd7c64812..f4f8ebb03c2cb 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php @@ -9,7 +9,7 @@ use Magento\Framework\Code\Generator\Io; use Magento\Framework\CompiledInterception\Generator\CompiledInterceptor; -use Magento\Framework\CompiledInterception\Test\Unit\PluginList\CompiledPluginListTest; +use Magento\Framework\CompiledInterception\Test\Unit\CompiledPluginList\CompiledPluginListTest; use \PHPUnit_Framework_MockObject_MockObject as MockObject; class CompiledInterceptorTest extends \PHPUnit\Framework\TestCase diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt index a205f447422f6..6b328df65ab03 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt @@ -1,7 +1,7 @@ namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem; -use Magento\Framework\App\ObjectManager; use Magento\Framework\Config\Scope; +use Magento\Framework\ObjectManagerInterface; /** * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem @@ -13,6 +13,11 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ private $____scope = null; + /** + * @var ObjectManagerInterface + */ + private $____om = null; + /** * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced */ @@ -26,9 +31,10 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust /** * {@inheritdoc} */ - public function __construct(\Magento\Framework\Config\Scope $scope) + public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\Scope $____scope) { - $this->____scope = $scope; + $this->____om = $____om; + $this->____scope = $____scope; } /** @@ -41,18 +47,23 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust case 'frontend': case 'emptyscope': $this->____plugin_advanced_plugin()->beforeGetName($this); + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ return parent::getName(); }); + return $this->____plugin_advanced_plugin()->afterGetName($this, $result); case 'backend': $this->____plugin_advanced_plugin()->beforeGetName($this); + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ $result = $this->____plugin_complex_plugin()->aroundGetName($this, function(){ return parent::getName(); }); + return $this->____plugin_complex_plugin()->afterGetName($this, $result); }); + return $this->____plugin_advanced_plugin()->afterGetName($this, $result); default: return parent::getName(); @@ -68,6 +79,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust case 'backend': $beforeResult = $this->____plugin_complex_plugin()->beforeSetValue($this, $value); if ($beforeResult !== null) list($value) = (array)$beforeResult; + return $this->____plugin_complex_plugin()->aroundSetValue($this, function($value){ return parent::setValue($value); }, $value); @@ -143,7 +155,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust public function ____plugin_advanced_plugin() { if ($this->____plugin_advanced_plugin === null) { - $this->____plugin_advanced_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); } return $this->____plugin_advanced_plugin; } @@ -155,7 +167,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust public function ____plugin_complex_plugin() { if ($this->____plugin_complex_plugin === null) { - $this->____plugin_complex_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); + $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); } return $this->____plugin_complex_plugin; } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt index 791cd66456189..2946bcc008050 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -1,7 +1,7 @@ namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped; -use Magento\Framework\App\ObjectManager; use Magento\Framework\Config\Scope; +use Magento\Framework\ObjectManagerInterface; /** * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped @@ -13,6 +13,11 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ private $____scope = null; + /** + * @var ObjectManagerInterface + */ + private $____om = null; + /** * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex */ @@ -26,9 +31,10 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust /** * {@inheritdoc} */ - public function __construct(\Magento\Framework\Config\Scope $scope) + public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\Scope $____scope) { - $this->____scope = $scope; + $this->____om = $____om; + $this->____scope = $____scope; } /** @@ -39,6 +45,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust switch($this->____scope->getCurrentScope()){ case 'backend': parent::returnVoid(); + (($tmp = $this->____plugin_complex_plugin()->afterReturnVoid($this, $result)) !== null) ? $tmp : $result; default: parent::returnVoid(); @@ -53,6 +60,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust switch($this->____scope->getCurrentScope()){ case 'backend': $result = parent::getNullableValue(); + return $this->____plugin_complex_plugin()->afterGetNullableValue($this, $result); default: return parent::getNullableValue(); @@ -67,12 +75,15 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust switch($this->____scope->getCurrentScope()){ case 'backend': $this->____plugin_advanced_plugin()->beforeGetName($this); + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ $result = $this->____plugin_complex_plugin()->aroundGetName($this, function(){ return parent::getName(); }); + return $this->____plugin_complex_plugin()->afterGetName($this, $result); }); + return $this->____plugin_advanced_plugin()->afterGetName($this, $result); default: return parent::getName(); @@ -88,6 +99,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust case 'backend': $beforeResult = $this->____plugin_complex_plugin()->beforeSetValue($this, $value); if ($beforeResult !== null) list($value) = (array)$beforeResult; + return $this->____plugin_complex_plugin()->aroundSetValue($this, function($value){ return parent::setValue($value); }, $value); @@ -148,7 +160,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust public function ____plugin_complex_plugin() { if ($this->____plugin_complex_plugin === null) { - $this->____plugin_complex_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); + $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); } return $this->____plugin_complex_plugin; } @@ -160,7 +172,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust public function ____plugin_advanced_plugin() { if ($this->____plugin_advanced_plugin === null) { - $this->____plugin_advanced_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); } return $this->____plugin_advanced_plugin; } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt index 416ab0de7f875..959f4edd8cb55 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt @@ -1,7 +1,7 @@ namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; -use Magento\Framework\App\ObjectManager; use Magento\Framework\Config\Scope; +use Magento\Framework\ObjectManagerInterface; /** * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item @@ -13,6 +13,11 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ private $____scope = null; + /** + * @var ObjectManagerInterface + */ + private $____om = null; + /** * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple */ @@ -26,9 +31,10 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust /** * {@inheritdoc} */ - public function __construct(\Magento\Framework\Config\Scope $scope) + public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\Scope $____scope) { - $this->____scope = $scope; + $this->____om = $____om; + $this->____scope = $____scope; } /** @@ -40,13 +46,17 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust case 'global': case 'emptyscope': $result = parent::getName(); + return $this->____plugin_simple_plugin()->afterGetName($this, $result); case 'backend': $this->____plugin_advanced_plugin()->beforeGetName($this); + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ $result = parent::getName(); + return $this->____plugin_simple_plugin()->afterGetName($this, $result); }); + return $this->____plugin_advanced_plugin()->afterGetName($this, $result); default: return parent::getName(); @@ -60,7 +70,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust public function ____plugin_simple_plugin() { if ($this->____plugin_simple_plugin === null) { - $this->____plugin_simple_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class); + $this->____plugin_simple_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class); } return $this->____plugin_simple_plugin; } @@ -72,7 +82,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust public function ____plugin_advanced_plugin() { if ($this->____plugin_advanced_plugin === null) { - $this->____plugin_advanced_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); } return $this->____plugin_advanced_plugin; } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php index eb56e44e62ef9..8af927f62a2a8 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php @@ -3,7 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ -namespace Magento\Framework\CompiledInterception\Test\Unit\PluginList; +namespace Magento\Framework\CompiledInterception\Test\Unit\CompiledPluginList; use Magento\Framework\CompiledInterception\Generator\CompiledPluginList; use Magento\Framework\ObjectManagerInterface; diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php index a09d7e2609371..dddcc8eaa6b93 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php @@ -1,4 +1,8 @@ Date: Wed, 15 May 2019 12:57:07 +0200 Subject: [PATCH 04/52] clean up code repetition by extending core plugins list --- .../Generator/CompiledInterceptor.php | 5 +- .../Generator/CompiledPluginList.php | 366 ++---------------- .../Generator/FileCache.php | 145 +++++++ .../Generator/StaticScope.php | 43 ++ .../CompiledPluginListTest.php | 6 + .../Module/Model/ItemPlugin/Complex.php | 64 +++ 6 files changed, 290 insertions(+), 339 deletions(-) create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 5ca327cbeacb6..04eb7316cea67 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -6,6 +6,7 @@ namespace Magento\Framework\CompiledInterception\Generator; +use Magento\Framework\App\ObjectManager; use Magento\Framework\ObjectManagerInterface; use Magento\Framework\Code\Generator\EntityAbstract; use Magento\Framework\Config\Scope; @@ -40,7 +41,7 @@ public function __construct( } else { $this->plugins = []; foreach (['primary', 'frontend', 'adminhtml', 'crontab', 'webapi_rest', 'webapi_soap'] as $scope) { - $this->plugins[$scope] = new CompiledPluginList($scope); + $this->plugins[$scope] = new CompiledPluginList(ObjectManager::getInstance(), $scope); } } } @@ -172,6 +173,8 @@ private function addCodeSubBlock(&$body, $sub, $indent = 1) * @param $conf * @param $parameters * @return array + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function _getMethodSourceFromConfig(\ReflectionMethod $method, $conf, $parameters, $returnVoid) { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php index 62ace69bb192f..898aefb8fe7c6 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php @@ -6,239 +6,44 @@ namespace Magento\Framework\CompiledInterception\Generator; use Magento\Framework\App\ObjectManager; -use Magento\Framework\Config\ReaderInterface; -use Magento\Framework\Interception\DefinitionInterface; -use Magento\Framework\Interception\PluginListInterface as InterceptionPluginList; +use Magento\Framework\Interception\PluginList\PluginList; use Magento\Framework\Interception\ObjectManager\ConfigInterface; use Magento\Framework\ObjectManager\Config\Reader\Dom; -use Magento\Framework\ObjectManager\RelationsInterface; -use Magento\Framework\ObjectManager\DefinitionInterface as ClassDefinitions; -/** - * Plugin config, provides list of plugins for a type - * - * @SuppressWarnings(PHPMD.CouplingBetweenObjects) - */ -class CompiledPluginList implements InterceptionPluginList +class CompiledPluginList extends PluginList { /** - * Configuration reader - * - * @var ReaderInterface - */ - protected $_reader; - - /** - * Config data - * - * @var array - */ - protected $_data = []; - - //SCOPED - - /** - * Scope priority loading scheme - * - * @var string[] + * CompiledPluginList constructor. + * @param $objectManager ObjectManager + * @param $scope + * @param null $reader + * @param null $omConfig + * @param null $cachePath */ - protected $_scopePriorityScheme = []; - - /** - * Loaded scopes - * - * @var array - */ - protected $_loadedScopes = []; - - /** - * Inherited plugin data - * - * @var array - */ - protected $_inherited; - - /** - * Inherited plugin data, preprocessed for read - * - * @var array - */ - protected $_processed; - - /** - * Type config - * - * @var ConfigInterface - */ - protected $_omConfig; - - /** - * Class relations information provider - * - * @var RelationsInterface - */ - protected $_relations; - - /** - * List of interception methods per plugin - * - * @var DefinitionInterface - */ - protected $_definitions; - - /** - * List of interceptable application classes - * - * @var ClassDefinitions - */ - protected $_classDefinitions; - - /** - * @var array - */ - protected $_pluginInstances = []; - - /** - * @var array - */ - protected $currentScope; - - /** - * @var array - */ - protected $_cachePath; - public function __construct( + $objectManager, $scope, $reader = null, $omConfig = null, $cachePath = null ) { - $this->currentScope = $scope; - - if ($reader && $omConfig) { - $this->_reader = $reader; - $this->_omConfig = $omConfig; - } else { - $objectManager = ObjectManager::getInstance(); - $this->_reader = $objectManager->get(Dom::class); - $this->_omConfig = $objectManager->get(ConfigInterface::class); - } - $this->_relations = new \Magento\Framework\ObjectManager\Relations\Runtime(); - $this->_definitions = new \Magento\Framework\Interception\Definition\Runtime(); - $this->_classDefinitions = new \Magento\Framework\ObjectManager\Definition\Runtime(); - $this->_scopePriorityScheme = ['first' => 'global']; - $this->_cachePath = ($cachePath === null ? BP . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'cache' : $cachePath); - } - - /** - * Collect parent types configuration for requested type - * - * @param string $type - * @return array - * @throws \InvalidArgumentException - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - * @SuppressWarnings(PHPMD.NPathComplexity) - */ - protected function _inheritPlugins($type) - { - $type = ltrim($type, '\\'); - if (!array_key_exists($type, $this->_inherited)) { - $realType = $this->_omConfig->getOriginalInstanceType($type); - - if ($realType !== $type) { - $plugins = $this->_inheritPlugins($realType); - } elseif ($this->_relations->has($type)) { - $relations = $this->_relations->getParents($type); - $plugins = []; - foreach ($relations as $relation) { - if ($relation) { - $relationPlugins = $this->_inheritPlugins($relation); - if ($relationPlugins) { - $plugins = array_replace_recursive($plugins, $relationPlugins); - } - } - } - } else { - $plugins = []; - } - if (isset($this->_data[$type])) { - if (!$plugins) { - $plugins = $this->_data[$type]; - } else { - $plugins = array_replace_recursive($plugins, $this->_data[$type]); - } - } - $this->_inherited[$type] = null; - if (is_array($plugins) && count($plugins)) { - $this->filterPlugins($plugins); - uasort($plugins, [$this, '_sort']); - $this->trimInstanceStartingBackslash($plugins); - $this->_inherited[$type] = $plugins; - $lastPerMethod = []; - foreach ($plugins as $key => $plugin) { - // skip disabled plugins - if (isset($plugin['disabled']) && $plugin['disabled']) { - unset($plugins[$key]); - continue; - } - $pluginType = $this->_omConfig->getOriginalInstanceType($plugin['instance']); - if (!class_exists($pluginType)) { - throw new \InvalidArgumentException('Plugin class ' . $pluginType . ' doesn\'t exist'); - } - foreach ($this->_definitions->getMethodList($pluginType) as $pluginMethod => $methodTypes) { - $current = isset($lastPerMethod[$pluginMethod]) ? $lastPerMethod[$pluginMethod] : '__self'; - $currentKey = $type . '_' . $pluginMethod . '_' . $current; - if ($methodTypes & DefinitionInterface::LISTENER_AROUND) { - $this->_processed[$currentKey][DefinitionInterface::LISTENER_AROUND] = $key; - $lastPerMethod[$pluginMethod] = $key; - } - if ($methodTypes & DefinitionInterface::LISTENER_BEFORE) { - $this->_processed[$currentKey][DefinitionInterface::LISTENER_BEFORE][] = $key; - } - if ($methodTypes & DefinitionInterface::LISTENER_AFTER) { - $this->_processed[$currentKey][DefinitionInterface::LISTENER_AFTER][] = $key; - } - } - } - } - return $plugins; - } - return $this->_inherited[$type]; - } - - /** - * Trims starting backslash from plugin instance name - * - * @param array $plugins - * @return void - */ - private function trimInstanceStartingBackslash(&$plugins) - { - foreach ($plugins as &$plugin) { - $plugin['instance'] = ltrim($plugin['instance'], '\\'); - } - } - - /** - * Sort items - * - * @param array $itemA - * @param array $itemB - * @return int - */ - protected function _sort($itemA, $itemB) - { - if (isset($itemA['sortOrder'])) { - if (isset($itemB['sortOrder'])) { - return $itemA['sortOrder'] - $itemB['sortOrder']; - } - return $itemA['sortOrder']; - } elseif (isset($itemB['sortOrder'])) { - return (0 - (int)$itemB['sortOrder']); - } else { - return 0; + if (!$reader || !$omConfig) { + $reader = $objectManager->get(Dom::class); + $omConfig = $objectManager->get(ConfigInterface::class); } + parent::__construct( + $reader, + new StaticScope($scope), + new FileCache($cachePath), + new \Magento\Framework\ObjectManager\Relations\Runtime(), + $omConfig, + new \Magento\Framework\Interception\Definition\Runtime(), + $objectManager, + new \Magento\Framework\ObjectManager\Definition\Runtime(), + ['first' => 'global'], + $cacheId = 'compiled_plugins_' . $scope, + new FileCache($cachePath) + ); } /** @@ -254,128 +59,13 @@ public function getPlugin($type, $code) } /** - * Retrieve next plugins in chain - * - * @param string $type - * @param string $method - * @param string $code - * @return array - */ - public function getNext($type, $method, $code = '__self') - { - $this->_loadScopedData(); - if (!isset($this->_inherited[$type]) && !array_key_exists($type, $this->_inherited)) { - $this->_inheritPlugins($type); - } - $key = $type . '_' . lcfirst($method) . '_' . $code; - return isset($this->_processed[$key]) ? $this->_processed[$key] : null; - } - - protected function _loadScopedData() - { - if (false == isset($this->_loadedScopes[$this->currentScope])) { - if (false == in_array($this->currentScope, $this->_scopePriorityScheme)) { - $this->_scopePriorityScheme[] = $this->currentScope; - } - if ($this->_cachePath) { - $cacheId = $this->_cachePath . DIRECTORY_SEPARATOR . 'compiled-plugins_' . implode('_', $this->_scopePriorityScheme) . '.php'; - $data = @include($cacheId); - } else { - $data = null; - } - - if ($data) { - list($this->_data, $this->_inherited, $this->_processed) = $data; - foreach ($this->_scopePriorityScheme as $scopeCode) { - $this->_loadedScopes[$scopeCode] = true; - } - } else { - $virtualTypes = []; - foreach ($this->_scopePriorityScheme as $scopeCode) { - if (false == isset($this->_loadedScopes[$scopeCode])) { - $data = $this->_reader->read($scopeCode); - unset($data['preferences']); - if (count($data) > 0) { - $this->_inherited = []; - $this->_processed = []; - $this->merge($data); - foreach ($data as $class => $config) { - if (isset($config['type'])) { - $virtualTypes[] = $class; - } - } - } - $this->_loadedScopes[$scopeCode] = true; - } - if ($scopeCode == $this->currentScope) { - break; - } - } - foreach ($virtualTypes as $class) { - $this->_inheritPlugins($class); - } - foreach ($this->getClassDefinitions() as $class) { - $this->_inheritPlugins($class); - } - if ($this->_cachePath) { - file_put_contents( - $cacheId, - '_data, $this->_inherited, $this->_processed], true) . '?>' - ); - } - } - $this->_pluginInstances = []; - } - } - - /** - * Returns class definitions - * - * @return array - */ - protected function getClassDefinitions() - { - return $this->_classDefinitions->getClasses(); - } - - /** - * Merge configuration - * - * @param array $config - * @return void + * @param $type + * @param $code + * @return mixed */ - public function merge(array $config) - { - foreach ($config as $type => $typeConfig) { - if (isset($typeConfig['plugins'])) { - $type = ltrim($type, '\\'); - if (isset($this->_data[$type])) { - $this->_data[$type] = array_replace_recursive($this->_data[$type], $typeConfig['plugins']); - } else { - $this->_data[$type] = $typeConfig['plugins']; - } - } - } - } - public function getPluginType($type, $code) { return $this->_inherited[$type][$code]['instance']; } - /** - * Remove from list not existing plugins - * - * @param array $plugins - * @return void - */ - private function filterPlugins(array &$plugins) - { - foreach ($plugins as $name => $plugin) { - if (empty($plugin['instance'])) { - unset($plugins[$name]); - //$this->getLogger()->info("Reference to undeclared plugin with name '{$name}'."); - } - } - } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php new file mode 100644 index 0000000000000..7f02a34302da6 --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php @@ -0,0 +1,145 @@ +cachePath = ($cachePath === null ? BP . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'cache' : $cachePath); + } + + + private function getCachePath($identifier) + { + return $this->cachePath . DIRECTORY_SEPARATOR . str_replace('|', '_', $identifier . '.php'); + } + + /** + * Test if a cache is available for the given id + * + * @param string $identifier Cache id + * @return int|bool Last modified time of cache entry if it is available, false otherwise + */ + public function test($identifier) + { + // TODO: Implement test() method. + } + + /** + * Load cache record by its unique identifier + * + * @param string $identifier + * @return string|bool + * @api + */ + public function load($identifier) + { + return $this->cachePath ? @include($this->getCachePath($identifier)) : false; + } + + /** + * Save cache record + * + * @param string $data + * @param string $identifier + * @param array $tags + * @param int|bool|null $lifeTime + * @return bool + * @api + */ + public function save($data, $identifier, array $tags = [], $lifeTime = null) + { + if ($this->cachePath) { + file_put_contents( + $this->getCachePath($identifier), + '' + ); + } + } + + /** + * Remove cache record by its unique identifier + * + * @param string $identifier + * @return bool + * @api + */ + public function remove($identifier) + { + // TODO: Implement remove() method. + } + + /** + * Clean cache records matching specified tags + * + * @param string $mode + * @param array $tags + * @return bool + * @api + */ + public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = []) + { + // TODO: Implement clean() method. + } + + /** + * Retrieve backend instance + * + * @return \Zend_Cache_Backend_Interface + */ + public function getBackend() + { + // TODO: Implement getBackend() method. + } + + /** + * Retrieve frontend instance compatible with Zend Locale Data setCache() to be used as a workaround + * + * @return \Zend_Cache_Core + */ + public function getLowLevelFrontend() + { + // TODO: Implement getLowLevelFrontend() method. + } + + /** + * Serialize data into string + * + * @param string|int|float|bool|array|null $data + * @return string|bool + * @throws \InvalidArgumentException + * @since 101.0.0 + */ + public function serialize($data) + { + return $data; + } + + /** + * Unserialize the given string + * + * @param string $string + * @return string|int|float|bool|array|null + * @throws \InvalidArgumentException + * @since 101.0.0 + */ + public function unserialize($string) + { + return $string; + } +} \ No newline at end of file diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php new file mode 100644 index 0000000000000..bb6a66e6965ef --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php @@ -0,0 +1,43 @@ +scope = $scope; + } + + /** + * Get current configuration scope identifier + * + * @return string + */ + public function getCurrentScope() { + return $this->scope; + } + + /** + * @param string $scope + * @throws \Exception + */ + public function setCurrentScope($scope){ + throw new \Exception('readonly'); + } + +} \ No newline at end of file diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php index 8af927f62a2a8..109ec517e8d17 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php @@ -5,8 +5,10 @@ */ namespace Magento\Framework\CompiledInterception\Test\Unit\CompiledPluginList; +use Magento\Framework\App\ObjectManager; use Magento\Framework\CompiledInterception\Generator\CompiledPluginList; use Magento\Framework\ObjectManagerInterface; +use Psr\Log\NullLogger; require_once __DIR__ . '/../Custom/Module/Model/Item.php'; require_once __DIR__ . '/../Custom/Module/Model/Item/Enhanced.php'; @@ -34,6 +36,9 @@ public function createScopeReaders() $readerMock = $this->createMock(\Magento\Framework\ObjectManager\Config\Reader\Dom::class); $readerMock->expects($this->any())->method('read')->will($this->returnValueMap($readerMap)); + $omMock = $this->createMock(ObjectManager::class); + $omMock->method('get')->with(\Psr\Log\LoggerInterface::class)->willReturn(new NullLogger()); + $omConfigMock = $this->getMockForAbstractClass( \Magento\Framework\Interception\ObjectManager\ConfigInterface::class ); @@ -45,6 +50,7 @@ public function createScopeReaders() $ret[$readerLine[0]] = $objectManagerHelper->getObject( CompiledPluginList::class, [ + 'objectManager' => $omMock, 'scope' => $readerLine[0], 'reader' => $readerMock, 'omConfig' => $omConfigMock, diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Complex.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Complex.php index 8081236687983..c6373122bfafc 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Complex.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Complex.php @@ -9,51 +9,115 @@ class Complex { + /** + * @param $subject + * @param $proceed + * @param $arg + * @return string + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ public function aroundGetName($subject, $proceed, $arg) { return '[' . $proceed($arg) . ']'; } + /** + * @param $subject + * @param $result + * @return string + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ public function afterGetName($subject, $result) { return $result . '%'; } + /** + * @param $subject + * @param $proceed + * @param $arg + * @return mixed + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ public function aroundSetValue($subject, $proceed, $arg) { return $proceed('[' . $arg . ']'); } + /** + * @param $subject + * @param $arg + * @return string + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ public function beforeSetValue($subject, $arg) { return '%' . $arg; } + /** + * @param $subject + * @param $proceed + * @return mixed + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ public function aroundGetReference($subject, $proceed) { return $proceed(); } + /** + * @param $subject + * @param $proceed + * @param mixed ...$variadicValue + * @return mixed + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ public function aroundFirstVariadicParameter($subject, $proceed, ...$variadicValue) { return $proceed(); } + /** + * @param $subject + * @param $proceed + * @param $value + * @param mixed ...$variadicValue + * @return mixed + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ public function aroundSecondVariadicParameter($subject, $proceed, $value, ...$variadicValue) { return $proceed(); } + /** + * @param $subject + * @param $proceed + * @param mixed ...$variadicValue + * @return mixed + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ public function aroundByRefVariadic($subject, $proceed, & ...$variadicValue) { return $proceed(); } + /** + * @param $subject + * @param $ret + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ public function afterReturnVoid($subject, $ret) { } + /** + * @param $subject + * @param $ret + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ public function afterGetNullableValue($subject, $ret) { From 32e3ccad7a38b8c978f949e10622d0168f11b995 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Wed, 15 May 2019 13:55:33 +0200 Subject: [PATCH 05/52] static code check fixes --- .../Generator/CompiledPluginList.php | 3 ++- .../Generator/FileCache.php | 21 +++++++++++++------ .../Generator/StaticScope.php | 9 +++----- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php index 898aefb8fe7c6..a5ea9503f4035 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php @@ -41,7 +41,7 @@ public function __construct( $objectManager, new \Magento\Framework\ObjectManager\Definition\Runtime(), ['first' => 'global'], - $cacheId = 'compiled_plugins_' . $scope, + 'compiled_plugins_' . $scope, new FileCache($cachePath) ); } @@ -52,6 +52,7 @@ public function __construct( * @param string $type * @param string $code * @return mixed + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function getPlugin($type, $code) { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php index 7f02a34302da6..f62a35da2b9f9 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php @@ -1,12 +1,8 @@ cachePath = ($cachePath === null ? BP . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'cache' : $cachePath); } + /** + * @param $identifier + * @return string + */ private function getCachePath($identifier) { return $this->cachePath . DIRECTORY_SEPARATOR . str_replace('|', '_', $identifier . '.php'); @@ -34,6 +38,7 @@ private function getCachePath($identifier) * * @param string $identifier Cache id * @return int|bool Last modified time of cache entry if it is available, false otherwise + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function test($identifier) { @@ -46,6 +51,7 @@ public function test($identifier) * @param string $identifier * @return string|bool * @api + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function load($identifier) { @@ -61,6 +67,7 @@ public function load($identifier) * @param int|bool|null $lifeTime * @return bool * @api + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function save($data, $identifier, array $tags = [], $lifeTime = null) { @@ -78,6 +85,7 @@ public function save($data, $identifier, array $tags = [], $lifeTime = null) * @param string $identifier * @return bool * @api + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function remove($identifier) { @@ -91,6 +99,7 @@ public function remove($identifier) * @param array $tags * @return bool * @api + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = []) { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php index bb6a66e6965ef..037fc2bdddb60 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php @@ -1,12 +1,8 @@ Date: Tue, 21 May 2019 11:35:14 +0200 Subject: [PATCH 06/52] hotfic for production mode --- .../Generator/CompiledInterceptor.php | 12 ++++-- .../Generator/FileCache.php | 6 ++- .../Framework/CompiledInterception/README.md | 40 +++++++++++-------- .../_out_interceptors/ComplexItem.txt | 10 +++-- .../_out_interceptors/ComplexItemTyped.txt | 10 +++-- .../_out_interceptors/Item.txt | 10 +++-- 6 files changed, 57 insertions(+), 31 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 04eb7316cea67..ab86d66184ea9 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -9,7 +9,7 @@ use Magento\Framework\App\ObjectManager; use Magento\Framework\ObjectManagerInterface; use Magento\Framework\Code\Generator\EntityAbstract; -use Magento\Framework\Config\Scope; +use Magento\Framework\Config\ScopeInterface; use Magento\Framework\Interception\Code\Generator\Interceptor; use Magento\Framework\Interception\DefinitionInterface; @@ -68,7 +68,7 @@ protected function _generateCode() $this->classMethods = []; $this->classProperties = []; $this->injectPropertiesSettersToConstructor($reflection->getConstructor(), [ - Scope::class => '____scope', + ScopeInterface::class => '____scope', ObjectManagerInterface::class => '____om', ]); $this->overrideMethodsAndGeneratePluginGetters($reflection); @@ -132,15 +132,19 @@ protected function injectPropertiesSettersToConstructor(\ReflectionMethod $paren } } $parameters = array_map(array($this, '_getMethodParameterInfo'), $parameters); - foreach ($extraParams as $type => $name) { + /* foreach ($extraParams as $type => $name) { array_unshift($parameters, [ 'name' => $name, 'type' => $type ]); - } + } */ foreach ($extraSetters as $name => $paramName) { array_unshift($body, "\$this->$name = \$$paramName;"); } + foreach ($extraParams as $type => $name) { + array_unshift($body, "//TODO fix di in production mode"); + array_unshift($body, "\$$name = \\Magento\\Framework\\App\\ObjectManager::getInstance()->get(\\$type::class);"); + } $this->classMethods[] = [ 'name' => '__construct', diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php index f62a35da2b9f9..ddf02b34e3f78 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php @@ -72,8 +72,12 @@ public function load($identifier) public function save($data, $identifier, array $tags = [], $lifeTime = null) { if ($this->cachePath) { + $path = $this->getCachePath($identifier); + if (!is_dir(dirname($path))) { + mkdir(dirname($path)); + } file_put_contents( - $this->getCachePath($identifier), + $path, '' ); } diff --git a/lib/internal/Magento/Framework/CompiledInterception/README.md b/lib/internal/Magento/Framework/CompiledInterception/README.md index c0ca3711245f9..83837fa4f1f86 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/README.md +++ b/lib/internal/Magento/Framework/CompiledInterception/README.md @@ -3,28 +3,34 @@ This component changes the way Magento 2 generates Interceptor classes (a mechanism that allows plugins to work together). Instead of generating boilerplate code it compiles the Interceptor using information from source code. -This makes plugins slightly faster and as Magento uses a lot of plugins even at its core, it lowers request time by ~10%. -This is important in places where there is a lot of non-cached PHP logic going (for example admin panel is noticeably faster). +This makes plugins slightly faster and as Magento uses a lot of plugins, even at its core, it lowers request time by ~10%. +This is important in places where there is a lot of non-cached PHP logic going on (for example admin panel is noticeably faster). + +The default method uses code that is called on nearly each method to see if there are any plugins connected, in generated code this is not required and call-stack is reduced. -The dafault method uses code that is called on nearly eatch method to see if there are any plugins connected, in generated code this is not required and call-stack is reduced. Having plugins called directly also makes code easier to debug and bugs easier to find. -Interceptors generated by this plugin are 100% compatible with ones generated by Magento by default so there is no need to change anything in your plugins. +The Interceptors generated by this plugin are 100% compatible with the ones generated by Magento by default, so there is no need to change anything in your plugins. ### ENABLING -to use compiled interceptors in developer and production mode please add following preference to your di.xml +To use compiled interceptors in developer mode please add following preference to your di.xml ``` ``` -clear generated files and cache: +To use compiled interceptors in production mode please add following preference to your di.xml +``` + +``` + +Clear generated files and cache: `rm -rf generated/* && rm -rf var/cache/* && bin/magento cache:clean` ### DISABLING -Replace remove preference from `app/etc/di.xml`, remove module and clear cache and generated files. +Remove preferences from `app/etc/di.xml`, remove module and clear cache and generated files. ### TECHNICAL DETAILS @@ -41,7 +47,7 @@ public function methodX($arg) { } ``` -This generator generates static interceptors like: +This generator generates static interceptors like this: ``` @@ -65,18 +71,18 @@ public function methodX($arg) { #### PROS -* easier debugging. +* Easier debugging. * If you ever stumbled upon `___callPlugins` when debugging you should know how painful it is to debug issues inside plugins. - * generated code is decorated with PHPDoc for easier debugging in IDE + * Generated code is decorated with PHPDoc for easier debugging in IDE -* fastest response time (5%-15% faster in developer and production mode) - * no redundant calls to `___callPlugins` in call stack. - * methods with no plugins are not overridden in parent at all. +* Fastest response time (5%-15% faster in developer and production mode) + * No redundant calls to `___callPlugins` in call stack. + * Methods with no plugins are not overridden in parent at all. -* implemented as a module and can be easily reverted to default `Generator\Interceptor` +* Implemented as a module and can be easily reverted to the default `Generator\Interceptor` #### CONS -* each time after making change in etc plugins config, `generated/code/*` needs to be purged -* tiny longer code generation step when run with no cache -* as this does not load plugins at runtime might not work in an edge case of plugging into core Magento classes like PluginsList etc. +* Each time after making change in etc plugins config, `generated/code/*` needs to be purged +* Tiny longer code generation step when run with no cache +* As this does not load plugins at runtime, might not work in an edge case of plugging into core Magento classes like `PluginsList` etc. diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt index 6b328df65ab03..f8e0edab1dffc 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt @@ -1,6 +1,6 @@ namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem; -use Magento\Framework\Config\Scope; +use Magento\Framework\Config\ScopeInterface; use Magento\Framework\ObjectManagerInterface; /** @@ -9,7 +9,7 @@ use Magento\Framework\ObjectManagerInterface; class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem { /** - * @var Scope + * @var ScopeInterface */ private $____scope = null; @@ -31,8 +31,12 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust /** * {@inheritdoc} */ - public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\Scope $____scope) + public function __construct() { + $____om = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\ObjectManagerInterface::class); + //TODO fix di in production mode + $____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); + //TODO fix di in production mode $this->____om = $____om; $this->____scope = $____scope; } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt index 2946bcc008050..121010f4a891a 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -1,6 +1,6 @@ namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped; -use Magento\Framework\Config\Scope; +use Magento\Framework\Config\ScopeInterface; use Magento\Framework\ObjectManagerInterface; /** @@ -9,7 +9,7 @@ use Magento\Framework\ObjectManagerInterface; class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped { /** - * @var Scope + * @var ScopeInterface */ private $____scope = null; @@ -31,8 +31,12 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust /** * {@inheritdoc} */ - public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\Scope $____scope) + public function __construct() { + $____om = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\ObjectManagerInterface::class); + //TODO fix di in production mode + $____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); + //TODO fix di in production mode $this->____om = $____om; $this->____scope = $____scope; } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt index 959f4edd8cb55..15ec7cc153253 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt @@ -1,6 +1,6 @@ namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; -use Magento\Framework\Config\Scope; +use Magento\Framework\Config\ScopeInterface; use Magento\Framework\ObjectManagerInterface; /** @@ -9,7 +9,7 @@ use Magento\Framework\ObjectManagerInterface; class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item { /** - * @var Scope + * @var ScopeInterface */ private $____scope = null; @@ -31,8 +31,12 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust /** * {@inheritdoc} */ - public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\Scope $____scope) + public function __construct() { + $____om = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\ObjectManagerInterface::class); + //TODO fix di in production mode + $____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); + //TODO fix di in production mode $this->____om = $____om; $this->____scope = $____scope; } From e57df097ad16ef43e6bfd080797c85cce2ea37ec Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Thu, 23 May 2019 09:35:57 +0200 Subject: [PATCH 07/52] removed unnecessary inheritance, fix for methods returning self --- .../Generator/CompiledInterceptor.php | 81 ++++++++++++++----- .../Generator/FileCache.php | 4 - .../CompiledInterceptorTest.php | 12 +++ .../_out_interceptors/ComplexItem.txt | 19 ++++- .../_out_interceptors/ComplexItemTyped.txt | 36 ++++++++- .../_out_interceptors/Item.txt | 4 +- .../Unit/Custom/Module/Model/ComplexItem.php | 8 ++ .../Custom/Module/Model/ComplexItemTyped.php | 16 ++++ .../Module/Model/ItemPlugin/Complex.php | 15 ++++ 9 files changed, 164 insertions(+), 31 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index ab86d66184ea9..34f16d8f63a42 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -7,26 +7,37 @@ namespace Magento\Framework\CompiledInterception\Generator; use Magento\Framework\App\ObjectManager; +use Magento\Framework\Code\Generator\CodeGeneratorInterface; +use Magento\Framework\Code\Generator\DefinedClasses; +use Magento\Framework\Code\Generator\Io; use Magento\Framework\ObjectManagerInterface; use Magento\Framework\Code\Generator\EntityAbstract; use Magento\Framework\Config\ScopeInterface; -use Magento\Framework\Interception\Code\Generator\Interceptor; use Magento\Framework\Interception\DefinitionInterface; +use Magento\Framework\App\AreaList; -class CompiledInterceptor extends Interceptor +class CompiledInterceptor extends EntityAbstract { + /** + * Entity type + */ + const ENTITY_TYPE = 'interceptor'; protected $plugins; protected $classMethods = []; + protected $classProperties = []; + protected $areaList; + public function __construct( + AreaList $areaList, $sourceClassName = null, $resultClassName = null, - \Magento\Framework\Code\Generator\Io $ioObject = null, - \Magento\Framework\Code\Generator\CodeGeneratorInterface $classGenerator = null, - \Magento\Framework\Code\Generator\DefinedClasses $definedClasses = null, + Io $ioObject = null, + CodeGeneratorInterface $classGenerator = null, + DefinedClasses $definedClasses = null, $plugins = null ) { @@ -36,14 +47,8 @@ public function __construct( $classGenerator, $definedClasses); - if ($plugins !== null) { - $this->plugins = $plugins; - } else { - $this->plugins = []; - foreach (['primary', 'frontend', 'adminhtml', 'crontab', 'webapi_rest', 'webapi_soap'] as $scope) { - $this->plugins[$scope] = new CompiledPluginList(ObjectManager::getInstance(), $scope); - } - } + $this->areaList = $areaList; + $this->plugins = $plugins; } /** @@ -54,6 +59,10 @@ public function setInterceptedMethods($interceptedMethods) //NOOP } + /** + * @return bool|string + * @throws \ReflectionException + */ protected function _generateCode() { $typeName = $this->getSourceClassName(); @@ -73,8 +82,19 @@ protected function _generateCode() ]); $this->overrideMethodsAndGeneratePluginGetters($reflection); - //return parent::_generateCode(); - return EntityAbstract::_generateCode(); + return parent::_generateCode(); + } + + /** + * Whether method is intercepted + * + * @param \ReflectionMethod $method + * @return bool + */ + protected function isInterceptedMethod(\ReflectionMethod $method) + { + return !($method->isConstructor() || $method->isFinal() || $method->isStatic() || $method->isDestructor()) && + !in_array($method->getName(), ['__sleep', '__wakeup', '__clone']); } protected function overrideMethodsAndGeneratePluginGetters(\ReflectionClass $reflection) @@ -86,7 +106,7 @@ protected function overrideMethodsAndGeneratePluginGetters(\ReflectionClass $ref if ($this->isInterceptedMethod($method)) { $config = $this->_getPluginsConfig($method, $allPlugins); if (!empty($config)) { - $this->classMethods[] = $this->_getCompiledMethodInfo($method, $config); + $this->classMethods[] = $this->getCompiledMethodInfo($method, $config); } } } @@ -300,14 +320,14 @@ protected function _getPluginGetterInfo($plugin) 'name' => $this->getGetterName($plugin), 'parameters' => [], 'body' => implode("\n", $body), - //'returnType' => $class, + 'returnType' => $plugin['class'], 'docblock' => [ 'shortDescription' => 'plugin "' . $plugin['code'] . '"' . "\n" . '@return \\' . $plugin['class'] ], ]; } - protected function _getCompiledMethodInfo(\ReflectionMethod $method, $config) + private function getCompiledMethodInfo(\ReflectionMethod $method, $config) { $parameters = $method->getParameters(); $returnsVoid = ($method->hasReturnType() && $method->getReturnType()->getName() == 'void'); @@ -333,12 +353,18 @@ protected function _getCompiledMethodInfo(\ReflectionMethod $method, $config) } $body[] = "}"; - + $returnType = $method->getReturnType(); + $returnTypeValue = $returnType + ? ($returnType->allowsNull() ? '?' : '') .$returnType->getName() + : null; + if ($returnTypeValue === 'self') { + $returnTypeValue = $method->getDeclaringClass()->getName(); + } return [ 'name' => ($method->returnsReference() ? '& ' : '') . $method->getName(), 'parameters' =>array_map(array($this, '_getMethodParameterInfo'), $parameters), 'body' => implode("\n", $body), - 'returnType' => $method->getReturnType(), + 'returnType' => $returnTypeValue, 'docblock' => ['shortDescription' => '{@inheritdoc}'], ]; } @@ -386,6 +412,12 @@ protected function _getPluginsConfig(\ReflectionMethod $method, &$allPlugins) $className = ltrim($this->getSourceClassName(), '\\'); $ret = array(); + if ($this->plugins === null) { + $this->plugins = []; + foreach ($this->areaList->getCodes() as $scope) { + $this->plugins[$scope] = new CompiledPluginList(ObjectManager::getInstance(), $scope); + } + } foreach ($this->plugins as $scope => $pluginsList) { $p = $this->_getPluginsChain($pluginsList, $className, $method->getName(), $allPlugins); if ($p) { @@ -396,4 +428,13 @@ protected function _getPluginsConfig(\ReflectionMethod $method, &$allPlugins) return $ret; } + /** + * Get default constructor definition for generated class + * + * @return array + */ + protected function _getDefaultConstructorDefinition() + { + // TODO: Implement _getDefaultConstructorDefinition() method. + } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php index ddf02b34e3f78..f30a69fa86f58 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php @@ -50,7 +50,6 @@ public function test($identifier) * * @param string $identifier * @return string|bool - * @api * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function load($identifier) @@ -66,7 +65,6 @@ public function load($identifier) * @param array $tags * @param int|bool|null $lifeTime * @return bool - * @api * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function save($data, $identifier, array $tags = [], $lifeTime = null) @@ -88,7 +86,6 @@ public function save($data, $identifier, array $tags = [], $lifeTime = null) * * @param string $identifier * @return bool - * @api * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function remove($identifier) @@ -102,7 +99,6 @@ public function remove($identifier) * @param string $mode * @param array $tags * @return bool - * @api * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = []) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php index f4f8ebb03c2cb..d1d6f927b3a97 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php @@ -6,6 +6,7 @@ namespace Magento\Framework\CompiledInterception\Test\Unit\CompiledInterceptor; +use Magento\Framework\App\AreaList; use Magento\Framework\Code\Generator\Io; use Magento\Framework\CompiledInterception\Generator\CompiledInterceptor; @@ -19,6 +20,11 @@ class CompiledInterceptorTest extends \PHPUnit\Framework\TestCase */ private $ioGenerator; + /** + * @var AreaList|MockObject + */ + private $areaList; + /** * @inheritdoc */ @@ -27,6 +33,11 @@ protected function setUp() $this->ioGenerator = $this->getMockBuilder(Io::class) ->disableOriginalConstructor() ->getMock(); + + + $this->areaList = $this->getMockBuilder(AreaList::class) + ->disableOriginalConstructor() + ->getMock(); } /** @@ -43,6 +54,7 @@ public function testGenerate($className, $resultClassName, $fileName) $interceptor = $this->getMockBuilder(CompiledInterceptor::class) ->setMethods(['_validateData']) ->setConstructorArgs([ + $this->areaList, $className, $resultClassName, $this->ioGenerator, diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt index f8e0edab1dffc..fcd18ec16d113 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt @@ -152,11 +152,26 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust } } + /** + * {@inheritdoc} + */ + public function returnsSelf() + { + switch($this->____scope->getCurrentScope()){ + case 'backend': + $this->____plugin_complex_plugin()->beforeReturnsSelf($this); + + return parent::returnsSelf(); + default: + return parent::returnsSelf(); + } + } + /** * plugin "advanced_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced */ - public function ____plugin_advanced_plugin() + public function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced { if ($this->____plugin_advanced_plugin === null) { $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); @@ -168,7 +183,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust * plugin "complex_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex */ - public function ____plugin_complex_plugin() + public function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex { if ($this->____plugin_complex_plugin === null) { $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt index 121010f4a891a..b59cd5eaf408c 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -59,7 +59,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust /** * {@inheritdoc} */ - public function getNullableValue() : string + public function getNullableValue() : ?string { switch($this->____scope->getCurrentScope()){ case 'backend': @@ -157,11 +157,41 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust } } + /** + * {@inheritdoc} + */ + public function returnsSelf() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped + { + switch($this->____scope->getCurrentScope()){ + case 'backend': + $this->____plugin_complex_plugin()->beforeReturnsSelf($this); + + return parent::returnsSelf(); + default: + return parent::returnsSelf(); + } + } + + /** + * {@inheritdoc} + */ + public function returnsType() : \Magento\Framework\Something + { + switch($this->____scope->getCurrentScope()){ + case 'backend': + $this->____plugin_complex_plugin()->beforeReturnsType($this); + + return parent::returnsType(); + default: + return parent::returnsType(); + } + } + /** * plugin "complex_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex */ - public function ____plugin_complex_plugin() + public function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex { if ($this->____plugin_complex_plugin === null) { $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); @@ -173,7 +203,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust * plugin "advanced_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced */ - public function ____plugin_advanced_plugin() + public function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced { if ($this->____plugin_advanced_plugin === null) { $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt index 15ec7cc153253..ff31369ef5e82 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt @@ -71,7 +71,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust * plugin "simple_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple */ - public function ____plugin_simple_plugin() + public function ____plugin_simple_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple { if ($this->____plugin_simple_plugin === null) { $this->____plugin_simple_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class); @@ -83,7 +83,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust * plugin "advanced_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced */ - public function ____plugin_advanced_plugin() + public function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced { if ($this->____plugin_advanced_plugin === null) { $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php index 6b44fb700358d..f8a792cd2d793 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php @@ -54,4 +54,12 @@ public function byRefVariadic(& ...$variadicValue) { $this->variadicAttribute = $variadicValue; } + + /** + * + */ + public function returnsSelf() + { + return $this; + } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php index e26e51ba6022d..0cada04d4c908 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php @@ -66,4 +66,20 @@ public function byRefVariadic(string & ...$variadicValue) { $this->variadicValue = $variadicValue; } + + /** + * + */ + public function returnsSelf() : self + { + return $this; + } + + /** + * + */ + public function returnsType() : \Magento\Framework\Something + { + return null; + } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Complex.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Complex.php index c6373122bfafc..079a0c1027130 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Complex.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Complex.php @@ -123,4 +123,19 @@ public function afterGetNullableValue($subject, $ret) } + /** + * + */ + public function beforeReturnsSelf() + { + + } + + /** + * + */ + public function beforeReturnsType() + { + + } } From 8071fb23ff9e1d099d8fd8c547b82dfb99695b84 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Thu, 23 May 2019 10:07:39 +0200 Subject: [PATCH 08/52] bugfix for methods returning void --- .../Generator/CompiledInterceptor.php | 4 ++-- .../CompiledInterceptor/_out_interceptors/ComplexItem.txt | 6 +++--- .../_out_interceptors/ComplexItemTyped.txt | 8 ++++---- .../Unit/CompiledInterceptor/_out_interceptors/Item.txt | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 34f16d8f63a42..ab76041b0a49e 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -237,10 +237,10 @@ protected function _getMethodSourceFromConfig(\ReflectionMethod $method, $conf, if (isset($conf[DefinitionInterface::LISTENER_AFTER])) { foreach ($conf[DefinitionInterface::LISTENER_AFTER] as $plugin) { - if ($returnVoid) { + if (!$returnVoid) { $chain[] = ["((\$tmp = \$this->" . $this->getGetterName($plugin) . "()->after$capName(\$this, \$result$extraParams)) !== null) ? \$tmp : \$result;"]; } else { - $chain[] = ["\$this->" . $this->getGetterName($plugin) . "()->after$capName(\$this, \$result$extraParams);"]; + $chain[] = ["\$this->" . $this->getGetterName($plugin) . "()->after$capName(\$this, null$extraParams);"]; } } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt index fcd18ec16d113..fae50cfe8265a 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt @@ -56,7 +56,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust return parent::getName(); }); - return $this->____plugin_advanced_plugin()->afterGetName($this, $result); + return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; case 'backend': $this->____plugin_advanced_plugin()->beforeGetName($this); @@ -65,10 +65,10 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust return parent::getName(); }); - return $this->____plugin_complex_plugin()->afterGetName($this, $result); + return (($tmp = $this->____plugin_complex_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; }); - return $this->____plugin_advanced_plugin()->afterGetName($this, $result); + return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; default: return parent::getName(); } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt index b59cd5eaf408c..19735f5bb9e67 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -50,7 +50,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust case 'backend': parent::returnVoid(); - (($tmp = $this->____plugin_complex_plugin()->afterReturnVoid($this, $result)) !== null) ? $tmp : $result; + $this->____plugin_complex_plugin()->afterReturnVoid($this, null); default: parent::returnVoid(); } @@ -65,7 +65,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust case 'backend': $result = parent::getNullableValue(); - return $this->____plugin_complex_plugin()->afterGetNullableValue($this, $result); + return (($tmp = $this->____plugin_complex_plugin()->afterGetNullableValue($this, $result)) !== null) ? $tmp : $result; default: return parent::getNullableValue(); } @@ -85,10 +85,10 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust return parent::getName(); }); - return $this->____plugin_complex_plugin()->afterGetName($this, $result); + return (($tmp = $this->____plugin_complex_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; }); - return $this->____plugin_advanced_plugin()->afterGetName($this, $result); + return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; default: return parent::getName(); } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt index ff31369ef5e82..1acb4562ac9d5 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt @@ -51,17 +51,17 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust case 'emptyscope': $result = parent::getName(); - return $this->____plugin_simple_plugin()->afterGetName($this, $result); + return (($tmp = $this->____plugin_simple_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; case 'backend': $this->____plugin_advanced_plugin()->beforeGetName($this); $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ $result = parent::getName(); - return $this->____plugin_simple_plugin()->afterGetName($this, $result); + return (($tmp = $this->____plugin_simple_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; }); - return $this->____plugin_advanced_plugin()->afterGetName($this, $result); + return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; default: return parent::getName(); } From 4948604074bdb4bc13a8e04ac00118f5f1a72bb7 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Thu, 23 May 2019 10:36:46 +0200 Subject: [PATCH 09/52] void methods fix --- .../CompiledInterception/Generator/CompiledInterceptor.php | 3 +++ .../CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt | 2 ++ 2 files changed, 5 insertions(+) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index ab76041b0a49e..26ae865ecf38b 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -253,6 +253,9 @@ protected function _getMethodSourceFromConfig(\ReflectionMethod $method, $conf, $body[] = $line; } } + if ($returnVoid) { + $body[] = 'break;'; + } return $body; } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt index 19735f5bb9e67..f2e34c86d89be 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -51,8 +51,10 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust parent::returnVoid(); $this->____plugin_complex_plugin()->afterReturnVoid($this, null); + break; default: parent::returnVoid(); + break; } } From 75e65ac0c2fc362d4949c93a7686c42082cd0652 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Thu, 23 May 2019 10:53:03 +0200 Subject: [PATCH 10/52] void methods fix --- .../CompiledInterception/Generator/CompiledInterceptor.php | 2 +- .../_out_interceptors/ComplexItemTyped.txt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 26ae865ecf38b..bbc08b6a94322 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -254,7 +254,7 @@ protected function _getMethodSourceFromConfig(\ReflectionMethod $method, $conf, } } if ($returnVoid) { - $body[] = 'break;'; + $body[] = 'return;'; } return $body; diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt index f2e34c86d89be..138fe1c7b91f7 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -51,10 +51,10 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust parent::returnVoid(); $this->____plugin_complex_plugin()->afterReturnVoid($this, null); - break; + return; default: parent::returnVoid(); - break; + return; } } From 5fc398c59560244141dee5b64e5d0f7b792fd7cf Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Fri, 24 May 2019 10:34:49 +0200 Subject: [PATCH 11/52] refactor of CompiledInterceptor --- .../Generator/CompiledInterceptor.php | 163 +++++++++++------- .../Generator/CompiledPluginList.php | 2 +- .../Generator/FileCache.php | 28 +-- .../Generator/NoSerialize.php | 37 ++++ .../_out_interceptors/ComplexItemTyped.txt | 4 +- 5 files changed, 141 insertions(+), 93 deletions(-) create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Generator/NoSerialize.php diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index bbc08b6a94322..2fe6c8cd48917 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -192,76 +192,111 @@ private function addCodeSubBlock(&$body, $sub, $indent = 1) } } + /** + * @param $plugins + * @param $methodName + * @param $extraParams + * @param $parametersList + * @return array + */ + private function compileBeforePlugins($plugins, $methodName, $extraParams, $parametersList) + { + $lines = []; + foreach ($plugins as $plugin) { + $call = "\$this->" . $this->getGetterName($plugin) . "()->$methodName(\$this$extraParams);"; + + if (!empty($parametersList)) { + $lines[] = "\$beforeResult = " . $call; + $lines[] = "if (\$beforeResult !== null) list({$parametersList}) = (array)\$beforeResult;"; + } else { + $lines[] = $call; + } + $lines[] = ""; + } + return $lines; + } + + /** + * @param $methodName + * @param $plugin + * @param $capitalizedName + * @param $extraParams + * @param $parameters + * @param $returnVoid + * @return array + */ + private function compileAroundPlugin($methodName, $plugin, $capitalizedName, $extraParams, $parameters, $returnVoid) + { + $lines = []; + $lines[] = "\$this->" . $this->getGetterName($plugin) . "()->around$capitalizedName(\$this, function({$this->_getParameterListForNextCallback($parameters)}){"; + $this->addCodeSubBlock($lines, $this->getMethodSourceFromConfig($methodName, $plugin['next'] ?: [], $parameters, $returnVoid)); + $lines[] = "}$extraParams);"; + return $lines; + } + + /** + * @param $plugins + * @param $methodName + * @param $extraParams + * @param $returnVoid + * @return array + */ + private function compileAfterPlugins($plugins, $methodName, $extraParams, $returnVoid) + { + $lines = []; + foreach ($plugins as $plugin) { + if (!$returnVoid) { + $lines[] = ["((\$tmp = \$this->" . $this->getGetterName($plugin) . "()->$methodName(\$this, \$result$extraParams)) !== null) ? \$tmp : \$result;"]; + } else { + $lines[] = ["\$this->" . $this->getGetterName($plugin) . "()->$methodName(\$this, null$extraParams);"]; + } + } + return $lines; + } + /** * @param \ReflectionMethod $method * @param $conf * @param $parameters * @return array - * @SuppressWarnings(PHPMD.CyclomaticComplexity) - * @SuppressWarnings(PHPMD.NPathComplexity) */ - protected function _getMethodSourceFromConfig(\ReflectionMethod $method, $conf, $parameters, $returnVoid) + private function getMethodSourceFromConfig($methodName, $conf, $parameters, $returnVoid) { - $body = []; $first = true; - $capName = ucfirst($method->getName()); - $extraParams = empty($parameters) ? '' : (', ' . $this->_getParameterList($parameters)); + $capitalizedName = ucfirst($methodName); + $parametersList = $this->_getParameterList($parameters); + $extraParams = empty($parameters) ? '' : (', ' . $parametersList); if (isset($conf[DefinitionInterface::LISTENER_BEFORE])) { - foreach ($conf[DefinitionInterface::LISTENER_BEFORE] as $plugin) { - if ($first) $first = false; else $body[] = ""; - - $call = "\$this->" . $this->getGetterName($plugin) . "()->before$capName(\$this$extraParams);"; - - if (!empty($parameters)) { - $body[] = "\$beforeResult = " . $call; - $body[] = "if (\$beforeResult !== null) list({$this->_getParameterList($parameters)}) = (array)\$beforeResult;"; - } else { - $body[] = $call; - } - } + $body = $this->compileBeforePlugins($conf[DefinitionInterface::LISTENER_BEFORE], 'before' . $capitalizedName, $extraParams, $parametersList); + } else { + $body = []; } - - $chain = []; - $main = []; + $resultChain = []; if (isset($conf[DefinitionInterface::LISTENER_AROUND])) { - $plugin = $conf[DefinitionInterface::LISTENER_AROUND]; - $main[] = "\$this->" . $this->getGetterName($plugin) . "()->around$capName(\$this, function({$this->_getParameterListForNextCallback($parameters)}){"; - $this->addCodeSubBlock($main, $this->_getMethodSourceFromConfig($method, $plugin['next'] ?: [], $parameters, $returnVoid)); - $main[] = "}$extraParams);"; + $resultChain[] = $this->compileAroundPlugin($methodName, $conf[DefinitionInterface::LISTENER_AROUND], $capitalizedName, $extraParams, $parameters, $returnVoid); } else { - $main[] = "parent::{$method->getName()}({$this->_getParameterList($parameters)});"; + $resultChain[] = ["parent::{$methodName}({$this->_getParameterList($parameters)});"]; } - $chain[] = $main; if (isset($conf[DefinitionInterface::LISTENER_AFTER])) { - foreach ($conf[DefinitionInterface::LISTENER_AFTER] as $plugin) { - if (!$returnVoid) { - $chain[] = ["((\$tmp = \$this->" . $this->getGetterName($plugin) . "()->after$capName(\$this, \$result$extraParams)) !== null) ? \$tmp : \$result;"]; - } else { - $chain[] = ["\$this->" . $this->getGetterName($plugin) . "()->after$capName(\$this, null$extraParams);"]; - } - } + $resultChain = array_merge($resultChain, $this->compileAfterPlugins($conf[DefinitionInterface::LISTENER_AFTER], 'after' . $capitalizedName, $extraParams, $returnVoid)); } - foreach ($chain as $lp => $piece) { + foreach ($resultChain as $lp => $piece) { if ($first) $first = false; else $body[] = ""; if (!$returnVoid) { - $piece[0] = (($lp + 1 == count($chain)) ? "return " : "\$result = ") . $piece[0]; + $piece[0] = (($lp + 1 == count($resultChain)) ? "return " : "\$result = ") . $piece[0]; } foreach ($piece as $line) { $body[] = $line; } } - if ($returnVoid) { - $body[] = 'return;'; - } - return $body; } /** - * @param \ReflectionParameter[] $parameters + * @param array $parameters * @return string */ protected function _getParameterListForNextCallback(array $parameters) @@ -351,8 +386,10 @@ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) foreach ($cases as $case) { $body = array_merge($body, $case['cases']); - $this->addCodeSubBlock($body, $this->_getMethodSourceFromConfig($method, $case['conf'], $parameters, $returnsVoid), 2); - //$body[] = "\t\tbreak;"; + $this->addCodeSubBlock($body, $this->getMethodSourceFromConfig($method->getName(), $case['conf'], $parameters, $returnsVoid), 2); + if ($returnsVoid) { + $body[] = "\t\tbreak;"; + } } $body[] = "}"; @@ -384,37 +421,37 @@ protected function _getPluginInfo(CompiledPluginList $plugins, $code, $className 'clean_name' => preg_replace("/[^A-Za-z0-9_]/", '_', $code . $suffix) ]; } - $ret = $allPlugins[$code][$className]; - $ret['next'] = $next; - return $ret; + $result = $allPlugins[$code][$className]; + $result['next'] = $next; + return $result; } protected function _getPluginsChain(CompiledPluginList $plugins, $className, $method, &$allPlugins, $next = '__self') { - $ret = $plugins->getNext($className, $method, $next); - if(!empty($ret[DefinitionInterface::LISTENER_BEFORE])) { - foreach ($ret[DefinitionInterface::LISTENER_BEFORE] as $k => $code) { - $ret[DefinitionInterface::LISTENER_BEFORE][$k] = $this->_getPluginInfo($plugins, $code, $className, $allPlugins); + $result = $plugins->getNext($className, $method, $next); + if(!empty($result[DefinitionInterface::LISTENER_BEFORE])) { + foreach ($result[DefinitionInterface::LISTENER_BEFORE] as $k => $code) { + $result[DefinitionInterface::LISTENER_BEFORE][$k] = $this->_getPluginInfo($plugins, $code, $className, $allPlugins); } } - if(!empty($ret[DefinitionInterface::LISTENER_AFTER])) { - foreach ($ret[DefinitionInterface::LISTENER_AFTER] as $k => $code) { - $ret[DefinitionInterface::LISTENER_AFTER][$k] = $this->_getPluginInfo($plugins, $code, $className, $allPlugins); + if(!empty($result[DefinitionInterface::LISTENER_AFTER])) { + foreach ($result[DefinitionInterface::LISTENER_AFTER] as $k => $code) { + $result[DefinitionInterface::LISTENER_AFTER][$k] = $this->_getPluginInfo($plugins, $code, $className, $allPlugins); } } - if (isset($ret[DefinitionInterface::LISTENER_AROUND])) { - $ret[DefinitionInterface::LISTENER_AROUND] = $this->_getPluginInfo($plugins, $ret[DefinitionInterface::LISTENER_AROUND], $className, $allPlugins, - $this->_getPluginsChain($plugins, $className, $method, $allPlugins, $ret[DefinitionInterface::LISTENER_AROUND])); + if (isset($result[DefinitionInterface::LISTENER_AROUND])) { + $result[DefinitionInterface::LISTENER_AROUND] = $this->_getPluginInfo($plugins, $result[DefinitionInterface::LISTENER_AROUND], $className, $allPlugins, + $this->_getPluginsChain($plugins, $className, $method, $allPlugins, $result[DefinitionInterface::LISTENER_AROUND])); } - return $ret; + return $result; } protected function _getPluginsConfig(\ReflectionMethod $method, &$allPlugins) { $className = ltrim($this->getSourceClassName(), '\\'); - $ret = array(); + $result = array(); if ($this->plugins === null) { $this->plugins = []; foreach ($this->areaList->getCodes() as $scope) { @@ -422,13 +459,13 @@ protected function _getPluginsConfig(\ReflectionMethod $method, &$allPlugins) } } foreach ($this->plugins as $scope => $pluginsList) { - $p = $this->_getPluginsChain($pluginsList, $className, $method->getName(), $allPlugins); - if ($p) { - $ret[$scope] = $p; + $pluginChain = $this->_getPluginsChain($pluginsList, $className, $method->getName(), $allPlugins); + if ($pluginChain) { + $result[$scope] = $pluginChain; } } - return $ret; + return $result; } /** @@ -438,6 +475,6 @@ protected function _getPluginsConfig(\ReflectionMethod $method, &$allPlugins) */ protected function _getDefaultConstructorDefinition() { - // TODO: Implement _getDefaultConstructorDefinition() method. + } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php index a5ea9503f4035..5a4957dd32146 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php @@ -42,7 +42,7 @@ public function __construct( new \Magento\Framework\ObjectManager\Definition\Runtime(), ['first' => 'global'], 'compiled_plugins_' . $scope, - new FileCache($cachePath) + new NoSerialize() ); } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php index f30a69fa86f58..6dba513c4d487 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php @@ -7,9 +7,8 @@ use Magento\Framework\Config\CacheInterface; -use Magento\Framework\Serialize\SerializerInterface; -class FileCache implements CacheInterface, SerializerInterface +class FileCache implements CacheInterface { private $cachePath; @@ -126,29 +125,4 @@ public function getLowLevelFrontend() // TODO: Implement getLowLevelFrontend() method. } - /** - * Serialize data into string - * - * @param string|int|float|bool|array|null $data - * @return string|bool - * @throws \InvalidArgumentException - * @since 101.0.0 - */ - public function serialize($data) - { - return $data; - } - - /** - * Unserialize the given string - * - * @param string $string - * @return string|int|float|bool|array|null - * @throws \InvalidArgumentException - * @since 101.0.0 - */ - public function unserialize($string) - { - return $string; - } } \ No newline at end of file diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/NoSerialize.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/NoSerialize.php new file mode 100644 index 0000000000000..6f8247e55e4dc --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/NoSerialize.php @@ -0,0 +1,37 @@ +____plugin_complex_plugin()->afterReturnVoid($this, null); - return; + break; default: parent::returnVoid(); - return; + break; } } From 1999072d495c3a988132b7f3795037e4b8f2cc25 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Fri, 24 May 2019 12:30:53 +0200 Subject: [PATCH 12/52] fixes for coding standard --- .../Generator/CompiledInterceptor.php | 164 ++++++++++++------ 1 file changed, 108 insertions(+), 56 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 2fe6c8cd48917..55f37c5fff106 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -25,12 +25,24 @@ class CompiledInterceptor extends EntityAbstract protected $plugins; - protected $classMethods = []; + protected $classMethods = null; - protected $classProperties = []; + protected $classProperties = null; + + protected $baseReflection = null; protected $areaList; + /** + * CompiledInterceptor constructor. + * @param AreaList $areaList + * @param null $sourceClassName + * @param null $resultClassName + * @param Io|null $ioObject + * @param CodeGeneratorInterface|null $classGenerator + * @param DefinedClasses|null $definedClasses + * @param null $plugins + */ public function __construct( AreaList $areaList, $sourceClassName = null, @@ -59,30 +71,73 @@ public function setInterceptedMethods($interceptedMethods) //NOOP } + /** + * @return array|null + * @throws \ReflectionException + */ + protected function _getClassMethods() + { + $this->generateMethodsAndProperties(); + return $this->classMethods; + } + + /** + * @return array|null + * @throws \ReflectionException + */ + protected function _getClassProperties() + { + $this->generateMethodsAndProperties(); + return $this->classProperties; + } + + /** + * @return array|void + */ + protected function _getDefaultConstructorDefinition() + { + + } + /** * @return bool|string * @throws \ReflectionException */ protected function _generateCode() { - $typeName = $this->getSourceClassName(); - $reflection = new \ReflectionClass($typeName); - - if ($reflection->isInterface()) { + if ($this->getSourceClassReflection()->isInterface()) { return false; } else { - $this->_classGenerator->setExtendedClass($typeName); + $this->_classGenerator->setExtendedClass($this->getSourceClassName()); } + $this->generateMethodsAndProperties(); + return parent::_generateCode(); + } - $this->classMethods = []; - $this->classProperties = []; - $this->injectPropertiesSettersToConstructor($reflection->getConstructor(), [ - ScopeInterface::class => '____scope', - ObjectManagerInterface::class => '____om', - ]); - $this->overrideMethodsAndGeneratePluginGetters($reflection); + /** + * @throws \ReflectionException + */ + private function generateMethodsAndProperties() + { + if ($this->classMethods === null) { + $this->classMethods = []; + $this->classProperties = []; - return parent::_generateCode(); + $this->injectPropertiesSettersToConstructor($this->getSourceClassReflection()->getConstructor(), [ + ScopeInterface::class => '____scope', + ObjectManagerInterface::class => '____om', + ]); + $this->overrideMethodsAndGeneratePluginGetters($this->getSourceClassReflection()); + } + } + + /** + * @return \ReflectionClass + * @throws \ReflectionException + */ + private function getSourceClassReflection() + { + return new \ReflectionClass($this->getSourceClassName()); } /** @@ -97,14 +152,14 @@ protected function isInterceptedMethod(\ReflectionMethod $method) !in_array($method->getName(), ['__sleep', '__wakeup', '__clone']); } - protected function overrideMethodsAndGeneratePluginGetters(\ReflectionClass $reflection) + private function overrideMethodsAndGeneratePluginGetters(\ReflectionClass $reflection) { $publicMethods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); $allPlugins = []; foreach ($publicMethods as $method) { if ($this->isInterceptedMethod($method)) { - $config = $this->_getPluginsConfig($method, $allPlugins); + $config = $this->getPluginsConfig($method, $allPlugins); if (!empty($config)) { $this->classMethods[] = $this->getCompiledMethodInfo($method, $config); } @@ -112,13 +167,13 @@ protected function overrideMethodsAndGeneratePluginGetters(\ReflectionClass $ref } foreach ($allPlugins as $plugins) { foreach ($plugins as $plugin) { - $this->classMethods[] = $this->_getPluginGetterInfo($plugin); - $this->classProperties[] = $this->_getPluginPropertyInfo($plugin); + $this->classMethods[] = $this->getPluginGetterInfo($plugin); + $this->classProperties[] = $this->getPluginPropertyInfo($plugin); } } } - protected function injectPropertiesSettersToConstructor(\ReflectionMethod $parentConstructor = null, $properties = []) + private function injectPropertiesSettersToConstructor(\ReflectionMethod $parentConstructor = null, $properties = []) { if ($parentConstructor == null) { $parameters = []; @@ -175,16 +230,6 @@ protected function injectPropertiesSettersToConstructor(\ReflectionMethod $paren } - protected function _getClassMethods() - { - return $this->classMethods; - } - - protected function _getClassProperties() - { - return $this->classProperties; - } - private function addCodeSubBlock(&$body, $sub, $indent = 1) { foreach ($sub as $line) { @@ -228,7 +273,7 @@ private function compileBeforePlugins($plugins, $methodName, $extraParams, $para private function compileAroundPlugin($methodName, $plugin, $capitalizedName, $extraParams, $parameters, $returnVoid) { $lines = []; - $lines[] = "\$this->" . $this->getGetterName($plugin) . "()->around$capitalizedName(\$this, function({$this->_getParameterListForNextCallback($parameters)}){"; + $lines[] = "\$this->" . $this->getGetterName($plugin) . "()->around$capitalizedName(\$this, function({$this->getParameterListForNextCallback($parameters)}){"; $this->addCodeSubBlock($lines, $this->getMethodSourceFromConfig($methodName, $plugin['next'] ?: [], $parameters, $returnVoid)); $lines[] = "}$extraParams);"; return $lines; @@ -264,7 +309,7 @@ private function getMethodSourceFromConfig($methodName, $conf, $parameters, $ret { $first = true; $capitalizedName = ucfirst($methodName); - $parametersList = $this->_getParameterList($parameters); + $parametersList = $this->getParameterList($parameters); $extraParams = empty($parameters) ? '' : (', ' . $parametersList); if (isset($conf[DefinitionInterface::LISTENER_BEFORE])) { @@ -277,7 +322,7 @@ private function getMethodSourceFromConfig($methodName, $conf, $parameters, $ret if (isset($conf[DefinitionInterface::LISTENER_AROUND])) { $resultChain[] = $this->compileAroundPlugin($methodName, $conf[DefinitionInterface::LISTENER_AROUND], $capitalizedName, $extraParams, $parameters, $returnVoid); } else { - $resultChain[] = ["parent::{$methodName}({$this->_getParameterList($parameters)});"]; + $resultChain[] = ["parent::{$methodName}({$this->getParameterList($parameters)});"]; } if (isset($conf[DefinitionInterface::LISTENER_AFTER])) { @@ -299,7 +344,7 @@ private function getMethodSourceFromConfig($methodName, $conf, $parameters, $ret * @param array $parameters * @return string */ - protected function _getParameterListForNextCallback(array $parameters) + private function getParameterListForNextCallback(array $parameters) { $ret = []; foreach ($parameters as $parameter) { @@ -319,7 +364,7 @@ protected function _getParameterListForNextCallback(array $parameters) * @param \ReflectionParameter[] $parameters * @return string */ - protected function _getParameterList(array $parameters) + private function getParameterList(array $parameters) { $ret = []; foreach ($parameters as $parameter) { @@ -328,12 +373,20 @@ protected function _getParameterList(array $parameters) return implode(', ', $ret); } - protected function getGetterName($plugin) + /** + * @param $plugin + * @return string + */ + private function getGetterName($plugin) { return '____plugin_' . $plugin['clean_name']; } - protected function _getPluginPropertyInfo($plugin) + /** + * @param $plugin + * @return array + */ + private function getPluginPropertyInfo($plugin) { return [ 'name' => '____plugin_' . $plugin['clean_name'], @@ -344,7 +397,11 @@ protected function _getPluginPropertyInfo($plugin) ]; } - protected function _getPluginGetterInfo($plugin) + /** + * @param $plugin + * @return array + */ + private function getPluginGetterInfo($plugin) { $body = []; $varName = "\$this->____plugin_" . $plugin['clean_name']; @@ -365,6 +422,11 @@ protected function _getPluginGetterInfo($plugin) ]; } + /** + * @param \ReflectionMethod $method + * @param $config + * @return array + */ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) { $parameters = $method->getParameters(); @@ -409,7 +471,7 @@ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) ]; } - protected function _getPluginInfo(CompiledPluginList $plugins, $code, $className, &$allPlugins, $next = null) + private function getPluginInfo(CompiledPluginList $plugins, $code, $className, &$allPlugins, $next = null) { $className = $plugins->getPluginType($className, $code); if (!isset($allPlugins[$code])) $allPlugins[$code] = []; @@ -427,27 +489,27 @@ protected function _getPluginInfo(CompiledPluginList $plugins, $code, $className } - protected function _getPluginsChain(CompiledPluginList $plugins, $className, $method, &$allPlugins, $next = '__self') + private function getPluginsChain(CompiledPluginList $plugins, $className, $method, &$allPlugins, $next = '__self') { $result = $plugins->getNext($className, $method, $next); if(!empty($result[DefinitionInterface::LISTENER_BEFORE])) { foreach ($result[DefinitionInterface::LISTENER_BEFORE] as $k => $code) { - $result[DefinitionInterface::LISTENER_BEFORE][$k] = $this->_getPluginInfo($plugins, $code, $className, $allPlugins); + $result[DefinitionInterface::LISTENER_BEFORE][$k] = $this->getPluginInfo($plugins, $code, $className, $allPlugins); } } if(!empty($result[DefinitionInterface::LISTENER_AFTER])) { foreach ($result[DefinitionInterface::LISTENER_AFTER] as $k => $code) { - $result[DefinitionInterface::LISTENER_AFTER][$k] = $this->_getPluginInfo($plugins, $code, $className, $allPlugins); + $result[DefinitionInterface::LISTENER_AFTER][$k] = $this->getPluginInfo($plugins, $code, $className, $allPlugins); } } if (isset($result[DefinitionInterface::LISTENER_AROUND])) { - $result[DefinitionInterface::LISTENER_AROUND] = $this->_getPluginInfo($plugins, $result[DefinitionInterface::LISTENER_AROUND], $className, $allPlugins, - $this->_getPluginsChain($plugins, $className, $method, $allPlugins, $result[DefinitionInterface::LISTENER_AROUND])); + $result[DefinitionInterface::LISTENER_AROUND] = $this->getPluginInfo($plugins, $result[DefinitionInterface::LISTENER_AROUND], $className, $allPlugins, + $this->getPluginsChain($plugins, $className, $method, $allPlugins, $result[DefinitionInterface::LISTENER_AROUND])); } return $result; } - protected function _getPluginsConfig(\ReflectionMethod $method, &$allPlugins) + private function getPluginsConfig(\ReflectionMethod $method, &$allPlugins) { $className = ltrim($this->getSourceClassName(), '\\'); @@ -459,7 +521,7 @@ protected function _getPluginsConfig(\ReflectionMethod $method, &$allPlugins) } } foreach ($this->plugins as $scope => $pluginsList) { - $pluginChain = $this->_getPluginsChain($pluginsList, $className, $method->getName(), $allPlugins); + $pluginChain = $this->getPluginsChain($pluginsList, $className, $method->getName(), $allPlugins); if ($pluginChain) { $result[$scope] = $pluginChain; } @@ -467,14 +529,4 @@ protected function _getPluginsConfig(\ReflectionMethod $method, &$allPlugins) } return $result; } - - /** - * Get default constructor definition for generated class - * - * @return array - */ - protected function _getDefaultConstructorDefinition() - { - - } } From f30e9cfb699665c36d5a90e0e183ff2a0f84967b Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Mon, 27 May 2019 10:24:15 +0200 Subject: [PATCH 13/52] coding standards fix --- .../Generator/CompiledInterceptor.php | 41 ++++++++++++------- .../Generator/CompiledPluginList.php | 9 ++-- .../CompiledInterceptorTest.php | 2 +- .../_out_interceptors/ComplexItem.txt | 18 ++++---- .../_out_interceptors/ComplexItemTyped.txt | 22 +++++----- .../_out_interceptors/Item.txt | 6 +-- .../Custom/Module/Model/ComplexItemTyped.php | 10 ++--- 7 files changed, 62 insertions(+), 46 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 55f37c5fff106..dc31546b04f72 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -16,6 +16,10 @@ use Magento\Framework\Interception\DefinitionInterface; use Magento\Framework\App\AreaList; +/** + * compiled interceptors generator + * please see ../README.md for details + */ class CompiledInterceptor extends EntityAbstract { /** @@ -23,15 +27,18 @@ class CompiledInterceptor extends EntityAbstract */ const ENTITY_TYPE = 'interceptor'; - protected $plugins; + private $plugins; - protected $classMethods = null; + private $classMethods; - protected $classProperties = null; + private $classProperties; - protected $baseReflection = null; + /** + * @var \ReflectionClass + */ + private $baseReflection; - protected $areaList; + private $areaList; /** * CompiledInterceptor constructor. @@ -51,8 +58,7 @@ public function __construct( CodeGeneratorInterface $classGenerator = null, DefinedClasses $definedClasses = null, $plugins = null - ) - { + ){ parent::__construct($sourceClassName, $resultClassName , $ioObject, @@ -123,10 +129,13 @@ private function generateMethodsAndProperties() $this->classMethods = []; $this->classProperties = []; - $this->injectPropertiesSettersToConstructor($this->getSourceClassReflection()->getConstructor(), [ - ScopeInterface::class => '____scope', - ObjectManagerInterface::class => '____om', - ]); + $this->injectPropertiesSettersToConstructor( + $this->getSourceClassReflection()->getConstructor(), + [ + ScopeInterface::class => '____scope', + ObjectManagerInterface::class => '____om', + ] + ); $this->overrideMethodsAndGeneratePluginGetters($this->getSourceClassReflection()); } } @@ -137,7 +146,10 @@ private function generateMethodsAndProperties() */ private function getSourceClassReflection() { - return new \ReflectionClass($this->getSourceClassName()); + if ($this->baseReflection === null) { + $this->baseReflection = new \ReflectionClass($this->getSourceClassName()); + } + return $this->baseReflection; } /** @@ -413,6 +425,7 @@ private function getPluginGetterInfo($plugin) return [ 'name' => $this->getGetterName($plugin), + 'visibility' => 'private', 'parameters' => [], 'body' => implode("\n", $body), 'returnType' => $plugin['class'], @@ -433,7 +446,7 @@ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) $returnsVoid = ($method->hasReturnType() && $method->getReturnType()->getName() == 'void'); $body = [ - 'switch($this->____scope->getCurrentScope()){' + 'switch ($this->____scope->getCurrentScope()) {' ]; $cases = []; @@ -457,7 +470,7 @@ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) $body[] = "}"; $returnType = $method->getReturnType(); $returnTypeValue = $returnType - ? ($returnType->allowsNull() ? '?' : '') .$returnType->getName() + ? ($returnType->allowsNull() ? '?' : '') . $returnType->getName() : null; if ($returnTypeValue === 'self') { $returnTypeValue = $method->getDeclaringClass()->getName(); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php index 5a4957dd32146..a8cebd2a563ee 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php @@ -9,6 +9,9 @@ use Magento\Framework\Interception\PluginList\PluginList; use Magento\Framework\Interception\ObjectManager\ConfigInterface; use Magento\Framework\ObjectManager\Config\Reader\Dom; +use Magento\Framework\ObjectManager\Relations\Runtime as ObjectManagerRelationsRuntime; +use Magento\Framework\Interception\Definition\Runtime as InterceptionDefinitionRuntime; +use Magento\Framework\ObjectManager\Definition\Runtime as ObjectManagerDefinitionRuntime; class CompiledPluginList extends PluginList { @@ -35,11 +38,11 @@ public function __construct( $reader, new StaticScope($scope), new FileCache($cachePath), - new \Magento\Framework\ObjectManager\Relations\Runtime(), + new ObjectManagerRelationsRuntime(), $omConfig, - new \Magento\Framework\Interception\Definition\Runtime(), + new InterceptionDefinitionRuntime(), $objectManager, - new \Magento\Framework\ObjectManager\Definition\Runtime(), + new ObjectManagerDefinitionRuntime(), ['first' => 'global'], 'compiled_plugins_' . $scope, new NoSerialize() diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php index d1d6f927b3a97..2d06e97e30a00 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php @@ -11,7 +11,7 @@ use Magento\Framework\CompiledInterception\Generator\CompiledInterceptor; use Magento\Framework\CompiledInterception\Test\Unit\CompiledPluginList\CompiledPluginListTest; -use \PHPUnit_Framework_MockObject_MockObject as MockObject; +use PHPUnit\Framework\MockObject\MockObject as MockObject; class CompiledInterceptorTest extends \PHPUnit\Framework\TestCase { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt index fae50cfe8265a..44b4695fcb7c9 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt @@ -46,7 +46,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function getName() { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'global': case 'frontend': case 'emptyscope': @@ -79,7 +79,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function setValue($value) { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': $beforeResult = $this->____plugin_complex_plugin()->beforeSetValue($this, $value); if ($beforeResult !== null) list($value) = (array)$beforeResult; @@ -97,7 +97,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function & getReference() { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': return $this->____plugin_complex_plugin()->aroundGetReference($this, function(){ return parent::getReference(); @@ -112,7 +112,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function firstVariadicParameter(... $variadicValue) { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': return $this->____plugin_complex_plugin()->aroundFirstVariadicParameter($this, function($variadicValue){ return parent::firstVariadicParameter($variadicValue); @@ -127,7 +127,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function secondVariadicParameter($value, ... $variadicValue) { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': return $this->____plugin_complex_plugin()->aroundSecondVariadicParameter($this, function($value, $variadicValue){ return parent::secondVariadicParameter($value, $variadicValue); @@ -142,7 +142,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function byRefVariadic(&... $variadicValue) { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': return $this->____plugin_complex_plugin()->aroundByRefVariadic($this, function(&$variadicValue){ return parent::byRefVariadic($variadicValue); @@ -157,7 +157,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function returnsSelf() { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': $this->____plugin_complex_plugin()->beforeReturnsSelf($this); @@ -171,7 +171,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust * plugin "advanced_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced */ - public function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced { if ($this->____plugin_advanced_plugin === null) { $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); @@ -183,7 +183,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust * plugin "complex_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex */ - public function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + private function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex { if ($this->____plugin_complex_plugin === null) { $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt index f2e34c86d89be..d5c195994cd3d 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -46,7 +46,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function returnVoid() : void { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': parent::returnVoid(); @@ -63,7 +63,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function getNullableValue() : ?string { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': $result = parent::getNullableValue(); @@ -78,7 +78,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function getName() : string { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': $this->____plugin_advanced_plugin()->beforeGetName($this); @@ -101,7 +101,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function setValue(string $value) { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': $beforeResult = $this->____plugin_complex_plugin()->beforeSetValue($this, $value); if ($beforeResult !== null) list($value) = (array)$beforeResult; @@ -119,7 +119,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function firstVariadicParameter(string ... $variadicValue) { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': return $this->____plugin_complex_plugin()->aroundFirstVariadicParameter($this, function($variadicValue){ return parent::firstVariadicParameter($variadicValue); @@ -134,7 +134,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function secondVariadicParameter(string $value, string ... $variadicValue) { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': return $this->____plugin_complex_plugin()->aroundSecondVariadicParameter($this, function($value, $variadicValue){ return parent::secondVariadicParameter($value, $variadicValue); @@ -149,7 +149,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function byRefVariadic(string &... $variadicValue) { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': return $this->____plugin_complex_plugin()->aroundByRefVariadic($this, function(&$variadicValue){ return parent::byRefVariadic($variadicValue); @@ -164,7 +164,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function returnsSelf() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': $this->____plugin_complex_plugin()->beforeReturnsSelf($this); @@ -179,7 +179,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function returnsType() : \Magento\Framework\Something { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'backend': $this->____plugin_complex_plugin()->beforeReturnsType($this); @@ -193,7 +193,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust * plugin "complex_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex */ - public function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + private function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex { if ($this->____plugin_complex_plugin === null) { $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); @@ -205,7 +205,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust * plugin "advanced_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced */ - public function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced { if ($this->____plugin_advanced_plugin === null) { $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt index 1acb4562ac9d5..abd42aa5b8e36 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt @@ -46,7 +46,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust */ public function getName() { - switch($this->____scope->getCurrentScope()){ + switch ($this->____scope->getCurrentScope()) { case 'global': case 'emptyscope': $result = parent::getName(); @@ -71,7 +71,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust * plugin "simple_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple */ - public function ____plugin_simple_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple + private function ____plugin_simple_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple { if ($this->____plugin_simple_plugin === null) { $this->____plugin_simple_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class); @@ -83,7 +83,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust * plugin "advanced_plugin" * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced */ - public function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced { if ($this->____plugin_advanced_plugin === null) { $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php index 0cada04d4c908..60159abd985d5 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php @@ -12,7 +12,7 @@ class ComplexItemTyped private $value; private $variadicValue; - public function returnVoid() : void + public function returnVoid(): void { // Nothing to do here } @@ -20,7 +20,7 @@ public function returnVoid() : void /** * @return null|string */ - public function getNullableValue() : ?string + public function getNullableValue(): ?string { return null; } @@ -28,7 +28,7 @@ public function getNullableValue() : ?string /** * @return string */ - public function getName() : string + public function getName(): string { return $this->value; } @@ -70,7 +70,7 @@ public function byRefVariadic(string & ...$variadicValue) /** * */ - public function returnsSelf() : self + public function returnsSelf(): self { return $this; } @@ -78,7 +78,7 @@ public function returnsSelf() : self /** * */ - public function returnsType() : \Magento\Framework\Something + public function returnsType(): \Magento\Framework\Something { return null; } From 82dc39f97b61e3a5c188943320b37fe6e0e519f9 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Mon, 27 May 2019 12:43:05 +0200 Subject: [PATCH 14/52] coding standard fixes --- .../Generator/CompiledInterceptor.php | 16 +- .../Generator/FileCache.php | 3 + .../Generator/NoSerialize.php | 5 +- .../Generator/StaticScope.php | 16 +- .../CompiledInterceptorTest.php | 111 +++++++++ .../_out_interceptors/ComplexItem.txt | 193 ++++++++++++++++ .../_out_interceptors/ComplexItemTyped.txt | 215 ++++++++++++++++++ .../_out_interceptors/Item.txt | 93 ++++++++ .../CompiledPluginListTest.php | 30 +-- .../Test/Unit/_files/reader_mock_map.php | 38 ++-- 10 files changed, 681 insertions(+), 39 deletions(-) create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index dc31546b04f72..c704af017de62 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -17,8 +17,7 @@ use Magento\Framework\App\AreaList; /** - * compiled interceptors generator - * please see ../README.md for details + * Compiled interceptors generator, please see ../README.md for details */ class CompiledInterceptor extends EntityAbstract { @@ -43,8 +42,8 @@ class CompiledInterceptor extends EntityAbstract /** * CompiledInterceptor constructor. * @param AreaList $areaList - * @param null $sourceClassName - * @param null $resultClassName + * @param null|string $sourceClassName + * @param null|string $resultClassName * @param Io|null $ioObject * @param CodeGeneratorInterface|null $classGenerator * @param DefinedClasses|null $definedClasses @@ -516,8 +515,13 @@ private function getPluginsChain(CompiledPluginList $plugins, $className, $metho } } if (isset($result[DefinitionInterface::LISTENER_AROUND])) { - $result[DefinitionInterface::LISTENER_AROUND] = $this->getPluginInfo($plugins, $result[DefinitionInterface::LISTENER_AROUND], $className, $allPlugins, - $this->getPluginsChain($plugins, $className, $method, $allPlugins, $result[DefinitionInterface::LISTENER_AROUND])); + $result[DefinitionInterface::LISTENER_AROUND] = $this->getPluginInfo( + $plugins, + $result[DefinitionInterface::LISTENER_AROUND], + $className, + $allPlugins, + $this->getPluginsChain($plugins, $className, $method, $allPlugins, $result[DefinitionInterface::LISTENER_AROUND]) + ); } return $result; } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php index 6dba513c4d487..0297ea31ff5e2 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php @@ -8,6 +8,9 @@ use Magento\Framework\Config\CacheInterface; +/** + * Class FileCache + */ class FileCache implements CacheInterface { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/NoSerialize.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/NoSerialize.php index 6f8247e55e4dc..bee3e2804ecc1 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/NoSerialize.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/NoSerialize.php @@ -7,6 +7,9 @@ use Magento\Framework\Serialize\SerializerInterface; +/** + * Class NoSerialize + */ class NoSerialize implements SerializerInterface { /** @@ -34,4 +37,4 @@ public function unserialize($string) { return $string; } -} \ No newline at end of file +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php index 037fc2bdddb60..b63c1cb3d2822 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php @@ -7,6 +7,9 @@ use Magento\Framework\Config\ScopeInterface; +/** + * Class StaticScope + */ class StaticScope implements ScopeInterface { /** @@ -14,6 +17,10 @@ class StaticScope implements ScopeInterface */ protected $scope; + /** + * StaticScope constructor. + * @param $scope + */ public function __construct($scope) { $this->scope = $scope; @@ -24,17 +31,20 @@ public function __construct($scope) * * @return string */ - public function getCurrentScope() { + public function getCurrentScope() + { return $this->scope; } /** + * * @param string $scope * @throws \Exception * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ - public function setCurrentScope($scope){ + public function setCurrentScope($scope) + { throw new \Exception('readonly'); } -} \ No newline at end of file +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php new file mode 100644 index 0000000000000..2202bf8eec8a1 --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php @@ -0,0 +1,111 @@ +ioGenerator = $this->getMockBuilder(Io::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->areaList = $this->getMockBuilder(AreaList::class) + ->disableOriginalConstructor() + ->getMock(); + } + + /** + * Checks a test case when interceptor generates code for the specified class. + * + * @param string $className + * @param string $resultClassName + * @param string $fileName + * @dataProvider interceptorDataProvider + */ + public function testGenerate($className, $resultClassName, $fileName) + { + /** @var CompiledInterceptor|MockObject $interceptor */ + $interceptor = $this->getMockBuilder(CompiledInterceptor::class) + ->setMethods(['_validateData']) + ->setConstructorArgs( + [ + $this->areaList, + $className, + $resultClassName, + $this->ioGenerator, + null, + null, + (new CompiledPluginListTest())->createScopeReaders() + ] + ) + ->getMock(); + + $this->ioGenerator->method('generateResultFileName')->with('\\' . $resultClassName) + ->willReturn($fileName . '.php'); + + $code = file_get_contents(__DIR__ . '/_out_interceptors/' . $fileName . '.txt'); + + $this->ioGenerator->method('writeResultFile')->with($fileName . '.php', $code); + $interceptor->method('_validateData')->willReturn(true); + + $generated = $interceptor->generate(); + $this->assertEquals($fileName . '.php', $generated, 'Generated interceptor is invalid.'); + } + + /** + * Gets list of interceptor samples. + * + * @return array + */ + public function interceptorDataProvider() + { + return [ + [ + Item::class, + Item::class . '\Interceptor', + 'Item' + ], + [ + ComplexItem::class, + ComplexItem::class . '\Interceptor', + 'ComplexItem' + ], + [ + ComplexItemTyped::class, + ComplexItemTyped::class . '\Interceptor', + 'ComplexItemTyped' + ], + ]; + } +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt new file mode 100644 index 0000000000000..44b4695fcb7c9 --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt @@ -0,0 +1,193 @@ +namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem; + +use Magento\Framework\Config\ScopeInterface; +use Magento\Framework\ObjectManagerInterface; + +/** + * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem + */ +class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem +{ + /** + * @var ScopeInterface + */ + private $____scope = null; + + /** + * @var ObjectManagerInterface + */ + private $____om = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + */ + private $____plugin_advanced_plugin = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + */ + private $____plugin_complex_plugin = null; + + /** + * {@inheritdoc} + */ + public function __construct() + { + $____om = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\ObjectManagerInterface::class); + //TODO fix di in production mode + $____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); + //TODO fix di in production mode + $this->____om = $____om; + $this->____scope = $____scope; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + switch ($this->____scope->getCurrentScope()) { + case 'global': + case 'frontend': + case 'emptyscope': + $this->____plugin_advanced_plugin()->beforeGetName($this); + + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ + return parent::getName(); + }); + + return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + case 'backend': + $this->____plugin_advanced_plugin()->beforeGetName($this); + + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ + $result = $this->____plugin_complex_plugin()->aroundGetName($this, function(){ + return parent::getName(); + }); + + return (($tmp = $this->____plugin_complex_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + }); + + return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + default: + return parent::getName(); + } + } + + /** + * {@inheritdoc} + */ + public function setValue($value) + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + $beforeResult = $this->____plugin_complex_plugin()->beforeSetValue($this, $value); + if ($beforeResult !== null) list($value) = (array)$beforeResult; + + return $this->____plugin_complex_plugin()->aroundSetValue($this, function($value){ + return parent::setValue($value); + }, $value); + default: + return parent::setValue($value); + } + } + + /** + * {@inheritdoc} + */ + public function & getReference() + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + return $this->____plugin_complex_plugin()->aroundGetReference($this, function(){ + return parent::getReference(); + }); + default: + return parent::getReference(); + } + } + + /** + * {@inheritdoc} + */ + public function firstVariadicParameter(... $variadicValue) + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + return $this->____plugin_complex_plugin()->aroundFirstVariadicParameter($this, function($variadicValue){ + return parent::firstVariadicParameter($variadicValue); + }, $variadicValue); + default: + return parent::firstVariadicParameter($variadicValue); + } + } + + /** + * {@inheritdoc} + */ + public function secondVariadicParameter($value, ... $variadicValue) + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + return $this->____plugin_complex_plugin()->aroundSecondVariadicParameter($this, function($value, $variadicValue){ + return parent::secondVariadicParameter($value, $variadicValue); + }, $value, $variadicValue); + default: + return parent::secondVariadicParameter($value, $variadicValue); + } + } + + /** + * {@inheritdoc} + */ + public function byRefVariadic(&... $variadicValue) + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + return $this->____plugin_complex_plugin()->aroundByRefVariadic($this, function(&$variadicValue){ + return parent::byRefVariadic($variadicValue); + }, $variadicValue); + default: + return parent::byRefVariadic($variadicValue); + } + } + + /** + * {@inheritdoc} + */ + public function returnsSelf() + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + $this->____plugin_complex_plugin()->beforeReturnsSelf($this); + + return parent::returnsSelf(); + default: + return parent::returnsSelf(); + } + } + + /** + * plugin "advanced_plugin" + * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + */ + private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + { + if ($this->____plugin_advanced_plugin === null) { + $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + } + return $this->____plugin_advanced_plugin; + } + + /** + * plugin "complex_plugin" + * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + */ + private function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + { + if ($this->____plugin_complex_plugin === null) { + $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); + } + return $this->____plugin_complex_plugin; + } +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt new file mode 100644 index 0000000000000..d5c195994cd3d --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -0,0 +1,215 @@ +namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped; + +use Magento\Framework\Config\ScopeInterface; +use Magento\Framework\ObjectManagerInterface; + +/** + * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped + */ +class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped +{ + /** + * @var ScopeInterface + */ + private $____scope = null; + + /** + * @var ObjectManagerInterface + */ + private $____om = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + */ + private $____plugin_complex_plugin = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + */ + private $____plugin_advanced_plugin = null; + + /** + * {@inheritdoc} + */ + public function __construct() + { + $____om = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\ObjectManagerInterface::class); + //TODO fix di in production mode + $____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); + //TODO fix di in production mode + $this->____om = $____om; + $this->____scope = $____scope; + } + + /** + * {@inheritdoc} + */ + public function returnVoid() : void + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + parent::returnVoid(); + + $this->____plugin_complex_plugin()->afterReturnVoid($this, null); + break; + default: + parent::returnVoid(); + break; + } + } + + /** + * {@inheritdoc} + */ + public function getNullableValue() : ?string + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + $result = parent::getNullableValue(); + + return (($tmp = $this->____plugin_complex_plugin()->afterGetNullableValue($this, $result)) !== null) ? $tmp : $result; + default: + return parent::getNullableValue(); + } + } + + /** + * {@inheritdoc} + */ + public function getName() : string + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + $this->____plugin_advanced_plugin()->beforeGetName($this); + + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ + $result = $this->____plugin_complex_plugin()->aroundGetName($this, function(){ + return parent::getName(); + }); + + return (($tmp = $this->____plugin_complex_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + }); + + return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + default: + return parent::getName(); + } + } + + /** + * {@inheritdoc} + */ + public function setValue(string $value) + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + $beforeResult = $this->____plugin_complex_plugin()->beforeSetValue($this, $value); + if ($beforeResult !== null) list($value) = (array)$beforeResult; + + return $this->____plugin_complex_plugin()->aroundSetValue($this, function($value){ + return parent::setValue($value); + }, $value); + default: + return parent::setValue($value); + } + } + + /** + * {@inheritdoc} + */ + public function firstVariadicParameter(string ... $variadicValue) + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + return $this->____plugin_complex_plugin()->aroundFirstVariadicParameter($this, function($variadicValue){ + return parent::firstVariadicParameter($variadicValue); + }, $variadicValue); + default: + return parent::firstVariadicParameter($variadicValue); + } + } + + /** + * {@inheritdoc} + */ + public function secondVariadicParameter(string $value, string ... $variadicValue) + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + return $this->____plugin_complex_plugin()->aroundSecondVariadicParameter($this, function($value, $variadicValue){ + return parent::secondVariadicParameter($value, $variadicValue); + }, $value, $variadicValue); + default: + return parent::secondVariadicParameter($value, $variadicValue); + } + } + + /** + * {@inheritdoc} + */ + public function byRefVariadic(string &... $variadicValue) + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + return $this->____plugin_complex_plugin()->aroundByRefVariadic($this, function(&$variadicValue){ + return parent::byRefVariadic($variadicValue); + }, $variadicValue); + default: + return parent::byRefVariadic($variadicValue); + } + } + + /** + * {@inheritdoc} + */ + public function returnsSelf() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + $this->____plugin_complex_plugin()->beforeReturnsSelf($this); + + return parent::returnsSelf(); + default: + return parent::returnsSelf(); + } + } + + /** + * {@inheritdoc} + */ + public function returnsType() : \Magento\Framework\Something + { + switch ($this->____scope->getCurrentScope()) { + case 'backend': + $this->____plugin_complex_plugin()->beforeReturnsType($this); + + return parent::returnsType(); + default: + return parent::returnsType(); + } + } + + /** + * plugin "complex_plugin" + * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + */ + private function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + { + if ($this->____plugin_complex_plugin === null) { + $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); + } + return $this->____plugin_complex_plugin; + } + + /** + * plugin "advanced_plugin" + * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + */ + private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + { + if ($this->____plugin_advanced_plugin === null) { + $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + } + return $this->____plugin_advanced_plugin; + } +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt new file mode 100644 index 0000000000000..abd42aa5b8e36 --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt @@ -0,0 +1,93 @@ +namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; + +use Magento\Framework\Config\ScopeInterface; +use Magento\Framework\ObjectManagerInterface; + +/** + * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item + */ +class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item +{ + /** + * @var ScopeInterface + */ + private $____scope = null; + + /** + * @var ObjectManagerInterface + */ + private $____om = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple + */ + private $____plugin_simple_plugin = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + */ + private $____plugin_advanced_plugin = null; + + /** + * {@inheritdoc} + */ + public function __construct() + { + $____om = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\ObjectManagerInterface::class); + //TODO fix di in production mode + $____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); + //TODO fix di in production mode + $this->____om = $____om; + $this->____scope = $____scope; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + switch ($this->____scope->getCurrentScope()) { + case 'global': + case 'emptyscope': + $result = parent::getName(); + + return (($tmp = $this->____plugin_simple_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + case 'backend': + $this->____plugin_advanced_plugin()->beforeGetName($this); + + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ + $result = parent::getName(); + + return (($tmp = $this->____plugin_simple_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + }); + + return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + default: + return parent::getName(); + } + } + + /** + * plugin "simple_plugin" + * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple + */ + private function ____plugin_simple_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple + { + if ($this->____plugin_simple_plugin === null) { + $this->____plugin_simple_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class); + } + return $this->____plugin_simple_plugin; + } + + /** + * plugin "advanced_plugin" + * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + */ + private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + { + if ($this->____plugin_advanced_plugin === null) { + $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + } + return $this->____plugin_advanced_plugin; + } +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php index 109ec517e8d17..ff63413df707a 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php @@ -7,7 +7,10 @@ use Magento\Framework\App\ObjectManager; use Magento\Framework\CompiledInterception\Generator\CompiledPluginList; -use Magento\Framework\ObjectManagerInterface; +use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; +use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item\Enhanced; +use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced; +use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple; use Psr\Log\NullLogger; require_once __DIR__ . '/../Custom/Module/Model/Item.php'; @@ -46,7 +49,7 @@ public function createScopeReaders() $omConfigMock->expects($this->any())->method('getOriginalInstanceType')->will($this->returnArgument(0)); $ret = []; $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - foreach($readerMap as $readerLine) { + foreach ($readerMap as $readerLine) { $ret[$readerLine[0]] = $objectManagerHelper->getObject( CompiledPluginList::class, [ @@ -64,18 +67,18 @@ public function createScopeReaders() public function testGetPlugin() { - $this->objects['backend']->getNext(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, 'getName'); + $this->objects['backend']->getNext(Item::class, 'getName'); $this->assertEquals( - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class, + Simple::class, $this->objects['backend']->getPluginType( - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, + Item::class, 'simple_plugin' ) ); $this->assertEquals( - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class, + Advanced::class, $this->objects['backend']->getPluginType( - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, + Item::class, 'advanced_plugin' ) ); @@ -101,39 +104,39 @@ public function getPluginsDataProvider() { return [ [ - [4 => ['simple_plugin']], \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, + [4 => ['simple_plugin']], Item::class, 'getName', 'global', ], [ // advanced plugin has lower sort order [2 => 'advanced_plugin', 4 => ['advanced_plugin'], 1 => ['advanced_plugin']], - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, + Item::class, 'getName', 'backend' ], [ // advanced plugin has lower sort order [4 => ['simple_plugin']], - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, + Item::class, 'getName', 'backend', 'advanced_plugin' ], // simple plugin is disabled in configuration for // \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item in frontend - [null, \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, 'getName', 'frontend'], + [null, Item::class, 'getName', 'frontend'], // test plugin inheritance [ [4 => ['simple_plugin']], - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item\Enhanced::class, + Enhanced::class, 'getName', 'global' ], [ // simple plugin is disabled in configuration for parent [2 => 'advanced_plugin', 4 => ['advanced_plugin'], 1 => ['advanced_plugin']], - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item\Enhanced::class, + Enhanced::class, 'getName', 'frontend' ] @@ -158,5 +161,4 @@ public function testInheritPluginsWithNotExistingPlugin() { $this->assertNull($this->objects['frontend']->getNext('typeWithoutInstance', 'someMethod')); } - } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php index dddcc8eaa6b93..010ba48920cc1 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php @@ -4,25 +4,33 @@ * See COPYING.txt for license details. */ +use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem; +use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped; +use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; +use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item\Enhanced; +use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced; +use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex; +use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple; + return [ [ 'global', [ - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class => [ + Item::class => [ 'plugins' => [ 'simple_plugin' => [ 'sortOrder' => 10, 'instance' => - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class, + Simple::class, ], ], ], - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem::class => [ + ComplexItem::class => [ 'plugins' => [ 'advanced_plugin' => [ 'sortOrder' => 5, 'instance' => - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class, + Advanced::class, ], ], ], @@ -31,40 +39,40 @@ [ 'backend', [ - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class => [ + Item::class => [ 'plugins' => [ 'advanced_plugin' => [ 'sortOrder' => 5, 'instance' => - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class, + Advanced::class, ], ], ], - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem::class => [ + ComplexItem::class => [ 'plugins' => [ 'complex_plugin' => [ 'sortOrder' => 15, 'instance' => - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class, + Complex::class, ], 'advanced_plugin' => [ 'sortOrder' => 5, 'instance' => - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class, + Advanced::class, ], ], ], - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped::class => [ + ComplexItemTyped::class => [ 'plugins' => [ 'complex_plugin' => [ 'sortOrder' => 25, 'instance' => - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class, + Complex::class, ], 'advanced_plugin' => [ 'sortOrder' => 5, 'instance' => - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class, + Advanced::class, ], ], ], @@ -72,14 +80,14 @@ ], [ 'frontend', - [\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class => [ + [Item::class => [ 'plugins' => ['simple_plugin' => ['disabled' => true]], - ], \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item\Enhanced::class => [ + ], Enhanced::class => [ 'plugins' => [ 'advanced_plugin' => [ 'sortOrder' => 5, 'instance' => - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class, + Advanced::class, ], ], ], From 41ca3b0631575be16d55c8d0fe38ea8bc0e8f36a Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Mon, 27 May 2019 15:31:21 +0200 Subject: [PATCH 15/52] coding standard fixes --- .../Generator/CompiledInterceptor.php | 235 ++++++++++++++---- .../Generator/CompiledPluginList.php | 25 +- .../Generator/FileCache.php | 20 +- .../Generator/StaticScope.php | 5 +- .../CompiledInterceptorTest.php | 103 -------- .../_out_interceptors/ComplexItem.txt | 193 -------------- .../_out_interceptors/ComplexItemTyped.txt | 215 ---------------- .../_out_interceptors/Item.txt | 93 ------- 8 files changed, 214 insertions(+), 675 deletions(-) delete mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php delete mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt delete mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt delete mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index c704af017de62..b6bfa7a53308b 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -47,7 +47,7 @@ class CompiledInterceptor extends EntityAbstract * @param Io|null $ioObject * @param CodeGeneratorInterface|null $classGenerator * @param DefinedClasses|null $definedClasses - * @param null $plugins + * @param null|array $plugins */ public function __construct( AreaList $areaList, @@ -57,18 +57,23 @@ public function __construct( CodeGeneratorInterface $classGenerator = null, DefinedClasses $definedClasses = null, $plugins = null - ){ - parent::__construct($sourceClassName, - $resultClassName , + ) { + parent::__construct( + $sourceClassName, + $resultClassName, $ioObject, $classGenerator, - $definedClasses); + $definedClasses + ); $this->areaList = $areaList; $this->plugins = $plugins; } /** + * Unused function required by production mode interface + * + * @param mixed $interceptedMethods * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function setInterceptedMethods($interceptedMethods) @@ -77,6 +82,8 @@ public function setInterceptedMethods($interceptedMethods) } /** + * Get all class methods + * * @return array|null * @throws \ReflectionException */ @@ -87,6 +94,8 @@ protected function _getClassMethods() } /** + * Get all class properties + * * @return array|null * @throws \ReflectionException */ @@ -97,14 +106,17 @@ protected function _getClassProperties() } /** + * Unused, required by interface + * * @return array|void */ protected function _getDefaultConstructorDefinition() { - } /** + * Generate class source + * * @return bool|string * @throws \ReflectionException */ @@ -120,6 +132,8 @@ protected function _generateCode() } /** + * Generate all methods and properties + * * @throws \ReflectionException */ private function generateMethodsAndProperties() @@ -140,6 +154,8 @@ private function generateMethodsAndProperties() } /** + * Get reflection of source class + * * @return \ReflectionClass * @throws \ReflectionException */ @@ -163,6 +179,11 @@ protected function isInterceptedMethod(\ReflectionMethod $method) !in_array($method->getName(), ['__sleep', '__wakeup', '__clone']); } + /** + * Generate compiled methods and plugin getters + * + * @param \ReflectionClass $reflection + */ private function overrideMethodsAndGeneratePluginGetters(\ReflectionClass $reflection) { $publicMethods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); @@ -184,6 +205,12 @@ private function overrideMethodsAndGeneratePluginGetters(\ReflectionClass $refle } } + /** + * Generate class constructor adding required properties + * + * @param \ReflectionMethod|null $parentConstructor + * @param array $properties + */ private function injectPropertiesSettersToConstructor(\ReflectionMethod $parentConstructor = null, $properties = []) { if ($parentConstructor == null) { @@ -217,7 +244,7 @@ private function injectPropertiesSettersToConstructor(\ReflectionMethod $parentC } } } - $parameters = array_map(array($this, '_getMethodParameterInfo'), $parameters); + $parameters = array_map([$this, '_getMethodParameterInfo'], $parameters); /* foreach ($extraParams as $type => $name) { array_unshift($parameters, [ 'name' => $name, @@ -229,7 +256,10 @@ private function injectPropertiesSettersToConstructor(\ReflectionMethod $parentC } foreach ($extraParams as $type => $name) { array_unshift($body, "//TODO fix di in production mode"); - array_unshift($body, "\$$name = \\Magento\\Framework\\App\\ObjectManager::getInstance()->get(\\$type::class);"); + array_unshift( + $body, + "\$$name = \\Magento\\Framework\\App\\ObjectManager::getInstance()->get(\\$type::class);" + ); } $this->classMethods[] = [ @@ -238,9 +268,15 @@ private function injectPropertiesSettersToConstructor(\ReflectionMethod $parentC 'body' => implode("\n", $body), 'docblock' => ['shortDescription' => '{@inheritdoc}'], ]; - } + /** + * Adds tabulation to nested code block + * + * @param array $body + * @param array $sub + * @param int $indent + */ private function addCodeSubBlock(&$body, $sub, $indent = 1) { foreach ($sub as $line) { @@ -249,10 +285,12 @@ private function addCodeSubBlock(&$body, $sub, $indent = 1) } /** - * @param $plugins - * @param $methodName - * @param $extraParams - * @param $parametersList + * Generate source of before plugins + * + * @param array $plugins + * @param string $methodName + * @param string $extraParams + * @param string $parametersList * @return array */ private function compileBeforePlugins($plugins, $methodName, $extraParams, $parametersList) @@ -273,47 +311,60 @@ private function compileBeforePlugins($plugins, $methodName, $extraParams, $para } /** - * @param $methodName - * @param $plugin - * @param $capitalizedName - * @param $extraParams - * @param $parameters - * @param $returnVoid + * Generate source of around plugin + * + * @param string $methodName + * @param array $plugin + * @param string $capitalizedName + * @param string $extraParams + * @param array $parameters + * @param bool $returnVoid * @return array */ private function compileAroundPlugin($methodName, $plugin, $capitalizedName, $extraParams, $parameters, $returnVoid) { $lines = []; - $lines[] = "\$this->" . $this->getGetterName($plugin) . "()->around$capitalizedName(\$this, function({$this->getParameterListForNextCallback($parameters)}){"; - $this->addCodeSubBlock($lines, $this->getMethodSourceFromConfig($methodName, $plugin['next'] ?: [], $parameters, $returnVoid)); + $lines[] = "\$this->{$this->getGetterName($plugin)}()->around$capitalizedName" . + "(\$this, function({$this->getParameterListForNextCallback($parameters)}){"; + $this->addCodeSubBlock( + $lines, + $this->getMethodSourceFromConfig($methodName, $plugin['next'] ?: [], $parameters, $returnVoid) + ); $lines[] = "}$extraParams);"; return $lines; } /** - * @param $plugins - * @param $methodName - * @param $extraParams - * @param $returnVoid + * Generate source of after plugins + * + * @param array $plugins + * @param string $methodName + * @param string $extraParams + * @param bool $returnVoid * @return array */ private function compileAfterPlugins($plugins, $methodName, $extraParams, $returnVoid) { $lines = []; foreach ($plugins as $plugin) { + $call = "\$this->" . $this->getGetterName($plugin) . "()->$methodName(\$this, "; + if (!$returnVoid) { - $lines[] = ["((\$tmp = \$this->" . $this->getGetterName($plugin) . "()->$methodName(\$this, \$result$extraParams)) !== null) ? \$tmp : \$result;"]; + $lines[] = ["((\$tmp = $call\$result$extraParams)) !== null) ? \$tmp : \$result;"]; } else { - $lines[] = ["\$this->" . $this->getGetterName($plugin) . "()->$methodName(\$this, null$extraParams);"]; + $lines[] = ["{$call}null$extraParams);"]; } } return $lines; } /** - * @param \ReflectionMethod $method - * @param $conf - * @param $parameters + * Generate interceptor source using config + * + * @param string $methodName + * @param array $conf + * @param array $parameters + * @param bool $returnVoid * @return array */ private function getMethodSourceFromConfig($methodName, $conf, $parameters, $returnVoid) @@ -324,23 +375,44 @@ private function getMethodSourceFromConfig($methodName, $conf, $parameters, $ret $extraParams = empty($parameters) ? '' : (', ' . $parametersList); if (isset($conf[DefinitionInterface::LISTENER_BEFORE])) { - $body = $this->compileBeforePlugins($conf[DefinitionInterface::LISTENER_BEFORE], 'before' . $capitalizedName, $extraParams, $parametersList); + $body = $this->compileBeforePlugins( + $conf[DefinitionInterface::LISTENER_BEFORE], + 'before' . $capitalizedName, + $extraParams, + $parametersList + ); } else { $body = []; } $resultChain = []; if (isset($conf[DefinitionInterface::LISTENER_AROUND])) { - $resultChain[] = $this->compileAroundPlugin($methodName, $conf[DefinitionInterface::LISTENER_AROUND], $capitalizedName, $extraParams, $parameters, $returnVoid); + $resultChain[] = $this->compileAroundPlugin( + $methodName, + $conf[DefinitionInterface::LISTENER_AROUND], + $capitalizedName, + $extraParams, + $parameters, + $returnVoid + ); } else { $resultChain[] = ["parent::{$methodName}({$this->getParameterList($parameters)});"]; } if (isset($conf[DefinitionInterface::LISTENER_AFTER])) { - $resultChain = array_merge($resultChain, $this->compileAfterPlugins($conf[DefinitionInterface::LISTENER_AFTER], 'after' . $capitalizedName, $extraParams, $returnVoid)); + $resultChain = array_merge($resultChain, $this->compileAfterPlugins( + $conf[DefinitionInterface::LISTENER_AFTER], + 'after' . $capitalizedName, + $extraParams, + $returnVoid + )); } foreach ($resultChain as $lp => $piece) { - if ($first) $first = false; else $body[] = ""; + if ($first) { + $first = false; + } else { + $body[] = ""; + } if (!$returnVoid) { $piece[0] = (($lp + 1 == count($resultChain)) ? "return " : "\$result = ") . $piece[0]; } @@ -352,6 +424,8 @@ private function getMethodSourceFromConfig($methodName, $conf, $parameters, $ret } /** + * Get parameters definition for next callback + * * @param array $parameters * @return string */ @@ -372,7 +446,9 @@ private function getParameterListForNextCallback(array $parameters) } /** - * @param \ReflectionParameter[] $parameters + * Implodes parameters into list for call + * + * @param array(\ReflectionParameter) $parameters * @return string */ private function getParameterList(array $parameters) @@ -385,7 +461,9 @@ private function getParameterList(array $parameters) } /** - * @param $plugin + * Get plugin getter name + * + * @param array $plugin * @return string */ private function getGetterName($plugin) @@ -394,7 +472,9 @@ private function getGetterName($plugin) } /** - * @param $plugin + * Get plugin property cache attribute + * + * @param array $plugin * @return array */ private function getPluginPropertyInfo($plugin) @@ -409,7 +489,9 @@ private function getPluginPropertyInfo($plugin) } /** - * @param $plugin + * Prepares plugin getter for code generator + * + * @param array $plugin * @return array */ private function getPluginGetterInfo($plugin) @@ -435,8 +517,10 @@ private function getPluginGetterInfo($plugin) } /** + * Get compiled method data for code generator + * * @param \ReflectionMethod $method - * @param $config + * @param array $config * @return array */ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) @@ -452,7 +536,9 @@ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) //group cases by config foreach ($config as $scope => $conf) { $key = md5(serialize($conf)); - if (!isset($cases[$key])) $cases[$key] = ['cases'=>[], 'conf'=>$conf]; + if (!isset($cases[$key])) { + $cases[$key] = ['cases'=>[], 'conf'=>$conf]; + } $cases[$key]['cases'][] = "\tcase '$scope':"; } //call parent method for scopes with no plugins (or when no scope is set) @@ -460,7 +546,11 @@ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) foreach ($cases as $case) { $body = array_merge($body, $case['cases']); - $this->addCodeSubBlock($body, $this->getMethodSourceFromConfig($method->getName(), $case['conf'], $parameters, $returnsVoid), 2); + $this->addCodeSubBlock( + $body, + $this->getMethodSourceFromConfig($method->getName(), $case['conf'], $parameters, $returnsVoid), + 2 + ); if ($returnsVoid) { $body[] = "\t\tbreak;"; } @@ -476,17 +566,29 @@ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) } return [ 'name' => ($method->returnsReference() ? '& ' : '') . $method->getName(), - 'parameters' =>array_map(array($this, '_getMethodParameterInfo'), $parameters), + 'parameters' =>array_map([$this, '_getMethodParameterInfo'], $parameters), 'body' => implode("\n", $body), 'returnType' => $returnTypeValue, 'docblock' => ['shortDescription' => '{@inheritdoc}'], ]; } + /** + * Generate array with plugin info + * + * @param CompiledPluginList $plugins + * @param string $code + * @param string $className + * @param array $allPlugins + * @param null|string $next + * @return mixed + */ private function getPluginInfo(CompiledPluginList $plugins, $code, $className, &$allPlugins, $next = null) { $className = $plugins->getPluginType($className, $code); - if (!isset($allPlugins[$code])) $allPlugins[$code] = []; + if (!isset($allPlugins[$code])) { + $allPlugins[$code] = []; + } if (empty($allPlugins[$code][$className])) { $suffix = count($allPlugins[$code]) ? count($allPlugins[$code]) + 1 : ''; $allPlugins[$code][$className] = [ @@ -498,20 +600,39 @@ private function getPluginInfo(CompiledPluginList $plugins, $code, $className, & $result = $allPlugins[$code][$className]; $result['next'] = $next; return $result; - } + /** + * Get next set of plugins + * + * @param CompiledPluginList $plugins + * @param string $className + * @param string $method + * @param array $allPlugins + * @param string $next + * @return array + */ private function getPluginsChain(CompiledPluginList $plugins, $className, $method, &$allPlugins, $next = '__self') { $result = $plugins->getNext($className, $method, $next); - if(!empty($result[DefinitionInterface::LISTENER_BEFORE])) { + if (!empty($result[DefinitionInterface::LISTENER_BEFORE])) { foreach ($result[DefinitionInterface::LISTENER_BEFORE] as $k => $code) { - $result[DefinitionInterface::LISTENER_BEFORE][$k] = $this->getPluginInfo($plugins, $code, $className, $allPlugins); + $result[DefinitionInterface::LISTENER_BEFORE][$k] = $this->getPluginInfo( + $plugins, + $code, + $className, + $allPlugins + ); } } - if(!empty($result[DefinitionInterface::LISTENER_AFTER])) { + if (!empty($result[DefinitionInterface::LISTENER_AFTER])) { foreach ($result[DefinitionInterface::LISTENER_AFTER] as $k => $code) { - $result[DefinitionInterface::LISTENER_AFTER][$k] = $this->getPluginInfo($plugins, $code, $className, $allPlugins); + $result[DefinitionInterface::LISTENER_AFTER][$k] = $this->getPluginInfo( + $plugins, + $code, + $className, + $allPlugins + ); } } if (isset($result[DefinitionInterface::LISTENER_AROUND])) { @@ -520,17 +641,30 @@ private function getPluginsChain(CompiledPluginList $plugins, $className, $metho $result[DefinitionInterface::LISTENER_AROUND], $className, $allPlugins, - $this->getPluginsChain($plugins, $className, $method, $allPlugins, $result[DefinitionInterface::LISTENER_AROUND]) + $this->getPluginsChain( + $plugins, + $className, + $method, + $allPlugins, + $result[DefinitionInterface::LISTENER_AROUND] + ) ); } return $result; } + /** + * Generates recursive maps of plugins for given method + * + * @param \ReflectionMethod $method + * @param array $allPlugins + * @return array + */ private function getPluginsConfig(\ReflectionMethod $method, &$allPlugins) { $className = ltrim($this->getSourceClassName(), '\\'); - $result = array(); + $result = []; if ($this->plugins === null) { $this->plugins = []; foreach ($this->areaList->getCodes() as $scope) { @@ -542,7 +676,6 @@ private function getPluginsConfig(\ReflectionMethod $method, &$allPlugins) if ($pluginChain) { $result[$scope] = $pluginChain; } - } return $result; } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php index a8cebd2a563ee..dff64bc12382e 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php @@ -6,6 +6,7 @@ namespace Magento\Framework\CompiledInterception\Generator; use Magento\Framework\App\ObjectManager; +use Magento\Framework\Config\ReaderInterface; use Magento\Framework\Interception\PluginList\PluginList; use Magento\Framework\Interception\ObjectManager\ConfigInterface; use Magento\Framework\ObjectManager\Config\Reader\Dom; @@ -13,21 +14,24 @@ use Magento\Framework\Interception\Definition\Runtime as InterceptionDefinitionRuntime; use Magento\Framework\ObjectManager\Definition\Runtime as ObjectManagerDefinitionRuntime; +/** + * Class CompiledPluginList + */ class CompiledPluginList extends PluginList { /** * CompiledPluginList constructor. - * @param $objectManager ObjectManager - * @param $scope - * @param null $reader - * @param null $omConfig - * @param null $cachePath + * @param ObjectManager $objectManager + * @param string $scope + * @param null|ReaderInterface $reader + * @param null|ConfigInterface $omConfig + * @param null|string $cachePath */ public function __construct( $objectManager, $scope, - $reader = null, - $omConfig = null, + ReaderInterface $reader = null, + ConfigInterface $omConfig = null, $cachePath = null ) { if (!$reader || !$omConfig) { @@ -63,13 +67,14 @@ public function getPlugin($type, $code) } /** - * @param $type - * @param $code + * Get class of a plugin + * + * @param string $type + * @param string $code * @return mixed */ public function getPluginType($type, $code) { return $this->_inherited[$type][$code]['instance']; } - } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php index 0297ea31ff5e2..95a315d887ff0 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php @@ -5,7 +5,6 @@ */ namespace Magento\Framework\CompiledInterception\Generator; - use Magento\Framework\Config\CacheInterface; /** @@ -18,21 +17,27 @@ class FileCache implements CacheInterface /** * FileCache constructor. - * @param null $cachePath + * @param null|string $cachePath */ public function __construct($cachePath = null) { - $this->cachePath = ($cachePath === null ? BP . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'cache' : $cachePath); + if ($cachePath === null) { + $this->cachePath = BP . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'cache'; + } else { + $this->cachePath = $cachePath; + } } - /** - * @param $identifier + * Get cache path + * + * @param string $identifier * @return string */ private function getCachePath($identifier) { - return $this->cachePath . DIRECTORY_SEPARATOR . str_replace('|', '_', $identifier . '.php'); + $identifier = str_replace('|', '_', $identifier); + return $this->cachePath . DIRECTORY_SEPARATOR . $identifier . '.php'; } /** @@ -127,5 +132,4 @@ public function getLowLevelFrontend() { // TODO: Implement getLowLevelFrontend() method. } - -} \ No newline at end of file +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php index b63c1cb3d2822..e0cf887266d43 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php @@ -19,7 +19,8 @@ class StaticScope implements ScopeInterface /** * StaticScope constructor. - * @param $scope + * + * @param string $scope */ public function __construct($scope) { @@ -37,6 +38,7 @@ public function getCurrentScope() } /** + * Unused interface method * * @param string $scope * @throws \Exception @@ -46,5 +48,4 @@ public function setCurrentScope($scope) { throw new \Exception('readonly'); } - } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php deleted file mode 100644 index 2d06e97e30a00..0000000000000 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php +++ /dev/null @@ -1,103 +0,0 @@ -ioGenerator = $this->getMockBuilder(Io::class) - ->disableOriginalConstructor() - ->getMock(); - - - $this->areaList = $this->getMockBuilder(AreaList::class) - ->disableOriginalConstructor() - ->getMock(); - } - - /** - * Checks a test case when interceptor generates code for the specified class. - * - * @param string $className - * @param string $resultClassName - * @param string $fileName - * @dataProvider interceptorDataProvider - */ - public function testGenerate($className, $resultClassName, $fileName) - { - /** @var CompiledInterceptor|MockObject $interceptor */ - $interceptor = $this->getMockBuilder(CompiledInterceptor::class) - ->setMethods(['_validateData']) - ->setConstructorArgs([ - $this->areaList, - $className, - $resultClassName, - $this->ioGenerator, - null, - null, - (new CompiledPluginListTest())->createScopeReaders() - ]) - ->getMock(); - - $this->ioGenerator->method('generateResultFileName')->with('\\' . $resultClassName)->willReturn($fileName . '.php'); - - $code = file_get_contents(__DIR__ . '/_out_interceptors/' . $fileName . '.txt'); - - $this->ioGenerator->method('writeResultFile')->with($fileName . '.php', $code); - $interceptor->method('_validateData')->willReturn(true); - - $generated = $interceptor->generate(); - $this->assertEquals($fileName . '.php', $generated, 'Generated interceptor is invalid.'); - } - - /** - * Gets list of interceptor samples. - * - * @return array - */ - public function interceptorDataProvider() - { - return [ - [ - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item::class, - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item\Interceptor::class, - 'Item' - ], - [ - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem::class, - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem\Interceptor::class, - 'ComplexItem' - ], - [ - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped::class, - \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped\Interceptor::class, - 'ComplexItemTyped' - ], - ]; - } -} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt deleted file mode 100644 index 44b4695fcb7c9..0000000000000 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt +++ /dev/null @@ -1,193 +0,0 @@ -namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem; - -use Magento\Framework\Config\ScopeInterface; -use Magento\Framework\ObjectManagerInterface; - -/** - * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem - */ -class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem -{ - /** - * @var ScopeInterface - */ - private $____scope = null; - - /** - * @var ObjectManagerInterface - */ - private $____om = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced - */ - private $____plugin_advanced_plugin = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex - */ - private $____plugin_complex_plugin = null; - - /** - * {@inheritdoc} - */ - public function __construct() - { - $____om = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\ObjectManagerInterface::class); - //TODO fix di in production mode - $____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); - //TODO fix di in production mode - $this->____om = $____om; - $this->____scope = $____scope; - } - - /** - * {@inheritdoc} - */ - public function getName() - { - switch ($this->____scope->getCurrentScope()) { - case 'global': - case 'frontend': - case 'emptyscope': - $this->____plugin_advanced_plugin()->beforeGetName($this); - - $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ - return parent::getName(); - }); - - return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; - case 'backend': - $this->____plugin_advanced_plugin()->beforeGetName($this); - - $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ - $result = $this->____plugin_complex_plugin()->aroundGetName($this, function(){ - return parent::getName(); - }); - - return (($tmp = $this->____plugin_complex_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; - }); - - return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; - default: - return parent::getName(); - } - } - - /** - * {@inheritdoc} - */ - public function setValue($value) - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - $beforeResult = $this->____plugin_complex_plugin()->beforeSetValue($this, $value); - if ($beforeResult !== null) list($value) = (array)$beforeResult; - - return $this->____plugin_complex_plugin()->aroundSetValue($this, function($value){ - return parent::setValue($value); - }, $value); - default: - return parent::setValue($value); - } - } - - /** - * {@inheritdoc} - */ - public function & getReference() - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - return $this->____plugin_complex_plugin()->aroundGetReference($this, function(){ - return parent::getReference(); - }); - default: - return parent::getReference(); - } - } - - /** - * {@inheritdoc} - */ - public function firstVariadicParameter(... $variadicValue) - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - return $this->____plugin_complex_plugin()->aroundFirstVariadicParameter($this, function($variadicValue){ - return parent::firstVariadicParameter($variadicValue); - }, $variadicValue); - default: - return parent::firstVariadicParameter($variadicValue); - } - } - - /** - * {@inheritdoc} - */ - public function secondVariadicParameter($value, ... $variadicValue) - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - return $this->____plugin_complex_plugin()->aroundSecondVariadicParameter($this, function($value, $variadicValue){ - return parent::secondVariadicParameter($value, $variadicValue); - }, $value, $variadicValue); - default: - return parent::secondVariadicParameter($value, $variadicValue); - } - } - - /** - * {@inheritdoc} - */ - public function byRefVariadic(&... $variadicValue) - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - return $this->____plugin_complex_plugin()->aroundByRefVariadic($this, function(&$variadicValue){ - return parent::byRefVariadic($variadicValue); - }, $variadicValue); - default: - return parent::byRefVariadic($variadicValue); - } - } - - /** - * {@inheritdoc} - */ - public function returnsSelf() - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - $this->____plugin_complex_plugin()->beforeReturnsSelf($this); - - return parent::returnsSelf(); - default: - return parent::returnsSelf(); - } - } - - /** - * plugin "advanced_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced - */ - private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced - { - if ($this->____plugin_advanced_plugin === null) { - $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); - } - return $this->____plugin_advanced_plugin; - } - - /** - * plugin "complex_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex - */ - private function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex - { - if ($this->____plugin_complex_plugin === null) { - $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); - } - return $this->____plugin_complex_plugin; - } -} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt deleted file mode 100644 index d5c195994cd3d..0000000000000 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt +++ /dev/null @@ -1,215 +0,0 @@ -namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped; - -use Magento\Framework\Config\ScopeInterface; -use Magento\Framework\ObjectManagerInterface; - -/** - * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped - */ -class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped -{ - /** - * @var ScopeInterface - */ - private $____scope = null; - - /** - * @var ObjectManagerInterface - */ - private $____om = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex - */ - private $____plugin_complex_plugin = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced - */ - private $____plugin_advanced_plugin = null; - - /** - * {@inheritdoc} - */ - public function __construct() - { - $____om = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\ObjectManagerInterface::class); - //TODO fix di in production mode - $____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); - //TODO fix di in production mode - $this->____om = $____om; - $this->____scope = $____scope; - } - - /** - * {@inheritdoc} - */ - public function returnVoid() : void - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - parent::returnVoid(); - - $this->____plugin_complex_plugin()->afterReturnVoid($this, null); - break; - default: - parent::returnVoid(); - break; - } - } - - /** - * {@inheritdoc} - */ - public function getNullableValue() : ?string - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - $result = parent::getNullableValue(); - - return (($tmp = $this->____plugin_complex_plugin()->afterGetNullableValue($this, $result)) !== null) ? $tmp : $result; - default: - return parent::getNullableValue(); - } - } - - /** - * {@inheritdoc} - */ - public function getName() : string - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - $this->____plugin_advanced_plugin()->beforeGetName($this); - - $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ - $result = $this->____plugin_complex_plugin()->aroundGetName($this, function(){ - return parent::getName(); - }); - - return (($tmp = $this->____plugin_complex_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; - }); - - return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; - default: - return parent::getName(); - } - } - - /** - * {@inheritdoc} - */ - public function setValue(string $value) - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - $beforeResult = $this->____plugin_complex_plugin()->beforeSetValue($this, $value); - if ($beforeResult !== null) list($value) = (array)$beforeResult; - - return $this->____plugin_complex_plugin()->aroundSetValue($this, function($value){ - return parent::setValue($value); - }, $value); - default: - return parent::setValue($value); - } - } - - /** - * {@inheritdoc} - */ - public function firstVariadicParameter(string ... $variadicValue) - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - return $this->____plugin_complex_plugin()->aroundFirstVariadicParameter($this, function($variadicValue){ - return parent::firstVariadicParameter($variadicValue); - }, $variadicValue); - default: - return parent::firstVariadicParameter($variadicValue); - } - } - - /** - * {@inheritdoc} - */ - public function secondVariadicParameter(string $value, string ... $variadicValue) - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - return $this->____plugin_complex_plugin()->aroundSecondVariadicParameter($this, function($value, $variadicValue){ - return parent::secondVariadicParameter($value, $variadicValue); - }, $value, $variadicValue); - default: - return parent::secondVariadicParameter($value, $variadicValue); - } - } - - /** - * {@inheritdoc} - */ - public function byRefVariadic(string &... $variadicValue) - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - return $this->____plugin_complex_plugin()->aroundByRefVariadic($this, function(&$variadicValue){ - return parent::byRefVariadic($variadicValue); - }, $variadicValue); - default: - return parent::byRefVariadic($variadicValue); - } - } - - /** - * {@inheritdoc} - */ - public function returnsSelf() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - $this->____plugin_complex_plugin()->beforeReturnsSelf($this); - - return parent::returnsSelf(); - default: - return parent::returnsSelf(); - } - } - - /** - * {@inheritdoc} - */ - public function returnsType() : \Magento\Framework\Something - { - switch ($this->____scope->getCurrentScope()) { - case 'backend': - $this->____plugin_complex_plugin()->beforeReturnsType($this); - - return parent::returnsType(); - default: - return parent::returnsType(); - } - } - - /** - * plugin "complex_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex - */ - private function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex - { - if ($this->____plugin_complex_plugin === null) { - $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); - } - return $this->____plugin_complex_plugin; - } - - /** - * plugin "advanced_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced - */ - private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced - { - if ($this->____plugin_advanced_plugin === null) { - $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); - } - return $this->____plugin_advanced_plugin; - } -} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt deleted file mode 100644 index abd42aa5b8e36..0000000000000 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/Item.txt +++ /dev/null @@ -1,93 +0,0 @@ -namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; - -use Magento\Framework\Config\ScopeInterface; -use Magento\Framework\ObjectManagerInterface; - -/** - * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item - */ -class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item -{ - /** - * @var ScopeInterface - */ - private $____scope = null; - - /** - * @var ObjectManagerInterface - */ - private $____om = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple - */ - private $____plugin_simple_plugin = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced - */ - private $____plugin_advanced_plugin = null; - - /** - * {@inheritdoc} - */ - public function __construct() - { - $____om = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\ObjectManagerInterface::class); - //TODO fix di in production mode - $____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); - //TODO fix di in production mode - $this->____om = $____om; - $this->____scope = $____scope; - } - - /** - * {@inheritdoc} - */ - public function getName() - { - switch ($this->____scope->getCurrentScope()) { - case 'global': - case 'emptyscope': - $result = parent::getName(); - - return (($tmp = $this->____plugin_simple_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; - case 'backend': - $this->____plugin_advanced_plugin()->beforeGetName($this); - - $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ - $result = parent::getName(); - - return (($tmp = $this->____plugin_simple_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; - }); - - return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; - default: - return parent::getName(); - } - } - - /** - * plugin "simple_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple - */ - private function ____plugin_simple_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple - { - if ($this->____plugin_simple_plugin === null) { - $this->____plugin_simple_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class); - } - return $this->____plugin_simple_plugin; - } - - /** - * plugin "advanced_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced - */ - private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced - { - if ($this->____plugin_advanced_plugin === null) { - $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); - } - return $this->____plugin_advanced_plugin; - } -} From 944144ae5f8eadbc00c1b198f41f1b2c0412581a Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Mon, 27 May 2019 15:46:58 +0200 Subject: [PATCH 16/52] cyclomatic complexity refactor --- .../Generator/CompiledInterceptor.php | 57 +++++++++++++------ 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index b6bfa7a53308b..e5e893f843f12 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -369,7 +369,6 @@ private function compileAfterPlugins($plugins, $methodName, $extraParams, $retur */ private function getMethodSourceFromConfig($methodName, $conf, $parameters, $returnVoid) { - $first = true; $capitalizedName = ucfirst($methodName); $parametersList = $this->getParameterList($parameters); $extraParams = empty($parameters) ? '' : (', ' . $parametersList); @@ -407,20 +406,34 @@ private function getMethodSourceFromConfig($methodName, $conf, $parameters, $ret $returnVoid )); } + return array_merge($body, $this->getResultChainLines($resultChain, $returnVoid)); + } + + /** + * Implode result chain into list of assignments + * + * @param array $resultChain + * @param bool $returnVoid + * @return array + */ + private function getResultChainLines($resultChain, $returnVoid) + { + $lines = []; + $first = true; foreach ($resultChain as $lp => $piece) { if ($first) { $first = false; } else { - $body[] = ""; + $lines[] = ""; } if (!$returnVoid) { $piece[0] = (($lp + 1 == count($resultChain)) ? "return " : "\$result = ") . $piece[0]; } foreach ($piece as $line) { - $body[] = $line; + $lines[] = $line; } } - return $body; + return $lines; } /** @@ -532,19 +545,7 @@ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) 'switch ($this->____scope->getCurrentScope()) {' ]; - $cases = []; - //group cases by config - foreach ($config as $scope => $conf) { - $key = md5(serialize($conf)); - if (!isset($cases[$key])) { - $cases[$key] = ['cases'=>[], 'conf'=>$conf]; - } - $cases[$key]['cases'][] = "\tcase '$scope':"; - } - //call parent method for scopes with no plugins (or when no scope is set) - $cases[] = ['cases'=>["\tdefault:"], 'conf'=>[]]; - - foreach ($cases as $case) { + foreach ($this->getScopeCasesFromConfig($config) as $case) { $body = array_merge($body, $case['cases']); $this->addCodeSubBlock( $body, @@ -573,6 +574,28 @@ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) ]; } + /** + * Get scope cases from config + * + * @param array $config + * @return array + */ + private function getScopeCasesFromConfig($config) + { + $cases = []; + //group cases by config + foreach ($config as $scope => $conf) { + $key = md5(serialize($conf)); + if (!isset($cases[$key])) { + $cases[$key] = ['cases'=>[], 'conf'=>$conf]; + } + $cases[$key]['cases'][] = "\tcase '$scope':"; + } + //call parent method for scopes with no plugins (or when no scope is set) + $cases[] = ['cases'=>["\tdefault:"], 'conf'=>[]]; + return $cases; + } + /** * Generate array with plugin info * From d64c89a6b4f354565879eea22d6c08f5a4ff3cb6 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Mon, 27 May 2019 16:19:08 +0200 Subject: [PATCH 17/52] coding standards fixes --- .../Generator/CompiledInterceptor.php | 17 ++++++++++------- .../Generator/FileCache.php | 11 ++++++----- .../Generator/StaticScope.php | 2 +- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index e5e893f843f12..121be72f936e5 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -78,7 +78,7 @@ public function __construct( */ public function setInterceptedMethods($interceptedMethods) { - //NOOP + return; } /** @@ -399,12 +399,15 @@ private function getMethodSourceFromConfig($methodName, $conf, $parameters, $ret } if (isset($conf[DefinitionInterface::LISTENER_AFTER])) { - $resultChain = array_merge($resultChain, $this->compileAfterPlugins( - $conf[DefinitionInterface::LISTENER_AFTER], - 'after' . $capitalizedName, - $extraParams, - $returnVoid - )); + $resultChain = array_merge( + $resultChain, + $this->compileAfterPlugins( + $conf[DefinitionInterface::LISTENER_AFTER], + 'after' . $capitalizedName, + $extraParams, + $returnVoid + ) + ); } return array_merge($body, $this->getResultChainLines($resultChain, $returnVoid)); } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php index 95a315d887ff0..289275f71d1bb 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php @@ -49,7 +49,7 @@ private function getCachePath($identifier) */ public function test($identifier) { - // TODO: Implement test() method. + return file_exists($this->getCachePath($identifier)); } /** @@ -85,6 +85,7 @@ public function save($data, $identifier, array $tags = [], $lifeTime = null) $path, '' ); + return true; } } @@ -97,7 +98,7 @@ public function save($data, $identifier, array $tags = [], $lifeTime = null) */ public function remove($identifier) { - // TODO: Implement remove() method. + return false; } /** @@ -110,7 +111,7 @@ public function remove($identifier) */ public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = []) { - // TODO: Implement clean() method. + return false; } /** @@ -120,7 +121,7 @@ public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = []) */ public function getBackend() { - // TODO: Implement getBackend() method. + return null; } /** @@ -130,6 +131,6 @@ public function getBackend() */ public function getLowLevelFrontend() { - // TODO: Implement getLowLevelFrontend() method. + return null; } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php index e0cf887266d43..99769b19274ad 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php @@ -46,6 +46,6 @@ public function getCurrentScope() */ public function setCurrentScope($scope) { - throw new \Exception('readonly'); + throw new \RuntimeException('readonly'); } } From 1058be98d762351a5c8c42dac9d217b627973419 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Thu, 30 May 2019 16:32:43 +0200 Subject: [PATCH 18/52] fixes for compiled mode --- .../Generator/CompiledInterceptor.php | 78 ++++--- .../CompiledInterceptorSubstitution.php | 41 ++++ .../Generator/FileCache.php | 2 +- .../Framework/CompiledInterception/README.md | 3 +- .../CompiledInterceptorTest.php | 61 +++++- .../Custom/Module/Model/ComplexItem.php | 2 +- .../Custom/Module/Model/ComplexItemTyped.php | 2 +- .../Custom/Module/Model/Item.php | 19 ++ .../Custom/Module/Model/Item/Enhanced.php | 18 ++ .../Module/Model/ItemPlugin/Advanced.php | 48 +++++ .../Module/Model/ItemPlugin/Complex.php | 2 +- .../Custom/Module/Model/ItemPlugin/Simple.php | 23 +++ .../Custom/Module/Model/SecondItem.php | 19 ++ .../_out_interceptors/ComplexItem.txt | 28 ++- .../_out_interceptors/ComplexItemTyped.txt | 30 ++- .../_out_interceptors/Item.txt | 28 ++- .../_out_interceptors/SecondItem.txt | 191 ++++++++++++++++++ .../Integration/_files/reader_mock_map.php | 141 +++++++++++++ .../CompiledPluginListTest.php | 5 - .../Test/Unit/_files/reader_mock_map.php | 55 +---- 20 files changed, 651 insertions(+), 145 deletions(-) create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php rename lib/internal/Magento/Framework/CompiledInterception/Test/{Unit => Integration/CompiledInterceptor}/Custom/Module/Model/ComplexItem.php (91%) rename lib/internal/Magento/Framework/CompiledInterception/Test/{Unit => Integration/CompiledInterceptor}/Custom/Module/Model/ComplexItemTyped.php (93%) create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/Item.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/Item/Enhanced.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Advanced.php rename lib/internal/Magento/Framework/CompiledInterception/Test/{Unit => Integration/CompiledInterceptor}/Custom/Module/Model/ItemPlugin/Complex.php (96%) create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Simple.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/SecondItem.php create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Test/Integration/_files/reader_mock_map.php diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 121be72f936e5..89bd30439622c 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -78,7 +78,7 @@ public function __construct( */ public function setInterceptedMethods($interceptedMethods) { - return; + //NOOP } /** @@ -106,12 +106,17 @@ protected function _getClassProperties() } /** - * Unused, required by interface + * Get default constructor definition for generated class * - * @return array|void + * @return array + * @throws \ReflectionException */ protected function _getDefaultConstructorDefinition() { + return $this->injectPropertiesSettersToConstructor( + $this->getSourceClassReflection()->getConstructor(), + static::propertiesToInjectToConstructor() + ); } /** @@ -142,17 +147,34 @@ private function generateMethodsAndProperties() $this->classMethods = []; $this->classProperties = []; - $this->injectPropertiesSettersToConstructor( - $this->getSourceClassReflection()->getConstructor(), - [ - ScopeInterface::class => '____scope', - ObjectManagerInterface::class => '____om', - ] - ); + foreach (static::propertiesToInjectToConstructor() as $type => $name) { + $this->_classGenerator->addUse($type); + $this->classProperties[] = [ + 'name' => $name, + 'visibility' => 'private', + 'docblock' => [ + 'tags' => [['name' => 'var', 'description' => substr(strrchr($type, "\\"), 1)]], + ] + ]; + } + $this->classMethods[] = $this->_getDefaultConstructorDefinition(); $this->overrideMethodsAndGeneratePluginGetters($this->getSourceClassReflection()); } } + /** + * Get properties to be injected from DI + * + * @return array + */ + public static function propertiesToInjectToConstructor() + { + return [ + ScopeInterface::class => '____scope', + ObjectManagerInterface::class => '____om', + ]; + } + /** * Get reflection of source class * @@ -206,10 +228,11 @@ private function overrideMethodsAndGeneratePluginGetters(\ReflectionClass $refle } /** - * Generate class constructor adding required properties + * Generate class constructor adding required properties when types are not present in parent constructor * * @param \ReflectionMethod|null $parentConstructor * @param array $properties + * @return array */ private function injectPropertiesSettersToConstructor(\ReflectionMethod $parentConstructor = null, $properties = []) { @@ -218,20 +241,11 @@ private function injectPropertiesSettersToConstructor(\ReflectionMethod $parentC $body = []; } else { $parameters = $parentConstructor->getParameters(); + $parentCallParams = []; foreach ($parameters as $parameter) { $parentCallParams[] = '$' . $parameter->getName(); } - $body = ["parent::__construct(" . implode(', ', $parentCallParams) .");"]; - } - foreach ($properties as $type => $name) { - $this->_classGenerator->addUse($type); - $this->classProperties[] = [ - 'name' => $name, - 'visibility' => 'private', - 'docblock' => [ - 'tags' => [['name' => 'var', 'description' => substr(strrchr($type, "\\"), 1)]], - ] - ]; + $body = ["parent::__construct(" . implode(', ', $parentCallParams) . ");"]; } $extraParams = $properties; $extraSetters = array_combine($properties, $properties); @@ -245,24 +259,24 @@ private function injectPropertiesSettersToConstructor(\ReflectionMethod $parentC } } $parameters = array_map([$this, '_getMethodParameterInfo'], $parameters); - /* foreach ($extraParams as $type => $name) { + foreach ($extraParams as $type => $name) { array_unshift($parameters, [ 'name' => $name, 'type' => $type ]); - } */ + } foreach ($extraSetters as $name => $paramName) { array_unshift($body, "\$this->$name = \$$paramName;"); } - foreach ($extraParams as $type => $name) { + /*foreach ($extraParams as $type => $name) { array_unshift($body, "//TODO fix di in production mode"); array_unshift( $body, "\$$name = \\Magento\\Framework\\App\\ObjectManager::getInstance()->get(\\$type::class);" ); - } + }*/ - $this->classMethods[] = [ + return [ 'name' => '__construct', 'parameters' => $parameters, 'body' => implode("\n", $body), @@ -588,11 +602,13 @@ private function getScopeCasesFromConfig($config) $cases = []; //group cases by config foreach ($config as $scope => $conf) { - $key = md5(serialize($conf)); - if (!isset($cases[$key])) { - $cases[$key] = ['cases'=>[], 'conf'=>$conf]; + foreach ($cases as &$case) { + if ($case['conf'] == $conf) { + $case['cases'][] = "\tcase '$scope':"; + continue 2; + } } - $cases[$key]['cases'][] = "\tcase '$scope':"; + $cases[] = ['cases'=>["\tcase '$scope':"], 'conf'=>$conf]; } //call parent method for scopes with no plugins (or when no scope is set) $cases[] = ['cases'=>["\tdefault:"], 'conf'=>[]]; diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php new file mode 100644 index 0000000000000..6efee3fec4b6f --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php @@ -0,0 +1,41 @@ + &$arguments) { + if (substr($instanceName, -12) === '\Interceptor') { + foreach (CompiledInterceptor::propertiesToInjectToConstructor() as $type => $name) { + $preference = isset($config['preferences'][$type]) ? $config['preferences'][$type] : $type; + foreach ($arguments as $argument) { + if (isset($argument['_i_']) && $argument['_i_'] == $preference) { + continue 2; + } + } + $arguments = array_merge([$name => ['_i_' => $preference]], $arguments); + } + + } + } + return $config; + } +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php index 289275f71d1bb..13318a0159539 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php @@ -57,7 +57,7 @@ public function test($identifier) * * @param string $identifier * @return string|bool - * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * @SuppressWarnings(PHPMD) */ public function load($identifier) { diff --git a/lib/internal/Magento/Framework/CompiledInterception/README.md b/lib/internal/Magento/Framework/CompiledInterception/README.md index 83837fa4f1f86..3d9a1f7177069 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/README.md +++ b/lib/internal/Magento/Framework/CompiledInterception/README.md @@ -19,9 +19,10 @@ To use compiled interceptors in developer mode please add following preference t ``` -To use compiled interceptors in production mode please add following preference to your di.xml +To use compiled interceptors in production or default mode please add following preferences to your di.xml ``` + ``` Clear generated files and cache: diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php index 2202bf8eec8a1..f52a06db3e3cc 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php @@ -9,12 +9,14 @@ use Magento\Framework\App\AreaList; use Magento\Framework\Code\Generator\Io; use Magento\Framework\CompiledInterception\Generator\CompiledInterceptor; - -use Magento\Framework\CompiledInterception\Test\Unit\CompiledPluginList\CompiledPluginListTest; -use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem; -use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped; -use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; -use PHPUnit\Framework\MockObject\MockObject as MockObject; +use Magento\Framework\CompiledInterception\Generator\CompiledPluginList; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\SecondItem; +use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItem; +use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped; +use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\Item; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\NullLogger; /** * Class CompiledInterceptorTest @@ -45,6 +47,40 @@ protected function setUp() ->getMock(); } + /** + * @return array + */ + public function createScopeReaders() + { + $readerMap = include __DIR__ . '/../_files/reader_mock_map.php'; + $readerMock = $this->createMock(\Magento\Framework\ObjectManager\Config\Reader\Dom::class); + $readerMock->expects($this->any())->method('read')->will($this->returnValueMap($readerMap)); + + $omMock = $this->createMock(ObjectManager::class); + $omMock->method('get')->with(\Psr\Log\LoggerInterface::class)->willReturn(new NullLogger()); + + $omConfigMock = $this->getMockForAbstractClass( + \Magento\Framework\Interception\ObjectManager\ConfigInterface::class + ); + + $omConfigMock->expects($this->any())->method('getOriginalInstanceType')->will($this->returnArgument(0)); + $ret = []; + $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + foreach ($readerMap as $readerLine) { + $ret[$readerLine[0]] = $objectManagerHelper->getObject( + CompiledPluginList::class, + [ + 'objectManager' => $omMock, + 'scope' => $readerLine[0], + 'reader' => $readerMock, + 'omConfig' => $omConfigMock, + 'cachePath' => false + ] + ); + } + return $ret; + } + /** * Checks a test case when interceptor generates code for the specified class. * @@ -66,7 +102,7 @@ public function testGenerate($className, $resultClassName, $fileName) $this->ioGenerator, null, null, - (new CompiledPluginListTest())->createScopeReaders() + $this->createScopeReaders() ] ) ->getMock(); @@ -81,6 +117,12 @@ public function testGenerate($className, $resultClassName, $fileName) $generated = $interceptor->generate(); $this->assertEquals($fileName . '.php', $generated, 'Generated interceptor is invalid.'); + + /* + eval( $code ); + $className = "\\$resultClassName"; + $interceptor = new $className(); + */ } /** @@ -106,6 +148,11 @@ public function interceptorDataProvider() ComplexItemTyped::class . '\Interceptor', 'ComplexItemTyped' ], + [ + SecondItem::class, + SecondItem::class . '\Interceptor', + 'SecondItem' + ], ]; } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItem.php similarity index 91% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php rename to lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItem.php index f8a792cd2d793..60868390377bf 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItem.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItem.php @@ -5,7 +5,7 @@ */ // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model; +namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model; class ComplexItem { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItemTyped.php similarity index 93% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php rename to lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItemTyped.php index 60159abd985d5..1037f71ee9901 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ComplexItemTyped.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItemTyped.php @@ -5,7 +5,7 @@ */ // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model; +namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model; class ComplexItemTyped { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/Item.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/Item.php new file mode 100644 index 0000000000000..1ffd3f49796c3 --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/Item.php @@ -0,0 +1,19 @@ +get(\Magento\Framework\ObjectManagerInterface::class); - //TODO fix di in production mode - $____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); - //TODO fix di in production mode $this->____om = $____om; $this->____scope = $____scope; } @@ -169,24 +165,24 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust /** * plugin "advanced_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ - private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced { if ($this->____plugin_advanced_plugin === null) { - $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } return $this->____plugin_advanced_plugin; } /** * plugin "complex_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex */ - private function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + private function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex { if ($this->____plugin_complex_plugin === null) { - $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); + $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex::class); } return $this->____plugin_complex_plugin; } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt index d5c195994cd3d..fb4aa4836a2e3 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -1,12 +1,12 @@ -namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped; +namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped; use Magento\Framework\Config\ScopeInterface; use Magento\Framework\ObjectManagerInterface; /** - * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped + * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped */ -class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped +class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped { /** * @var ScopeInterface @@ -19,24 +19,20 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust private $____om = null; /** - * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex */ private $____plugin_complex_plugin = null; /** - * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ private $____plugin_advanced_plugin = null; /** * {@inheritdoc} */ - public function __construct() + public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\ScopeInterface $____scope) { - $____om = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\ObjectManagerInterface::class); - //TODO fix di in production mode - $____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); - //TODO fix di in production mode $this->____om = $____om; $this->____scope = $____scope; } @@ -162,7 +158,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust /** * {@inheritdoc} */ - public function returnsSelf() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped + public function returnsSelf() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped { switch ($this->____scope->getCurrentScope()) { case 'backend': @@ -191,24 +187,24 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust /** * plugin "complex_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex */ - private function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex + private function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex { if ($this->____plugin_complex_plugin === null) { - $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class); + $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex::class); } return $this->____plugin_complex_plugin; } /** * plugin "advanced_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ - private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced { if ($this->____plugin_advanced_plugin === null) { - $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } return $this->____plugin_advanced_plugin; } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt index abd42aa5b8e36..3573cd0e44054 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt @@ -1,12 +1,12 @@ -namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; +namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\Item; use Magento\Framework\Config\ScopeInterface; use Magento\Framework\ObjectManagerInterface; /** - * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item + * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\Item */ -class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item +class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\Item { /** * @var ScopeInterface @@ -19,24 +19,20 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust private $____om = null; /** - * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple + * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple */ private $____plugin_simple_plugin = null; /** - * @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ private $____plugin_advanced_plugin = null; /** * {@inheritdoc} */ - public function __construct() + public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\ScopeInterface $____scope) { - $____om = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\ObjectManagerInterface::class); - //TODO fix di in production mode - $____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); - //TODO fix di in production mode $this->____om = $____om; $this->____scope = $____scope; } @@ -69,24 +65,24 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust /** * plugin "simple_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple + * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple */ - private function ____plugin_simple_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple + private function ____plugin_simple_plugin() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple { if ($this->____plugin_simple_plugin === null) { - $this->____plugin_simple_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple::class); + $this->____plugin_simple_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); } return $this->____plugin_simple_plugin; } /** * plugin "advanced_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ - private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced + private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced { if ($this->____plugin_advanced_plugin === null) { - $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class); + $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } return $this->____plugin_advanced_plugin; } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt new file mode 100644 index 0000000000000..9d16855e89398 --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt @@ -0,0 +1,191 @@ +namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\SecondItem; + +use Magento\Framework\Config\ScopeInterface; +use Magento\Framework\ObjectManagerInterface; + +/** + * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\SecondItem + */ +class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\SecondItem +{ + /** + * @var ScopeInterface + */ + private $____scope = null; + + /** + * @var ObjectManagerInterface + */ + private $____om = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + */ + private $____plugin_advanced_plugin1 = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + */ + private $____plugin_simple_plugin1 = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + */ + private $____plugin_advanced_plugin2 = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + */ + private $____plugin_advanced_plugin3 = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + */ + private $____plugin_simple_plugin2 = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + */ + private $____plugin_simple_plugin3 = null; + + /** + * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + */ + private $____plugin_advanced_plugin4 = null; + + /** + * {@inheritdoc} + */ + public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\ScopeInterface $____scope) + { + $this->____om = $____om; + $this->____scope = $____scope; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + switch ($this->____scope->getCurrentScope()) { + case 'frontend': + $this->____plugin_advanced_plugin1()->beforeGetName($this); + + $result = $this->____plugin_advanced_plugin1()->aroundGetName($this, function(){ + $this->____plugin_advanced_plugin2()->beforeGetName($this); + + $result = $this->____plugin_advanced_plugin2()->aroundGetName($this, function(){ + $this->____plugin_advanced_plugin3()->beforeGetName($this); + + $result = $this->____plugin_advanced_plugin3()->aroundGetName($this, function(){ + $this->____plugin_advanced_plugin4()->beforeGetName($this); + + $result = $this->____plugin_advanced_plugin4()->aroundGetName($this, function(){ + return parent::getName(); + }); + + return (($tmp = $this->____plugin_advanced_plugin4()->afterGetName($this, $result)) !== null) ? $tmp : $result; + }); + + $result = (($tmp = $this->____plugin_simple_plugin2()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + $result = (($tmp = $this->____plugin_simple_plugin3()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + return (($tmp = $this->____plugin_advanced_plugin3()->afterGetName($this, $result)) !== null) ? $tmp : $result; + }); + + return (($tmp = $this->____plugin_advanced_plugin2()->afterGetName($this, $result)) !== null) ? $tmp : $result; + }); + + $result = (($tmp = $this->____plugin_simple_plugin1()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + return (($tmp = $this->____plugin_advanced_plugin1()->afterGetName($this, $result)) !== null) ? $tmp : $result; + default: + return parent::getName(); + } + } + + /** + * plugin "advanced_plugin1" + * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + */ + private function ____plugin_advanced_plugin1() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + { + if ($this->____plugin_advanced_plugin1 === null) { + $this->____plugin_advanced_plugin1 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); + } + return $this->____plugin_advanced_plugin1; + } + + /** + * plugin "simple_plugin1" + * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + */ + private function ____plugin_simple_plugin1() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + { + if ($this->____plugin_simple_plugin1 === null) { + $this->____plugin_simple_plugin1 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); + } + return $this->____plugin_simple_plugin1; + } + + /** + * plugin "advanced_plugin2" + * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + */ + private function ____plugin_advanced_plugin2() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + { + if ($this->____plugin_advanced_plugin2 === null) { + $this->____plugin_advanced_plugin2 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); + } + return $this->____plugin_advanced_plugin2; + } + + /** + * plugin "advanced_plugin3" + * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + */ + private function ____plugin_advanced_plugin3() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + { + if ($this->____plugin_advanced_plugin3 === null) { + $this->____plugin_advanced_plugin3 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); + } + return $this->____plugin_advanced_plugin3; + } + + /** + * plugin "simple_plugin2" + * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + */ + private function ____plugin_simple_plugin2() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + { + if ($this->____plugin_simple_plugin2 === null) { + $this->____plugin_simple_plugin2 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); + } + return $this->____plugin_simple_plugin2; + } + + /** + * plugin "simple_plugin3" + * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + */ + private function ____plugin_simple_plugin3() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + { + if ($this->____plugin_simple_plugin3 === null) { + $this->____plugin_simple_plugin3 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); + } + return $this->____plugin_simple_plugin3; + } + + /** + * plugin "advanced_plugin4" + * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + */ + private function ____plugin_advanced_plugin4() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + { + if ($this->____plugin_advanced_plugin4 === null) { + $this->____plugin_advanced_plugin4 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); + } + return $this->____plugin_advanced_plugin4; + } +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/_files/reader_mock_map.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/_files/reader_mock_map.php new file mode 100644 index 0000000000000..279b3a2eea88a --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/_files/reader_mock_map.php @@ -0,0 +1,141 @@ + [ + 'plugins' => [ + 'simple_plugin' => [ + 'sortOrder' => 10, + 'instance' => Simple::class, + ], + ], + ], + ComplexItem::class => [ + 'plugins' => [ + 'advanced_plugin' => [ + 'sortOrder' => 5, + 'instance' => Advanced::class, + ], + ], + ], + ], + ], + [ + 'backend', + [ + Item::class => [ + 'plugins' => [ + 'advanced_plugin' => [ + 'sortOrder' => 5, + 'instance' => Advanced::class, + ], + ], + ], + ComplexItem::class => [ + 'plugins' => [ + 'complex_plugin' => [ + 'sortOrder' => 15, + 'instance' => Complex::class, + ], + 'advanced_plugin' => [ + 'sortOrder' => 5, + 'instance' => Advanced::class, + ], + ], + ], + ComplexItemTyped::class => [ + 'plugins' => [ + 'complex_plugin' => [ + 'sortOrder' => 25, + 'instance' => Complex::class, + ], + 'advanced_plugin' => [ + 'sortOrder' => 5, + 'instance' => Advanced::class, + ], + ], + ], + ] + ], + [ + 'frontend', + [ + Item::class => [ + 'plugins' => ['simple_plugin' => ['disabled' => true]], + ], + Enhanced::class => [ + 'plugins' => [ + 'advanced_plugin' => [ + 'sortOrder' => 5, + 'instance' => Advanced::class, + ], + ], + ], + 'SomeType' => [ + 'plugins' => [ + 'simple_plugin' => [ + 'instance' => 'NonExistingPluginClass', + ], + ], + ], + 'typeWithoutInstance' => [ + 'plugins' => [ + 'simple_plugin' => [], + ], + ], + SecondItem::class => [ + 'plugins' => [ + 'simple_plugin1' => [ + 'sortOrder' => 5, + 'instance' => Simple::class, + ], + 'advanced_plugin1' => [ + 'sortOrder' => 5, + 'instance' => Advanced::class, + ], + 'advanced_plugin2' => [ + 'sortOrder' => 10, + 'instance' => Advanced::class, + ], + 'simple_plugin2' => [ + 'sortOrder' => 11, + 'instance' => Simple::class, + ], + 'simple_plugin3' => [ + 'sortOrder' => 12, + 'instance' => Simple::class, + ], + 'advanced_plugin3' => [ + 'sortOrder' => 15, + 'instance' => Advanced::class, + ], + 'advanced_plugin4' => [ + 'sortOrder' => 25, + 'instance' => Advanced::class, + ], + ], + ] + ] + ], + [ + 'emptyscope', + [ + + ] + ] +]; diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php index ff63413df707a..debf7a415d3ed 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php @@ -13,11 +13,6 @@ use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple; use Psr\Log\NullLogger; -require_once __DIR__ . '/../Custom/Module/Model/Item.php'; -require_once __DIR__ . '/../Custom/Module/Model/Item/Enhanced.php'; -require_once __DIR__ . '/../Custom/Module/Model/ItemPlugin/Simple.php'; -require_once __DIR__ . '/../Custom/Module/Model/ItemPlugin/Advanced.php'; - /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php index 010ba48920cc1..c597bc9ae9dcc 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php @@ -4,12 +4,9 @@ * See COPYING.txt for license details. */ -use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem; -use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped; use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item\Enhanced; use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced; -use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex; use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple; return [ @@ -20,17 +17,7 @@ 'plugins' => [ 'simple_plugin' => [ 'sortOrder' => 10, - 'instance' => - Simple::class, - ], - ], - ], - ComplexItem::class => [ - 'plugins' => [ - 'advanced_plugin' => [ - 'sortOrder' => 5, - 'instance' => - Advanced::class, + 'instance' => Simple::class, ], ], ], @@ -43,36 +30,7 @@ 'plugins' => [ 'advanced_plugin' => [ 'sortOrder' => 5, - 'instance' => - Advanced::class, - ], - ], - ], - ComplexItem::class => [ - 'plugins' => [ - 'complex_plugin' => [ - 'sortOrder' => 15, - 'instance' => - Complex::class, - ], - 'advanced_plugin' => [ - 'sortOrder' => 5, - 'instance' => - Advanced::class, - ], - ], - ], - ComplexItemTyped::class => [ - 'plugins' => [ - 'complex_plugin' => [ - 'sortOrder' => 25, - 'instance' => - Complex::class, - ], - 'advanced_plugin' => [ - 'sortOrder' => 5, - 'instance' => - Advanced::class, + 'instance' => Advanced::class, ], ], ], @@ -80,14 +38,15 @@ ], [ 'frontend', - [Item::class => [ + [ + Item::class => [ 'plugins' => ['simple_plugin' => ['disabled' => true]], - ], Enhanced::class => [ + ], + Enhanced::class => [ 'plugins' => [ 'advanced_plugin' => [ 'sortOrder' => 5, - 'instance' => - Advanced::class, + 'instance' => Advanced::class, ], ], ], From ecd9b5461931d1f54f7bb0f35368abffc88b4b93 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Thu, 6 Jun 2019 13:33:32 +0200 Subject: [PATCH 19/52] bugfix for primary scope for compiled mode --- .../CompiledInterception/Generator/CompiledInterceptor.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 89bd30439622c..838ab600dd49a 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -712,6 +712,7 @@ private function getPluginsConfig(\ReflectionMethod $method, &$allPlugins) foreach ($this->areaList->getCodes() as $scope) { $this->plugins[$scope] = new CompiledPluginList(ObjectManager::getInstance(), $scope); } + $this->plugins['primary'] = new CompiledPluginList(ObjectManager::getInstance(), 'primary'); } foreach ($this->plugins as $scope => $pluginsList) { $pluginChain = $this->getPluginsChain($pluginsList, $className, $method->getName(), $allPlugins); From 6285273a5a4bd3d2ce8c6f08fa76c6100c0bc325 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Fri, 7 Jun 2019 13:52:50 +0200 Subject: [PATCH 20/52] generating interceptors performance fix --- .../Generator/AreasPluginList.php | 44 +++++++++++++++++++ .../Generator/CompiledInterceptor.php | 36 ++++----------- .../CompiledInterceptorTest.php | 17 +++++-- 3 files changed, 67 insertions(+), 30 deletions(-) create mode 100644 lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php new file mode 100644 index 0000000000000..09f1cdb50f342 --- /dev/null +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php @@ -0,0 +1,44 @@ +areaList = $areaList; + $this->plugins = $plugins; + } + + /** + * @return array + */ + public function getPluginsConfigForAllAreas() + { + if ($this->plugins === null) { + $this->plugins = []; + foreach ($this->areaList->getCodes() as $scope) { + $this->plugins[$scope] = new CompiledPluginList(ObjectManager::getInstance(), $scope); + } + $this->plugins['primary'] = new CompiledPluginList(ObjectManager::getInstance(), 'primary'); + } + return $this->plugins; + } + +} diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 838ab600dd49a..55a0defe3e526 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -26,8 +26,6 @@ class CompiledInterceptor extends EntityAbstract */ const ENTITY_TYPE = 'interceptor'; - private $plugins; - private $classMethods; private $classProperties; @@ -37,26 +35,27 @@ class CompiledInterceptor extends EntityAbstract */ private $baseReflection; - private $areaList; + /** + * @var AreasPluginList + */ + private $areasPlugins; /** * CompiledInterceptor constructor. - * @param AreaList $areaList + * @param AreasPluginList $areasPlugins * @param null|string $sourceClassName * @param null|string $resultClassName * @param Io|null $ioObject * @param CodeGeneratorInterface|null $classGenerator * @param DefinedClasses|null $definedClasses - * @param null|array $plugins */ public function __construct( - AreaList $areaList, + AreasPluginList $areasPlugins, $sourceClassName = null, $resultClassName = null, Io $ioObject = null, CodeGeneratorInterface $classGenerator = null, - DefinedClasses $definedClasses = null, - $plugins = null + DefinedClasses $definedClasses = null ) { parent::__construct( $sourceClassName, @@ -65,9 +64,7 @@ public function __construct( $classGenerator, $definedClasses ); - - $this->areaList = $areaList; - $this->plugins = $plugins; + $this->areasPlugins = $areasPlugins; } /** @@ -268,14 +265,6 @@ private function injectPropertiesSettersToConstructor(\ReflectionMethod $parentC foreach ($extraSetters as $name => $paramName) { array_unshift($body, "\$this->$name = \$$paramName;"); } - /*foreach ($extraParams as $type => $name) { - array_unshift($body, "//TODO fix di in production mode"); - array_unshift( - $body, - "\$$name = \\Magento\\Framework\\App\\ObjectManager::getInstance()->get(\\$type::class);" - ); - }*/ - return [ 'name' => '__construct', 'parameters' => $parameters, @@ -707,14 +696,7 @@ private function getPluginsConfig(\ReflectionMethod $method, &$allPlugins) $className = ltrim($this->getSourceClassName(), '\\'); $result = []; - if ($this->plugins === null) { - $this->plugins = []; - foreach ($this->areaList->getCodes() as $scope) { - $this->plugins[$scope] = new CompiledPluginList(ObjectManager::getInstance(), $scope); - } - $this->plugins['primary'] = new CompiledPluginList(ObjectManager::getInstance(), 'primary'); - } - foreach ($this->plugins as $scope => $pluginsList) { + foreach ($this->areasPlugins->getPluginsConfigForAllAreas() as $scope => $pluginsList) { $pluginChain = $this->getPluginsChain($pluginsList, $className, $method->getName(), $allPlugins); if ($pluginChain) { $result[$scope] = $pluginChain; diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php index f52a06db3e3cc..0e8856d3baf05 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php @@ -6,6 +6,7 @@ namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor; +use Magento\Framework\CompiledInterception\Generator\AreasPluginList; use Magento\Framework\App\AreaList; use Magento\Framework\Code\Generator\Io; use Magento\Framework\CompiledInterception\Generator\CompiledInterceptor; @@ -91,18 +92,28 @@ public function createScopeReaders() */ public function testGenerate($className, $resultClassName, $fileName) { + $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + /** @var AreasPluginList $areaPlugins */ + $areaPlugins = $objectManagerHelper->getObject( + AreasPluginList::class, + [ + 'areaList' => $this->areaList, + 'plugins' => $this->createScopeReaders() + ] + ); + + /** @var CompiledInterceptor|MockObject $interceptor */ $interceptor = $this->getMockBuilder(CompiledInterceptor::class) ->setMethods(['_validateData']) ->setConstructorArgs( [ - $this->areaList, + $areaPlugins, $className, $resultClassName, $this->ioGenerator, null, - null, - $this->createScopeReaders() + null ] ) ->getMock(); From d22a34eecdeab54478d9206ea5a4d4e6cca459c4 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Mon, 10 Jun 2019 15:53:58 +0200 Subject: [PATCH 21/52] optimise generation time from empty cache --- .../Generator/AreasPluginList.php | 27 +++++++++++++------ .../Generator/CompiledInterceptor.php | 2 -- .../Generator/CompiledPluginList.php | 21 ++++++++++----- .../Generator/StaticScope.php | 4 +-- .../CompiledInterceptorTest.php | 9 +++++-- .../CompiledPluginListTest.php | 3 ++- 6 files changed, 44 insertions(+), 22 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php index 09f1cdb50f342..c3ba703a195aa 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php @@ -7,22 +7,27 @@ use Magento\Framework\App\AreaList; use Magento\Framework\App\ObjectManager; +use Magento\Framework\Config\Scope; class AreasPluginList { /** - * @var AreaList + * @var Scope */ - private $areaList; + private $scope; private $plugins; + /** + * AreasPluginList constructor. + * @param Scope $scope + * @param null $plugins + */ public function __construct( - AreaList $areaList, + Scope $scope, $plugins = null ) { - - $this->areaList = $areaList; + $this->scope = $scope; $this->plugins = $plugins; } @@ -33,10 +38,16 @@ public function getPluginsConfigForAllAreas() { if ($this->plugins === null) { $this->plugins = []; - foreach ($this->areaList->getCodes() as $scope) { - $this->plugins[$scope] = new CompiledPluginList(ObjectManager::getInstance(), $scope); + //this is to emulate order M2 is reading scopes config to use scope cache + //"global|primary" should be loaded first and then "global|primary|frontend" etc. + $core = new CompiledPluginList(ObjectManager::getInstance(), new StaticScope('primary')); + $core->getNext('dummy', 'dummy'); + $this->plugins['primary'] = $core; + foreach ($this->scope->getAllScopes() as $scope) { + $this->plugins[$scope] = clone $core; + $this->plugins[$scope]->setScope(new StaticScope($scope)); + $this->plugins[$scope]->getNext('dummy', 'dummy'); } - $this->plugins['primary'] = new CompiledPluginList(ObjectManager::getInstance(), 'primary'); } return $this->plugins; } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 55a0defe3e526..b01b802393a47 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -6,7 +6,6 @@ namespace Magento\Framework\CompiledInterception\Generator; -use Magento\Framework\App\ObjectManager; use Magento\Framework\Code\Generator\CodeGeneratorInterface; use Magento\Framework\Code\Generator\DefinedClasses; use Magento\Framework\Code\Generator\Io; @@ -14,7 +13,6 @@ use Magento\Framework\Code\Generator\EntityAbstract; use Magento\Framework\Config\ScopeInterface; use Magento\Framework\Interception\DefinitionInterface; -use Magento\Framework\App\AreaList; /** * Compiled interceptors generator, please see ../README.md for details diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php index dff64bc12382e..6fdc84ab8dfdd 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php @@ -7,6 +7,7 @@ use Magento\Framework\App\ObjectManager; use Magento\Framework\Config\ReaderInterface; +use Magento\Framework\Config\ScopeInterface; use Magento\Framework\Interception\PluginList\PluginList; use Magento\Framework\Interception\ObjectManager\ConfigInterface; use Magento\Framework\ObjectManager\Config\Reader\Dom; @@ -22,14 +23,14 @@ class CompiledPluginList extends PluginList /** * CompiledPluginList constructor. * @param ObjectManager $objectManager - * @param string $scope + * @param ScopeInterface $scope * @param null|ReaderInterface $reader * @param null|ConfigInterface $omConfig * @param null|string $cachePath */ public function __construct( - $objectManager, - $scope, + ObjectManager $objectManager, + ScopeInterface $scope, ReaderInterface $reader = null, ConfigInterface $omConfig = null, $cachePath = null @@ -40,15 +41,15 @@ public function __construct( } parent::__construct( $reader, - new StaticScope($scope), + $scope, new FileCache($cachePath), new ObjectManagerRelationsRuntime(), $omConfig, new InterceptionDefinitionRuntime(), $objectManager, new ObjectManagerDefinitionRuntime(), - ['first' => 'global'], - 'compiled_plugins_' . $scope, + ['global'], + 'compiled_plugins', new NoSerialize() ); } @@ -77,4 +78,12 @@ public function getPluginType($type, $code) { return $this->_inherited[$type][$code]['instance']; } + + /** + * @param ScopeInterface $scope + */ + public function setScope(ScopeInterface $scope) + { + $this->_configScope = $scope; + } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php index 99769b19274ad..f4a362c721ba4 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php @@ -41,11 +41,9 @@ public function getCurrentScope() * Unused interface method * * @param string $scope - * @throws \Exception - * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function setCurrentScope($scope) { - throw new \RuntimeException('readonly'); + $this->scope = $scope; } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php index 0e8856d3baf05..eb7a954527d8b 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php @@ -7,6 +7,7 @@ namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor; use Magento\Framework\CompiledInterception\Generator\AreasPluginList; +use Magento\Framework\CompiledInterception\Generator\StaticScope; use Magento\Framework\App\AreaList; use Magento\Framework\Code\Generator\Io; use Magento\Framework\CompiledInterception\Generator\CompiledInterceptor; @@ -72,7 +73,7 @@ public function createScopeReaders() CompiledPluginList::class, [ 'objectManager' => $omMock, - 'scope' => $readerLine[0], + 'scope' => new StaticScope($readerLine[0]), 'reader' => $readerMock, 'omConfig' => $omConfigMock, 'cachePath' => false @@ -132,7 +133,11 @@ public function testGenerate($className, $resultClassName, $fileName) /* eval( $code ); $className = "\\$resultClassName"; - $interceptor = new $className(); + $interceptor = new $className( + ??, + new StaticScope('frontend') + ); + $interceptor->getName(); */ } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php index debf7a415d3ed..2552c06950e2f 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php @@ -5,6 +5,7 @@ */ namespace Magento\Framework\CompiledInterception\Test\Unit\CompiledPluginList; +use Magento\Framework\CompiledInterception\Generator\StaticScope; use Magento\Framework\App\ObjectManager; use Magento\Framework\CompiledInterception\Generator\CompiledPluginList; use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; @@ -49,7 +50,7 @@ public function createScopeReaders() CompiledPluginList::class, [ 'objectManager' => $omMock, - 'scope' => $readerLine[0], + 'scope' => new StaticScope($readerLine[0]), 'reader' => $readerMock, 'omConfig' => $omConfigMock, 'cachePath' => false From 5e62ebcc5592299fd0be7fe77c4ec1a11b448bd0 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Wed, 12 Jun 2019 15:11:13 +0200 Subject: [PATCH 22/52] further optimisation and default scope fix --- .../Generator/AreasPluginList.php | 23 ++++++---- .../Generator/CompiledInterceptor.php | 46 ++++++++++++------- .../Framework/CompiledInterception/README.md | 3 +- .../_out_interceptors/ComplexItem.txt | 4 +- .../_out_interceptors/Item.txt | 4 +- 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php index c3ba703a195aa..92465968a5488 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php @@ -5,17 +5,15 @@ */ namespace Magento\Framework\CompiledInterception\Generator; -use Magento\Framework\App\AreaList; use Magento\Framework\App\ObjectManager; use Magento\Framework\Config\Scope; class AreasPluginList { - /** - * @var Scope - */ + /** @var Scope */ private $scope; + /** @var null */ private $plugins; /** @@ -40,14 +38,19 @@ public function getPluginsConfigForAllAreas() $this->plugins = []; //this is to emulate order M2 is reading scopes config to use scope cache //"global|primary" should be loaded first and then "global|primary|frontend" etc. - $core = new CompiledPluginList(ObjectManager::getInstance(), new StaticScope('primary')); - $core->getNext('dummy', 'dummy'); - $this->plugins['primary'] = $core; + $defaultScopePluginList = $defaultScope = null; foreach ($this->scope->getAllScopes() as $scope) { - $this->plugins[$scope] = clone $core; - $this->plugins[$scope]->setScope(new StaticScope($scope)); - $this->plugins[$scope]->getNext('dummy', 'dummy'); + if ($defaultScopePluginList === null) { + $defaultScopePluginList = new CompiledPluginList(ObjectManager::getInstance(), new StaticScope($scope)); + $defaultScopePluginList->getNext('dummy', 'dummy'); + $defaultScope = $scope; + } else { + $this->plugins[$scope] = clone $defaultScopePluginList; + $this->plugins[$scope]->setScope(new StaticScope($scope)); + //$this->plugins[$scope]->getNext('dummy', 'dummy'); + } } + $this->plugins[$defaultScope] = $defaultScopePluginList; } return $this->plugins; } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index b01b802393a47..45d9e08a8540e 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -13,6 +13,7 @@ use Magento\Framework\Code\Generator\EntityAbstract; use Magento\Framework\Config\ScopeInterface; use Magento\Framework\Interception\DefinitionInterface; +use Magento\Setup\Module\Di\App\Task\Operation\Area; /** * Compiled interceptors generator, please see ../README.md for details @@ -545,23 +546,30 @@ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) $parameters = $method->getParameters(); $returnsVoid = ($method->hasReturnType() && $method->getReturnType()->getName() == 'void'); - $body = [ - 'switch ($this->____scope->getCurrentScope()) {' - ]; + $cases = $this->getScopeCasesFromConfig($config); - foreach ($this->getScopeCasesFromConfig($config) as $case) { - $body = array_merge($body, $case['cases']); - $this->addCodeSubBlock( - $body, - $this->getMethodSourceFromConfig($method->getName(), $case['conf'], $parameters, $returnsVoid), - 2 - ); - if ($returnsVoid) { - $body[] = "\t\tbreak;"; + if (count($cases) == 1) { + $body = $this->getMethodSourceFromConfig($method->getName(), $cases[0]['conf'], $parameters, $returnsVoid); + } else { + $body = [ + 'switch ($this->____scope->getCurrentScope()) {' + ]; + + foreach ($cases as $case) { + $body = array_merge($body, $case['cases']); + $this->addCodeSubBlock( + $body, + $this->getMethodSourceFromConfig($method->getName(), $case['conf'], $parameters, $returnsVoid), + 2 + ); + if ($returnsVoid) { + $body[] = "\t\tbreak;"; + } } + + $body[] = "}"; } - $body[] = "}"; $returnType = $method->getReturnType(); $returnTypeValue = $returnType ? ($returnType->allowsNull() ? '?' : '') . $returnType->getName() @@ -589,16 +597,16 @@ private function getScopeCasesFromConfig($config) $cases = []; //group cases by config foreach ($config as $scope => $conf) { + $caseStr = "\tcase '$scope':"; foreach ($cases as &$case) { if ($case['conf'] == $conf) { - $case['cases'][] = "\tcase '$scope':"; + $case['cases'][] = $caseStr; continue 2; } } - $cases[] = ['cases'=>["\tcase '$scope':"], 'conf'=>$conf]; + $cases[] = ['cases'=>[$caseStr], 'conf'=>$conf]; } - //call parent method for scopes with no plugins (or when no scope is set) - $cases[] = ['cases'=>["\tdefault:"], 'conf'=>[]]; + $cases[count($cases) - 1]['cases'] = ["\tdefault:"]; return $cases; } @@ -700,6 +708,10 @@ private function getPluginsConfig(\ReflectionMethod $method, &$allPlugins) $result[$scope] = $pluginChain; } } + //if plugins are not empty make sure default case will be handled + if (!empty($result) && !$pluginChain) { + $result[$scope] = []; + } return $result; } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/README.md b/lib/internal/Magento/Framework/CompiledInterception/README.md index 3d9a1f7177069..ca85ea99cd55b 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/README.md +++ b/lib/internal/Magento/Framework/CompiledInterception/README.md @@ -84,6 +84,5 @@ public function methodX($arg) { #### CONS -* Each time after making change in etc plugins config, `generated/code/*` needs to be purged -* Tiny longer code generation step when run with no cache +* Each time after making change in etc plugins config, `generated/code/*` and `var/cache/*` needs to be purged * As this does not load plugins at runtime, might not work in an edge case of plugging into core Magento classes like `PluginsList` etc. diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt index b43036b47ffed..082f7703b2d41 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt @@ -53,7 +53,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati }); return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; - case 'backend': + default: $this->____plugin_advanced_plugin()->beforeGetName($this); $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ @@ -65,8 +65,6 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati }); return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; - default: - return parent::getName(); } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt index 3573cd0e44054..566f67d82149d 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt @@ -48,7 +48,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati $result = parent::getName(); return (($tmp = $this->____plugin_simple_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; - case 'backend': + default: $this->____plugin_advanced_plugin()->beforeGetName($this); $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ @@ -58,8 +58,6 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati }); return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; - default: - return parent::getName(); } } From 9c0aad85ca773dcd3ddebc8f2be145df57d1ce9f Mon Sep 17 00:00:00 2001 From: Yevhen Sentiabov Date: Thu, 20 Jun 2019 11:08:46 -0500 Subject: [PATCH 23/52] Replaced preferences usage --- app/etc/di.xml | 4 +- .../Code/Generator/Interceptor.php | 2 +- .../Code/Generator/InterceptorInterface.php | 13 +++ .../Console/Command/DiCompileCommand.php | 81 +++++++++---------- .../Di/App/Task/Operation/Interception.php | 15 +++- .../Config/Chain/InterceptorSubstitution.php | 2 +- .../InterceptorSubstitutionInterface.php | 13 +++ 7 files changed, 83 insertions(+), 47 deletions(-) create mode 100644 lib/internal/Magento/Framework/Interception/Code/Generator/InterceptorInterface.php create mode 100644 setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitutionInterface.php diff --git a/app/etc/di.xml b/app/etc/di.xml index fd685361be92a..31775f052f568 100755 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -751,7 +751,7 @@ \Magento\Framework\Api\Code\Generator\ExtensionAttributesInterfaceFactoryGenerator \Magento\Framework\ObjectManager\Code\Generator\Factory \Magento\Framework\ObjectManager\Code\Generator\Proxy - \Magento\Framework\Interception\Code\Generator\Interceptor + Magento\Framework\Interception\Code\Generator\InterceptorInterface \Magento\Framework\ObjectManager\Profiler\Code\Generator\Logger \Magento\Framework\Api\Code\Generator\Mapper \Magento\Framework\ObjectManager\Code\Generator\Persistor @@ -764,6 +764,8 @@ + + diff --git a/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php b/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php index 9297ca25928d3..7d8c218843fbf 100644 --- a/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php +++ b/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php @@ -11,7 +11,7 @@ ˚* * @package Magento\Framework\Interception\Code\Generator */ -class Interceptor extends \Magento\Framework\Code\Generator\EntityAbstract +class Interceptor extends \Magento\Framework\Code\Generator\EntityAbstract implements InterceptorInterface { /** * Entity type diff --git a/lib/internal/Magento/Framework/Interception/Code/Generator/InterceptorInterface.php b/lib/internal/Magento/Framework/Interception/Code/Generator/InterceptorInterface.php new file mode 100644 index 0000000000000..3595053682691 --- /dev/null +++ b/lib/internal/Magento/Framework/Interception/Code/Generator/InterceptorInterface.php @@ -0,0 +1,13 @@ +deploymentConfig->get(ConfigOptionsListConstants::KEY_MODULES); if (!$config) { $messages[] = 'You cannot run this command because modules are not enabled. You can enable modules by' - . ' running the \'module:enable --all\' command.'; + . ' running the \'module:enable --all\' command.'; } return $messages; @@ -310,40 +310,39 @@ private function configureObjectManager(OutputInterface $output) 'preferences' => [\Magento\Framework\App\ObjectManager\ConfigWriterInterface::class => \Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem::class, ], \Magento\Setup\Module\Di\Compiler\Config\ModificationChain::class => [ - 'arguments' => [ - 'modificationsList' => [ - 'BackslashTrim' => [ - 'instance' => - \Magento\Setup\Module\Di\Compiler\Config\Chain\BackslashTrim::class - ], - 'PreferencesResolving' => [ - 'instance' => - \Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving::class - ], - 'InterceptorSubstitution' => [ - 'instance' => - \Magento\Setup\Module\Di\Compiler\Config\Chain\InterceptorSubstitution::class - ], - 'InterceptionPreferencesResolving' => [ - 'instance' => \Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving::class - ], - ] - ] - ], \Magento\Setup\Module\Di\Code\Generator\PluginList::class => [ - 'arguments' => [ - 'cache' => [ - 'instance' => \Magento\Framework\App\Interception\Cache\CompiledConfig::class - ] + 'arguments' => [ + 'modificationsList' => [ + 'BackslashTrim' => [ + 'instance' => + \Magento\Setup\Module\Di\Compiler\Config\Chain\BackslashTrim::class + ], + 'PreferencesResolving' => [ + 'instance' => + \Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving::class + ], + 'InterceptorSubstitution' => [ + 'instance' => InterceptorSubstitutionInterface::class + ], + 'InterceptionPreferencesResolving' => [ + 'instance' => \Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving::class + ], ] - ], \Magento\Setup\Module\Di\Code\Reader\ClassesScanner::class => [ - 'arguments' => [ - 'excludePatterns' => $this->excludedPathsList - ] - ], \Magento\Setup\Module\Di\Compiler\Log\Writer\Console::class => [ - 'arguments' => [ - 'output' => $output, + ] + ], \Magento\Setup\Module\Di\Code\Generator\PluginList::class => [ + 'arguments' => [ + 'cache' => [ + 'instance' => \Magento\Framework\App\Interception\Cache\CompiledConfig::class ] - ], + ] + ], \Magento\Setup\Module\Di\Code\Reader\ClassesScanner::class => [ + 'arguments' => [ + 'excludePatterns' => $this->excludedPathsList + ] + ], \Magento\Setup\Module\Di\Compiler\Log\Writer\Console::class => [ + 'arguments' => [ + 'output' => $output, + ] + ], ] ); } diff --git a/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php b/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php index 9c629998e555d..7e704a6d22daa 100644 --- a/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php +++ b/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php @@ -5,10 +5,11 @@ */ namespace Magento\Setup\Module\Di\App\Task\Operation; +use Magento\Framework\App; +use Magento\Framework\Interception\Code\Generator\Interceptor; use Magento\Setup\Module\Di\App\Task\OperationInterface; use Magento\Setup\Module\Di\Code\Generator\InterceptionConfigurationBuilder; -use Magento\Framework\Interception\Code\Generator\Interceptor; -use Magento\Framework\App; +use Magento\Setup\Module\Di\Code\Generator\Interceptor as InterceptorGenerator; use Magento\Setup\Module\Di\Code\GeneratorFactory; use Magento\Setup\Module\Di\Code\Reader\ClassesScanner; @@ -39,11 +40,17 @@ class Interception implements OperationInterface */ private $generatorFactory; + /** + * @var string + */ + private $interceptorGeneratorClass; + /** * @param InterceptionConfigurationBuilder $interceptionConfigurationBuilder * @param App\AreaList $areaList * @param ClassesScanner $classesScanner * @param GeneratorFactory $generatorFactory + * @param string $interceptorGeneratorClass * @param array $data */ public function __construct( @@ -51,6 +58,7 @@ public function __construct( App\AreaList $areaList, ClassesScanner $classesScanner, GeneratorFactory $generatorFactory, + string $interceptorGeneratorClass = InterceptorGenerator::class, $data = [] ) { $this->interceptionConfigurationBuilder = $interceptionConfigurationBuilder; @@ -58,6 +66,7 @@ public function __construct( $this->data = $data; $this->classesScanner = $classesScanner; $this->generatorFactory = $generatorFactory; + $this->interceptorGeneratorClass = $interceptorGeneratorClass; } /** @@ -92,7 +101,7 @@ public function doOperation() [ 'ioObject' => $generatorIo, 'generatedEntities' => [ - Interceptor::ENTITY_TYPE => \Magento\Setup\Module\Di\Code\Generator\Interceptor::class, + Interceptor::ENTITY_TYPE => $this->interceptorGeneratorClass, ] ] ); diff --git a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitution.php b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitution.php index 66cd7b1a595e6..9f99d5da90a84 100644 --- a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitution.php +++ b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitution.php @@ -7,7 +7,7 @@ use Magento\Setup\Module\Di\Compiler\Config\ModificationInterface; -class InterceptorSubstitution implements ModificationInterface +class InterceptorSubstitution implements ModificationInterface, InterceptorSubstitutionInterface { /** * Modifies input config diff --git a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitutionInterface.php b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitutionInterface.php new file mode 100644 index 0000000000000..52bb286a87033 --- /dev/null +++ b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitutionInterface.php @@ -0,0 +1,13 @@ + Date: Mon, 24 Jun 2019 09:30:06 +0200 Subject: [PATCH 24/52] manual backport of preferences usage fix --- .../Generator/CompiledInterceptor.php | 3 ++- .../CompiledInterceptorSubstitution.php | 20 +++++++++++++++++-- .../Framework/CompiledInterception/README.md | 10 +++++++--- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 45d9e08a8540e..253dd7bc3f4a7 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -9,6 +9,7 @@ use Magento\Framework\Code\Generator\CodeGeneratorInterface; use Magento\Framework\Code\Generator\DefinedClasses; use Magento\Framework\Code\Generator\Io; +use Magento\Framework\Interception\Code\Generator\InterceptorInterface; use Magento\Framework\ObjectManagerInterface; use Magento\Framework\Code\Generator\EntityAbstract; use Magento\Framework\Config\ScopeInterface; @@ -18,7 +19,7 @@ /** * Compiled interceptors generator, please see ../README.md for details */ -class CompiledInterceptor extends EntityAbstract +class CompiledInterceptor extends EntityAbstract implements InterceptorInterface { /** * Entity type diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php index 6efee3fec4b6f..10bbb54d4d7f4 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php @@ -6,12 +6,27 @@ namespace Magento\Framework\CompiledInterception\Generator; use Magento\Setup\Module\Di\Compiler\Config\Chain\InterceptorSubstitution; +use Magento\Setup\Module\Di\Compiler\Config\Chain\InterceptorSubstitutionInterface; +use Magento\Setup\Module\Di\Compiler\Config\ModificationInterface; /** * Class CompiledInterceptorSubstitution adds required parameters to interceptor constructor */ -class CompiledInterceptorSubstitution extends InterceptorSubstitution +class CompiledInterceptorSubstitution implements InterceptorSubstitutionInterface, ModificationInterface { + /** + * @var InterceptorSubstitution + */ + private $interceptorSubstitution; + + /** + * @param InterceptorSubstitution $interceptorSubstitution + */ + public function __construct(InterceptorSubstitution $interceptorSubstitution) + { + $this->interceptorSubstitution = $interceptorSubstitution; + } + /** * Modifies input config * @@ -20,7 +35,7 @@ class CompiledInterceptorSubstitution extends InterceptorSubstitution */ public function modify(array $config) { - $config = parent::modify($config); + $config = $this->interceptorSubstitution->modify($config); foreach ($config['arguments'] as $instanceName => &$arguments) { if (substr($instanceName, -12) === '\Interceptor') { @@ -36,6 +51,7 @@ public function modify(array $config) } } + return $config; } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/README.md b/lib/internal/Magento/Framework/CompiledInterception/README.md index ca85ea99cd55b..90a461ce91553 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/README.md +++ b/lib/internal/Magento/Framework/CompiledInterception/README.md @@ -16,13 +16,17 @@ The Interceptors generated by this plugin are 100% compatible with the ones gene To use compiled interceptors in developer mode please add following preference to your di.xml ``` - + ``` To use compiled interceptors in production or default mode please add following preferences to your di.xml ``` - - + + + + Creatuity\Interception\Generator\CompiledInterceptor + + ``` Clear generated files and cache: From a7fc720078c34db51be3f3ea22491bd5b8b46f95 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Mon, 24 Jun 2019 10:20:28 +0200 Subject: [PATCH 25/52] save plugins cache in generated code folder to simplify cache cleaning --- .../CompiledInterception/Generator/FileCache.php | 13 +++++++++++-- .../Framework/CompiledInterception/README.md | 2 +- .../CompiledInterceptor/CompiledInterceptorTest.php | 7 +++++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php index 13318a0159539..e13ea50997354 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php @@ -5,6 +5,7 @@ */ namespace Magento\Framework\CompiledInterception\Generator; +use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Config\CacheInterface; /** @@ -22,7 +23,7 @@ class FileCache implements CacheInterface public function __construct($cachePath = null) { if ($cachePath === null) { - $this->cachePath = BP . DIRECTORY_SEPARATOR . 'var' . DIRECTORY_SEPARATOR . 'cache'; + $this->cachePath = BP . DIRECTORY_SEPARATOR . DirectoryList::GENERATED . DIRECTORY_SEPARATOR . 'staticcache'; } else { $this->cachePath = $cachePath; } @@ -79,7 +80,7 @@ public function save($data, $identifier, array $tags = [], $lifeTime = null) if ($this->cachePath) { $path = $this->getCachePath($identifier); if (!is_dir(dirname($path))) { - mkdir(dirname($path)); + mkdir(dirname($path), 0777, true); } file_put_contents( $path, @@ -111,6 +112,14 @@ public function remove($identifier) */ public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = []) { + if ($this->cachePath) { + foreach (glob($this->cachePath . '/*') as $file) { + if (is_file($file)) { + unlink($file); + } + } + return true; + } return false; } diff --git a/lib/internal/Magento/Framework/CompiledInterception/README.md b/lib/internal/Magento/Framework/CompiledInterception/README.md index 90a461ce91553..b86a298d004c3 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/README.md +++ b/lib/internal/Magento/Framework/CompiledInterception/README.md @@ -31,7 +31,7 @@ To use compiled interceptors in production or default mode please add following Clear generated files and cache: -`rm -rf generated/* && rm -rf var/cache/* && bin/magento cache:clean` +`rm -rf generated/* && bin/magento cache:clean` ### DISABLING diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php index eb7a954527d8b..0d78c5c100b1f 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php @@ -6,7 +6,9 @@ namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor; +use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\CompiledInterception\Generator\AreasPluginList; +use Magento\Framework\CompiledInterception\Generator\FileCache; use Magento\Framework\CompiledInterception\Generator\StaticScope; use Magento\Framework\App\AreaList; use Magento\Framework\Code\Generator\Io; @@ -68,6 +70,8 @@ public function createScopeReaders() $omConfigMock->expects($this->any())->method('getOriginalInstanceType')->will($this->returnArgument(0)); $ret = []; $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + //clear static cache + (new FileCache())->clean(); foreach ($readerMap as $readerLine) { $ret[$readerLine[0]] = $objectManagerHelper->getObject( CompiledPluginList::class, @@ -75,8 +79,7 @@ public function createScopeReaders() 'objectManager' => $omMock, 'scope' => new StaticScope($readerLine[0]), 'reader' => $readerMock, - 'omConfig' => $omConfigMock, - 'cachePath' => false + 'omConfig' => $omConfigMock ] ); } From 1657b46e52d5f73e150e1dee755be75144b24461 Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Mon, 24 Jun 2019 11:00:49 +0200 Subject: [PATCH 26/52] coding standards fix --- .../Generator/AreasPluginList.php | 15 +++++++++++---- .../Generator/CompiledInterceptor.php | 15 +++++++++------ .../Generator/CompiledInterceptorSubstitution.php | 2 +- .../Generator/CompiledPluginList.php | 2 ++ .../CompiledInterception/Generator/FileCache.php | 7 +++++-- .../CompiledInterceptorTest.php | 3 +-- 6 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php index 92465968a5488..6bd7ea252d29f 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php @@ -8,18 +8,21 @@ use Magento\Framework\App\ObjectManager; use Magento\Framework\Config\Scope; +/** + * Class AreasPluginList + */ class AreasPluginList { /** @var Scope */ private $scope; - /** @var null */ + /** @var array */ private $plugins; /** * AreasPluginList constructor. * @param Scope $scope - * @param null $plugins + * @param array|null $plugins */ public function __construct( Scope $scope, @@ -30,6 +33,8 @@ public function __construct( } /** + * Get array of plugns config indexed by scope code + * * @return array */ public function getPluginsConfigForAllAreas() @@ -41,7 +46,10 @@ public function getPluginsConfigForAllAreas() $defaultScopePluginList = $defaultScope = null; foreach ($this->scope->getAllScopes() as $scope) { if ($defaultScopePluginList === null) { - $defaultScopePluginList = new CompiledPluginList(ObjectManager::getInstance(), new StaticScope($scope)); + $defaultScopePluginList = new CompiledPluginList( + ObjectManager::getInstance(), + new StaticScope($scope) + ); $defaultScopePluginList->getNext('dummy', 'dummy'); $defaultScope = $scope; } else { @@ -54,5 +62,4 @@ public function getPluginsConfigForAllAreas() } return $this->plugins; } - } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 253dd7bc3f4a7..528286afc2ec1 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -71,11 +71,11 @@ public function __construct( * Unused function required by production mode interface * * @param mixed $interceptedMethods - * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function setInterceptedMethods($interceptedMethods) { - //NOOP + //this is not used as methods are read from reflaction + $this->interceptedMethods = $interceptedMethods; } /** @@ -257,10 +257,13 @@ private function injectPropertiesSettersToConstructor(\ReflectionMethod $parentC } $parameters = array_map([$this, '_getMethodParameterInfo'], $parameters); foreach ($extraParams as $type => $name) { - array_unshift($parameters, [ - 'name' => $name, - 'type' => $type - ]); + array_unshift( + $parameters, + [ + 'name' => $name, + 'type' => $type + ] + ); } foreach ($extraSetters as $name => $paramName) { array_unshift($body, "\$this->$name = \$$paramName;"); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php index 10bbb54d4d7f4..cd5d373a51bd4 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php @@ -39,7 +39,7 @@ public function modify(array $config) foreach ($config['arguments'] as $instanceName => &$arguments) { if (substr($instanceName, -12) === '\Interceptor') { - foreach (CompiledInterceptor::propertiesToInjectToConstructor() as $type => $name) { + foreach (CompiledInterceptor::propertiesToInjectToConstructor() as $type => $name) { $preference = isset($config['preferences'][$type]) ? $config['preferences'][$type] : $type; foreach ($arguments as $argument) { if (isset($argument['_i_']) && $argument['_i_'] == $preference) { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php index 6fdc84ab8dfdd..ed4cdf0e5f0ea 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php @@ -80,6 +80,8 @@ public function getPluginType($type, $code) } /** + * Set current scope + * * @param ScopeInterface $scope */ public function setScope(ScopeInterface $scope) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php index e13ea50997354..6baf7dc28c871 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php @@ -23,7 +23,9 @@ class FileCache implements CacheInterface public function __construct($cachePath = null) { if ($cachePath === null) { - $this->cachePath = BP . DIRECTORY_SEPARATOR . DirectoryList::GENERATED . DIRECTORY_SEPARATOR . 'staticcache'; + $this->cachePath = BP . DIRECTORY_SEPARATOR . + DirectoryList::GENERATED . DIRECTORY_SEPARATOR . + 'staticcache'; } else { $this->cachePath = $cachePath; } @@ -62,7 +64,8 @@ public function test($identifier) */ public function load($identifier) { - return $this->cachePath ? @include($this->getCachePath($identifier)) : false; + // @codingStandardsIgnoreLine + return $this->cachePath ? @include $this->getCachePath($identifier) : false; } /** diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php index 0d78c5c100b1f..2474182ce5d41 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php @@ -6,7 +6,6 @@ namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor; -use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\CompiledInterception\Generator\AreasPluginList; use Magento\Framework\CompiledInterception\Generator\FileCache; use Magento\Framework\CompiledInterception\Generator\StaticScope; @@ -24,6 +23,7 @@ /** * Class CompiledInterceptorTest + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class CompiledInterceptorTest extends \PHPUnit\Framework\TestCase { @@ -106,7 +106,6 @@ public function testGenerate($className, $resultClassName, $fileName) ] ); - /** @var CompiledInterceptor|MockObject $interceptor */ $interceptor = $this->getMockBuilder(CompiledInterceptor::class) ->setMethods(['_validateData']) From 07bfca96e419f380390ffc20874aa258e2dd0f0a Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Mon, 24 Jun 2019 12:06:12 +0200 Subject: [PATCH 27/52] move sompiledinterceptorsubstitution to avoid dependency issues, fix CS in modified core files --- .../Framework/CompiledInterception/README.md | 6 +- .../Code/Generator/InterceptorInterface.php | 8 ++ .../Console/Command/DiCompileCommand.php | 84 +++++++++++-------- .../Di/App/Task/Operation/Interception.php | 5 +- .../CompiledInterceptorSubstitution.php | 5 +- .../Config/Chain/InterceptorSubstitution.php | 3 + 6 files changed, 67 insertions(+), 44 deletions(-) rename {lib/internal/Magento/Framework/CompiledInterception/Generator => setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain}/CompiledInterceptorSubstitution.php (88%) diff --git a/lib/internal/Magento/Framework/CompiledInterception/README.md b/lib/internal/Magento/Framework/CompiledInterception/README.md index b86a298d004c3..a6993ce0ec25f 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/README.md +++ b/lib/internal/Magento/Framework/CompiledInterception/README.md @@ -16,15 +16,15 @@ The Interceptors generated by this plugin are 100% compatible with the ones gene To use compiled interceptors in developer mode please add following preference to your di.xml ``` - + ``` To use compiled interceptors in production or default mode please add following preferences to your di.xml ``` - + - Creatuity\Interception\Generator\CompiledInterceptor + Magento\Framework\CompiledInterception\Generator\CompiledInterceptor ``` diff --git a/lib/internal/Magento/Framework/Interception/Code/Generator/InterceptorInterface.php b/lib/internal/Magento/Framework/Interception/Code/Generator/InterceptorInterface.php index 3595053682691..4cf23952d8864 100644 --- a/lib/internal/Magento/Framework/Interception/Code/Generator/InterceptorInterface.php +++ b/lib/internal/Magento/Framework/Interception/Code/Generator/InterceptorInterface.php @@ -7,7 +7,15 @@ namespace Magento\Framework\Interception\Code\Generator; +/** + * Interface InterceptorInterface + */ interface InterceptorInterface { + /** + * Generation template method + * + * @return mixed + */ public function generate(); } diff --git a/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php b/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php index b2bbba4a733f9..beda6c8ec572a 100644 --- a/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php +++ b/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php @@ -7,6 +7,7 @@ use Magento\Framework\App\DeploymentConfig; use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Framework\App\ObjectManager\ConfigWriterInterface; use Magento\Framework\Component\ComponentRegistrar; use Magento\Framework\Config\ConfigOptionsListConstants; use Magento\Framework\Console\Cli; @@ -227,9 +228,13 @@ private function getExcludedModulePaths(array $modulePaths) { $modulesByBasePath = []; foreach ($modulePaths as $modulePath) { + // phpcs:ignore Magento2.Functions.DiscouragedFunction $moduleDir = basename($modulePath); + // phpcs:ignore Magento2.Functions.DiscouragedFunction $vendorPath = dirname($modulePath); + // phpcs:ignore Magento2.Functions.DiscouragedFunction $vendorDir = basename($vendorPath); + // phpcs:ignore Magento2.Functions.DiscouragedFunction $basePath = dirname($vendorPath); $modulesByBasePath[$basePath][$vendorDir][] = $moduleDir; } @@ -260,9 +265,12 @@ private function getExcludedModulePaths(array $modulePaths) */ private function getExcludedLibraryPaths(array $libraryPaths) { - $libraryPaths = array_map(function ($libraryPath) { - return preg_quote($libraryPath, '#'); - }, $libraryPaths); + $libraryPaths = array_map( + function ($libraryPath) { + return preg_quote($libraryPath, '#'); + }, + $libraryPaths + ); $excludedLibraryPaths = [ '#^(?:' . join('|', $libraryPaths) . ')/([\\w]+/)?Test#', @@ -307,42 +315,44 @@ private function configureObjectManager(OutputInterface $output) { $this->objectManager->configure( [ - 'preferences' => [\Magento\Framework\App\ObjectManager\ConfigWriterInterface::class => - \Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem::class, - ], \Magento\Setup\Module\Di\Compiler\Config\ModificationChain::class => [ - 'arguments' => [ - 'modificationsList' => [ - 'BackslashTrim' => [ - 'instance' => - \Magento\Setup\Module\Di\Compiler\Config\Chain\BackslashTrim::class - ], - 'PreferencesResolving' => [ - 'instance' => - \Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving::class - ], - 'InterceptorSubstitution' => [ - 'instance' => InterceptorSubstitutionInterface::class - ], - 'InterceptionPreferencesResolving' => [ - 'instance' => \Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving::class - ], + 'preferences' => [ + ConfigWriterInterface::class => \Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem::class, + ], + \Magento\Setup\Module\Di\Compiler\Config\ModificationChain::class => [ + 'arguments' => [ + 'modificationsList' => [ + 'BackslashTrim' => [ + 'instance' => \Magento\Setup\Module\Di\Compiler\Config\Chain\BackslashTrim::class + ], + 'PreferencesResolving' => [ + 'instance' => \Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving::class + ], + 'InterceptorSubstitution' => [ + 'instance' => InterceptorSubstitutionInterface::class + ], + 'InterceptionPreferencesResolving' => [ + 'instance' => \Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving::class + ], + ] ] - ] - ], \Magento\Setup\Module\Di\Code\Generator\PluginList::class => [ - 'arguments' => [ - 'cache' => [ - 'instance' => \Magento\Framework\App\Interception\Cache\CompiledConfig::class + ], + \Magento\Setup\Module\Di\Code\Generator\PluginList::class => [ + 'arguments' => [ + 'cache' => [ + 'instance' => \Magento\Framework\App\Interception\Cache\CompiledConfig::class + ] ] - ] - ], \Magento\Setup\Module\Di\Code\Reader\ClassesScanner::class => [ - 'arguments' => [ - 'excludePatterns' => $this->excludedPathsList - ] - ], \Magento\Setup\Module\Di\Compiler\Log\Writer\Console::class => [ - 'arguments' => [ - 'output' => $output, - ] - ], + ], + \Magento\Setup\Module\Di\Code\Reader\ClassesScanner::class => [ + 'arguments' => [ + 'excludePatterns' => $this->excludedPathsList + ] + ], + \Magento\Setup\Module\Di\Compiler\Log\Writer\Console::class => [ + 'arguments' => [ + 'output' => $output, + ] + ], ] ); } diff --git a/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php b/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php index 7e704a6d22daa..54e29d525969f 100644 --- a/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php +++ b/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php @@ -13,6 +13,9 @@ use Magento\Setup\Module\Di\Code\GeneratorFactory; use Magento\Setup\Module\Di\Code\Reader\ClassesScanner; +/** + * Class Interception + */ class Interception implements OperationInterface { /** @@ -70,7 +73,7 @@ public function __construct( } /** - * {@inheritdoc} + * @inheritdoc */ public function doOperation() { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php similarity index 88% rename from lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php rename to setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php index cd5d373a51bd4..ab54a47adf8ca 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptorSubstitution.php +++ b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php @@ -3,10 +3,9 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ -namespace Magento\Framework\CompiledInterception\Generator; +namespace Magento\Setup\Module\Di\Compiler\Config\Chain; -use Magento\Setup\Module\Di\Compiler\Config\Chain\InterceptorSubstitution; -use Magento\Setup\Module\Di\Compiler\Config\Chain\InterceptorSubstitutionInterface; +use Magento\Framework\CompiledInterception\Generator\CompiledInterceptor; use Magento\Setup\Module\Di\Compiler\Config\ModificationInterface; /** diff --git a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitution.php b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitution.php index 9f99d5da90a84..d3a3439af91dc 100644 --- a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitution.php +++ b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitution.php @@ -7,6 +7,9 @@ use Magento\Setup\Module\Di\Compiler\Config\ModificationInterface; +/** + * Class InterceptorSubstitution + */ class InterceptorSubstitution implements ModificationInterface, InterceptorSubstitutionInterface { /** From 7f268ce7cbe8ffaa4317507c2d4020c4e258a0ff Mon Sep 17 00:00:00 2001 From: Franciszek Wawrzak Date: Fri, 12 Jul 2019 11:30:57 +0200 Subject: [PATCH 28/52] fix for plugins overriden by preferences --- .../CompiledInterception/Generator/CompiledInterceptor.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 528286afc2ec1..dcbf347b3eb9b 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -531,7 +531,6 @@ private function getPluginGetterInfo($plugin) 'visibility' => 'private', 'parameters' => [], 'body' => implode("\n", $body), - 'returnType' => $plugin['class'], 'docblock' => [ 'shortDescription' => 'plugin "' . $plugin['code'] . '"' . "\n" . '@return \\' . $plugin['class'] ], From 8b3cae647e0f37392a9c5dc0182040a3ecca699e Mon Sep 17 00:00:00 2001 From: Stanislav Idolov Date: Tue, 1 Oct 2019 14:47:08 -0500 Subject: [PATCH 29/52] Removed commented line --- .../Framework/CompiledInterception/Generator/AreasPluginList.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php index 6bd7ea252d29f..d7553311cc392 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php @@ -55,7 +55,6 @@ public function getPluginsConfigForAllAreas() } else { $this->plugins[$scope] = clone $defaultScopePluginList; $this->plugins[$scope]->setScope(new StaticScope($scope)); - //$this->plugins[$scope]->getNext('dummy', 'dummy'); } } $this->plugins[$defaultScope] = $defaultScopePluginList; From 2bc739d23a2b6ae683e88508b98318eed776dca8 Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Thu, 17 Oct 2019 16:34:12 +0300 Subject: [PATCH 30/52] magento/magento2#22826: Refactoring. --- app/etc/di.xml | 13 ++++ .../Generator/AreasPluginList.php | 45 +++++++++-- .../Generator/CompiledInterceptor.php | 47 +++++------ .../Generator/CompiledPluginList.php | 77 +++++++++---------- .../Generator/StaticScope.php | 2 +- .../CompiledInterceptorTest.php | 54 +++++++------ .../_files/reader_mock_map.php | 2 + .../_out_interceptors/ComplexItem.txt | 4 +- .../_out_interceptors/ComplexItemTyped.txt | 4 +- .../_out_interceptors/Item.txt | 4 +- .../_out_interceptors/SecondItem.txt | 14 ++-- .../CompiledPluginListTest.php | 53 +++++++++---- .../Custom/Module/Model/Item.php | 5 +- .../Custom/Module/Model/Item/Enhanced.php | 7 +- .../Module/Model/ItemPlugin/Advanced.php | 5 +- .../Custom/Module/Model/ItemPlugin/Simple.php | 5 +- .../_files/reader_mock_map.php | 10 ++- .../Interception/PluginList/PluginList.php | 24 ++++++ 18 files changed, 244 insertions(+), 131 deletions(-) rename lib/internal/Magento/Framework/CompiledInterception/Test/Integration/{ => CompiledInterceptor}/_files/reader_mock_map.php (99%) rename lib/internal/Magento/Framework/CompiledInterception/Test/{Unit => Integration}/CompiledPluginList/CompiledPluginListTest.php (69%) rename lib/internal/Magento/Framework/CompiledInterception/Test/{Unit => Integration/CompiledPluginList}/Custom/Module/Model/Item.php (65%) rename lib/internal/Magento/Framework/CompiledInterception/Test/{Unit => Integration/CompiledPluginList}/Custom/Module/Model/Item/Enhanced.php (62%) rename lib/internal/Magento/Framework/CompiledInterception/Test/{Unit => Integration/CompiledPluginList}/Custom/Module/Model/ItemPlugin/Advanced.php (86%) rename lib/internal/Magento/Framework/CompiledInterception/Test/{Unit => Integration/CompiledPluginList}/Custom/Module/Model/ItemPlugin/Simple.php (74%) rename lib/internal/Magento/Framework/CompiledInterception/Test/{Unit => Integration/CompiledPluginList}/_files/reader_mock_map.php (76%) diff --git a/app/etc/di.xml b/app/etc/di.xml index e6826dc88a2f7..9d86b3c4683e1 100755 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -765,6 +765,19 @@ + + + \Magento\Framework\CompiledInterception\Generator\FileCache + compiled_plugins + \Magento\Framework\CompiledInterception\Generator\NoSerialize + + + + + \Magento\Framework\CompiledInterception\PluginList\PluginList + + diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php index d7553311cc392..cc1d168bc6192 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php @@ -3,37 +3,59 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Framework\CompiledInterception\Generator; use Magento\Framework\App\ObjectManager; use Magento\Framework\Config\Scope; +use Magento\Framework\Config\ScopeInterfaceFactory; /** * Class AreasPluginList */ class AreasPluginList { - /** @var Scope */ + /** + * @var Scope + */ private $scope; - /** @var array */ + /** + * @var ScopeInterfaceFactory + */ + private $scopeInterfaceFactory; + + /** + * @var CompiledPluginListFactory + */ + private $compiledPluginListFactory; + + /** + * @var array + */ private $plugins; /** * AreasPluginList constructor. * @param Scope $scope + * @param ScopeInterfaceFactory $scopeInterfaceFactory + * @param CompiledPluginListFactory $compiledPluginListFactory * @param array|null $plugins */ public function __construct( Scope $scope, - $plugins = null + ScopeInterfaceFactory $scopeInterfaceFactory, + CompiledPluginListFactory $compiledPluginListFactory, + ?array $plugins = null ) { $this->scope = $scope; + $this->scopeInterfaceFactory = $scopeInterfaceFactory; $this->plugins = $plugins; + $this->compiledPluginListFactory = $compiledPluginListFactory; } /** - * Get array of plugns config indexed by scope code + * Get array of plugins config indexed by scope code * * @return array */ @@ -45,16 +67,23 @@ public function getPluginsConfigForAllAreas() //"global|primary" should be loaded first and then "global|primary|frontend" etc. $defaultScopePluginList = $defaultScope = null; foreach ($this->scope->getAllScopes() as $scope) { + $configScope = $this->scopeInterfaceFactory->create( + [ + 'scope' => $scope, + ] + ); if ($defaultScopePluginList === null) { - $defaultScopePluginList = new CompiledPluginList( - ObjectManager::getInstance(), - new StaticScope($scope) + $defaultScopePluginList = $this->compiledPluginListFactory->create( + [ + 'objectManager' => ObjectManager::getInstance(), + 'scope' => $configScope + ] ); $defaultScopePluginList->getNext('dummy', 'dummy'); $defaultScope = $scope; } else { $this->plugins[$scope] = clone $defaultScopePluginList; - $this->plugins[$scope]->setScope(new StaticScope($scope)); + $this->plugins[$scope]->setScope($configScope); } } $this->plugins[$defaultScope] = $defaultScopePluginList; diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index dcbf347b3eb9b..896ec3bd4cb21 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -8,26 +8,28 @@ use Magento\Framework\Code\Generator\CodeGeneratorInterface; use Magento\Framework\Code\Generator\DefinedClasses; -use Magento\Framework\Code\Generator\Io; -use Magento\Framework\Interception\Code\Generator\InterceptorInterface; -use Magento\Framework\ObjectManagerInterface; use Magento\Framework\Code\Generator\EntityAbstract; +use Magento\Framework\Code\Generator\Io; use Magento\Framework\Config\ScopeInterface; +use Magento\Framework\Interception\Code\Generator\InterceptorInterface; use Magento\Framework\Interception\DefinitionInterface; -use Magento\Setup\Module\Di\App\Task\Operation\Area; +use Magento\Framework\ObjectManagerInterface; /** * Compiled interceptors generator, please see ../README.md for details */ class CompiledInterceptor extends EntityAbstract implements InterceptorInterface { + public const ENTITY_TYPE = 'interceptor'; + /** - * Entity type + * @var array */ - const ENTITY_TYPE = 'interceptor'; - private $classMethods; + /** + * @var array + */ private $classProperties; /** @@ -74,10 +76,23 @@ public function __construct( */ public function setInterceptedMethods($interceptedMethods) { - //this is not used as methods are read from reflaction + // this is not used as methods are read from reflection $this->interceptedMethods = $interceptedMethods; } + /** + * Get properties to be injected from DI + * + * @return array + */ + public static function propertiesToInjectToConstructor() + { + return [ + ScopeInterface::class => '____scope', + ObjectManagerInterface::class => '____om', + ]; + } + /** * Get all class methods * @@ -159,19 +174,6 @@ private function generateMethodsAndProperties() } } - /** - * Get properties to be injected from DI - * - * @return array - */ - public static function propertiesToInjectToConstructor() - { - return [ - ScopeInterface::class => '____scope', - ObjectManagerInterface::class => '____om', - ]; - } - /** * Get reflection of source class * @@ -192,7 +194,7 @@ private function getSourceClassReflection() * @param \ReflectionMethod $method * @return bool */ - protected function isInterceptedMethod(\ReflectionMethod $method) + private function isInterceptedMethod(\ReflectionMethod $method) { return !($method->isConstructor() || $method->isFinal() || $method->isStatic() || $method->isDestructor()) && !in_array($method->getName(), ['__sleep', '__wakeup', '__clone']); @@ -559,6 +561,7 @@ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) ]; foreach ($cases as $case) { + // phpcs:ignore Magento2.Performance.ForeachArrayMerge $body = array_merge($body, $case['cases']); $this->addCodeSubBlock( $body, diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php index ed4cdf0e5f0ea..c6631b72ad31b 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php @@ -3,55 +3,30 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Framework\CompiledInterception\Generator; -use Magento\Framework\App\ObjectManager; -use Magento\Framework\Config\ReaderInterface; use Magento\Framework\Config\ScopeInterface; use Magento\Framework\Interception\PluginList\PluginList; -use Magento\Framework\Interception\ObjectManager\ConfigInterface; -use Magento\Framework\ObjectManager\Config\Reader\Dom; -use Magento\Framework\ObjectManager\Relations\Runtime as ObjectManagerRelationsRuntime; -use Magento\Framework\Interception\Definition\Runtime as InterceptionDefinitionRuntime; -use Magento\Framework\ObjectManager\Definition\Runtime as ObjectManagerDefinitionRuntime; +use Magento\Framework\Interception\PluginListInterface; /** * Class CompiledPluginList */ -class CompiledPluginList extends PluginList +class CompiledPluginList implements PluginListInterface { /** - * CompiledPluginList constructor. - * @param ObjectManager $objectManager - * @param ScopeInterface $scope - * @param null|ReaderInterface $reader - * @param null|ConfigInterface $omConfig - * @param null|string $cachePath + * @var PluginList + */ + private $pluginList; + + /** + * @param PluginList $pluginList */ public function __construct( - ObjectManager $objectManager, - ScopeInterface $scope, - ReaderInterface $reader = null, - ConfigInterface $omConfig = null, - $cachePath = null + PluginList $pluginList ) { - if (!$reader || !$omConfig) { - $reader = $objectManager->get(Dom::class); - $omConfig = $objectManager->get(ConfigInterface::class); - } - parent::__construct( - $reader, - $scope, - new FileCache($cachePath), - new ObjectManagerRelationsRuntime(), - $omConfig, - new InterceptionDefinitionRuntime(), - $objectManager, - new ObjectManagerDefinitionRuntime(), - ['global'], - 'compiled_plugins', - new NoSerialize() - ); + $this->pluginList = $pluginList; } /** @@ -67,6 +42,17 @@ public function getPlugin($type, $code) return null; } + /** + * Merge configuration + * + * @param array $config + * @return void + */ + public function merge(array $config) + { + $this->pluginList->merge($config); + } + /** * Get class of a plugin * @@ -74,9 +60,9 @@ public function getPlugin($type, $code) * @param string $code * @return mixed */ - public function getPluginType($type, $code) + public function getPluginType(string $type, string $code) { - return $this->_inherited[$type][$code]['instance']; + return $this->pluginList->getPluginType($type, $code); } /** @@ -86,6 +72,19 @@ public function getPluginType($type, $code) */ public function setScope(ScopeInterface $scope) { - $this->_configScope = $scope; + $this->pluginList->setScope($scope); + } + + /** + * Retrieve next plugins in chain + * + * @param string $type + * @param string $method + * @param string $code + * @return array + */ + public function getNext($type, $method, $code = null) + { + return $this->pluginList->getNext($type, $method, $code); } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php index f4a362c721ba4..4159c5bd22eb7 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php @@ -15,7 +15,7 @@ class StaticScope implements ScopeInterface /** * @var string */ - protected $scope; + private $scope; /** * StaticScope constructor. diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php index 2474182ce5d41..0d7f04aa32302 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php @@ -7,6 +7,7 @@ namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor; use Magento\Framework\CompiledInterception\Generator\AreasPluginList; +use Magento\Framework\CompiledInterception\Generator\CompiledPluginListFactory; use Magento\Framework\CompiledInterception\Generator\FileCache; use Magento\Framework\CompiledInterception\Generator\StaticScope; use Magento\Framework\App\AreaList; @@ -18,8 +19,13 @@ use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItem; use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped; use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\Item; +use Magento\Framework\Config\ScopeInterfaceFactory; +use Magento\Framework\Interception\ObjectManager\ConfigInterface; +use Magento\Framework\Interception\PluginList\PluginList; +use Magento\Framework\ObjectManager\Config\Reader\Dom; +use Magento\Framework\Serialize\Serializer\Serialize; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; use PHPUnit\Framework\MockObject\MockObject; -use Psr\Log\NullLogger; /** * Class CompiledInterceptorTest @@ -56,32 +62,44 @@ protected function setUp() */ public function createScopeReaders() { - $readerMap = include __DIR__ . '/../_files/reader_mock_map.php'; - $readerMock = $this->createMock(\Magento\Framework\ObjectManager\Config\Reader\Dom::class); - $readerMock->expects($this->any())->method('read')->will($this->returnValueMap($readerMap)); + $readerMap = include __DIR__ . '/_files/reader_mock_map.php'; + $readerMock = $this->createMock(Dom::class); + $readerMock->method('read')->willReturnMap($readerMap); $omMock = $this->createMock(ObjectManager::class); - $omMock->method('get')->with(\Psr\Log\LoggerInterface::class)->willReturn(new NullLogger()); + + $omMock->method('get') + ->with(Serialize::class) + ->willReturn( + ObjectManager::getInstance()->create(Serialize::class) + ); $omConfigMock = $this->getMockForAbstractClass( - \Magento\Framework\Interception\ObjectManager\ConfigInterface::class + ConfigInterface::class ); - $omConfigMock->expects($this->any())->method('getOriginalInstanceType')->will($this->returnArgument(0)); + $omConfigMock->method('getOriginalInstanceType')->willReturnArgument(0); $ret = []; - $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $objectManagerHelper = new ObjectManagerHelper($this); //clear static cache (new FileCache())->clean(); foreach ($readerMap as $readerLine) { - $ret[$readerLine[0]] = $objectManagerHelper->getObject( - CompiledPluginList::class, + $pluginList = ObjectManager::getInstance()->create( + PluginList::class, [ 'objectManager' => $omMock, - 'scope' => new StaticScope($readerLine[0]), + 'configScope' => new StaticScope($readerLine[0]), 'reader' => $readerMock, 'omConfig' => $omConfigMock ] ); + + $ret[$readerLine[0]] = $objectManagerHelper->getObject( + CompiledPluginList::class, + [ + 'pluginList' => $pluginList + ] + ); } return $ret; } @@ -96,12 +114,14 @@ public function createScopeReaders() */ public function testGenerate($className, $resultClassName, $fileName) { - $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $objectManagerHelper = new ObjectManagerHelper($this); /** @var AreasPluginList $areaPlugins */ $areaPlugins = $objectManagerHelper->getObject( AreasPluginList::class, [ 'areaList' => $this->areaList, + 'scopeInterfaceFactory' => $objectManagerHelper->getObject(ScopeInterfaceFactory::class), + 'compiledPluginListFactory' => $objectManagerHelper->getObject(CompiledPluginListFactory::class), 'plugins' => $this->createScopeReaders() ] ); @@ -131,16 +151,6 @@ public function testGenerate($className, $resultClassName, $fileName) $generated = $interceptor->generate(); $this->assertEquals($fileName . '.php', $generated, 'Generated interceptor is invalid.'); - - /* - eval( $code ); - $className = "\\$resultClassName"; - $interceptor = new $className( - ??, - new StaticScope('frontend') - ); - $interceptor->getName(); - */ } /** diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/_files/reader_mock_map.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_files/reader_mock_map.php similarity index 99% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/_files/reader_mock_map.php rename to lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_files/reader_mock_map.php index 279b3a2eea88a..c46e7417536e3 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/_files/reader_mock_map.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_files/reader_mock_map.php @@ -4,6 +4,8 @@ * See COPYING.txt for license details. */ +declare(strict_types=1); + use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItem; use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped; use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\Item; diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt index 082f7703b2d41..b9aca48724361 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt @@ -165,7 +165,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati * plugin "advanced_plugin" * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ - private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + private function ____plugin_advanced_plugin() { if ($this->____plugin_advanced_plugin === null) { $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); @@ -177,7 +177,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati * plugin "complex_plugin" * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex */ - private function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex + private function ____plugin_complex_plugin() { if ($this->____plugin_complex_plugin === null) { $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex::class); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt index fb4aa4836a2e3..b87e0f9e33c8d 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -189,7 +189,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati * plugin "complex_plugin" * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex */ - private function ____plugin_complex_plugin() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex + private function ____plugin_complex_plugin() { if ($this->____plugin_complex_plugin === null) { $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex::class); @@ -201,7 +201,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati * plugin "advanced_plugin" * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ - private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + private function ____plugin_advanced_plugin() { if ($this->____plugin_advanced_plugin === null) { $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt index 566f67d82149d..4f405c51b0752 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt @@ -65,7 +65,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati * plugin "simple_plugin" * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple */ - private function ____plugin_simple_plugin() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + private function ____plugin_simple_plugin() { if ($this->____plugin_simple_plugin === null) { $this->____plugin_simple_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); @@ -77,7 +77,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati * plugin "advanced_plugin" * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ - private function ____plugin_advanced_plugin() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + private function ____plugin_advanced_plugin() { if ($this->____plugin_advanced_plugin === null) { $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt index 9d16855e89398..f253592e6f933 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt @@ -109,7 +109,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati * plugin "advanced_plugin1" * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ - private function ____plugin_advanced_plugin1() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + private function ____plugin_advanced_plugin1() { if ($this->____plugin_advanced_plugin1 === null) { $this->____plugin_advanced_plugin1 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); @@ -121,7 +121,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati * plugin "simple_plugin1" * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple */ - private function ____plugin_simple_plugin1() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + private function ____plugin_simple_plugin1() { if ($this->____plugin_simple_plugin1 === null) { $this->____plugin_simple_plugin1 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); @@ -133,7 +133,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati * plugin "advanced_plugin2" * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ - private function ____plugin_advanced_plugin2() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + private function ____plugin_advanced_plugin2() { if ($this->____plugin_advanced_plugin2 === null) { $this->____plugin_advanced_plugin2 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); @@ -145,7 +145,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati * plugin "advanced_plugin3" * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ - private function ____plugin_advanced_plugin3() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + private function ____plugin_advanced_plugin3() { if ($this->____plugin_advanced_plugin3 === null) { $this->____plugin_advanced_plugin3 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); @@ -157,7 +157,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati * plugin "simple_plugin2" * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple */ - private function ____plugin_simple_plugin2() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + private function ____plugin_simple_plugin2() { if ($this->____plugin_simple_plugin2 === null) { $this->____plugin_simple_plugin2 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); @@ -169,7 +169,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati * plugin "simple_plugin3" * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple */ - private function ____plugin_simple_plugin3() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + private function ____plugin_simple_plugin3() { if ($this->____plugin_simple_plugin3 === null) { $this->____plugin_simple_plugin3 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); @@ -181,7 +181,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati * plugin "advanced_plugin4" * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ - private function ____plugin_advanced_plugin4() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + private function ____plugin_advanced_plugin4() { if ($this->____plugin_advanced_plugin4 === null) { $this->____plugin_advanced_plugin4 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/CompiledPluginListTest.php similarity index 69% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php rename to lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/CompiledPluginListTest.php index 2552c06950e2f..d675ce9f1d5ab 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledPluginList/CompiledPluginListTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/CompiledPluginListTest.php @@ -3,15 +3,25 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + +declare(strict_types=1); + namespace Magento\Framework\CompiledInterception\Test\Unit\CompiledPluginList; -use Magento\Framework\CompiledInterception\Generator\StaticScope; use Magento\Framework\App\ObjectManager; use Magento\Framework\CompiledInterception\Generator\CompiledPluginList; -use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; -use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item\Enhanced; -use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced; -use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple; +use Magento\Framework\CompiledInterception\Generator\FileCache; +use Magento\Framework\CompiledInterception\Generator\NoSerialize; +use Magento\Framework\CompiledInterception\Generator\StaticScope; +use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\Item; +use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\Item\Enhanced; +use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\ItemPlugin\Advanced; +use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\ItemPlugin\Simple; +use Magento\Framework\Interception\ObjectManager\ConfigInterface; +use Magento\Framework\Interception\PluginList\PluginList; +use Magento\Framework\ObjectManager\Config\Reader\Dom; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; /** @@ -31,29 +41,37 @@ protected function setUp() public function createScopeReaders() { - $readerMap = include __DIR__ . '/../_files/reader_mock_map.php'; - $readerMock = $this->createMock(\Magento\Framework\ObjectManager\Config\Reader\Dom::class); - $readerMock->expects($this->any())->method('read')->will($this->returnValueMap($readerMap)); + $readerMap = include __DIR__ . '/_files/reader_mock_map.php'; + $readerMock = $this->createMock(Dom::class); + $readerMock->method('read')->willReturnMap($readerMap); $omMock = $this->createMock(ObjectManager::class); - $omMock->method('get')->with(\Psr\Log\LoggerInterface::class)->willReturn(new NullLogger()); + $omMock->method('get')->with(LoggerInterface::class)->willReturn(new NullLogger()); $omConfigMock = $this->getMockForAbstractClass( - \Magento\Framework\Interception\ObjectManager\ConfigInterface::class + ConfigInterface::class ); - $omConfigMock->expects($this->any())->method('getOriginalInstanceType')->will($this->returnArgument(0)); + $omConfigMock->method('getOriginalInstanceType')->willReturnArgument(0); $ret = []; - $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $objectManagerHelper = new ObjectManagerHelper($this); foreach ($readerMap as $readerLine) { - $ret[$readerLine[0]] = $objectManagerHelper->getObject( - CompiledPluginList::class, + $pluginList = ObjectManager::getInstance()->create( + PluginList::class, [ 'objectManager' => $omMock, - 'scope' => new StaticScope($readerLine[0]), + 'configScope' => new StaticScope($readerLine[0]), 'reader' => $readerMock, 'omConfig' => $omConfigMock, - 'cachePath' => false + 'cache' => new FileCache(), + 'cachePath' => false, + 'serializer' => new NoSerialize() + ] + ); + $ret[$readerLine[0]] = $objectManagerHelper->getObject( + CompiledPluginList::class, + [ + 'pluginList' => $pluginList, ] ); } @@ -120,7 +138,8 @@ public function getPluginsDataProvider() 'advanced_plugin' ], // simple plugin is disabled in configuration for - // \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item in frontend + // \Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\Item + // in frontend [null, Item::class, 'getName', 'frontend'], // test plugin inheritance [ diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/Item.php similarity index 65% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item.php rename to lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/Item.php index 705d37face0fa..e12465eeb9c62 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/Item.php @@ -3,9 +3,12 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + +declare(strict_types=1); + // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model; +namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model; class Item { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item/Enhanced.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/Item/Enhanced.php similarity index 62% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item/Enhanced.php rename to lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/Item/Enhanced.php index 8fa8590d677b8..c8006867a3702 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/Item/Enhanced.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/Item/Enhanced.php @@ -3,10 +3,13 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + +declare(strict_types=1); + // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; +namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\Item; -class Enhanced extends \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item +class Enhanced extends \Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\Item { /** * @return string diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Advanced.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/ItemPlugin/Advanced.php similarity index 86% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Advanced.php rename to lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/ItemPlugin/Advanced.php index 65388c2735918..2f943fcc4802a 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Advanced.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/ItemPlugin/Advanced.php @@ -3,9 +3,12 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + +declare(strict_types=1); + // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin; +namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\ItemPlugin; class Advanced { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Simple.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/ItemPlugin/Simple.php similarity index 74% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Simple.php rename to lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/ItemPlugin/Simple.php index 13c854e2da4d1..6d891e80ce607 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/Custom/Module/Model/ItemPlugin/Simple.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/ItemPlugin/Simple.php @@ -3,9 +3,12 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + +declare(strict_types=1); + // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin; +namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\ItemPlugin; class Simple { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/_files/reader_mock_map.php similarity index 76% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php rename to lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/_files/reader_mock_map.php index c597bc9ae9dcc..b9e722c4bcdf4 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Unit/_files/reader_mock_map.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/_files/reader_mock_map.php @@ -4,10 +4,12 @@ * See COPYING.txt for license details. */ -use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item; -use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\Item\Enhanced; -use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced; -use Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Simple; +declare(strict_types=1); + +use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\Item; +use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\Item\Enhanced; +use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\ItemPlugin\Advanced; +use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\ItemPlugin\Simple; return [ [ diff --git a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php index a4f728454a524..5209e4427d48d 100644 --- a/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php +++ b/lib/internal/Magento/Framework/Interception/PluginList/PluginList.php @@ -276,6 +276,7 @@ public function getNext($type, $method, $code = '__self') * * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * phpcs:disable Generic.Metrics.NestingLevel */ protected function _loadScopedData() { @@ -327,6 +328,7 @@ protected function _loadScopedData() $this->_pluginInstances = []; } } + // phpcs:enable /** * Whether scope code is current scope code @@ -398,4 +400,26 @@ private function getLogger() } return $this->logger; } + + /** + * Get class of a plugin + * + * @param string $type + * @param string $code + * @return mixed + */ + public function getPluginType(string $type, string $code) + { + return $this->_inherited[$type][$code]['instance']; + } + + /** + * Set current scope + * + * @param ScopeInterface $scope + */ + public function setScope(ScopeInterface $scope) + { + $this->_configScope = $scope; + } } From 29a6d8ee030c6d1a98a6c760544efd88452d2bc9 Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Fri, 18 Oct 2019 12:23:11 +0300 Subject: [PATCH 31/52] magento/magento2#22826: Static test fix. --- .../CompiledInterceptor/CompiledInterceptorTest.php | 13 +++++-------- .../CompiledPluginList/CompiledPluginListTest.php | 5 +++-- .../Interception/Code/Generator/Interceptor.php | 3 --- .../Setup/Console/Command/DiCompileCommand.php | 5 +---- .../Module/Di/App/Task/Operation/Interception.php | 1 + .../Chain/CompiledInterceptorSubstitution.php | 1 + 6 files changed, 11 insertions(+), 17 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php index 0d7f04aa32302..2e7c42e7a344d 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php @@ -9,6 +9,7 @@ use Magento\Framework\CompiledInterception\Generator\AreasPluginList; use Magento\Framework\CompiledInterception\Generator\CompiledPluginListFactory; use Magento\Framework\CompiledInterception\Generator\FileCache; +use Magento\Framework\CompiledInterception\Generator\NoSerialize; use Magento\Framework\CompiledInterception\Generator\StaticScope; use Magento\Framework\App\AreaList; use Magento\Framework\Code\Generator\Io; @@ -23,7 +24,6 @@ use Magento\Framework\Interception\ObjectManager\ConfigInterface; use Magento\Framework\Interception\PluginList\PluginList; use Magento\Framework\ObjectManager\Config\Reader\Dom; -use Magento\Framework\Serialize\Serializer\Serialize; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; use PHPUnit\Framework\MockObject\MockObject; @@ -68,12 +68,6 @@ public function createScopeReaders() $omMock = $this->createMock(ObjectManager::class); - $omMock->method('get') - ->with(Serialize::class) - ->willReturn( - ObjectManager::getInstance()->create(Serialize::class) - ); - $omConfigMock = $this->getMockForAbstractClass( ConfigInterface::class ); @@ -90,7 +84,10 @@ public function createScopeReaders() 'objectManager' => $omMock, 'configScope' => new StaticScope($readerLine[0]), 'reader' => $readerMock, - 'omConfig' => $omConfigMock + 'omConfig' => $omConfigMock, + 'cache' => new FileCache(), + 'cachePath' => false, + 'serializer' => new NoSerialize() ] ); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/CompiledPluginListTest.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/CompiledPluginListTest.php index d675ce9f1d5ab..36f4e9ec77ac8 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/CompiledPluginListTest.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/CompiledPluginListTest.php @@ -6,7 +6,7 @@ declare(strict_types=1); -namespace Magento\Framework\CompiledInterception\Test\Unit\CompiledPluginList; +namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList; use Magento\Framework\App\ObjectManager; use Magento\Framework\CompiledInterception\Generator\CompiledPluginList; @@ -55,6 +55,8 @@ public function createScopeReaders() $omConfigMock->method('getOriginalInstanceType')->willReturnArgument(0); $ret = []; $objectManagerHelper = new ObjectManagerHelper($this); + //clear static cache + (new FileCache())->clean(); foreach ($readerMap as $readerLine) { $pluginList = ObjectManager::getInstance()->create( PluginList::class, @@ -80,7 +82,6 @@ public function createScopeReaders() public function testGetPlugin() { - $this->objects['backend']->getNext(Item::class, 'getName'); $this->assertEquals( Simple::class, diff --git a/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php b/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php index 7d8c218843fbf..4c1aeca50a517 100644 --- a/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php +++ b/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php @@ -13,9 +13,6 @@ */ class Interceptor extends \Magento\Framework\Code\Generator\EntityAbstract implements InterceptorInterface { - /** - * Entity type - */ const ENTITY_TYPE = 'interceptor'; /** diff --git a/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php b/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php index beda6c8ec572a..b0a2c20688410 100644 --- a/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php +++ b/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php @@ -366,10 +366,7 @@ private function configureObjectManager(OutputInterface $output) private function getOperationsConfiguration( array $compiledPathsList ) { - $excludePatterns = []; - foreach ($this->excludedPathsList as $excludedPaths) { - $excludePatterns = array_merge($excludedPaths, $excludePatterns); - } + $excludePatterns = array_merge(...array_values($this->excludedPathsList)); $operations = [ OperationFactory::PROXY_GENERATOR => [], diff --git a/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php b/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php index 54e29d525969f..b61a95df1c88d 100644 --- a/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php +++ b/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php @@ -92,6 +92,7 @@ public function doOperation() $paths = (array)$paths; } foreach ($paths as $path) { + // phpcs:ignore Magento2.Performance.ForeachArrayMerge $classesList = array_merge($classesList, $this->classesScanner->getList($path)); } } diff --git a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php index ab54a47adf8ca..454a12f29738d 100644 --- a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php +++ b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php @@ -45,6 +45,7 @@ public function modify(array $config) continue 2; } } + // phpcs:ignore Magento2.Performance.ForeachArrayMerge $arguments = array_merge([$name => ['_i_' => $preference]], $arguments); } From dca21f7a4fc91cf2c7f87d8d1926928fa2fe302a Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Tue, 22 Oct 2019 14:19:35 +0300 Subject: [PATCH 32/52] magento/magento2#22826: Class level caching for compiled plugins removed. --- .../Generator/AreasPluginList.php | 3 +- .../Generator/CompiledInterceptor.php | 38 ++-------- .../Generator/FileCache.php | 2 +- .../Custom/Module/Model/ComplexItemTyped.php | 2 +- .../_out_interceptors/ComplexItem.txt | 36 +++------ .../_out_interceptors/ComplexItemTyped.txt | 40 +++------- .../_out_interceptors/Item.txt | 24 +----- .../_out_interceptors/SecondItem.txt | 74 +++---------------- .../Chain/CompiledInterceptorSubstitution.php | 2 +- 9 files changed, 46 insertions(+), 175 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php index cc1d168bc6192..bcf07cda341ec 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php @@ -66,6 +66,7 @@ public function getPluginsConfigForAllAreas() //this is to emulate order M2 is reading scopes config to use scope cache //"global|primary" should be loaded first and then "global|primary|frontend" etc. $defaultScopePluginList = $defaultScope = null; + $objectManager = ObjectManager::getInstance(); foreach ($this->scope->getAllScopes() as $scope) { $configScope = $this->scopeInterfaceFactory->create( [ @@ -75,7 +76,7 @@ public function getPluginsConfigForAllAreas() if ($defaultScopePluginList === null) { $defaultScopePluginList = $this->compiledPluginListFactory->create( [ - 'objectManager' => ObjectManager::getInstance(), + 'objectManager' => $objectManager, 'scope' => $configScope ] ); diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 896ec3bd4cb21..ce4c1b68354f1 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -45,8 +45,8 @@ class CompiledInterceptor extends EntityAbstract implements InterceptorInterface /** * CompiledInterceptor constructor. * @param AreasPluginList $areasPlugins - * @param null|string $sourceClassName - * @param null|string $resultClassName + * @param string|null $sourceClassName + * @param string|null $resultClassName * @param Io|null $ioObject * @param CodeGeneratorInterface|null $classGenerator * @param DefinedClasses|null $definedClasses @@ -221,7 +221,6 @@ private function overrideMethodsAndGeneratePluginGetters(\ReflectionClass $refle foreach ($allPlugins as $plugins) { foreach ($plugins as $plugin) { $this->classMethods[] = $this->getPluginGetterInfo($plugin); - $this->classProperties[] = $this->getPluginPropertyInfo($plugin); } } } @@ -274,7 +273,7 @@ private function injectPropertiesSettersToConstructor(\ReflectionMethod $parentC 'name' => '__construct', 'parameters' => $parameters, 'body' => implode("\n", $body), - 'docblock' => ['shortDescription' => '{@inheritdoc}'], + 'docblock' => ['shortDescription' => '@inheritdoc'], ]; } @@ -495,23 +494,6 @@ private function getGetterName($plugin) return '____plugin_' . $plugin['clean_name']; } - /** - * Get plugin property cache attribute - * - * @param array $plugin - * @return array - */ - private function getPluginPropertyInfo($plugin) - { - return [ - 'name' => '____plugin_' . $plugin['clean_name'], - 'visibility' => 'private', - 'docblock' => [ - 'tags' => [['name' => 'var', 'description' => '\\' . $plugin['class']]], - ] - ]; - } - /** * Prepares plugin getter for code generator * @@ -520,19 +502,11 @@ private function getPluginPropertyInfo($plugin) */ private function getPluginGetterInfo($plugin) { - $body = []; - $varName = "\$this->____plugin_" . $plugin['clean_name']; - - $body[] = "if ($varName === null) {"; - $body[] = "\t$varName = \$this->____om->get(\\" . "{$plugin['class']}::class);"; - $body[] = "}"; - $body[] = "return $varName;"; - return [ 'name' => $this->getGetterName($plugin), 'visibility' => 'private', 'parameters' => [], - 'body' => implode("\n", $body), + 'body' => "return \$this->____om->get(\\" . "{$plugin['class']}::class);", 'docblock' => [ 'shortDescription' => 'plugin "' . $plugin['code'] . '"' . "\n" . '@return \\' . $plugin['class'] ], @@ -588,7 +562,7 @@ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) 'parameters' =>array_map([$this, '_getMethodParameterInfo'], $parameters), 'body' => implode("\n", $body), 'returnType' => $returnTypeValue, - 'docblock' => ['shortDescription' => '{@inheritdoc}'], + 'docblock' => ['shortDescription' => '@inheritdoc'], ]; } @@ -623,7 +597,7 @@ private function getScopeCasesFromConfig($config) * @param string $code * @param string $className * @param array $allPlugins - * @param null|string $next + * @param string|null $next * @return mixed */ private function getPluginInfo(CompiledPluginList $plugins, $code, $className, &$allPlugins, $next = null) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php index 6baf7dc28c871..9a98015da5fbc 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php @@ -18,7 +18,7 @@ class FileCache implements CacheInterface /** * FileCache constructor. - * @param null|string $cachePath + * @param string|null $cachePath */ public function __construct($cachePath = null) { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItemTyped.php b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItemTyped.php index 1037f71ee9901..77f3c125d7f55 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItemTyped.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItemTyped.php @@ -18,7 +18,7 @@ public function returnVoid(): void } /** - * @return null|string + * @return string|null */ public function getNullableValue(): ?string { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt index b9aca48724361..8568026d8aa44 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt @@ -19,17 +19,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati private $____om = null; /** - * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced - */ - private $____plugin_advanced_plugin = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex - */ - private $____plugin_complex_plugin = null; - - /** - * {@inheritdoc} + * @inheritdoc */ public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\ScopeInterface $____scope) { @@ -38,7 +28,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function getName() { @@ -69,7 +59,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function setValue($value) { @@ -87,7 +77,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function & getReference() { @@ -102,7 +92,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function firstVariadicParameter(... $variadicValue) { @@ -117,7 +107,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function secondVariadicParameter($value, ... $variadicValue) { @@ -132,7 +122,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function byRefVariadic(&... $variadicValue) { @@ -147,7 +137,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function returnsSelf() { @@ -167,10 +157,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati */ private function ____plugin_advanced_plugin() { - if ($this->____plugin_advanced_plugin === null) { - $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); - } - return $this->____plugin_advanced_plugin; + return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } /** @@ -179,9 +166,6 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati */ private function ____plugin_complex_plugin() { - if ($this->____plugin_complex_plugin === null) { - $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex::class); - } - return $this->____plugin_complex_plugin; + return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex::class); } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt index b87e0f9e33c8d..b683bbbfbb6ba 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -19,17 +19,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati private $____om = null; /** - * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex - */ - private $____plugin_complex_plugin = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced - */ - private $____plugin_advanced_plugin = null; - - /** - * {@inheritdoc} + * @inheritdoc */ public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\ScopeInterface $____scope) { @@ -38,7 +28,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function returnVoid() : void { @@ -55,7 +45,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function getNullableValue() : ?string { @@ -70,7 +60,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function getName() : string { @@ -93,7 +83,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function setValue(string $value) { @@ -111,7 +101,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function firstVariadicParameter(string ... $variadicValue) { @@ -126,7 +116,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function secondVariadicParameter(string $value, string ... $variadicValue) { @@ -141,7 +131,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function byRefVariadic(string &... $variadicValue) { @@ -156,7 +146,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function returnsSelf() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped { @@ -171,7 +161,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function returnsType() : \Magento\Framework\Something { @@ -191,10 +181,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati */ private function ____plugin_complex_plugin() { - if ($this->____plugin_complex_plugin === null) { - $this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex::class); - } - return $this->____plugin_complex_plugin; + return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex::class); } /** @@ -203,9 +190,6 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati */ private function ____plugin_advanced_plugin() { - if ($this->____plugin_advanced_plugin === null) { - $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); - } - return $this->____plugin_advanced_plugin; + return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt index 4f405c51b0752..12b535f031061 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt @@ -19,17 +19,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati private $____om = null; /** - * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple - */ - private $____plugin_simple_plugin = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced - */ - private $____plugin_advanced_plugin = null; - - /** - * {@inheritdoc} + * @inheritdoc */ public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\ScopeInterface $____scope) { @@ -38,7 +28,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function getName() { @@ -67,10 +57,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati */ private function ____plugin_simple_plugin() { - if ($this->____plugin_simple_plugin === null) { - $this->____plugin_simple_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); - } - return $this->____plugin_simple_plugin; + return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); } /** @@ -79,9 +66,6 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati */ private function ____plugin_advanced_plugin() { - if ($this->____plugin_advanced_plugin === null) { - $this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); - } - return $this->____plugin_advanced_plugin; + return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt index f253592e6f933..25e7976ad0409 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt +++ b/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt @@ -19,42 +19,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati private $____om = null; /** - * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced - */ - private $____plugin_advanced_plugin1 = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple - */ - private $____plugin_simple_plugin1 = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced - */ - private $____plugin_advanced_plugin2 = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced - */ - private $____plugin_advanced_plugin3 = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple - */ - private $____plugin_simple_plugin2 = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple - */ - private $____plugin_simple_plugin3 = null; - - /** - * @var \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced - */ - private $____plugin_advanced_plugin4 = null; - - /** - * {@inheritdoc} + * @inheritdoc */ public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\ScopeInterface $____scope) { @@ -63,7 +28,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati } /** - * {@inheritdoc} + * @inheritdoc */ public function getName() { @@ -111,10 +76,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati */ private function ____plugin_advanced_plugin1() { - if ($this->____plugin_advanced_plugin1 === null) { - $this->____plugin_advanced_plugin1 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); - } - return $this->____plugin_advanced_plugin1; + return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } /** @@ -123,10 +85,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati */ private function ____plugin_simple_plugin1() { - if ($this->____plugin_simple_plugin1 === null) { - $this->____plugin_simple_plugin1 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); - } - return $this->____plugin_simple_plugin1; + return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); } /** @@ -135,10 +94,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati */ private function ____plugin_advanced_plugin2() { - if ($this->____plugin_advanced_plugin2 === null) { - $this->____plugin_advanced_plugin2 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); - } - return $this->____plugin_advanced_plugin2; + return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } /** @@ -147,10 +103,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati */ private function ____plugin_advanced_plugin3() { - if ($this->____plugin_advanced_plugin3 === null) { - $this->____plugin_advanced_plugin3 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); - } - return $this->____plugin_advanced_plugin3; + return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } /** @@ -159,10 +112,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati */ private function ____plugin_simple_plugin2() { - if ($this->____plugin_simple_plugin2 === null) { - $this->____plugin_simple_plugin2 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); - } - return $this->____plugin_simple_plugin2; + return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); } /** @@ -171,10 +121,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati */ private function ____plugin_simple_plugin3() { - if ($this->____plugin_simple_plugin3 === null) { - $this->____plugin_simple_plugin3 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); - } - return $this->____plugin_simple_plugin3; + return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); } /** @@ -183,9 +130,6 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati */ private function ____plugin_advanced_plugin4() { - if ($this->____plugin_advanced_plugin4 === null) { - $this->____plugin_advanced_plugin4 = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); - } - return $this->____plugin_advanced_plugin4; + return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } } diff --git a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php index 454a12f29738d..2dfc10f288150 100644 --- a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php +++ b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php @@ -41,7 +41,7 @@ public function modify(array $config) foreach (CompiledInterceptor::propertiesToInjectToConstructor() as $type => $name) { $preference = isset($config['preferences'][$type]) ? $config['preferences'][$type] : $type; foreach ($arguments as $argument) { - if (isset($argument['_i_']) && $argument['_i_'] == $preference) { + if (isset($argument['_i_']) && $argument['_i_'] === $preference) { continue 2; } } From 219382f8aeb61f55849bfa42c56659ef4342f58c Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Wed, 23 Oct 2019 13:40:58 +0300 Subject: [PATCH 33/52] magento/magento2#22826: Admin infinite redirect loop fix. --- app/etc/di.xml | 2 +- .../Generator/AreasPluginList.php | 46 +++++++++++-------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/app/etc/di.xml b/app/etc/di.xml index 9d86b3c4683e1..b71178d12d9b2 100755 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -775,7 +775,7 @@ - \Magento\Framework\CompiledInterception\PluginList\PluginList + \Magento\Framework\CompiledInterception\PluginList\PluginList diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php index bcf07cda341ec..646b5576d04e4 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php @@ -6,9 +6,7 @@ namespace Magento\Framework\CompiledInterception\Generator; -use Magento\Framework\App\ObjectManager; use Magento\Framework\Config\Scope; -use Magento\Framework\Config\ScopeInterfaceFactory; /** * Class AreasPluginList @@ -21,9 +19,9 @@ class AreasPluginList private $scope; /** - * @var ScopeInterfaceFactory + * @var StaticScopeFactory */ - private $scopeInterfaceFactory; + private $staticScopeFactory; /** * @var CompiledPluginListFactory @@ -36,22 +34,21 @@ class AreasPluginList private $plugins; /** - * AreasPluginList constructor. * @param Scope $scope - * @param ScopeInterfaceFactory $scopeInterfaceFactory + * @param StaticScopeFactory $staticScopeFactory * @param CompiledPluginListFactory $compiledPluginListFactory * @param array|null $plugins */ public function __construct( Scope $scope, - ScopeInterfaceFactory $scopeInterfaceFactory, + StaticScopeFactory $staticScopeFactory, CompiledPluginListFactory $compiledPluginListFactory, ?array $plugins = null ) { $this->scope = $scope; - $this->scopeInterfaceFactory = $scopeInterfaceFactory; - $this->plugins = $plugins; + $this->staticScopeFactory = $staticScopeFactory; $this->compiledPluginListFactory = $compiledPluginListFactory; + $this->plugins = $plugins; } /** @@ -66,29 +63,38 @@ public function getPluginsConfigForAllAreas() //this is to emulate order M2 is reading scopes config to use scope cache //"global|primary" should be loaded first and then "global|primary|frontend" etc. $defaultScopePluginList = $defaultScope = null; - $objectManager = ObjectManager::getInstance(); foreach ($this->scope->getAllScopes() as $scope) { - $configScope = $this->scopeInterfaceFactory->create( + $configScope = $this->staticScopeFactory->create( [ 'scope' => $scope, ] ); + $pluginList = $this->prepareCompiledPluginList($configScope); if ($defaultScopePluginList === null) { - $defaultScopePluginList = $this->compiledPluginListFactory->create( - [ - 'objectManager' => $objectManager, - 'scope' => $configScope - ] - ); - $defaultScopePluginList->getNext('dummy', 'dummy'); + $defaultScopePluginList = $pluginList; $defaultScope = $scope; } else { - $this->plugins[$scope] = clone $defaultScopePluginList; - $this->plugins[$scope]->setScope($configScope); + $this->plugins[$scope] = $pluginList; } } $this->plugins[$defaultScope] = $defaultScopePluginList; } + return $this->plugins; } + + /** + * Create CompiledPluginList and prepare it for use. + * + * @param StaticScope $configScope + * @return CompiledPluginList + */ + private function prepareCompiledPluginList(StaticScope $configScope): CompiledPluginList + { + $pluginList = $this->compiledPluginListFactory->create(); + $pluginList->setScope($configScope); + $pluginList->getNext('dummy', 'dummy'); + + return $pluginList; + } } From f96d546d4d047c8f68ab5c2ef7ca2590a9f944b7 Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Sun, 27 Oct 2019 10:05:20 +0200 Subject: [PATCH 34/52] magento/magento2#22826: Compiled plugin list optimization. --- .../Generator/AreasPluginList.php | 23 ++++--------------- .../Generator/CompiledPluginList.php | 8 +++++++ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php index 646b5576d04e4..dde8cb33e1e8c 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php @@ -69,12 +69,14 @@ public function getPluginsConfigForAllAreas() 'scope' => $scope, ] ); - $pluginList = $this->prepareCompiledPluginList($configScope); if ($defaultScopePluginList === null) { - $defaultScopePluginList = $pluginList; + $defaultScopePluginList = $this->compiledPluginListFactory->create(); + $defaultScopePluginList->setScope($configScope); + $defaultScopePluginList->getNext('dummy', 'dummy'); $defaultScope = $scope; } else { - $this->plugins[$scope] = $pluginList; + $this->plugins[$scope] = clone $defaultScopePluginList; + $this->plugins[$scope]->setScope($configScope); } } $this->plugins[$defaultScope] = $defaultScopePluginList; @@ -82,19 +84,4 @@ public function getPluginsConfigForAllAreas() return $this->plugins; } - - /** - * Create CompiledPluginList and prepare it for use. - * - * @param StaticScope $configScope - * @return CompiledPluginList - */ - private function prepareCompiledPluginList(StaticScope $configScope): CompiledPluginList - { - $pluginList = $this->compiledPluginListFactory->create(); - $pluginList->setScope($configScope); - $pluginList->getNext('dummy', 'dummy'); - - return $pluginList; - } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php index c6631b72ad31b..d559c86945e71 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php @@ -87,4 +87,12 @@ public function getNext($type, $method, $code = null) { return $this->pluginList->getNext($type, $method, $code); } + + /** + * PluginList instance should not be shared. + */ + public function __clone() + { + $this->pluginList = clone $this->pluginList; + } } From 528408d3eb8d6401a6eb2ad721e1666d6e322ff3 Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Sun, 27 Oct 2019 17:23:14 +0200 Subject: [PATCH 35/52] magento/magento2#22826: Static cache path update. --- .../Magento/TestFramework/Application.php | 3 + .../App/Filesystem/DirectoryList.php | 10 ++- .../Framework/App/State/CleanupFiles.php | 3 +- .../Generator/FileCache.php | 75 ++++++++++++------- 4 files changed, 60 insertions(+), 31 deletions(-) diff --git a/dev/tests/integration/framework/Magento/TestFramework/Application.php b/dev/tests/integration/framework/Magento/TestFramework/Application.php index 1bfc928f2916a..6e2d3982e2f53 100644 --- a/dev/tests/integration/framework/Magento/TestFramework/Application.php +++ b/dev/tests/integration/framework/Magento/TestFramework/Application.php @@ -17,6 +17,7 @@ * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.TooManyFields) + * phpcs:disable Magento2.Functions.DiscouragedFunction */ class Application { @@ -251,6 +252,7 @@ public function getDbInstance() protected function getInstallConfig() { if (null === $this->installConfig) { + // phpcs:ignore Magento2.Security.IncludeFile.FoundIncludeFile $this->installConfig = include $this->installConfigFile; } return $this->installConfig; @@ -708,6 +710,7 @@ protected function getCustomDirs() DirectoryList::STATIC_VIEW => [$path => "{$this->installDir}/pub/static"], DirectoryList::TMP_MATERIALIZATION_DIR => [$path => "{$var}/view_preprocessed/pub/static"], DirectoryList::GENERATED_CODE => [$path => "{$generated}/code"], + DirectoryList::STATIC_CACHE => [$path => "{$generated}/static_cache"], DirectoryList::CACHE => [$path => "{$var}/cache"], DirectoryList::LOG => [$path => "{$var}/log"], DirectoryList::SESSION => [$path => "{$var}/session"], diff --git a/lib/internal/Magento/Framework/App/Filesystem/DirectoryList.php b/lib/internal/Magento/Framework/App/Filesystem/DirectoryList.php index 4b10a503f423c..ad27bf4d100c6 100644 --- a/lib/internal/Magento/Framework/App/Filesystem/DirectoryList.php +++ b/lib/internal/Magento/Framework/App/Filesystem/DirectoryList.php @@ -12,6 +12,11 @@ */ class DirectoryList extends \Magento\Framework\Filesystem\DirectoryList { + /** + * Path for compiled interceptors static cache directory. + */ + public const STATIC_CACHE = 'static_cache'; + /** * Code base root */ @@ -136,7 +141,7 @@ class DirectoryList extends \Magento\Framework\Filesystem\DirectoryList const GENERATED_METADATA = 'metadata'; /** - * {@inheritdoc} + * @inheritdoc */ public static function getDefaultConfig() { @@ -164,12 +169,13 @@ public static function getDefaultConfig() self::GENERATED => [parent::PATH => 'generated'], self::GENERATED_CODE => [parent::PATH => Io::DEFAULT_DIRECTORY], self::GENERATED_METADATA => [parent::PATH => 'generated/metadata'], + self::STATIC_CACHE => [parent::PATH => 'generated/static_cache'], ]; return parent::getDefaultConfig() + $result; } /** - * {@inheritdoc} + * @inheritdoc */ public function __construct($root, array $config = []) { diff --git a/lib/internal/Magento/Framework/App/State/CleanupFiles.php b/lib/internal/Magento/Framework/App/State/CleanupFiles.php index c95caf8310b77..bd4988f55dc51 100644 --- a/lib/internal/Magento/Framework/App/State/CleanupFiles.php +++ b/lib/internal/Magento/Framework/App/State/CleanupFiles.php @@ -54,7 +54,8 @@ public function clearCodeGeneratedClasses() { return array_merge( $this->emptyDir(DirectoryList::GENERATED_CODE), - $this->emptyDir(DirectoryList::GENERATED_METADATA) + $this->emptyDir(DirectoryList::GENERATED_METADATA), + $this->emptyDir(DirectoryList::STATIC_CACHE) ); } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php index 9a98015da5fbc..f5567a64644d7 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Framework\CompiledInterception\Generator; use Magento\Framework\App\Filesystem\DirectoryList; @@ -13,34 +14,26 @@ */ class FileCache implements CacheInterface { - - private $cachePath; + /** + * @var DirectoryList + */ + private $directoryList; /** - * FileCache constructor. - * @param string|null $cachePath + * @var string|null */ - public function __construct($cachePath = null) - { - if ($cachePath === null) { - $this->cachePath = BP . DIRECTORY_SEPARATOR . - DirectoryList::GENERATED . DIRECTORY_SEPARATOR . - 'staticcache'; - } else { - $this->cachePath = $cachePath; - } - } + private $cachePath; /** - * Get cache path - * - * @param string $identifier - * @return string + * @param DirectoryList $directoryList + * @param ?string $cachePath */ - private function getCachePath($identifier) - { - $identifier = str_replace('|', '_', $identifier); - return $this->cachePath . DIRECTORY_SEPARATOR . $identifier . '.php'; + public function __construct( + DirectoryList $directoryList, + $cachePath = null + ) { + $this->directoryList = $directoryList; + $this->cachePath = $cachePath; } /** @@ -52,7 +45,7 @@ private function getCachePath($identifier) */ public function test($identifier) { - return file_exists($this->getCachePath($identifier)); + return file_exists($this->getCacheFilePath($identifier)); } /** @@ -65,7 +58,7 @@ public function test($identifier) public function load($identifier) { // @codingStandardsIgnoreLine - return $this->cachePath ? @include $this->getCachePath($identifier) : false; + return $this->getCachePath() ? @include $this->getCacheFilePath($identifier) : false; } /** @@ -80,8 +73,8 @@ public function load($identifier) */ public function save($data, $identifier, array $tags = [], $lifeTime = null) { - if ($this->cachePath) { - $path = $this->getCachePath($identifier); + if ($this->getCachePath()) { + $path = $this->getCacheFilePath($identifier); if (!is_dir(dirname($path))) { mkdir(dirname($path), 0777, true); } @@ -115,8 +108,8 @@ public function remove($identifier) */ public function clean($mode = \Zend_Cache::CLEANING_MODE_ALL, array $tags = []) { - if ($this->cachePath) { - foreach (glob($this->cachePath . '/*') as $file) { + if ($this->getCachePath()) { + foreach (glob($this->getCachePath() . '/*') as $file) { if (is_file($file)) { unlink($file); } @@ -145,4 +138,30 @@ public function getLowLevelFrontend() { return null; } + + /** + * Get file cache path. + * + * @param string $identifier + * @return string + */ + private function getCacheFilePath($identifier): string + { + $identifier = str_replace('|', '_', $identifier); + return $this->getCachePath() . DIRECTORY_SEPARATOR . $identifier . '.php'; + } + + /** + * Get cache path. + * + * @return string + */ + private function getCachePath(): string + { + if (!$this->cachePath) { + $this->cachePath = $this->directoryList->getPath(DirectoryList::STATIC_CACHE); + } + + return $this->cachePath; + } } From aa62a4dc89cc9b0c0f7f17e1b7698472912fd1bb Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Fri, 1 Nov 2019 12:12:48 +0200 Subject: [PATCH 36/52] magento/magento2#22826: Compiled interceptor return type fix. --- .../CompiledInterception/Generator/CompiledInterceptor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index ce4c1b68354f1..9abc773fd14b9 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -555,7 +555,7 @@ private function getCompiledMethodInfo(\ReflectionMethod $method, $config) ? ($returnType->allowsNull() ? '?' : '') . $returnType->getName() : null; if ($returnTypeValue === 'self') { - $returnTypeValue = $method->getDeclaringClass()->getName(); + $returnTypeValue = $this->_getFullyQualifiedClassName($method->getDeclaringClass()->getName()); } return [ 'name' => ($method->returnsReference() ? '& ' : '') . $method->getName(), From eb1018bad56b55646846b8de599fe0445eb7908d Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Fri, 1 Nov 2019 12:14:35 +0200 Subject: [PATCH 37/52] magento/magento2#22826: Integration tests fix. --- .../integration/etc/di/preferences/ce.php | 1 + .../Interception/CompiledPluginList.php | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 dev/tests/integration/framework/Magento/TestFramework/Interception/CompiledPluginList.php diff --git a/dev/tests/integration/etc/di/preferences/ce.php b/dev/tests/integration/etc/di/preferences/ce.php index b8264ea977750..7fd914479a02d 100644 --- a/dev/tests/integration/etc/di/preferences/ce.php +++ b/dev/tests/integration/etc/di/preferences/ce.php @@ -31,4 +31,5 @@ \Magento\TestFramework\Lock\Backend\DummyLocker::class, \Magento\Framework\Session\SessionStartChecker::class => \Magento\TestFramework\Session\SessionStartChecker::class, \Magento\Framework\HTTP\AsyncClientInterface::class => \Magento\TestFramework\HTTP\AsyncClientInterfaceMock::class, + \Magento\Payment\Model\MethodInterface::class => \Magento\OfflinePayments\Model\Checkmo::class ]; diff --git a/dev/tests/integration/framework/Magento/TestFramework/Interception/CompiledPluginList.php b/dev/tests/integration/framework/Magento/TestFramework/Interception/CompiledPluginList.php new file mode 100644 index 0000000000000..2c8b78d6df670 --- /dev/null +++ b/dev/tests/integration/framework/Magento/TestFramework/Interception/CompiledPluginList.php @@ -0,0 +1,66 @@ +pluginList = ObjectManager::getInstance()->create( + PluginList::class, + [ + $objectManager->get(Dom::class), + $objectManager->get(ScopeInterface ::class), + $objectManager->get(FileCache ::class), + $objectManager->get(ObjectManagerRelationsRuntime ::class), + $objectManager->get(ConfigInterface::class), + $objectManager->get(InterceptionDefinitionRuntime ::class), + $objectManager, + $objectManager->get(ObjectManagerDefinitionRuntime ::class), + ['global'], + 'compiled_plugins', + $objectManager->get(NoSerialize ::class), + ] + ); + parent::__construct($this->pluginList); + } + + /** + * Reset internal cache + */ + public function reset() + { + $this->pluginList->reset(); + } +} From 53e1e8d471a801c7d5720cc670d0f590ffac9a17 Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Fri, 1 Nov 2019 12:18:02 +0200 Subject: [PATCH 38/52] magento/magento2#22826: Enable compiled interceptors. --- app/etc/di.xml | 12 +++++++++--- dev/tests/integration/etc/di/preferences/ce.php | 2 +- .../Framework/Interception/AbstractPlugin.php | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/etc/di.xml b/app/etc/di.xml index ca43e7b341628..3266bf63d2006 100644 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -70,7 +70,7 @@ - + @@ -779,8 +779,14 @@ \Magento\Framework\CompiledInterception\PluginList\PluginList - - + + + + + Magento\Framework\CompiledInterception\Generator\CompiledInterceptor + + + diff --git a/dev/tests/integration/etc/di/preferences/ce.php b/dev/tests/integration/etc/di/preferences/ce.php index 7fd914479a02d..82884fa16f38d 100644 --- a/dev/tests/integration/etc/di/preferences/ce.php +++ b/dev/tests/integration/etc/di/preferences/ce.php @@ -15,7 +15,7 @@ \Magento\Framework\App\ResponseInterface::class => \Magento\TestFramework\Response::class, \Magento\Framework\App\Response\Http::class => \Magento\TestFramework\Response::class, \Magento\Framework\Interception\PluginListInterface::class => - \Magento\TestFramework\Interception\PluginList::class, + \Magento\TestFramework\Interception\CompiledPluginList::class, \Magento\Framework\Interception\ObjectManager\ConfigInterface::class => \Magento\TestFramework\ObjectManager\Config::class, \Magento\Framework\Interception\ObjectManager\Config\Developer::class => diff --git a/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php b/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php index 281a038a5a9a9..d5b9c89487c72 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php +++ b/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php @@ -117,7 +117,7 @@ public function setUpInterceptionConfig($pluginConfig) [ 'preferences' => [ \Magento\Framework\Interception\PluginListInterface::class => - \Magento\Framework\Interception\PluginList\PluginList::class, + \Magento\Framework\CompiledInterception\Generator\CompiledPluginList::class, \Magento\Framework\Interception\ChainInterface::class => \Magento\Framework\Interception\Chain\Chain::class, ], From 75dea82706cfc373feaa7d9030d5b1cf132dbe2d Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Fri, 1 Nov 2019 13:01:34 +0200 Subject: [PATCH 39/52] magento/magento2#22826: Remove ExecuteCommitCallbacks plugin. --- app/etc/di.xml | 3 --- .../Adapter/Pdo}/CallbackPool.php | 9 +++++++- .../Framework/DB/Adapter/Pdo/Mysql.php | 18 +++++++++++++--- .../EntityManager/CallbackHandler.php | 8 ++++++- .../Model/ExecuteCommitCallbacks.php | 21 +++++++++---------- .../Model/ResourceModel/AbstractResource.php | 3 ++- 6 files changed, 42 insertions(+), 20 deletions(-) rename lib/internal/Magento/Framework/{Model => DB/Adapter/Pdo}/CallbackPool.php (84%) diff --git a/app/etc/di.xml b/app/etc/di.xml index 3266bf63d2006..0bd34997eeaeb 100644 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -1799,8 +1799,5 @@ type="Magento\Framework\Mail\MimeMessage" /> - - - diff --git a/lib/internal/Magento/Framework/Model/CallbackPool.php b/lib/internal/Magento/Framework/DB/Adapter/Pdo/CallbackPool.php similarity index 84% rename from lib/internal/Magento/Framework/Model/CallbackPool.php rename to lib/internal/Magento/Framework/DB/Adapter/Pdo/CallbackPool.php index c4693e5575020..cd83afd4977ca 100644 --- a/lib/internal/Magento/Framework/Model/CallbackPool.php +++ b/lib/internal/Magento/Framework/DB/Adapter/Pdo/CallbackPool.php @@ -3,8 +3,9 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); -namespace Magento\Framework\Model; +namespace Magento\Framework\DB\Adapter\Pdo; /** * Class CallbackPool @@ -19,6 +20,8 @@ class CallbackPool private static $commitCallbacks = []; /** + * Add callback by hash key. + * * @param string $hashKey * @param array $callback * @return void @@ -29,6 +32,8 @@ public static function attach($hashKey, $callback) } /** + * Remove callbacks by hash key. + * * @param string $hashKey * @return void */ @@ -38,6 +43,8 @@ public static function clear($hashKey) } /** + * Get callbacks by hash key. + * * @param string $hashKey * @return array */ diff --git a/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php b/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php index 058bf18b16d3c..182448b42c268 100644 --- a/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php +++ b/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php @@ -22,6 +22,7 @@ use Magento\Framework\DB\SelectFactory; use Magento\Framework\DB\Statement\Parameter; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Model\ExecuteCommitCallbacks; use Magento\Framework\Phrase; use Magento\Framework\Serialize\SerializerInterface; use Magento\Framework\Stdlib\DateTime; @@ -223,6 +224,11 @@ class Mysql extends \Zend_Db_Adapter_Pdo_Mysql implements AdapterInterface */ private $schemaListener; + /** + * @var ExecuteCommitCallbacks + */ + private $executeCommitCallbacks; + /** * Constructor * @@ -232,6 +238,7 @@ class Mysql extends \Zend_Db_Adapter_Pdo_Mysql implements AdapterInterface * @param SelectFactory $selectFactory * @param array $config * @param SerializerInterface|null $serializer + * @param ExecuteCommitCallbacks $executeCommitCallbacks */ public function __construct( StringUtils $string, @@ -239,7 +246,8 @@ public function __construct( LoggerInterface $logger, SelectFactory $selectFactory, array $config = [], - SerializerInterface $serializer = null + SerializerInterface $serializer = null, + ExecuteCommitCallbacks $executeCommitCallbacks = null ) { $this->string = $string; $this->dateTime = $dateTime; @@ -263,6 +271,8 @@ public function __construct( } catch (\Zend_Db_Adapter_Exception $e) { throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e); } + $this->executeCommitCallbacks = $executeCommitCallbacks + ?? ObjectManager::getInstance()->get(ExecuteCommitCallbacks::class); } /** @@ -306,7 +316,8 @@ public function commit() throw new \Exception(AdapterInterface::ERROR_ROLLBACK_INCOMPLETE_MESSAGE); } --$this->_transactionLevel; - return $this; + + return $this->executeCommitCallbacks->afterCommit($this); } /** @@ -329,7 +340,8 @@ public function rollBack() $this->_isRolledBack = true; } --$this->_transactionLevel; - return $this; + + return $this->executeCommitCallbacks->afterRollBack($this); } /** diff --git a/lib/internal/Magento/Framework/EntityManager/CallbackHandler.php b/lib/internal/Magento/Framework/EntityManager/CallbackHandler.php index 255257fd4ac3d..fca369848ee0f 100644 --- a/lib/internal/Magento/Framework/EntityManager/CallbackHandler.php +++ b/lib/internal/Magento/Framework/EntityManager/CallbackHandler.php @@ -6,7 +6,7 @@ namespace Magento\Framework\EntityManager; -use Magento\Framework\Model\CallbackPool; +use Magento\Framework\DB\Adapter\Pdo\CallbackPool; use Psr\Log\LoggerInterface; /** @@ -39,6 +39,8 @@ public function __construct( } /** + * Process entity type. + * * @param string $entityType * @throws \Exception * @return void @@ -62,6 +64,8 @@ public function process($entityType) } /** + * Attach entity type to callback pool. + * * @param string $entityType * @param array $callback * @throws \Exception @@ -74,6 +78,8 @@ public function attach($entityType, $callback) } /** + * Remove entity type from callback pool. + * * @param string $entityType * @throws \Exception * @return void diff --git a/lib/internal/Magento/Framework/Model/ExecuteCommitCallbacks.php b/lib/internal/Magento/Framework/Model/ExecuteCommitCallbacks.php index 799f8ffda253c..96837d57225f5 100644 --- a/lib/internal/Magento/Framework/Model/ExecuteCommitCallbacks.php +++ b/lib/internal/Magento/Framework/Model/ExecuteCommitCallbacks.php @@ -8,6 +8,7 @@ namespace Magento\Framework\Model; use Magento\Framework\DB\Adapter\AdapterInterface; +use Magento\Framework\DB\Adapter\Pdo\CallbackPool; use Psr\Log\LoggerInterface; /** @@ -31,14 +32,13 @@ public function __construct(LoggerInterface $logger) /** * Execute callbacks after commit. * - * @param AdapterInterface $subject - * @param AdapterInterface $result + * @param AdapterInterface $adapter * @return AdapterInterface */ - public function afterCommit(AdapterInterface $subject, AdapterInterface $result): AdapterInterface + public function afterCommit(AdapterInterface $adapter): AdapterInterface { - if ($result->getTransactionLevel() === 0) { - $callbacks = CallbackPool::get(spl_object_hash($subject)); + if ($adapter->getTransactionLevel() === 0) { + $callbacks = CallbackPool::get(spl_object_hash($adapter)); foreach ($callbacks as $callback) { try { call_user_func($callback); @@ -48,20 +48,19 @@ public function afterCommit(AdapterInterface $subject, AdapterInterface $result) } } - return $result; + return $adapter; } /** * Drop callbacks after rollBack. * - * @param AdapterInterface $subject - * @param AdapterInterface $result + * @param AdapterInterface $adapter * @return AdapterInterface */ - public function afterRollBack(AdapterInterface $subject, AdapterInterface $result): AdapterInterface + public function afterRollBack(AdapterInterface $adapter): AdapterInterface { - CallbackPool::clear(spl_object_hash($subject)); + CallbackPool::clear(spl_object_hash($adapter)); - return $result; + return $adapter; } } diff --git a/lib/internal/Magento/Framework/Model/ResourceModel/AbstractResource.php b/lib/internal/Magento/Framework/Model/ResourceModel/AbstractResource.php index 255e6d94d741f..01226b1843d31 100644 --- a/lib/internal/Magento/Framework/Model/ResourceModel/AbstractResource.php +++ b/lib/internal/Magento/Framework/Model/ResourceModel/AbstractResource.php @@ -7,12 +7,13 @@ use Magento\Framework\App\ObjectManager; use Magento\Framework\DataObject; -use Magento\Framework\Model\CallbackPool; +use Magento\Framework\DB\Adapter\Pdo\CallbackPool; use Magento\Framework\Serialize\Serializer\Json; /** * Abstract resource model * + * phpcs:disable Magento2.Classes.AbstractApi * @api */ abstract class AbstractResource From b422bb12d247bb86ed421aba355937380c15d5de Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Sun, 3 Nov 2019 23:00:38 +0200 Subject: [PATCH 40/52] magento/magento2#22826: Autoload fix. --- .../Magento/Framework/Console/CliTest.php | 7 +++++++ .../Framework/Code/Generator/Autoloader.php | 16 +++++++++++++++ .../ObjectManager/DefinitionFactory.php | 20 ++++++++++++++++--- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Framework/Console/CliTest.php b/dev/tests/integration/testsuite/Magento/Framework/Console/CliTest.php index 7a439d84ce563..95ed07c1eaf03 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/Console/CliTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/Console/CliTest.php @@ -92,6 +92,13 @@ public function testDocumentRootIsPublic($isPub, $params) $cli = new Cli(); $cliReflection = new \ReflectionClass($cli); + foreach (spl_autoload_functions() as $autoloader) { + if (is_array($autoloader) && $autoloader[0] instanceof \Magento\Framework\Code\Generator\Autoloader) { + $autoloader[0]->getGenerator()->setObjectManager($this->objectManager); + break; + } + } + $serviceManagerProperty = $cliReflection->getProperty('serviceManager'); $serviceManagerProperty->setAccessible(true); $serviceManager = $serviceManagerProperty->getValue($cli); diff --git a/lib/internal/Magento/Framework/Code/Generator/Autoloader.php b/lib/internal/Magento/Framework/Code/Generator/Autoloader.php index 35c138147e9d3..007846b42d90a 100644 --- a/lib/internal/Magento/Framework/Code/Generator/Autoloader.php +++ b/lib/internal/Magento/Framework/Code/Generator/Autoloader.php @@ -93,4 +93,20 @@ private function tryToLogException(\Exception $exception): void // Do not take an action here, since the original exception might have been caused by logger } } + + /** + * @param Generator $generator + */ + public function setGenerator(Generator $generator): void + { + $this->_generator = $generator; + } + + /** + * @return Generator + */ + public function getGenerator(): Generator + { + return $this->_generator; + } } diff --git a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php index 492af22815311..d1658b3a99a63 100644 --- a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php +++ b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php @@ -5,9 +5,9 @@ */ namespace Magento\Framework\ObjectManager; +use Magento\Framework\Code\Generator\Autoloader; use Magento\Framework\Filesystem\DriverInterface; use Magento\Framework\ObjectManager\Definition\Runtime; -use Magento\Framework\Code\Generator\Autoloader; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) @@ -52,8 +52,22 @@ public function __construct( */ public function createClassDefinition() { - $autoloader = new Autoloader($this->getCodeGenerator()); - spl_autoload_register([$autoloader, 'load']); + $autoloaderAbsent = true; + $codeGenerator = $this->getCodeGenerator(); + + foreach (spl_autoload_functions() as $autoloader) { + if (is_array($autoloader) && $autoloader[0] instanceof \Magento\Framework\Code\Generator\Autoloader) { + $autoloader[0]->setGenerator($codeGenerator); + $autoloaderAbsent = false; + break; + } + } + + if ($autoloaderAbsent) { + $autoloaderInstance = new Autoloader($codeGenerator); + spl_autoload_register([$autoloaderInstance, 'load']); + } + return new Runtime(); } From b40eabe13b6cb51bfe52fd30afec8339c1523f0a Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Wed, 6 Nov 2019 17:08:36 +0200 Subject: [PATCH 41/52] magento/magento2#22826: Arguments resolving for interceptors fix. --- .../Magento/Framework/ObjectManager/Factory/Compiled.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php index b219d93b0f0fa..8d15734d32798 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php @@ -52,8 +52,14 @@ public function __construct( */ public function create($requestedType, array $arguments = []) { - $args = $this->config->getArguments($requestedType); $type = $this->config->getInstanceType($requestedType); + $requestedTypeArgs = $this->config->getArguments($requestedType); + $realTypeArgs = $this->config->getArguments($type); + if ($realTypeArgs !== null && $requestedTypeArgs !== null) { + $args = array_merge($realTypeArgs, $requestedTypeArgs); + } else { + $args = $requestedTypeArgs ?? $realTypeArgs; + } if ($args === []) { // Case 1: no arguments required From 53fa5c4347126637f0adb24e6120e1b8e1a1eebe Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Fri, 8 Nov 2019 12:11:09 +0200 Subject: [PATCH 42/52] magento/magento2#22826: After plugin calls generation fix. --- .../CompiledInterception/Generator/CompiledInterceptor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 9abc773fd14b9..78445d03c1e37 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -357,7 +357,7 @@ private function compileAfterPlugins($plugins, $methodName, $extraParams, $retur $call = "\$this->" . $this->getGetterName($plugin) . "()->$methodName(\$this, "; if (!$returnVoid) { - $lines[] = ["((\$tmp = $call\$result$extraParams)) !== null) ? \$tmp : \$result;"]; + $lines[] = ["$call\$result$extraParams);"]; } else { $lines[] = ["{$call}null$extraParams);"]; } From 727cfc91145731f020d41feaffebc4f6ca52073f Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Fri, 8 Nov 2019 14:44:06 +0200 Subject: [PATCH 43/52] magento/magento2#22826: Tests fix. --- .../CompiledInterceptorTest.php | 27 +++---- .../Custom/Module/Model/ComplexItem.php | 2 +- .../Custom/Module/Model/ComplexItemTyped.php | 2 +- .../Custom/Module/Model/Item.php | 2 +- .../Custom/Module/Model/Item/Enhanced.php | 4 +- .../Module/Model/ItemPlugin/Advanced.php | 2 +- .../Module/Model/ItemPlugin/Complex.php | 2 +- .../Custom/Module/Model/ItemPlugin/Simple.php | 2 +- .../Custom/Module/Model/SecondItem.php | 2 +- .../_files/reader_mock_map.php | 16 ++--- .../_out_interceptors/ComplexItem.txt | 34 ++++----- .../_out_interceptors/ComplexItemTyped.txt | 38 +++++----- .../_out_interceptors/Item.txt | 28 ++++---- .../_out_interceptors/SecondItem.txt | 70 +++++++++---------- .../CompiledPluginListTest.php | 19 ++--- .../Custom/Module/Model/Item.php | 2 +- .../Custom/Module/Model/Item/Enhanced.php | 4 +- .../Module/Model/ItemPlugin/Advanced.php | 2 +- .../Custom/Module/Model/ItemPlugin/Simple.php | 2 +- .../_files/reader_mock_map.php | 8 +-- .../Framework/Interception/AbstractPlugin.php | 33 ++++++++- .../App/Test/Unit/State/CleanupFilesTest.php | 21 ++++-- .../Framework/Code/Generator/Autoloader.php | 5 ++ .../Generator/CompiledInterceptor.php | 2 +- .../ObjectManager/DefinitionFactory.php | 2 + .../ObjectManager/Factory/Compiled.php | 3 + 26 files changed, 194 insertions(+), 140 deletions(-) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledInterceptor/CompiledInterceptorTest.php (89%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledInterceptor/Custom/Module/Model/ComplexItem.php (91%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledInterceptor/Custom/Module/Model/ComplexItemTyped.php (93%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledInterceptor/Custom/Module/Model/Item.php (70%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledInterceptor/Custom/Module/Model/Item/Enhanced.php (52%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Advanced.php (88%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Complex.php (96%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Simple.php (78%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledInterceptor/Custom/Module/Model/SecondItem.php (70%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledInterceptor/_files/reader_mock_map.php (81%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledInterceptor/_out_interceptors/ComplexItem.txt (79%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt (79%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledInterceptor/_out_interceptors/Item.txt (56%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledInterceptor/_out_interceptors/SecondItem.txt (50%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledPluginList/CompiledPluginListTest.php (87%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledPluginList/Custom/Module/Model/Item.php (72%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledPluginList/Custom/Module/Model/Item/Enhanced.php (55%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledPluginList/Custom/Module/Model/ItemPlugin/Advanced.php (88%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledPluginList/Custom/Module/Model/ItemPlugin/Simple.php (79%) rename {lib/internal/Magento/Framework/CompiledInterception/Test/Integration => dev/tests/integration/testsuite/Magento/Framework/CompiledInterception}/CompiledPluginList/_files/reader_mock_map.php (77%) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/CompiledInterceptorTest.php similarity index 89% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/CompiledInterceptorTest.php index 2e7c42e7a344d..8f1a871fad656 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/CompiledInterceptorTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/CompiledInterceptorTest.php @@ -4,22 +4,23 @@ * See COPYING.txt for license details. */ -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor; +namespace Magento\Framework\CompiledInterception\CompiledInterceptor; +use Magento\Framework\App\AreaList; +use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Code\Generator\Io; use Magento\Framework\CompiledInterception\Generator\AreasPluginList; +use Magento\Framework\CompiledInterception\Generator\CompiledInterceptor; +use Magento\Framework\CompiledInterception\Generator\CompiledPluginList; use Magento\Framework\CompiledInterception\Generator\CompiledPluginListFactory; use Magento\Framework\CompiledInterception\Generator\FileCache; use Magento\Framework\CompiledInterception\Generator\NoSerialize; use Magento\Framework\CompiledInterception\Generator\StaticScope; -use Magento\Framework\App\AreaList; -use Magento\Framework\Code\Generator\Io; -use Magento\Framework\CompiledInterception\Generator\CompiledInterceptor; -use Magento\Framework\CompiledInterception\Generator\CompiledPluginList; -use Magento\Framework\App\ObjectManager; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\SecondItem; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItem; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\Item; +use Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ComplexItem; +use Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped; +use Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\Item; +use Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\SecondItem; use Magento\Framework\Config\ScopeInterfaceFactory; use Magento\Framework\Interception\ObjectManager\ConfigInterface; use Magento\Framework\Interception\PluginList\PluginList; @@ -75,8 +76,10 @@ public function createScopeReaders() $omConfigMock->method('getOriginalInstanceType')->willReturnArgument(0); $ret = []; $objectManagerHelper = new ObjectManagerHelper($this); + $directoryList = ObjectManager::getInstance()->get(DirectoryList::class); //clear static cache - (new FileCache())->clean(); + $fileCache = new FileCache($directoryList); + $fileCache->clean(); foreach ($readerMap as $readerLine) { $pluginList = ObjectManager::getInstance()->create( PluginList::class, @@ -85,7 +88,7 @@ public function createScopeReaders() 'configScope' => new StaticScope($readerLine[0]), 'reader' => $readerMock, 'omConfig' => $omConfigMock, - 'cache' => new FileCache(), + 'cache' => $fileCache, 'cachePath' => false, 'serializer' => new NoSerialize() ] diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItem.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ComplexItem.php similarity index 91% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItem.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ComplexItem.php index 60868390377bf..313ca3462a41b 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItem.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ComplexItem.php @@ -5,7 +5,7 @@ */ // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model; +namespace Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model; class ComplexItem { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItemTyped.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ComplexItemTyped.php similarity index 93% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItemTyped.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ComplexItemTyped.php index 77f3c125d7f55..35874d688fe31 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ComplexItemTyped.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ComplexItemTyped.php @@ -5,7 +5,7 @@ */ // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model; +namespace Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model; class ComplexItemTyped { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/Item.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/Item.php similarity index 70% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/Item.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/Item.php index 1ffd3f49796c3..71b11fce7c714 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/Item.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/Item.php @@ -5,7 +5,7 @@ */ // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model; +namespace Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model; class Item { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/Item/Enhanced.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/Item/Enhanced.php similarity index 52% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/Item/Enhanced.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/Item/Enhanced.php index f7cff69ac3e98..79fa715919205 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/Item/Enhanced.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/Item/Enhanced.php @@ -4,9 +4,9 @@ * See COPYING.txt for license details. */ // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\Item; +namespace Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\Item; -class Enhanced extends \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\Item +class Enhanced extends \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\Item { /** * @return string diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Advanced.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Advanced.php similarity index 88% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Advanced.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Advanced.php index bbe4b7f77eef5..447c1fe30502c 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Advanced.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Advanced.php @@ -5,7 +5,7 @@ */ // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin; +namespace Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin; class Advanced { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Complex.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Complex.php similarity index 96% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Complex.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Complex.php index 30bb1af8c7121..1a1a1f0aaa7a2 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Complex.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Complex.php @@ -5,7 +5,7 @@ */ // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin; +namespace Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin; class Complex { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Simple.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Simple.php similarity index 78% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Simple.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Simple.php index 0c6410031c2f0..4fb351b00c6dc 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Simple.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/ItemPlugin/Simple.php @@ -5,7 +5,7 @@ */ // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin; +namespace Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin; class Simple { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/SecondItem.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/SecondItem.php similarity index 70% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/SecondItem.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/SecondItem.php index 0b6265a3994ca..58e1c2e6e456c 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/Custom/Module/Model/SecondItem.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/Custom/Module/Model/SecondItem.php @@ -5,7 +5,7 @@ */ // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model; +namespace Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model; class SecondItem { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_files/reader_mock_map.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_files/reader_mock_map.php similarity index 81% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_files/reader_mock_map.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_files/reader_mock_map.php index c46e7417536e3..a2a1082ec1952 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_files/reader_mock_map.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_files/reader_mock_map.php @@ -6,14 +6,14 @@ declare(strict_types=1); -use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItem; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\Item; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\Item\Enhanced; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\SecondItem; +use Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ComplexItem; +use Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped; +use Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\Item; +use Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\Item\Enhanced; +use Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced; +use Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex; +use Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple; +use Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\SecondItem; return [ [ diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/ComplexItem.txt similarity index 79% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/ComplexItem.txt index 8568026d8aa44..46d83d323fc13 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItem.txt +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/ComplexItem.txt @@ -1,12 +1,12 @@ -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItem; +namespace Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ComplexItem; use Magento\Framework\Config\ScopeInterface; use Magento\Framework\ObjectManagerInterface; /** - * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItem + * Interceptor class for @see \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ComplexItem */ -class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItem +class Interceptor extends \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ComplexItem { /** * @var ScopeInterface @@ -37,24 +37,24 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati case 'frontend': case 'emptyscope': $this->____plugin_advanced_plugin()->beforeGetName($this); - + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ return parent::getName(); }); - - return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + return $this->____plugin_advanced_plugin()->afterGetName($this, $result); default: $this->____plugin_advanced_plugin()->beforeGetName($this); - + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ $result = $this->____plugin_complex_plugin()->aroundGetName($this, function(){ return parent::getName(); }); - - return (($tmp = $this->____plugin_complex_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + return $this->____plugin_complex_plugin()->afterGetName($this, $result); }); - - return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + return $this->____plugin_advanced_plugin()->afterGetName($this, $result); } } @@ -67,7 +67,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati case 'backend': $beforeResult = $this->____plugin_complex_plugin()->beforeSetValue($this, $value); if ($beforeResult !== null) list($value) = (array)$beforeResult; - + return $this->____plugin_complex_plugin()->aroundSetValue($this, function($value){ return parent::setValue($value); }, $value); @@ -144,7 +144,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati switch ($this->____scope->getCurrentScope()) { case 'backend': $this->____plugin_complex_plugin()->beforeReturnsSelf($this); - + return parent::returnsSelf(); default: return parent::returnsSelf(); @@ -153,19 +153,19 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati /** * plugin "advanced_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + * @return \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ private function ____plugin_advanced_plugin() { - return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); + return $this->____om->get(\Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } /** * plugin "complex_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex + * @return \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex */ private function ____plugin_complex_plugin() { - return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex::class); + return $this->____om->get(\Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex::class); } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt similarity index 79% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt index b683bbbfbb6ba..f5106c08868f6 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -1,12 +1,12 @@ -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped; +namespace Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped; use Magento\Framework\Config\ScopeInterface; use Magento\Framework\ObjectManagerInterface; /** - * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped + * Interceptor class for @see \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped */ -class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped +class Interceptor extends \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped { /** * @var ScopeInterface @@ -35,7 +35,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati switch ($this->____scope->getCurrentScope()) { case 'backend': parent::returnVoid(); - + $this->____plugin_complex_plugin()->afterReturnVoid($this, null); break; default: @@ -52,8 +52,8 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati switch ($this->____scope->getCurrentScope()) { case 'backend': $result = parent::getNullableValue(); - - return (($tmp = $this->____plugin_complex_plugin()->afterGetNullableValue($this, $result)) !== null) ? $tmp : $result; + + return $this->____plugin_complex_plugin()->afterGetNullableValue($this, $result); default: return parent::getNullableValue(); } @@ -67,16 +67,16 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati switch ($this->____scope->getCurrentScope()) { case 'backend': $this->____plugin_advanced_plugin()->beforeGetName($this); - + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ $result = $this->____plugin_complex_plugin()->aroundGetName($this, function(){ return parent::getName(); }); - - return (($tmp = $this->____plugin_complex_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + return $this->____plugin_complex_plugin()->afterGetName($this, $result); }); - - return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + return $this->____plugin_advanced_plugin()->afterGetName($this, $result); default: return parent::getName(); } @@ -91,7 +91,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati case 'backend': $beforeResult = $this->____plugin_complex_plugin()->beforeSetValue($this, $value); if ($beforeResult !== null) list($value) = (array)$beforeResult; - + return $this->____plugin_complex_plugin()->aroundSetValue($this, function($value){ return parent::setValue($value); }, $value); @@ -148,12 +148,12 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati /** * @inheritdoc */ - public function returnsSelf() : \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped + public function returnsSelf() : \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ComplexItemTyped { switch ($this->____scope->getCurrentScope()) { case 'backend': $this->____plugin_complex_plugin()->beforeReturnsSelf($this); - + return parent::returnsSelf(); default: return parent::returnsSelf(); @@ -168,7 +168,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati switch ($this->____scope->getCurrentScope()) { case 'backend': $this->____plugin_complex_plugin()->beforeReturnsType($this); - + return parent::returnsType(); default: return parent::returnsType(); @@ -177,19 +177,19 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati /** * plugin "complex_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex + * @return \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex */ private function ____plugin_complex_plugin() { - return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex::class); + return $this->____om->get(\Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Complex::class); } /** * plugin "advanced_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + * @return \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ private function ____plugin_advanced_plugin() { - return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); + return $this->____om->get(\Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/Item.txt similarity index 56% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/Item.txt index 12b535f031061..af67ffa55e655 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/Item.txt +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/Item.txt @@ -1,12 +1,12 @@ -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\Item; +namespace Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\Item; use Magento\Framework\Config\ScopeInterface; use Magento\Framework\ObjectManagerInterface; /** - * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\Item + * Interceptor class for @see \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\Item */ -class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\Item +class Interceptor extends \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\Item { /** * @var ScopeInterface @@ -36,36 +36,36 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati case 'global': case 'emptyscope': $result = parent::getName(); - - return (($tmp = $this->____plugin_simple_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + return $this->____plugin_simple_plugin()->afterGetName($this, $result); default: $this->____plugin_advanced_plugin()->beforeGetName($this); - + $result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){ $result = parent::getName(); - - return (($tmp = $this->____plugin_simple_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + return $this->____plugin_simple_plugin()->afterGetName($this, $result); }); - - return (($tmp = $this->____plugin_advanced_plugin()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + return $this->____plugin_advanced_plugin()->afterGetName($this, $result); } } /** * plugin "simple_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + * @return \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple */ private function ____plugin_simple_plugin() { - return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); + return $this->____om->get(\Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); } /** * plugin "advanced_plugin" - * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + * @return \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ private function ____plugin_advanced_plugin() { - return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); + return $this->____om->get(\Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/SecondItem.txt similarity index 50% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/SecondItem.txt index 25e7976ad0409..08a0e92e0fb32 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledInterceptor/_out_interceptors/SecondItem.txt +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/SecondItem.txt @@ -1,12 +1,12 @@ -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\SecondItem; +namespace Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\SecondItem; use Magento\Framework\Config\ScopeInterface; use Magento\Framework\ObjectManagerInterface; /** - * Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\SecondItem + * Interceptor class for @see \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\SecondItem */ -class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\SecondItem +class Interceptor extends \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\SecondItem { /** * @var ScopeInterface @@ -35,36 +35,36 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati switch ($this->____scope->getCurrentScope()) { case 'frontend': $this->____plugin_advanced_plugin1()->beforeGetName($this); - + $result = $this->____plugin_advanced_plugin1()->aroundGetName($this, function(){ $this->____plugin_advanced_plugin2()->beforeGetName($this); - + $result = $this->____plugin_advanced_plugin2()->aroundGetName($this, function(){ $this->____plugin_advanced_plugin3()->beforeGetName($this); - + $result = $this->____plugin_advanced_plugin3()->aroundGetName($this, function(){ $this->____plugin_advanced_plugin4()->beforeGetName($this); - + $result = $this->____plugin_advanced_plugin4()->aroundGetName($this, function(){ return parent::getName(); }); - - return (($tmp = $this->____plugin_advanced_plugin4()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + return $this->____plugin_advanced_plugin4()->afterGetName($this, $result); }); - - $result = (($tmp = $this->____plugin_simple_plugin2()->afterGetName($this, $result)) !== null) ? $tmp : $result; - - $result = (($tmp = $this->____plugin_simple_plugin3()->afterGetName($this, $result)) !== null) ? $tmp : $result; - - return (($tmp = $this->____plugin_advanced_plugin3()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + $result = $this->____plugin_simple_plugin2()->afterGetName($this, $result); + + $result = $this->____plugin_simple_plugin3()->afterGetName($this, $result); + + return $this->____plugin_advanced_plugin3()->afterGetName($this, $result); }); - - return (($tmp = $this->____plugin_advanced_plugin2()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + return $this->____plugin_advanced_plugin2()->afterGetName($this, $result); }); - - $result = (($tmp = $this->____plugin_simple_plugin1()->afterGetName($this, $result)) !== null) ? $tmp : $result; - - return (($tmp = $this->____plugin_advanced_plugin1()->afterGetName($this, $result)) !== null) ? $tmp : $result; + + $result = $this->____plugin_simple_plugin1()->afterGetName($this, $result); + + return $this->____plugin_advanced_plugin1()->afterGetName($this, $result); default: return parent::getName(); } @@ -72,64 +72,64 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Integrati /** * plugin "advanced_plugin1" - * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + * @return \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ private function ____plugin_advanced_plugin1() { - return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); + return $this->____om->get(\Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } /** * plugin "simple_plugin1" - * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + * @return \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple */ private function ____plugin_simple_plugin1() { - return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); + return $this->____om->get(\Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); } /** * plugin "advanced_plugin2" - * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + * @return \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ private function ____plugin_advanced_plugin2() { - return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); + return $this->____om->get(\Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } /** * plugin "advanced_plugin3" - * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + * @return \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ private function ____plugin_advanced_plugin3() { - return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); + return $this->____om->get(\Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } /** * plugin "simple_plugin2" - * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + * @return \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple */ private function ____plugin_simple_plugin2() { - return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); + return $this->____om->get(\Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); } /** * plugin "simple_plugin3" - * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple + * @return \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple */ private function ____plugin_simple_plugin3() { - return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); + return $this->____om->get(\Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Simple::class); } /** * plugin "advanced_plugin4" - * @return \Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced + * @return \Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced */ private function ____plugin_advanced_plugin4() { - return $this->____om->get(\Magento\Framework\CompiledInterception\Test\Integration\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); + return $this->____om->get(\Magento\Framework\CompiledInterception\CompiledInterceptor\Custom\Module\Model\ItemPlugin\Advanced::class); } } diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/CompiledPluginListTest.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/CompiledPluginListTest.php similarity index 87% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/CompiledPluginListTest.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/CompiledPluginListTest.php index 36f4e9ec77ac8..c3d7f66857e81 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/CompiledPluginListTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/CompiledPluginListTest.php @@ -6,17 +6,18 @@ declare(strict_types=1); -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList; +namespace Magento\Framework\CompiledInterception\CompiledPluginList; +use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\App\ObjectManager; use Magento\Framework\CompiledInterception\Generator\CompiledPluginList; use Magento\Framework\CompiledInterception\Generator\FileCache; use Magento\Framework\CompiledInterception\Generator\NoSerialize; use Magento\Framework\CompiledInterception\Generator\StaticScope; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\Item; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\Item\Enhanced; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\ItemPlugin\Advanced; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\ItemPlugin\Simple; +use Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\Item; +use Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\Item\Enhanced; +use Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\ItemPlugin\Advanced; +use Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\ItemPlugin\Simple; use Magento\Framework\Interception\ObjectManager\ConfigInterface; use Magento\Framework\Interception\PluginList\PluginList; use Magento\Framework\ObjectManager\Config\Reader\Dom; @@ -55,8 +56,10 @@ public function createScopeReaders() $omConfigMock->method('getOriginalInstanceType')->willReturnArgument(0); $ret = []; $objectManagerHelper = new ObjectManagerHelper($this); + $directoryList = ObjectManager::getInstance()->get(DirectoryList::class); //clear static cache - (new FileCache())->clean(); + $fileCache = new FileCache($directoryList); + $fileCache->clean(); foreach ($readerMap as $readerLine) { $pluginList = ObjectManager::getInstance()->create( PluginList::class, @@ -65,7 +68,7 @@ public function createScopeReaders() 'configScope' => new StaticScope($readerLine[0]), 'reader' => $readerMock, 'omConfig' => $omConfigMock, - 'cache' => new FileCache(), + 'cache' => $fileCache, 'cachePath' => false, 'serializer' => new NoSerialize() ] @@ -139,7 +142,7 @@ public function getPluginsDataProvider() 'advanced_plugin' ], // simple plugin is disabled in configuration for - // \Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\Item + // \Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\Item // in frontend [null, Item::class, 'getName', 'frontend'], // test plugin inheritance diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/Item.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/Custom/Module/Model/Item.php similarity index 72% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/Item.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/Custom/Module/Model/Item.php index e12465eeb9c62..2a74773067307 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/Item.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/Custom/Module/Model/Item.php @@ -8,7 +8,7 @@ // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model; +namespace Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model; class Item { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/Item/Enhanced.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/Custom/Module/Model/Item/Enhanced.php similarity index 55% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/Item/Enhanced.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/Custom/Module/Model/Item/Enhanced.php index c8006867a3702..5fc7366c32517 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/Item/Enhanced.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/Custom/Module/Model/Item/Enhanced.php @@ -7,9 +7,9 @@ declare(strict_types=1); // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\Item; +namespace Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\Item; -class Enhanced extends \Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\Item +class Enhanced extends \Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\Item { /** * @return string diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/ItemPlugin/Advanced.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/Custom/Module/Model/ItemPlugin/Advanced.php similarity index 88% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/ItemPlugin/Advanced.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/Custom/Module/Model/ItemPlugin/Advanced.php index 2f943fcc4802a..e4e76f47aeb10 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/ItemPlugin/Advanced.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/Custom/Module/Model/ItemPlugin/Advanced.php @@ -8,7 +8,7 @@ // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\ItemPlugin; +namespace Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\ItemPlugin; class Advanced { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/ItemPlugin/Simple.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/Custom/Module/Model/ItemPlugin/Simple.php similarity index 79% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/ItemPlugin/Simple.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/Custom/Module/Model/ItemPlugin/Simple.php index 6d891e80ce607..2eeea3e110cfd 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/Custom/Module/Model/ItemPlugin/Simple.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/Custom/Module/Model/ItemPlugin/Simple.php @@ -8,7 +8,7 @@ // @codingStandardsIgnoreFile -namespace Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\ItemPlugin; +namespace Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\ItemPlugin; class Simple { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/_files/reader_mock_map.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/_files/reader_mock_map.php similarity index 77% rename from lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/_files/reader_mock_map.php rename to dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/_files/reader_mock_map.php index b9e722c4bcdf4..4f7e5c3844a46 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Test/Integration/CompiledPluginList/_files/reader_mock_map.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/_files/reader_mock_map.php @@ -6,10 +6,10 @@ declare(strict_types=1); -use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\Item; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\Item\Enhanced; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\ItemPlugin\Advanced; -use Magento\Framework\CompiledInterception\Test\Integration\CompiledPluginList\Custom\Module\Model\ItemPlugin\Simple; +use Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\Item; +use Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\Item\Enhanced; +use Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\ItemPlugin\Advanced; +use Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\ItemPlugin\Simple; return [ [ diff --git a/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php b/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php index d5b9c89487c72..264ec4fe2a0e0 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php +++ b/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php @@ -52,6 +52,7 @@ public function setUp() public function tearDown() { \Magento\Framework\App\ObjectManager::setInstance($this->applicationObjectManager); + $this->restoreOriginalObjectManager(); } /** @@ -117,11 +118,41 @@ public function setUpInterceptionConfig($pluginConfig) [ 'preferences' => [ \Magento\Framework\Interception\PluginListInterface::class => - \Magento\Framework\CompiledInterception\Generator\CompiledPluginList::class, + \Magento\Framework\Interception\PluginList\PluginList::class, \Magento\Framework\Interception\ChainInterface::class => \Magento\Framework\Interception\Chain\Chain::class, + \Magento\Framework\Interception\Code\Generator\InterceptorInterface::class => + \Magento\Framework\Interception\Code\Generator\Interceptor::class ], ] ); + + $this->injectConfiguredObjectManager(); + } + + /** + * Inject configured object manager into autoloader. + */ + private function injectConfiguredObjectManager(): void + { + foreach (spl_autoload_functions() as $autoloader) { + if (is_array($autoloader) && $autoloader[0] instanceof \Magento\Framework\Code\Generator\Autoloader) { + $autoloader[0]->getGenerator()->setObjectManager($this->_objectManager); + break; + } + } + } + + /** + * Restore original object manager in autoloader. + */ + private function restoreOriginalObjectManager(): void + { + foreach (spl_autoload_functions() as $autoloader) { + if (is_array($autoloader) && $autoloader[0] instanceof \Magento\Framework\Code\Generator\Autoloader) { + $autoloader[0]->getGenerator()->setObjectManager($this->applicationObjectManager); + break; + } + } } } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/State/CleanupFilesTest.php b/lib/internal/Magento/Framework/App/Test/Unit/State/CleanupFilesTest.php index 912b3d43103de..5d367251e3a8a 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/State/CleanupFilesTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/State/CleanupFilesTest.php @@ -6,9 +6,8 @@ namespace Magento\Framework\App\Test\Unit\State; -use \Magento\Framework\App\State\CleanupFiles; - use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Framework\App\State\CleanupFiles; use Magento\Framework\Filesystem\DriverPool; class CleanupFilesTest extends \PHPUnit\Framework\TestCase @@ -33,13 +32,15 @@ public function testClearCodeGeneratedClasses() { $dir1 = $this->getDirectoryCleanMock(); $dir2 = $this->getDirectoryCleanMock(); - $this->filesystem->expects($this->exactly(2)) + $dir3 = $this->getDirectoryCleanMock(); + $this->filesystem->expects($this->exactly(3)) ->method('getDirectoryWrite') ->will( $this->returnValueMap( [ [DirectoryList::GENERATED_CODE, DriverPool::FILE, $dir1], [DirectoryList::GENERATED_METADATA, DriverPool::FILE, $dir2], + [DirectoryList::STATIC_CACHE, DriverPool::FILE, $dir3], ] ) ); @@ -50,10 +51,16 @@ public function testClearMaterializedViewFiles() { $static = $this->getDirectoryCleanMock(); $var = $this->getDirectoryCleanMock(DirectoryList::TMP_MATERIALIZATION_DIR); - $this->filesystem->expects($this->exactly(2))->method('getDirectoryWrite')->will($this->returnValueMap([ - [DirectoryList::STATIC_VIEW, DriverPool::FILE, $static], - [DirectoryList::VAR_DIR, DriverPool::FILE, $var], - ])); + $this->filesystem->expects( + $this->exactly(2) + )->method('getDirectoryWrite')->will( + $this->returnValueMap( + [ + [DirectoryList::STATIC_VIEW, DriverPool::FILE, $static], + [DirectoryList::VAR_DIR, DriverPool::FILE, $var], + ] + ) + ); $this->object->clearMaterializedViewFiles(); } diff --git a/lib/internal/Magento/Framework/Code/Generator/Autoloader.php b/lib/internal/Magento/Framework/Code/Generator/Autoloader.php index 007846b42d90a..80faf3b47ca43 100644 --- a/lib/internal/Magento/Framework/Code/Generator/Autoloader.php +++ b/lib/internal/Magento/Framework/Code/Generator/Autoloader.php @@ -89,12 +89,15 @@ private function tryToLogException(\Exception $exception): void try { $logger = ObjectManager::getInstance()->get(LoggerInterface::class); $logger->debug($exception->getMessage(), ['exception' => $exception]); + // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock } catch (\Exception $ignoreThisException) { // Do not take an action here, since the original exception might have been caused by logger } } /** + * Set generator instance. + * * @param Generator $generator */ public function setGenerator(Generator $generator): void @@ -103,6 +106,8 @@ public function setGenerator(Generator $generator): void } /** + * Get generator instance. + * * @return Generator */ public function getGenerator(): Generator diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 78445d03c1e37..39c41ebb9f8e4 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -287,7 +287,7 @@ private function injectPropertiesSettersToConstructor(\ReflectionMethod $parentC private function addCodeSubBlock(&$body, $sub, $indent = 1) { foreach ($sub as $line) { - $body[] = str_repeat("\t", $indent) . $line; + $body[] = ('' === $line) ? '' : str_repeat("\t", $indent) . $line; } } diff --git a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php index d1658b3a99a63..49fb7648a24e3 100644 --- a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php +++ b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php @@ -10,6 +10,8 @@ use Magento\Framework\ObjectManager\Definition\Runtime; /** + * Definition factory. + * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class DefinitionFactory diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php index 8d15734d32798..04d9c0df5e152 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php @@ -5,6 +5,9 @@ */ namespace Magento\Framework\ObjectManager\Factory; +/** + * Compiled factory. + */ class Compiled extends AbstractFactory { /** From 23bdd029a881cb8f7bda84dbf4fdbcd72dc55975 Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Wed, 13 Nov 2019 16:34:18 +0200 Subject: [PATCH 44/52] magento/magento2#22826: Refactoring. --- .../Interception/CompiledPluginList.php | 2 +- .../Magento/Framework/Console/CliTest.php | 8 ++- .../Framework/Interception/AbstractPlugin.php | 34 ++++++------- .../Framework/Code/Generator/Autoloader.php | 20 -------- .../Magento/Framework/Model/CallbackPool.php | 50 +++++++++++++++++++ .../ObjectManager/DefinitionFactory.php | 13 ++--- 6 files changed, 76 insertions(+), 51 deletions(-) create mode 100644 lib/internal/Magento/Framework/Model/CallbackPool.php diff --git a/dev/tests/integration/framework/Magento/TestFramework/Interception/CompiledPluginList.php b/dev/tests/integration/framework/Magento/TestFramework/Interception/CompiledPluginList.php index 2c8b78d6df670..178e58fcb4f77 100644 --- a/dev/tests/integration/framework/Magento/TestFramework/Interception/CompiledPluginList.php +++ b/dev/tests/integration/framework/Magento/TestFramework/Interception/CompiledPluginList.php @@ -37,7 +37,7 @@ public function __construct( OriginalPluginList $unused ) { $objectManager = ObjectManager::getInstance(); - $this->pluginList = ObjectManager::getInstance()->create( + $this->pluginList = $objectManager->create( PluginList::class, [ $objectManager->get(Dom::class), diff --git a/dev/tests/integration/testsuite/Magento/Framework/Console/CliTest.php b/dev/tests/integration/testsuite/Magento/Framework/Console/CliTest.php index 95ed07c1eaf03..a1df401974e8d 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/Console/CliTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/Console/CliTest.php @@ -11,6 +11,7 @@ use Magento\Framework\App\DeploymentConfig\FileReader; use Magento\Framework\App\DeploymentConfig\Writer; use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Framework\Code\Generator\Autoloader; use Magento\Framework\Config\File\ConfigFilePool; use Magento\Framework\Filesystem; use Magento\Framework\ObjectManagerInterface; @@ -93,8 +94,11 @@ public function testDocumentRootIsPublic($isPub, $params) $cliReflection = new \ReflectionClass($cli); foreach (spl_autoload_functions() as $autoloader) { - if (is_array($autoloader) && $autoloader[0] instanceof \Magento\Framework\Code\Generator\Autoloader) { - $autoloader[0]->getGenerator()->setObjectManager($this->objectManager); + if (is_array($autoloader) && $autoloader[0] instanceof Autoloader) { + $autoloaderReflection = new \ReflectionClass($autoloader[0]); + $generatorProperty = $autoloaderReflection->getProperty('_generator'); + $generatorProperty->setAccessible(true); + $generatorProperty->getValue($autoloader[0])->setObjectManager($this->objectManager); break; } } diff --git a/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php b/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php index 264ec4fe2a0e0..41c2af289a331 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php +++ b/dev/tests/integration/testsuite/Magento/Framework/Interception/AbstractPlugin.php @@ -5,6 +5,9 @@ */ namespace Magento\Framework\Interception; +use Magento\Framework\Code\Generator\Autoloader; +use Magento\Framework\ObjectManagerInterface; + /** * Class GeneralTest * @@ -52,7 +55,7 @@ public function setUp() public function tearDown() { \Magento\Framework\App\ObjectManager::setInstance($this->applicationObjectManager); - $this->restoreOriginalObjectManager(); + $this->injectObjectManager($this->applicationObjectManager); } /** @@ -127,30 +130,23 @@ public function setUpInterceptionConfig($pluginConfig) ] ); - $this->injectConfiguredObjectManager(); + $this->injectObjectManager($this->_objectManager); } /** - * Inject configured object manager into autoloader. - */ - private function injectConfiguredObjectManager(): void - { - foreach (spl_autoload_functions() as $autoloader) { - if (is_array($autoloader) && $autoloader[0] instanceof \Magento\Framework\Code\Generator\Autoloader) { - $autoloader[0]->getGenerator()->setObjectManager($this->_objectManager); - break; - } - } - } - - /** - * Restore original object manager in autoloader. + * Inject object manager into autoloader. + * + * @param ObjectManagerInterface $objectManager + * @throws \ReflectionException */ - private function restoreOriginalObjectManager(): void + private function injectObjectManager(ObjectManagerInterface $objectManager): void { foreach (spl_autoload_functions() as $autoloader) { - if (is_array($autoloader) && $autoloader[0] instanceof \Magento\Framework\Code\Generator\Autoloader) { - $autoloader[0]->getGenerator()->setObjectManager($this->applicationObjectManager); + if (is_array($autoloader) && $autoloader[0] instanceof Autoloader) { + $autoloaderReflection = new \ReflectionClass($autoloader[0]); + $generatorProperty = $autoloaderReflection->getProperty('_generator'); + $generatorProperty->setAccessible(true); + $generatorProperty->getValue($autoloader[0])->setObjectManager($objectManager); break; } } diff --git a/lib/internal/Magento/Framework/Code/Generator/Autoloader.php b/lib/internal/Magento/Framework/Code/Generator/Autoloader.php index 80faf3b47ca43..8a57710afc700 100644 --- a/lib/internal/Magento/Framework/Code/Generator/Autoloader.php +++ b/lib/internal/Magento/Framework/Code/Generator/Autoloader.php @@ -94,24 +94,4 @@ private function tryToLogException(\Exception $exception): void // Do not take an action here, since the original exception might have been caused by logger } } - - /** - * Set generator instance. - * - * @param Generator $generator - */ - public function setGenerator(Generator $generator): void - { - $this->_generator = $generator; - } - - /** - * Get generator instance. - * - * @return Generator - */ - public function getGenerator(): Generator - { - return $this->_generator; - } } diff --git a/lib/internal/Magento/Framework/Model/CallbackPool.php b/lib/internal/Magento/Framework/Model/CallbackPool.php new file mode 100644 index 0000000000000..e908f368f8e06 --- /dev/null +++ b/lib/internal/Magento/Framework/Model/CallbackPool.php @@ -0,0 +1,50 @@ +getCodeGenerator(); - foreach (spl_autoload_functions() as $autoloader) { if (is_array($autoloader) && $autoloader[0] instanceof \Magento\Framework\Code\Generator\Autoloader) { - $autoloader[0]->setGenerator($codeGenerator); - $autoloaderAbsent = false; + spl_autoload_unregister($autoloader); break; } } - if ($autoloaderAbsent) { - $autoloaderInstance = new Autoloader($codeGenerator); - spl_autoload_register([$autoloaderInstance, 'load']); - } + $autoloader = new Autoloader($this->getCodeGenerator()); + spl_autoload_register([$autoloader, 'load']); return new Runtime(); } @@ -107,6 +101,7 @@ public function getCodeGenerator() ); $this->codeGenerator = new \Magento\Framework\Code\Generator($generatorIo); } + return $this->codeGenerator; } } From c263481cd6b87503a814edcea5f1983f086cd775 Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Thu, 14 Nov 2019 15:21:51 +0200 Subject: [PATCH 45/52] magento/magento2#22826: Compiled interceptor constructors generation fix. --- app/etc/di.xml | 2 +- .../_out_interceptors/ComplexItem.txt | 6 +- .../_out_interceptors/ComplexItemTyped.txt | 6 +- .../_out_interceptors/Item.txt | 6 +- .../_out_interceptors/SecondItem.txt | 6 +- .../Generator/CompiledInterceptor.php | 30 +++++----- .../ObjectManager/Factory/Compiled.php | 8 +-- .../Chain/CompiledInterceptorSubstitution.php | 57 ------------------- 8 files changed, 27 insertions(+), 94 deletions(-) delete mode 100644 setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php diff --git a/app/etc/di.xml b/app/etc/di.xml index 0dc700f897638..dec6a3fb7ef66 100644 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -787,7 +787,7 @@ - + Magento\Framework\CompiledInterception\Generator\CompiledInterceptor diff --git a/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/ComplexItem.txt b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/ComplexItem.txt index 46d83d323fc13..dd0c9155e95f4 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/ComplexItem.txt +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/ComplexItem.txt @@ -21,10 +21,10 @@ class Interceptor extends \Magento\Framework\CompiledInterception\CompiledInterc /** * @inheritdoc */ - public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\ScopeInterface $____scope) + public function __construct() { - $this->____om = $____om; - $this->____scope = $____scope; + $this->____om = \Magento\Framework\App\ObjectManager::getInstance(); + $this->____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); } /** diff --git a/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt index f5106c08868f6..2ba7c318be025 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt @@ -21,10 +21,10 @@ class Interceptor extends \Magento\Framework\CompiledInterception\CompiledInterc /** * @inheritdoc */ - public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\ScopeInterface $____scope) + public function __construct() { - $this->____om = $____om; - $this->____scope = $____scope; + $this->____om = \Magento\Framework\App\ObjectManager::getInstance(); + $this->____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); } /** diff --git a/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/Item.txt b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/Item.txt index af67ffa55e655..b14d36d5f0e23 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/Item.txt +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/Item.txt @@ -21,10 +21,10 @@ class Interceptor extends \Magento\Framework\CompiledInterception\CompiledInterc /** * @inheritdoc */ - public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\ScopeInterface $____scope) + public function __construct() { - $this->____om = $____om; - $this->____scope = $____scope; + $this->____om = \Magento\Framework\App\ObjectManager::getInstance(); + $this->____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); } /** diff --git a/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/SecondItem.txt b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/SecondItem.txt index 08a0e92e0fb32..4df8943d0974a 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/SecondItem.txt +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/_out_interceptors/SecondItem.txt @@ -21,10 +21,10 @@ class Interceptor extends \Magento\Framework\CompiledInterception\CompiledInterc /** * @inheritdoc */ - public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\ScopeInterface $____scope) + public function __construct() { - $this->____om = $____om; - $this->____scope = $____scope; + $this->____om = \Magento\Framework\App\ObjectManager::getInstance(); + $this->____scope = \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Framework\Config\ScopeInterface::class); } /** diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 39c41ebb9f8e4..3e1ca0ff1e511 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -81,11 +81,11 @@ public function setInterceptedMethods($interceptedMethods) } /** - * Get properties to be injected from DI + * Get properties to be set in constructor. * * @return array */ - public static function propertiesToInjectToConstructor() + public static function propertiesToSetInConstructor() { return [ ScopeInterface::class => '____scope', @@ -127,7 +127,7 @@ protected function _getDefaultConstructorDefinition() { return $this->injectPropertiesSettersToConstructor( $this->getSourceClassReflection()->getConstructor(), - static::propertiesToInjectToConstructor() + static::propertiesToSetInConstructor() ); } @@ -159,7 +159,7 @@ private function generateMethodsAndProperties() $this->classMethods = []; $this->classProperties = []; - foreach (static::propertiesToInjectToConstructor() as $type => $name) { + foreach (static::propertiesToSetInConstructor() as $type => $name) { $this->_classGenerator->addUse($type); $this->classProperties[] = [ 'name' => $name, @@ -245,29 +245,25 @@ private function injectPropertiesSettersToConstructor(\ReflectionMethod $parentC } $body = ["parent::__construct(" . implode(', ', $parentCallParams) . ");"]; } - $extraParams = $properties; - $extraSetters = array_combine($properties, $properties); + $extraSetters = array_fill_keys(array_values($properties), null); foreach ($parameters as $parameter) { if ($parameter->getType()) { $type = $parameter->getType()->getName(); if (isset($properties[$type])) { $extraSetters[$properties[$type]] = $parameter->getName(); - unset($extraParams[$type]); } } } $parameters = array_map([$this, '_getMethodParameterInfo'], $parameters); - foreach ($extraParams as $type => $name) { - array_unshift( - $parameters, - [ - 'name' => $name, - 'type' => $type - ] - ); - } foreach ($extraSetters as $name => $paramName) { - array_unshift($body, "\$this->$name = \$$paramName;"); + $class = array_search($name, $properties); + if ($paramName !== null) { + array_unshift($body, "\$this->$name = \$$paramName;"); + } elseif ($class === ObjectManagerInterface::class) { + array_unshift($body, "\$this->$name = \Magento\Framework\App\ObjectManager::getInstance();"); + } else { + array_unshift($body, "\$this->$name = \Magento\Framework\App\ObjectManager::getInstance()->get(\\$class::class);"); + } } return [ 'name' => '__construct', diff --git a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php index 04d9c0df5e152..64a67c0bb39fe 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Factory/Compiled.php @@ -55,14 +55,8 @@ public function __construct( */ public function create($requestedType, array $arguments = []) { + $args = $this->config->getArguments($requestedType); $type = $this->config->getInstanceType($requestedType); - $requestedTypeArgs = $this->config->getArguments($requestedType); - $realTypeArgs = $this->config->getArguments($type); - if ($realTypeArgs !== null && $requestedTypeArgs !== null) { - $args = array_merge($realTypeArgs, $requestedTypeArgs); - } else { - $args = $requestedTypeArgs ?? $realTypeArgs; - } if ($args === []) { // Case 1: no arguments required diff --git a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php deleted file mode 100644 index 2dfc10f288150..0000000000000 --- a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/CompiledInterceptorSubstitution.php +++ /dev/null @@ -1,57 +0,0 @@ -interceptorSubstitution = $interceptorSubstitution; - } - - /** - * Modifies input config - * - * @param array $config - * @return array - */ - public function modify(array $config) - { - $config = $this->interceptorSubstitution->modify($config); - - foreach ($config['arguments'] as $instanceName => &$arguments) { - if (substr($instanceName, -12) === '\Interceptor') { - foreach (CompiledInterceptor::propertiesToInjectToConstructor() as $type => $name) { - $preference = isset($config['preferences'][$type]) ? $config['preferences'][$type] : $type; - foreach ($arguments as $argument) { - if (isset($argument['_i_']) && $argument['_i_'] === $preference) { - continue 2; - } - } - // phpcs:ignore Magento2.Performance.ForeachArrayMerge - $arguments = array_merge([$name => ['_i_' => $preference]], $arguments); - } - - } - } - - return $config; - } -} From c082b692e81710ebf2419c223005ef0d7f714e8d Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Fri, 15 Nov 2019 22:49:06 +0200 Subject: [PATCH 46/52] magento/magento2#22826: Static test fix. --- .../Generator/CompiledInterceptor.php | 15 ++++++++++++--- .../Magento/Framework/Model/CallbackPool.php | 2 ++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php index 3e1ca0ff1e511..d69275fc39c1d 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php @@ -258,11 +258,20 @@ private function injectPropertiesSettersToConstructor(\ReflectionMethod $parentC foreach ($extraSetters as $name => $paramName) { $class = array_search($name, $properties); if ($paramName !== null) { - array_unshift($body, "\$this->$name = \$$paramName;"); + array_unshift( + $body, + "\$this->$name = \$$paramName;" + ); } elseif ($class === ObjectManagerInterface::class) { - array_unshift($body, "\$this->$name = \Magento\Framework\App\ObjectManager::getInstance();"); + array_unshift( + $body, + "\$this->$name = \Magento\Framework\App\ObjectManager::getInstance();" + ); } else { - array_unshift($body, "\$this->$name = \Magento\Framework\App\ObjectManager::getInstance()->get(\\$class::class);"); + array_unshift( + $body, + "\$this->$name = \Magento\Framework\App\ObjectManager::getInstance()->get(\\$class::class);" + ); } } return [ diff --git a/lib/internal/Magento/Framework/Model/CallbackPool.php b/lib/internal/Magento/Framework/Model/CallbackPool.php index e908f368f8e06..263a7bc94353f 100644 --- a/lib/internal/Magento/Framework/Model/CallbackPool.php +++ b/lib/internal/Magento/Framework/Model/CallbackPool.php @@ -10,6 +10,8 @@ use Magento\Framework\DB\Adapter\Pdo\CallbackPool as PdoCallbackPool; /** + * Callback pool. + * * @deprecated please use Magento\Framework\DB\Adapter\Pdo\CallbackPool. */ class CallbackPool From 5b57dbc3d838a15070e4796953c1c974b5cc83ba Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Mon, 18 Nov 2019 12:19:26 +0200 Subject: [PATCH 47/52] magento/magento2#22826: Compiled plugin list argument update. --- app/etc/di.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/etc/di.xml b/app/etc/di.xml index dec6a3fb7ef66..4377f74c3d79c 100644 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -783,7 +783,7 @@ - \Magento\Framework\CompiledInterception\PluginList\PluginList + \Magento\Framework\CompiledInterception\PluginList\PluginList From de6e95b66e9e37a08d23930f212628a961f0807b Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Mon, 18 Nov 2019 14:13:50 +0200 Subject: [PATCH 48/52] magento/magento2#22826: Readme update. --- .../Magento/Framework/CompiledInterception/README.md | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/internal/Magento/Framework/CompiledInterception/README.md b/lib/internal/Magento/Framework/CompiledInterception/README.md index a6993ce0ec25f..77b0bb52f47f1 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/README.md +++ b/lib/internal/Magento/Framework/CompiledInterception/README.md @@ -14,14 +14,10 @@ The Interceptors generated by this plugin are 100% compatible with the ones gene ### ENABLING -To use compiled interceptors in developer mode please add following preference to your di.xml +To use compiled interceptors please add following preference to your di.xml ``` - -``` - -To use compiled interceptors in production or default mode please add following preferences to your di.xml -``` - + + Magento\Framework\CompiledInterception\Generator\CompiledInterceptor From 7bc9f69373e244c1c245b8f0abd5a4f83efbf12a Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Mon, 18 Nov 2019 16:51:25 +0200 Subject: [PATCH 49/52] magento/magento2#22826: Disable compiled interceptors. --- app/etc/di.xml | 10 ++-------- dev/tests/integration/etc/di/preferences/ce.php | 2 +- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/app/etc/di.xml b/app/etc/di.xml index 4377f74c3d79c..1e0b1e2c0580d 100644 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -69,7 +69,7 @@ - + @@ -786,14 +786,8 @@ \Magento\Framework\CompiledInterception\PluginList\PluginList - + - - - Magento\Framework\CompiledInterception\Generator\CompiledInterceptor - - - diff --git a/dev/tests/integration/etc/di/preferences/ce.php b/dev/tests/integration/etc/di/preferences/ce.php index a24a5c5a9c7d5..9f96a9c2d1498 100644 --- a/dev/tests/integration/etc/di/preferences/ce.php +++ b/dev/tests/integration/etc/di/preferences/ce.php @@ -15,7 +15,7 @@ \Magento\Framework\App\ResponseInterface::class => \Magento\TestFramework\Response::class, \Magento\Framework\App\Response\Http::class => \Magento\TestFramework\Response::class, \Magento\Framework\Interception\PluginListInterface::class => - \Magento\TestFramework\Interception\CompiledPluginList::class, + \Magento\TestFramework\Interception\PluginList::class, \Magento\Framework\Interception\ObjectManager\ConfigInterface::class => \Magento\TestFramework\ObjectManager\Config::class, \Magento\Framework\Interception\ObjectManager\Config\Developer::class => From f6e7ac465beedd6d09eea56391372b7d3142d300 Mon Sep 17 00:00:00 2001 From: Stanislav Idolov Date: Thu, 17 Sep 2020 11:10:17 -0500 Subject: [PATCH 50/52] Fixed incorrectly resolved merge conflicts --- setup/src/Magento/Setup/Console/Command/DiCompileCommand.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php b/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php index cf0aa5522d799..e2231ed142285 100644 --- a/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php +++ b/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php @@ -10,8 +10,6 @@ use Magento\Framework\Filesystem\Io\File; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Magento\Framework\Filesystem; -use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\App\DeploymentConfig; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\App\ObjectManager\ConfigWriterInterface; @@ -19,7 +17,6 @@ use Magento\Framework\Config\ConfigOptionsListConstants; use Magento\Framework\Console\Cli; use Magento\Framework\Filesystem; -use Magento\Framework\Filesystem\DriverInterface; use Magento\Setup\Model\ObjectManagerProvider; use Magento\Setup\Module\Di\App\Task\Manager; use Magento\Setup\Module\Di\App\Task\OperationException; @@ -28,8 +25,6 @@ use Magento\Setup\Module\Di\Compiler\Config\Chain\InterceptorSubstitutionInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; /** * Command to run compile in single-tenant mode From 9d1f67c44d66738f024575facd28f4a723c9d4b0 Mon Sep 17 00:00:00 2001 From: Stanislav Idolov Date: Thu, 17 Sep 2020 11:41:41 -0500 Subject: [PATCH 51/52] Fixed incorrectly resolved merge conflicts --- .../Framework/Interception/Code/Generator/Interceptor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php b/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php index 794ecb7cceeb7..14f9563f43fbc 100644 --- a/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php +++ b/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php @@ -11,7 +11,7 @@ class Interceptor extends EntityAbstract implements InterceptorInterface { - public const ENTITY_TYPE = 'interceptor' + public const ENTITY_TYPE = 'interceptor'; /** * Returns default result class name From 3fb942231b43a33bd356f2900ba1b9db47b5815c Mon Sep 17 00:00:00 2001 From: Stanislav Idolov Date: Thu, 17 Sep 2020 15:40:30 -0500 Subject: [PATCH 52/52] Fixed tests failures --- .../CompiledInterceptor/CompiledInterceptorTest.php | 6 +----- .../CompiledPluginList/CompiledPluginListTest.php | 5 +++-- .../CompiledInterception/Generator/AreasPluginList.php | 2 +- .../CompiledInterception/Generator/CompiledPluginList.php | 2 +- .../Framework/CompiledInterception/Generator/FileCache.php | 2 +- .../CompiledInterception/Generator/NoSerialize.php | 2 +- .../CompiledInterception/Generator/StaticScope.php | 3 --- .../Magento/Framework/DB/Adapter/Pdo/CallbackPool.php | 3 --- .../Magento/Framework/EntityManager/CallbackHandler.php | 3 --- lib/internal/Magento/Framework/Model/CallbackPool.php | 2 -- .../Magento/Framework/ObjectManager/DefinitionFactory.php | 2 -- .../Setup/Module/Di/App/Task/Operation/Interception.php | 3 --- .../Di/Compiler/Config/Chain/InterceptorSubstitution.php | 3 --- 13 files changed, 8 insertions(+), 30 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/CompiledInterceptorTest.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/CompiledInterceptorTest.php index 8f1a871fad656..c18f92b0d3afc 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/CompiledInterceptorTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledInterceptor/CompiledInterceptorTest.php @@ -28,10 +28,6 @@ use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; use PHPUnit\Framework\MockObject\MockObject; -/** - * Class CompiledInterceptorTest - * @SuppressWarnings(PHPMD.CouplingBetweenObjects) - */ class CompiledInterceptorTest extends \PHPUnit\Framework\TestCase { /** @@ -47,7 +43,7 @@ class CompiledInterceptorTest extends \PHPUnit\Framework\TestCase /** * @inheritdoc */ - protected function setUp() + protected function setUp(): void { $this->ioGenerator = $this->getMockBuilder(Io::class) ->disableOriginalConstructor() diff --git a/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/CompiledPluginListTest.php b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/CompiledPluginListTest.php index c3d7f66857e81..82367bdd99952 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/CompiledPluginListTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/CompiledInterception/CompiledPluginList/CompiledPluginListTest.php @@ -18,6 +18,7 @@ use Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\Item\Enhanced; use Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\ItemPlugin\Advanced; use Magento\Framework\CompiledInterception\CompiledPluginList\Custom\Module\Model\ItemPlugin\Simple; +use Magento\Framework\Exception\InvalidArgumentException; use Magento\Framework\Interception\ObjectManager\ConfigInterface; use Magento\Framework\Interception\PluginList\PluginList; use Magento\Framework\ObjectManager\Config\Reader\Dom; @@ -35,7 +36,7 @@ class CompiledPluginListTest extends \PHPUnit\Framework\TestCase */ private $objects; - protected function setUp() + protected function setUp(): void { $this->objects = $this->createScopeReaders(); } @@ -163,13 +164,13 @@ public function getPluginsDataProvider() } /** - * @expectedException \InvalidArgumentException * @covers \Magento\Framework\Interception\PluginList\PluginList::getNext * @covers \Magento\Framework\Interception\PluginList\PluginList::_inheritPlugins */ public function testInheritPluginsWithNonExistingClass() { $this->objects['frontend']->getNext('SomeType', 'someMethod'); + $this->expectException(InvalidArgumentException::class); } /** diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php index dde8cb33e1e8c..1995ed4965224 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/AreasPluginList.php @@ -9,7 +9,7 @@ use Magento\Framework\Config\Scope; /** - * Class AreasPluginList + * Plugin list provider for area. */ class AreasPluginList { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php index d559c86945e71..062449125f7d6 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php @@ -11,7 +11,7 @@ use Magento\Framework\Interception\PluginListInterface; /** - * Class CompiledPluginList + * List provider for compiled pligins. */ class CompiledPluginList implements PluginListInterface { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php index f5567a64644d7..3b94c1b7f5982 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/FileCache.php @@ -10,7 +10,7 @@ use Magento\Framework\Config\CacheInterface; /** - * Class FileCache + * Files cache management. */ class FileCache implements CacheInterface { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/NoSerialize.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/NoSerialize.php index bee3e2804ecc1..dca9428d1e6b5 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/NoSerialize.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/NoSerialize.php @@ -8,7 +8,7 @@ use Magento\Framework\Serialize\SerializerInterface; /** - * Class NoSerialize + * Serializer. */ class NoSerialize implements SerializerInterface { diff --git a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php index 4159c5bd22eb7..d14a0c9e6386c 100644 --- a/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php +++ b/lib/internal/Magento/Framework/CompiledInterception/Generator/StaticScope.php @@ -7,9 +7,6 @@ use Magento\Framework\Config\ScopeInterface; -/** - * Class StaticScope - */ class StaticScope implements ScopeInterface { /** diff --git a/lib/internal/Magento/Framework/DB/Adapter/Pdo/CallbackPool.php b/lib/internal/Magento/Framework/DB/Adapter/Pdo/CallbackPool.php index cd83afd4977ca..d35e4ddf948d7 100644 --- a/lib/internal/Magento/Framework/DB/Adapter/Pdo/CallbackPool.php +++ b/lib/internal/Magento/Framework/DB/Adapter/Pdo/CallbackPool.php @@ -7,9 +7,6 @@ namespace Magento\Framework\DB\Adapter\Pdo; -/** - * Class CallbackPool - */ class CallbackPool { /** diff --git a/lib/internal/Magento/Framework/EntityManager/CallbackHandler.php b/lib/internal/Magento/Framework/EntityManager/CallbackHandler.php index fca369848ee0f..8d401ca8cb536 100644 --- a/lib/internal/Magento/Framework/EntityManager/CallbackHandler.php +++ b/lib/internal/Magento/Framework/EntityManager/CallbackHandler.php @@ -9,9 +9,6 @@ use Magento\Framework\DB\Adapter\Pdo\CallbackPool; use Psr\Log\LoggerInterface; -/** - * Class CallbackHandler - */ class CallbackHandler { /** diff --git a/lib/internal/Magento/Framework/Model/CallbackPool.php b/lib/internal/Magento/Framework/Model/CallbackPool.php index 263a7bc94353f..e908f368f8e06 100644 --- a/lib/internal/Magento/Framework/Model/CallbackPool.php +++ b/lib/internal/Magento/Framework/Model/CallbackPool.php @@ -10,8 +10,6 @@ use Magento\Framework\DB\Adapter\Pdo\CallbackPool as PdoCallbackPool; /** - * Callback pool. - * * @deprecated please use Magento\Framework\DB\Adapter\Pdo\CallbackPool. */ class CallbackPool diff --git a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php index fc93f3e1dac8c..d0f498e26db6b 100644 --- a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php +++ b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php @@ -10,8 +10,6 @@ use Magento\Framework\ObjectManager\Definition\Runtime; /** - * Definition factory. - * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class DefinitionFactory diff --git a/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php b/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php index b61a95df1c88d..e07535f532c4a 100644 --- a/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php +++ b/setup/src/Magento/Setup/Module/Di/App/Task/Operation/Interception.php @@ -13,9 +13,6 @@ use Magento\Setup\Module\Di\Code\GeneratorFactory; use Magento\Setup\Module\Di\Code\Reader\ClassesScanner; -/** - * Class Interception - */ class Interception implements OperationInterface { /** diff --git a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitution.php b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitution.php index d3a3439af91dc..9f99d5da90a84 100644 --- a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitution.php +++ b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/InterceptorSubstitution.php @@ -7,9 +7,6 @@ use Magento\Setup\Module\Di\Compiler\Config\ModificationInterface; -/** - * Class InterceptorSubstitution - */ class InterceptorSubstitution implements ModificationInterface, InterceptorSubstitutionInterface { /**