note 113912 added to book.phar

From: Date: Mon, 16 Dec 2013 20:38:57 +0000
Subject: note 113912 added to book.phar
Groups: php.notes 
Request: Send a blank email to php-notes+get-197954@lists.php.net to get a copy of this message
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; /** * @param $_sourceDirectory // This is the directory where your project is stored. * @param $stubFile // Name the entry point for your phar file. This file have to be within the source * directory. * @param null $_outputDirectory // Directory where the phar file will be placed. * @param string $pharFileName // Name of your final *.phar file. */ 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' ); ---- Server IP: 72.52.91.14 Probable Submitter: 72.52.91.14 ---- Manual Page -- http://php.net/manual/en/book.phar.php Edit -- https://master.php.net/note/edit/113912 Del: integrated -- https://master.php.net/note/delete/113912/integrated Del: useless -- https://master.php.net/note/delete/113912/useless Del: bad code -- https://master.php.net/note/delete/113912/bad+code Del: spam -- https://master.php.net/note/delete/113912/spam Del: non-english -- https://master.php.net/note/delete/113912/non-english Del: in docs -- https://master.php.net/note/delete/113912/in+docs Del: other reasons-- https://master.php.net/note/delete/113912 Reject -- https://master.php.net/note/reject/113912 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#197954) next »