Classes.php 11.6 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
<?php
/**
 * A helper for handling Magento-specific class names in various use cases
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\App\Utility;

use Magento\Framework\Component\ComponentRegistrar;

/**
 * Utility for class names processing
 */
class Classes
{
    /**
     * virtual class declarations collected from the whole system
     *
     * @var array
     */
    protected static $_virtualClasses = [];

    /**
     * Find all unique matches in specified content using specified PCRE
     *
     * @param string $contents
     * @param string $regex
     * @param array &$result
     * @return array
     */
    public static function getAllMatches($contents, $regex, &$result = [])
    {
        preg_match_all($regex, $contents, $matches);

        array_shift($matches);
        foreach ($matches as $row) {
            $result = array_merge($result, $row);
        }
        $result = array_filter(
            array_unique($result),
            function ($value) {
                return !empty($value);
            }
        );
        return $result;
    }

    /**
     * Get XML node text values using specified xPath
     *
     * The node must contain specified attribute
     *
     * @param \SimpleXMLElement $xml
     * @param string $xPath
     * @return array
     */
    public static function getXmlNodeValues(\SimpleXMLElement $xml, $xPath)
    {
        $result = [];
        $nodes = $xml->xpath($xPath) ?: [];
        foreach ($nodes as $node) {
            $result[] = (string)$node;
        }
        return $result;
    }

    /**
     * Get XML node names using specified xPath
     *
     * @param \SimpleXMLElement $xml
     * @param string $xpath
     * @return array
     */
    public static function getXmlNodeNames(\SimpleXMLElement $xml, $xpath)
    {
        $result = [];
        $nodes = $xml->xpath($xpath) ?: [];
        foreach ($nodes as $node) {
            $result[] = $node->getName();
        }
        return $result;
    }

    /**
     * Get XML node attribute values using specified xPath
     *
     * @param \SimpleXMLElement $xml
     * @param string $xPath
     * @param string $attributeName
     * @return array
     */
    public static function getXmlAttributeValues(\SimpleXMLElement $xml, $xPath, $attributeName)
    {
        $result = [];
        $nodes = $xml->xpath($xPath) ?: [];
        foreach ($nodes as $node) {
            $node = (array)$node;
            if (isset($node['@attributes'][$attributeName])) {
                $result[] = $node['@attributes'][$attributeName];
            }
        }
        return $result;
    }

    /**
     * Extract class name from a conventional callback specification "Class::method"
     *
     * @param string $callbackName
     * @return string
     */
    public static function getCallbackClass($callbackName)
    {
        $class = explode('::', $callbackName);
        return $class[0];
    }

    /**
     * Find classes in a configuration XML-file (assumes any files under Namespace/Module/etc/*.xml)
     *
     * @param \SimpleXMLElement $xml
     * @return array
     */
    public static function collectClassesInConfig(\SimpleXMLElement $xml)
    {
        // @todo this method must be refactored after implementation of MAGETWO-7689 (valid configuration)
        $classes = self::getXmlNodeValues(
            $xml,
            '
            /config//resource_adapter | /config/*[not(name()="sections")]//class[not(ancestor::observers)]
                | //model[not(parent::connection)] | //backend_model | //source_model | //price_model
                | //model_token | //writer_model | //clone_model | //frontend_model | //working_model
                | //admin_renderer | //renderer | /config/*/di/preferences/*'
        );
        $classes = array_merge($classes, self::getXmlAttributeValues($xml, '//@backend_model', 'backend_model'));
        $classes = array_merge(
            $classes,
            self::getXmlNodeNames(
                $xml,
                '/logging/*/expected_models/* | /logging/*/actions/*/expected_models/* | /config/*/di/preferences/*'
            )
        );

        $classes = array_map([\Magento\Framework\App\Utility\Classes::class, 'getCallbackClass'], $classes);
        $classes = array_map('trim', $classes);
        $classes = array_unique($classes);
        $classes = array_filter(
            $classes,
            function ($value) {
                return !empty($value);
            }
        );

        return $classes;
    }

