function verify_pheanstalk_phar() { $phar = new Phar(PHAR_FULLPATH); printf("- %s built with %d files.\n", PHAR_FILENAME, $phar->count()); }
/** * Creates a PHAR (PHP Archive) file from an entire directory. * * @param string $dir The directory we want to create a PHAR file for. * * @param string $to The new PHAR file location; with a `.phar` extension. * * @param string $stub_file Stub file path. The contents of this stub file will be used as * the stub for the resulting PHAR file. Required for all PHARs created by this routine. * A final call to `__HALT_COMPILER();` is automatically appended by this routine. * * @param boolean $strip_ws Optional. Defaults to a TRUE value (highly recommended). * If this is TRUE; any PHP files in the archive will be further reduced in filesize by this routine. * This is made possible by the `php_strip_whitespace()` function. * * @param boolean $compress Optional. Defaults to a TRUE value (highly recommended). * If this is TRUE; any compressable files in the archive will be further reduced in filesize. * * @param string|array $compressable_extensions Optional. An array of GZIP-compressable extensions. * This will default to only those which are 100% webPhar-compatible: `array('php', 'phps')`. * Or, you can provide your own array of compressable extensions. * * @param string $is_phar_var_suffix Optional. Defaults to `stub`. * A global variable at the top of your PHAR stub file will be declared as follows. * `$GLOBALS['is_phar_'.$is_phar_var_suffix] = 'phar://'.__FILE__;` (just a helpful identifier). * * @return string The new PHAR file path; else an exception is thrown on any type of failure. * * @throws exception If invalid types are passed through arguments list. * @throws exception If `$dir` is empty, does NOT exist; or is NOT readable for any reason. * @throws exception If `$to` is empty, or is NOT specified with a `.phar` extension. * @throws exception If `$to` parent directory does NOT exist; or is not writable. * @throws exception If `$stub_file` is empty, does NOT exist; or is NOT readable for any reason. * @throws exception If `$compressable_extensions` or `$is_phar_var_suffix` is empty. * @throws \exception If any Phar class failures occur. The Phar class may throw exceptions. * @throws exception On any type of failure (e.g. NOT successful). * * @WARNING This routine can become resource intensive on large directories. * Mostly because of the compression routines applied here intuitively. It takes some time. * See: {@link env::maximize_time_memory_limits()} */ public function phar_to($dir, $to, $stub_file, $strip_ws = TRUE, $compress = TRUE, $compressable_extensions = array('php', 'phps'), $is_phar_var_suffix = 'stub') { $this->check_arg_types('string:!empty', 'string:!empty', 'string:!empty', 'boolean', 'boolean', 'array:!empty', 'string:!empty', func_get_args()); $dir = $this->n_seps($dir); $to = $this->n_seps($to); $to_dir = $this->n_seps_up($to); if (!is_dir($dir)) { throw $this->©exception($this->method(__FUNCTION__) . '#source_dir_missing', get_defined_vars(), $this->__('Unable to PHAR a directory (source `dir` missing).') . sprintf($this->__('Non-existent source directory: `%1$s`.'), $dir)); } if (!is_readable($dir)) { throw $this->©exception($this->method(__FUNCTION__) . '#read_write_issues', get_defined_vars(), $this->__('Unable to PHAR a directory; not readable, due to permission issues.') . ' ' . sprintf($this->__('Need this directory to be readable please: `%1$s`.'), $dir)); } if (file_exists($to)) { throw $this->©exception($this->method(__FUNCTION__) . '#existing_phar', get_defined_vars(), $this->__('Unable to PHAR a directory; destination PHAR file already exists.') . ' ' . sprintf($this->__('Please delete this file first: `%1$s`.'), $to)); } if ($this->extension($to) !== 'phar') { throw $this->©exception($this->method(__FUNCTION__) . '#invalid_phar_file', get_defined_vars(), $this->__('Unable to PHAR a directory; invalid destination PHAR file.') . ' ' . sprintf($this->__('Please use a `.phar` extension instead of: `%1$s`.'), $to)); } if (!is_dir($to_dir)) { throw $this->©exception($this->method(__FUNCTION__) . '#phar_to_dir_missing', get_defined_vars(), $this->__('Destination PHAR directory does NOT exist yet.') . ' ' . sprintf($this->__('Please check this directory: `%1$s`.'), $to_dir)); } if (!is_writable($to_dir)) { throw $this->©exception($this->method(__FUNCTION__) . '#read_write_issues', get_defined_vars(), $this->__('Unable to PHAR a directory; destination not writable due to permission issues.') . ' ' . sprintf($this->__('Need this directory to be writable please: `%1$s`.'), $to_dir)); } if (!\Phar::canWrite()) { throw $this->©exception($this->method(__FUNCTION__) . '#read_write_issues', get_defined_vars(), $this->__('Unable to PHAR a directory; PHP configuration does NOT allow write access.') . ' ' . $this->__('Need this INI setting please: `phar.readonly = 0`.')); } if (!is_file($stub_file)) { throw $this->©exception($this->method(__FUNCTION__) . '#missing_stub_file', get_defined_vars(), $this->__('Unable to PHAR a directory; missing stub file.') . ' ' . sprintf($this->__('File does NOT exist: `%1$s`.'), $stub_file)); } if (!is_readable($stub_file)) { throw $this->©exception($this->method(__FUNCTION__) . '#stub_file_issues', get_defined_vars(), $this->__('Unable to PHAR a directory; permission issues with stub file.') . ' ' . sprintf($this->__('Need this file to be writable please: `%1$s`.'), $stub_file)); } // Phar class throws exceptions on failure. $_stub_file_is_phar_var = '$GLOBALS[\'is_phar_' . $this->©string->esc_sq($is_phar_var_suffix) . '\'] = \'phar://\'.__FILE__;'; $_stub_file_contents = $strip_ws ? $this->©php->strip_whitespace($stub_file) : file_get_contents($stub_file); $_stub_file_contents = trim(preg_replace('/\\W\\?\\>\\s*$/', '', $_stub_file_contents, 1)); // No close tag & trim. $_stub_file_contents = preg_replace('/\\<\\?php|\\<\\?/i', '<?php ' . $_stub_file_is_phar_var, $_stub_file_contents, 1); $_stub_file_contents = preg_replace('/\\W__HALT_COMPILER\\s*\\(\\s*\\)\\s*;/i', '', $_stub_file_contents, 1) . ' __HALT_COMPILER();'; $_phar = new \Phar($to, $this->iteration_flags()); $_phar->startBuffering(); // Don't create file yet (wait until we're done here). $_phar->setStub($_stub_file_contents); // Defines the stub for this PHAR file. if (!$strip_ws && !$compress) { $_phar->buildFromDirectory($dir); } else { $_strippable_extensions = array('php'); $_regex_compressable_extensions = $this->©string->preg_quote_deep($compressable_extensions, '/'); $_regex_compressable_extensions = '/\\.(?:' . implode('|', $_regex_compressable_extensions) . ')$/i'; $_temp_dir = $this->temp() . '/' . $this->©string->unique_id() . '-' . basename($dir); $this->copy_to($dir, $_temp_dir); $_temp_dir_iteration = $this->iteration($_temp_dir); if ($strip_ws) { foreach ($_temp_dir_iteration as $_dir_file) { if (!$_dir_file->isFile()) { continue; } $_path = $_dir_file->getPathname(); $_phar_path = $_dir_file->getSubPathName(); $_extension = $this->©file->extension($_path); if (in_array($_extension, $_strippable_extensions, TRUE)) { file_put_contents($_path, $this->©php->strip_whitespace($_path, TRUE)); } } } $_phar->buildFromDirectory($_temp_dir, $_regex_compressable_extensions); if ($compress && $_phar->count()) { // Compressing files? $_phar->compressFiles(\Phar::GZ); } foreach ($_temp_dir_iteration as $_dir_file) { if (!$_dir_file->isFile()) { continue; } $_path = $_dir_file->getPathname(); $_phar_path = $_dir_file->getSubPathName(); $_extension = $this->©file->extension($_path); if (!in_array($_extension, $compressable_extensions, TRUE)) { $_phar->addFile($_path, $_phar_path); } } } $_phar->stopBuffering(); // Write to disk now. unset($_phar, $_stub_file_is_phar_var, $_stub_file_contents, $_strippable_extensions, $_regex_compressable_extensions); unset($_temp_dir_iteration, $_dir_file, $_path, $_phar_path, $_extension); if (isset($_temp_dir)) { // A little more housekeeping now. $this->delete($_temp_dir); } unset($_temp_dir); return $to; // It's a good day in Eureka! }
info("Add File: {$file}"); $p->addFile($file, basename($file)); } } if ($index && $webindex) { info("Index: {$index}"); info("Web Index: {$webindex}"); $p->setDefaultStub($index, $webindex); } else { if ($index) { info("Index: {$index}"); $p->setDefaultStub($index); } else { if ($webindex) { info("Web Index: {$webindex}"); $p->setDefaultStub(null, $webindex); } } } if ($stub) { info("Stub: {$stub}"); if (is_file($stub)) { $stub = file_get_contents($stub); } $p->setStub($stub); } $p->stopBuffering(); info("Files: {$p->count()}"); } catch (\Exception $e) { error($e->getMessage()); }