©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
[#1] t dot habenreich at web dot de [2013-12-16 20:38:54]
Here is a very simple class to build a phar file from a given source directory. You can use this for your own project and simple deployment.
But my main goal was to show how to use PHAR functions in a simple way.
<?php
class BuildPhar
{
private $_sourceDirectory = null;
private $_stubFile = null;
private $_outputDirectory = null;
private $_pharFileName = null;
public function __construct($_sourceDirectory, $stubFile, $_outputDirectory = null, $pharFileName = 'myPhar.phar') {
if ((file_exists($_sourceDirectory) === false) || (is_dir($_sourceDirectory) === false)) {
throw new Exception('No valid source directory given.');
}
$this->_sourceDirectory = $_sourceDirectory;
if (file_exists($this->_sourceDirectory.'/'.$stubFile) === false) {
throw new Exception('Your given stub file doesn\'t exists.');
}
$this->_stubFile = $stubFile;
if(empty($pharFileName) === true) {
throw new Exception('Your given output name for your phar-file is empty.');
}
$this->_pharFileName = $pharFileName;
if ((empty($_outputDirectory) === true) || (file_exists($_outputDirectory) === false) || (is_dir($_outputDirectory) === false)) {
if ($_outputDirectory !== null) {
trigger_error ( 'Your output directory is invalid. We set the fallback to: "'.dirname(__FILE__).'".', E_USER_WARNING);
}
$this->_outputDirectory = dirname(__FILE__);
} else {
$this->_outputDirectory = $_outputDirectory;
}
$this->prepareBuildDirectory();
$this->buildPhar();
}
private function prepareBuildDirectory() {
if (preg_match('/.phar$/', $this->_pharFileName) == FALSE) {
$this->_pharFileName .= '.phar';
}
if (file_exists($this->_pharFileName) === true) {
unlink($this->_pharFileName);
}
}
private function buildPhar() {
$phar = new Phar($this->_outputDirectory.'/'.$this->_pharFileName);
$phar->buildFromDirectory($this->_sourceDirectory);
$phar->setDefaultStub($this->_stubFile);
}
}
//END Class
//Example Usage:
$builder = new BuildPhar(
dirname(__FILE__).'/_source',
'my_default_stub.php',
dirname(__FILE__).'/_output',
'my-phar-file.phar'
);
[#2] dava [2013-06-03 09:18:23]
Here is an apache2 htaccess example that prevents the downloading of phar-Archives by the user:
RewriteEngine on
RewriteRule ^(.*)\.phar$ - [F]
It triggers a "403 - Forbidden" message instead of delivering the archive.
[#3] v-mafick at microsoft dot com [2013-05-24 21:22:49]
Users should set the `sys_temp_dir` directive.
PHAR stores temporary files in either `sys_temp_dir` or the current working directory(CWD).
This is especially important if you're CWD is on a remote file system. Often web server clusters will share a common file system between each web server (using NFS, DFS, etc...). In such scenarios, if you don't set `sys_temp_dir` to a local directory, PHAR will be creating temporary files over the network, which will result in performance and functionality problems.
[#4] bohwaz [2011-11-21 02:43:46]
If you get blank pages when trying to access a phar web-page, then you probably have Suhosin on your PHP (like in Debian and Ubuntu), and you need to ad this to your php.ini to allow execution of PHAR archives :
suhosin.executor.include.whitelist="phar"