Created
January 20, 2015 16:39
-
-
Save juliovedovatto/559eb1d76c4ea8fb81d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Classe que serve de ponte de integração entre classes do sistema. | |
* Utilização: | |
* SistemaBridge::Agente()->metodo($param); | |
* SistemaBridge::Contrato()->metodo($param); | |
*/ | |
class SistemaBridge { | |
public function __call($name, $arguments) { | |
return self::_invokeClass($name, $arguments); | |
} | |
public static function __callStatic($name, $arguments) { | |
return self::_invokeClass($name, $arguments); | |
} | |
/** | |
* Método para invocar a Classe do sistema antigo, sem bagunçar a estrutura da nova parte do Sistema. | |
* @param string $class | |
* @return Object Instância da Classe | |
*/ | |
private function _invokeClass($class, $arguments = array()) { | |
if (!is_file($class_file = path_join(Sistema_CLASS_DIR, $class . '.class.php'))) | |
return false; | |
spl_autoload_unregister(array(__CLASS__, '_autoload')); | |
// Registrar autoloader customizado | |
spl_autoload_register(array(__CLASS__, '_autoload'), false, true); | |
$class = 'Sistema_' . $class; | |
if (method_exists($class, 'getInstance')) //verificar se existe método de obter uma instância da classe | |
return $class::getInstance(); | |
return new $class; | |
} | |
private static function _autoload($class) { | |
// PASSO 01: Verificar se é prefixo Sistema_ ou se classe é do sistema (verificando se arquvo existe) | |
if (!strstr($class, 'Sistema_') || !is_file($class_file = path_join(Sistema_CLASS_DIR, str_replace('Sistema_', '', $class) . '.class.php'))) | |
return false; | |
// PASSO 02: Pegar arquivo da classe e adicionar prefixo nela | |
$class_contents = preg_replace('#class\s{1,}([^\s]+)#', 'class Sistema_$1', file_get_contents($class_file)); | |
// PASSO 03: procurar e substituir qualquer instance de classe dentro da classe para adicionar prefixo | |
$class_whitelist = array('Conexao', 'Funcoes', 'parent', 'self', 'mPDF'); | |
$class_contents = preg_replace_callback('/(?<classe>(\w+::)|\bnew\b (\w+))/sim', function($matches) use ($class_whitelist, $class) { | |
$c = trim(str_replace(array('new ', '::'), '', $matches['classe'])); | |
if (in_array(strtolower($c), array_map('strtolower', $class_whitelist))) | |
return $matches['classe']; | |
return str_replace($c, 'Sistema_' . $c, $matches['classe']); | |
}, $class_contents); | |
// PASSO 04: criar arquivo temporário e salvar conteúdo alterado enal | |
$tmp_class_file = array_search('uri', @array_flip(stream_get_meta_data($GLOBALS[mt_rand()] = tmpfile()))); | |
if (!file_put_contents($tmp_class_file, $class_contents)) | |
return false; | |
// PASSO 05: Verificar se a classe herda propriedades da classe Sistema | |
if (strstr($class_contents, 'extends Sistema')) //verificar se precisa incluir classe Sistema | |
require_once path_join(Sistema_CLASS_DIR, 'Sistema.class.php'); | |
// PASSO 06: Registrar função de limpeza do arquivo temporário ao terminar execução | |
register_shutdown_function(function($tmp) { return @unlink($tmp); }, $tmp_class_file); | |
// FIM: fazer require do arquivo alterado | |
require_once $tmp_class_file; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment