本文整理汇总了PHP中Phar::addFromString方法的典型用法代码示例。如果您正苦于以下问题:PHP Phar::addFromString方法的具体用法?PHP Phar::addFromString怎么用?PHP Phar::addFromString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phar
的用法示例。
在下文中一共展示了Phar::addFromString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testItExtractsIconFromPhar
public function testItExtractsIconFromPhar()
{
$key = uniqid();
$iconContent = $key;
$rootPackage = dirname(dirname(__FILE__));
$iconRelativePath = 'Resources/notification/icon-' . $key . '.png';
$testDir = sys_get_temp_dir() . '/test-jolinotif';
$pharPath = $testDir . '/notification-extract-icon-' . $key . '.phar';
$extractedIconPath = sys_get_temp_dir() . '/jolinotif/' . $iconRelativePath;
if (!is_dir($testDir)) {
mkdir($testDir);
}
$bootstrap = <<<'PHAR_BOOTSTRAP'
<?php
require __DIR__.'/vendor/autoload.php';
$iconPath = THE_ICON;
$notification = new \Joli\JoliNotif\Notification();
$notification->setBody('My notification');
$notification->setIcon(__DIR__.$iconPath);
PHAR_BOOTSTRAP;
$phar = new \Phar($pharPath);
$phar->buildFromDirectory($rootPackage, '#(src|tests/fixtures|vendor/composer)#');
$phar->addFromString('bootstrap.php', str_replace('THE_ICON', '\'/' . $iconRelativePath . '\'', $bootstrap));
$phar->addFromString($iconRelativePath, $iconContent);
$phar->addFile('vendor/autoload.php');
$phar->setStub($phar->createDefaultStub('bootstrap.php'));
$this->assertTrue(is_file($pharPath));
exec('php ' . $pharPath);
$this->assertTrue(is_file($extractedIconPath));
$this->assertSame($iconContent, file_get_contents($extractedIconPath));
}
示例2: compile
public function compile($pharFile, $name = 'foo-app', array $files = array(), array $directories = array(), $stub_file, $base_path, $name_pattern = '*.php', $strip = TRUE)
{
if (file_exists($pharFile)) {
unlink($pharFile);
}
$phar = new \Phar($pharFile, 0, $name);
// The signature is automatically set unless we decide to compress. In that
// case we have to manually set it. Setting to the default just in case.
$phar->setSignatureAlgorithm(\Phar::SHA1);
$phar->startBuffering();
// CLI Component files
$iterator = Finder::create()->files()->name($name_pattern)->in($directories);
// We need to set the adapter to use the PhpAdapter because we are working
// with Phar files and the higher priority ones by default in symfony can
// run into issues.
//$iterator->removeAdapters();
//$iterator->addAdapter(new PhpAdapter());
$files = array_merge($files, iterator_to_array($iterator));
foreach ($files as $file) {
$path = str_replace($base_path . '/', '', $file);
if ($strip) {
$phar->addFromString($path, php_strip_whitespace($file));
} else {
$phar->addFromString($path, file_get_contents($file));
}
}
$phar->setStub(file_get_contents($stub_file));
$phar->stopBuffering();
// Not all systems support compressed Phars. For now disabling.
// $phar->compressFiles(\Phar::GZ);
chmod($pharFile, 0755);
unset($phar);
}
示例3: addFile
/**
* Add a file in phar removing whitespaces from the file
*
* @param Phar $phar
* @param string $sFile
* @param null $baseDir
*/
function addFile($phar, $sFile, $baseDir = null)
{
if (null !== $baseDir) {
$phar->addFromString(substr($sFile, strlen($baseDir) + 1), php_strip_whitespace($sFile));
} else {
$phar->addFromString($sFile, php_strip_whitespace($sFile));
}
}
示例4: addFile
/**
* @param \Phar $phar
* @param SplFileInfo $file
*/
private function addFile(\Phar $phar, SplFileInfo $file)
{
$path = $file->getRelativePathname();
$content = file_get_contents($file);
$content = preg_replace('{^#!/usr/bin/env php\\s*}', '', $content);
$phar->addFromString($path, $content);
}
示例5: compile
public function compile()
{
if (file_exists('symfony.phar')) {
unlink('symfony.phar');
}
$phar = new \Phar('symfony.phar', 0, 'Symfony');
$phar->setSignatureAlgorithm(\Phar::SHA1);
$phar->startBuffering();
// Files
foreach ($this->getFiles() as $file) {
$path = str_replace(__DIR__ . '/', '', $file);
if (false !== strpos($file, '.php')) {
$content = Kernel::stripComments(file_get_contents($file));
} else {
$content = file_get_contents($file);
}
$phar->addFromString($path, $content);
}
// Stubs
$phar['_cli_stub.php'] = $this->getCliStub();
$phar['_web_stub.php'] = $this->getWebStub();
$phar->setDefaultStub('_cli_stub.php', '_web_stub.php');
$phar->stopBuffering();
//$phar->compressFiles(\Phar::GZ);
unset($phar);
}
示例6: run
public function run()
{
$this->printTaskInfo('Creating {filename}', ['filename' => $this->filename]);
$this->phar->setSignatureAlgorithm(\Phar::SHA1);
$this->phar->startBuffering();
$this->printTaskInfo('Packing {file-count} files into phar', ['file-count' => count($this->files)]);
$this->startProgressIndicator();
foreach ($this->files as $path => $content) {
$this->phar->addFromString($path, $content);
$this->advanceProgressIndicator();
}
$this->phar->stopBuffering();
$this->advanceProgressIndicator();
if ($this->compress and in_array('GZ', \Phar::getSupportedCompression())) {
if (count($this->files) > 1000) {
$this->printTaskInfo('Too many files. Compression DISABLED');
} else {
$this->printTaskInfo('{filename} compressed', ['filename' => $this->filename]);
$this->phar = $this->phar->compressFiles(\Phar::GZ);
}
}
$this->advanceProgressIndicator();
$this->stopProgressIndicator();
$this->printTaskSuccess('{filename} produced', ['filename' => $this->filename]);
return Result::success($this, '', ['time' => $this->getExecutionTime()]);
}
示例7: run
public function run()
{
$this->printTaskInfo("Creating <info>{$this->filename}</info>");
$this->phar->setSignatureAlgorithm(\Phar::SHA1);
$this->phar->startBuffering();
$this->printTaskInfo('Packing ' . count($this->files) . ' files into phar');
$progress = new ProgressBar($this->getOutput());
$progress->start(count($this->files));
$this->startTimer();
foreach ($this->files as $path => $content) {
$this->phar->addFromString($path, $content);
$progress->advance();
}
$this->phar->stopBuffering();
$progress->finish();
$this->getOutput()->writeln('');
if ($this->compress and in_array('GZ', \Phar::getSupportedCompression())) {
if (count($this->files) > 1000) {
$this->printTaskInfo("Too many files. Compression DISABLED");
} else {
$this->printTaskInfo($this->filename . " compressed");
$this->phar = $this->phar->compressFiles(\Phar::GZ);
}
}
$this->stopTimer();
$this->printTaskSuccess("<info>{$this->filename}</info> produced");
return Result::success($this, '', ['time' => $this->getExecutionTime()]);
}
示例8: addFile
private function addFile(\Phar $phar, \SplFileInfo $file)
{
$path = str_replace(dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR, '', $file->getRealPath());
$content = file_get_contents($file);
$content = $this->stripWhitespace($content);
$phar->addFromString($path, $content);
}
示例9: addFile
protected function addFile(\Phar $phar, \splFileInfo $file, $strip = true)
{
$path = str_replace(dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR, '', $file->getRealPath());
$content = file_get_contents($file);
if ($strip) {
$content = self::stripWhitespace($content);
}
$phar->addFromString($path, $content);
}
示例10: addFile
/**
* Add a file into the Phar binary
* @param \SplFileInfo $file The file to be added
* @since 1.0.0
*/
protected function addFile(\SplFileInfo $file)
{
$path = str_replace($this->root, '', $file->getRealPath());
$content = file_get_contents($file);
$content = preg_replace('{^#!/usr/bin/env php\\s*}', '', $content);
if ($this->processor instanceof ProcessorInterface) {
$content = $this->processor->process($content);
}
$this->phar->addFromString($path, $content);
}
示例11: addFile
protected function addFile(\Phar $phar, \SplFileInfo $file, $strip = true)
{
$path = str_replace(dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR, '', $file->getRealPath());
$content = file_get_contents($file);
if ($strip) {
$content = self::stripWhitespace($content);
}
$content = preg_replace("/const VERSION = '.*?';/", "const VERSION = '" . $this->version . "';", $content);
$phar->addFromString($path, $content);
}
示例12: addFile
protected function addFile(File $file, $path, $stripBin = false)
{
$this->output->writeln(' <info>*</info> Add php file ' . $path);
$content = $file->getContents();
if ($stripBin) {
$content = preg_replace('~^#!/usr/bin/env php\\s*~', '', $content);
$content = str_replace('BundlerPackCommand', 'UpdateCommand', $content);
}
$this->phar->addFromString($path, $content);
}
示例13: addFile
/**
* Add a file to the psysh Phar.
*
* @param Phar $phar
* @param SplFileInfo $file
* @param bool $strip (default: true)
*/
private function addFile($phar, $file, $strip = true)
{
$path = str_replace(dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR, '', $file->getRealPath());
$content = file_get_contents($file);
if ($strip) {
$content = $this->stripWhitespace($content);
} elseif ('LICENSE' === basename($file)) {
$content = "\n" . $content . "\n";
}
$phar->addFromString($path, $content);
}
示例14: execute
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Building Phar archive');
$file = 'build/zip.phar.php';
if (file_exists($file)) {
$output->writeln('Removing previous phar archive');
unlink($file);
}
$output->writeln('Buffering contents ...');
$phar = new \Phar($file, 0, 'zip.phar');
$phar->setSignatureAlgorithm(\Phar::SHA1);
$phar->startBuffering();
$finder = new Finder();
$finder->in(array('app', 'src', 'vendor'))->files(array('*.php', '*.js'));
$count = $finder->count();
$output->writeln(sprintf('Found %d entries', $count));
$step = $count / 100;
$index = 0;
$indexStep = 0;
/** @var ProgressHelper $progress */
$progress = $this->getHelperSet()->get('progress');
$progress->setFormat(' [%bar%] %percent%%');
$progress->start($output, 100);
/** @var SplFileInfo $file */
$basePath = realpath(__DIR__ . '/../../../../../');
foreach ($finder as $file) {
$path = str_replace($basePath, '', $file->getRealPath());
$path = ltrim($path, DIRECTORY_SEPARATOR);
$path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
$phar->addFromString($path, $file->getContents());
if ($index++ % $step == 0 && $indexStep < 100) {
$progress->advance();
$indexStep++;
}
}
$phar->addFromString('index.php', file_get_contents($basePath . DIRECTORY_SEPARATOR . 'index.php'));
$phar->setStub('<?php Phar::mapPhar("zip.phar"); define("PHAR_RUNNING", true); /*Phar::interceptFileFuncs();*/ require "phar://zip.phar/index.php"; __HALT_COMPILER();');
$phar->stopBuffering();
$progress->finish();
$output->writeln('Build complete. Phar archive has been deployed to build/zip.phar.php');
}
示例15: build
public function build()
{
$file = getTmpFile("phar");
$phar = new \Phar($file);
$phar->setStub("<?php __HALT_COMPILER(); ?>");
$phar->setSignatureAlgorithm(\Phar::SHA1);
$phar->startBuffering();
$namespace = "_{$this->name}" . randomClass(8, "\\_");
$class = randomClass(8);
$main = "{$namespace}\\{$class}";
$manifest = ["name" => $this->name, "version" => $this->version, "authors" => $this->authors, "api" => "1.9.0", "main" => $main, "commands" => []];
foreach ($this->cmds as $cmd) {
$manifest["commands"][$cmd->name] = ["description" => $cmd->desc, "usage" => $cmd->usage];
}
$phar->addFromString("plugin.yml", yaml_emit($manifest));
$mainClass = <<<EOS
<?php
namespace {$namespace};
use pocketmine\\command as cmd;
use pocketmine\\event as evt;
class {$class} extends \\pocketmine\\plugin\\PluginBase implements evt\\Listener{
public function onCommand(cmd\\CommandSender \$sender, cmd\\Command\\ \$cmd, \$lbl, array \$args){
switch(\$cmd->getName()){
EOS;
foreach ($this->cmds as $cmd) {
$mainClass .= "case " . var_export($cmd->name, true) . ":" . PHP_EOL;
$mainClass .= "return \$this->onCommand_{$cmd->name}(\$args, \$sender);" . PHP_EOL;
}
$mainClass .= "}" . PHP_EOL . "return false;" . PHP_EOL . "}" . PHP_EOL;
foreach ($this->cmds as $cmd) {
$mainClass .= $cmd->executor->toPhp();
}
$mainClass .= "}";
$phar->addFromString("src/" . str_replace("\\", "/", $main) . ".php", $mainClass);
// $phar->compressFiles(\Phar::GZ);
$phar->stopBuffering();
$path = $phar->getPath();
header("Content-Type: application/octet-stream");
echo file_get_contents($path);
// unlink($path);
}