Daniel Siepmann
8db7a8bb1f
* One files is provided by us for TYPO3 mappings. * Second one keeps as is and contains the generated mappings. Relates: #43
182 lines
5.3 KiB
PHP
182 lines
5.3 KiB
PHP
<?php
|
|
namespace Typo3Update\Feature;
|
|
|
|
/*
|
|
* Copyright (C) 2017 Daniel Siepmann <coding@daniel-siepmann.de>
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
* 02110-1301, USA.
|
|
*/
|
|
|
|
use Typo3Update\Options;
|
|
|
|
/**
|
|
* Singleton wrapper for mappings.
|
|
*
|
|
* Will check the configured file for whether a class is legacy and provides further methods.
|
|
* Also can update to add new migrated class names.
|
|
*/
|
|
final class LegacyClassnameMapping
|
|
{
|
|
// Singleton implementation - Start
|
|
static protected $instance = null;
|
|
/**
|
|
* Get the singleton instance.
|
|
*
|
|
* @return Mapping
|
|
*/
|
|
public static function getInstance()
|
|
{
|
|
if (static::$instance === null) {
|
|
static::$instance = new LegacyClassnameMapping();
|
|
}
|
|
|
|
return static::$instance;
|
|
}
|
|
private function __clone()
|
|
{
|
|
}
|
|
/**
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) We just want to implement singleton pattern.
|
|
*/
|
|
private function __wakeup()
|
|
{
|
|
}
|
|
private function __construct()
|
|
{
|
|
if (is_file(Options::getMappingFile())) {
|
|
$this->mappings = require Options::getMappingFile();
|
|
$this->buildKeyArray($this->mappings, $this->mappingsKeys);
|
|
}
|
|
|
|
$this->typo3Mappings = require implode(DIRECTORY_SEPARATOR, [
|
|
__DIR__, '..', 'Configuration', '',
|
|
]) . 'LegacyClassnames.php';
|
|
$this->buildKeyArray($this->typo3Mappings, $this->typo3MappingsKeys);
|
|
}
|
|
// Singleton implementation - End
|
|
|
|
/**
|
|
* Contains mappings as defined by composer for alias mapping.
|
|
* @var array
|
|
*/
|
|
protected $typo3Mappings = [];
|
|
protected $typo3MappingsKeys = [];
|
|
|
|
protected $mappings = [];
|
|
protected $mappingsKeys = [];
|
|
|
|
protected function buildKeyArray($originalArray, &$targetVariable)
|
|
{
|
|
foreach (array_keys($originalArray) as $key) {
|
|
$targetVariable[strtolower($key)] = $key;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks whether a mapping exists for the given $classname,
|
|
* indicating it's legacy.
|
|
*
|
|
* @param string $classname
|
|
* @return bool
|
|
*/
|
|
public function isLegacyClassname($classname, $insensitive = true)
|
|
{
|
|
$lowerVersion = $classname;
|
|
if ($insensitive) {
|
|
$classname = strtolower($classname);
|
|
}
|
|
|
|
return $this->isLegacyTypo3Classname($classname) || $this->isLegacyMappingClassname($classname)
|
|
|| $this->isLegacyTypo3Classname($lowerVersion) || $this->isLegacyMappingClassname($lowerVersion);
|
|
}
|
|
|
|
/**
|
|
* @param string $classname
|
|
* @return string
|
|
*/
|
|
public function getNewClassname($classname)
|
|
{
|
|
$lowerVersion = strtolower($classname);
|
|
if ($this->isLegacyTypo3Classname($classname) || $this->isLegacyTypo3Classname(strtolower($classname))) {
|
|
return $this->typo3Mappings[$this->getTypo3MappingKey($classname)];
|
|
}
|
|
|
|
return $this>mappings[$this->getLegacyMappingKey($classname)];
|
|
}
|
|
|
|
protected function getTypo3MappingKey($classname)
|
|
{
|
|
$lowerVersion = strtolower($classname);
|
|
if (isset($this->typo3MappingsKeys[$lowerVersion])) {
|
|
return $this->typo3MappingsKeys[$lowerVersion];
|
|
}
|
|
|
|
return $classname;
|
|
}
|
|
|
|
protected function getLegacyMappingKey($classname)
|
|
{
|
|
$lowerVersion = strtolower($classname);
|
|
if (isset($this->mappingsKeys[$lowerVersion])) {
|
|
return $this->mappingsKeys[$lowerVersion];
|
|
}
|
|
|
|
return $classname;
|
|
}
|
|
|
|
protected function isLegacyTypo3Classname($classname)
|
|
{
|
|
return isset($this->typo3MappingsKeys[$classname]);
|
|
}
|
|
|
|
protected function isLegacyMappingClassname($classname)
|
|
{
|
|
return isset($this->mappingsKeys[$classname]);
|
|
}
|
|
|
|
/**
|
|
* Use to add new mappings found during parsing.
|
|
* E.g. in MissingNamespaceSniff old class definitions are fixed and a new mapping exists afterwards.
|
|
*
|
|
* @param string $legacyClassname
|
|
* @param string $newClassname
|
|
*
|
|
* @return void
|
|
*/
|
|
public function addLegacyClassname($legacyClassname, $newClassname)
|
|
{
|
|
$this->mappings[$legacyClassname] = $newClassname;
|
|
}
|
|
|
|
/**
|
|
* Used to persist new mappings.
|
|
*/
|
|
public function __destruct()
|
|
{
|
|
// For some reasons desctruct is called multiple times, while construct
|
|
// is called once. Until we know the issue and fix it, this is our
|
|
// workaround to not break the file and do stuff in an unkown instance.
|
|
if ($this !== static::$instance) {
|
|
return;
|
|
}
|
|
|
|
file_put_contents(
|
|
Options::getMappingFile(),
|
|
'<?php' . PHP_EOL . 'return ' . var_export($this->mappings, true) . ';'
|
|
);
|
|
}
|
|
}
|