    /**
     * Find classes in a layout configuration XML-file
     *
     * @param \SimpleXMLElement $xml
     * @return array
     */
    public static function collectLayoutClasses(\SimpleXMLElement $xml)
    {
        $classes = self::getXmlAttributeValues($xml, '/layout//block[@class]', 'class');
        $classes = array_merge(
            $classes,
            self::getXmlNodeValues(
                $xml,
                '/layout//action/attributeType | /layout//action[@method="addTab"]/content
                | /layout//action[@method="addMergeSettingsBlockType"
                    or @method="addInformationRenderer"
                    or @method="addDatabaseBlock"]/*[2]
                | /layout//action[@method="setMassactionBlockName"]/name
                | /layout//action[@method="setEntityModelClass"]/code'
            )
        );
        return array_unique($classes);
    }

    /**
     * Scan application source code and find classes
     *
     * Sub-type pattern allows to distinguish "type" of a class within a module (for example, Block, Model)
     * Returns array(<class> => <module>)
     *
     * @param string $subTypePattern
     * @return array
     */
    public static function collectModuleClasses($subTypePattern = '[A-Za-z]+')
    {
        $componentRegistrar = new ComponentRegistrar();
        $result = [];
        foreach ($componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleName => $modulePath) {
            $pattern = '/^' . preg_quote($modulePath, '/') . '\/(' . $subTypePattern . '\/.+)\.php$/';
            foreach (Files::init()->getFiles([$modulePath], '*.php') as $file) {
                if (preg_match($pattern, $file)) {
                    $partialFileName = substr($file, strlen($modulePath) + 1);
                    $partialFileName = substr($partialFileName, 0, strlen($partialFileName) - strlen('.php'));
                    $partialClassName = str_replace('/', '\\', $partialFileName);
                    $className = str_replace('_', '\\', $moduleName) . '\\' . $partialClassName;
                    $result[$className] = $moduleName;
                }
            }
        }
        return $result;
    }

    /**
     * Fetch virtual class declarations from DI configs
     *
     * @return array
     */
    public static function getVirtualClasses()
    {
        if (!empty(self::$_virtualClasses)) {
            return self::$_virtualClasses;
        }
        $configFiles = Files::init()->getDiConfigs();
        foreach ($configFiles as $fileName) {
            $configDom = new \DOMDocument();
            $configDom->load($fileName);
            $xPath = new \DOMXPath($configDom);
            $vTypes = $xPath->query('/config/virtualType');
            /** @var \DOMNode $virtualType */
            foreach ($vTypes as $virtualType) {
                $name = $virtualType->attributes->getNamedItem('name')->textContent;
                if (!$virtualType->attributes->getNamedItem('type')) {
                    continue;
                }
                $type = $virtualType->attributes->getNamedItem('type')->textContent;
                self::$_virtualClasses[$name] = $type;
            }
        }

        return self::$_virtualClasses;
    }

    /**
     * Check if instance is virtual type
     *
     * @param string $className
     * @return bool
     */
    public static function isVirtual($className)
    {
        //init virtual classes if necessary
        self::getVirtualClasses();

        return array_key_exists($className, self::$_virtualClasses);
    }

    /**
     * Get real type name for virtual type
     *
     * @param string $className
     * @return string
     */
    public static function resolveVirtualType($className)
    {
        if (false == self::isVirtual($className)) {
            return $className;
        }

        $resolvedName = self::$_virtualClasses[$className];
        return self::resolveVirtualType($resolvedName);
    }

    /**
     * Check class is auto-generated
     *
     * @param string $className
     * @return bool
     */
    public static function isAutogenerated($className)
    {
        if (preg_match(
            '/.*\\\\[a-zA-Z0-9]{1,}(Factory|SearchResults|DataBuilder|Extension|ExtensionInterface)$/',
            $className
        )
            || preg_match('/Magento\\\\[\w]+\\\\(Test\\\\(Page|Fixture))\\\\/', $className)
            || preg_match('/.*\\\\[a-zA-Z0-9]{1,}\\\\Proxy$/', $className)
        ) {
            return true;
        }

        return false;
    }

    /**
     * Scan contents as PHP-code and find class name occurrences
     *
     * @param string $contents
     * @param array &$classes
     * @return array
     */
    public static function collectPhpCodeClasses($contents, &$classes = [])
    {
        self::getAllMatches(
            $contents,
            '/
            # ::getModel ::getSingleton ::getResourceModel ::getResourceSingleton
            \:\:get(?:Resource)?(?:Model | Singleton)\(\s*[\'"]([^\'"]+)[\'"]\s*[\),]

            # addBlock createBlock getBlockSingleton
            | (?:addBlock | createBlock | getBlockSingleton)\(\s*[\'"]([^\'"]+)[\'"]\s*[\),]

            # various methods, first argument
            | \->(?:initReport | setEntityModelClass
                | setAttributeModel | setBackendModel | setFrontendModel | setSourceModel | setModel
            )\(\s*[\'"]([^\'"]+)[\'"]\s*[\),]

            # various methods, second argument
            | \->add(?:ProductConfigurationHelper | OptionsRenderCfg)\(.+,\s*[\'"]([^\'"]+)[\'"]\s*[\),]

            # models in install or setup
            | [\'"](?:resource_model | attribute_model | entity_model | entity_attribute_collection
                | source | backend | frontend | input_renderer | frontend_input_renderer
            )[\'"]\s*=>\s*[\'"]([^\'"]+)[\'"]

            # misc
            | function\s_getCollectionClass\(\)\s+{\s+return\s+[\'"]([a-z\d_\/]+)[\'"]
            | (?:_parentResourceModelName | _checkoutType | _apiType)\s*=\s*\'([a-z\d_\/]+)\'
            | \'renderer\'\s*=>\s*\'([a-z\d_\/]+)\'
            | protected\s+\$_(?:form|info|backendForm|iframe)BlockType\s*=\s*[\'"]([^\'"]+)[\'"]

            /Uix',
            $classes
        );

        // check ->_init | parent::_init
        $skipForInit = implode(
            '|',
            [
                'id',
                '[\w\d_]+_id',
                'pk',
                'code',
                'status',
                'serial_number',
                'entity_pk_value',
                'currency_code',
                'unique_key'
            ]
        );
        self::getAllMatches(
            $contents,
            '/
            (?:parent\:\: | \->)_init\(\s*[\'"]([^\'"]+)[\'"]\s*\)
            | (?:parent\:\: | \->)_init\(\s*[\'"]([^\'"]+)[\'"]\s*,\s*[\'"]((?!(' .
            $skipForInit .
            '))[^\'"]+)[\'"]\s*\)
            /Uix',
            $classes
        );
        return $classes;
    }

    /**
     * Retrieve module name by class
     *
     * @param string $class
     * @return string
     */
    public static function getClassModuleName($class)
    {
        $parts = explode('\\', trim($class, '\\'));
        return $parts[0] . '_' . $parts[1];
    }
}