Its not clear in the docs but __HALT_COMPILER() is required in the stub.
(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL phar >= 1.0.0)
Phar::setStub — Usado para definir o carregador PHP ou o stub de inicialização de um arquivo Phar
Nota:
Este método requer que a configuração
phar.readonly
do php.ini seja definida como0
para funcionar com objetos Phar. Caso contrário, uma exceção PharException será lançada.
Este método é usado para adicionar um stub de carregador de inicialização PHP a um novo arquivo Phar, ou para substituir o stub de carregador em um arquivo Phar existente.
O stub do carregador para um arquivo Phar é usado sempre que um arquivo é incluído diretamente como neste exemplo:
<?php
include 'meuphar.phar';
?>
O carregador não é acessado ao incluir um arquivo por meio do empacotador de fluxo phar
como a seguir:
<?php
include 'phar://meuphar.phar/algumarquivo.php';
?>
stub
Uma string ou um identificador de fluxo aberto para usar como stub executável para este arquivo phar.
length
UnexpectedValueException é lançada se phar.readonly estiver habilitado no php.ini. PharException é lançada se algum problema for encontrado ao liberar alterações no disco.
Versão | Descrição |
---|---|
8.3.0 |
Chamar Phar::setStub() com um
resource e um length
foi descontinuado. Tais chamadas devem ser substituídas por:
$phar->setStub(stream_get_contents($resource));
|
Exemplo #1 Um exemplo de Phar::setStub()
<?php
try {
$p = new Phar(dirname(__FILE__) . '/novophar.phar', 0, 'novophar.phar');
$p['a.php'] = '<?php var_dump("Olá");';
$p->setStub('<?php var_dump("Primeiro"); Phar::mapPhar("novophar.phar"); __HALT_COMPILER(); ?>');
include 'phar://novophar.phar/a.php';
var_dump($p->getStub());
$p['b.php'] = '<?php var_dump("Mundo");';
$p->setStub('<?php var_dump("Segundo"); Phar::mapPhar("novophar.phar"); __HALT_COMPILER(); ?>');
include 'phar://novophar.phar/b.php';
var_dump($p->getStub());
} catch (Exception $e) {
echo 'As operações de gravação falharam em novophar.phar: ', $e;
}
?>
O exemplo acima produzirá:
string(5) "Olá" string(82) "<?php var_dump("Primeiro"); Phar::mapPhar("novophar.phar"); __HALT_COMPILER(); ?>" string(5) "Mundo" string(83) "<?php var_dump("Segundo"); Phar::mapPhar("novophar.phar"); __HALT_COMPILER(); ?>"
One thing I had alot of problems with, is that i can't set the stub unless I put the whole operation inside of a try/catch block!
If i remove the try/catch block it will error our and not write the stub with the content i want it to have.
If your stub has a namespace, it is used for each include that doesn't define one.