Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ private function runSaver($code) {
if ($aliasName = $this->config->getPharAliasName()) {
$pharBuilder->setAliasName($aliasName);
}
if ($bootstrapName = $this->config->getPharBootstrapName()) {
$pharBuilder->setBootstrapName($bootstrapName);
}
if ($this->config->hasPharHashAlgorithm()) {
$pharBuilder->setSignatureType($this->config->getPharHashAlgorithm());
}
Expand Down
80 changes: 73 additions & 7 deletions src/AutoloadRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ class AutoloadRenderer {
*/
protected $variables = array();

/**
* Templates head
*
* @var string
*/
protected $head = "<?php\n";

/**
* Template tail
*
* @var string
*/
protected $tail = '';

/**
* Flag to toggle PHP 5.2 compat mode
*
Expand Down Expand Up @@ -218,6 +232,50 @@ public function setVariable($name, $value) {
$this->variables['___'.$name.'___'] = $value;
}

/**
* Set head of the template code.
*
* @param string|string[] $head One or more filenames or code fragments to be included at the beginning of the template
*/
public function setHead($head) {
$heads = (array) $head;
foreach ($heads as &$fileOrCode)
{
if (file_exists($fileOrCode)) {
$fileOrCode = file_get_contents($fileOrCode);
}
}
unset($fileOrCode);
$this->head = implode("\n;\n",$heads);
if (!preg_match('@<\?php\s@',$this->head)) {
$this->head = "<?php\n".$this->head;
}
}

/**
* Set tail of the template code.
*
* @param string|string[] $tail One or more filenames or code fragments to be included at the end of the
* template
*
* @throws \TheSeer\Autoload\AutoloadBuilderException
*/
public function setTail($tail) {
$tail = (array) $tail;
foreach ( $tail as &$fileOrCode)
{
if (file_exists($fileOrCode)) {
$fileOrCode = file_get_contents($fileOrCode);
$fileOrCode = preg_replace('@^(#!/.*?[\r\n]+)?<\?php\s@', '', $fileOrCode);
}
}
unset($fileOrCode);
$this->tail = implode("\n;\n",$tail);
$test = preg_replace('@\?>.*?<\?php\s@', '', $this->tail);
if (preg_match('@<\?php\s@', $test)) {
throw new AutoloadBuilderException("Template tail includes unmatched '<?php'", AutoloadBuilderException::TemplateIncludesPhpOpener);
}
}

/**
* Resolve relative location of file path to basedir if one is set and fix potential
Expand All @@ -237,14 +295,14 @@ protected function resolvePath($fname) {
$max = count($basedir);
while (isset($filedir[$pos]) && $filedir[$pos] == $basedir[$pos]) {
$pos++;
if ($pos == $max) {
if ($pos === $max) {
break;
}
}
if ($pos == 0) {
if ($pos === 0) {
return str_replace('\\', '/', $fname);
}
$rel = join('/', array_slice($filedir, $pos));
$rel = implode('/', array_slice($filedir, $pos));
if (!empty($rel)) {
$rel .= '/';
}
Expand Down Expand Up @@ -274,14 +332,21 @@ public function render($template) {
}

$replace = array_merge($this->variables, array(
'___CREATED___' => date( $this->dateformat, $this->timestamp ? $this->timestamp : time()),
'___CLASSLIST___' => join( ',' . $this->linebreak . $this->indent, $entries),
'___CREATED___' => date( $this->dateformat, $this->timestamp ?: time()),
'___CLASSLIST___' => implode( ',' . $this->linebreak . $this->indent, $entries),
'___BASEDIR___' => $baseDir,
'___AUTOLOAD___' => 'autoload' . md5(serialize($entries)),
'___EXCEPTION___' => $this->throwExceptions ? 'true' : 'false',
'___PREPEND___' => $this->usePrepend ? 'true' : 'false'
'___PREPEND___' => $this->usePrepend ? 'true' : 'false',
'___HEAD___' => $this->head,
'___TAIL___' => $this->tail
));
return str_replace(array_keys($replace), array_values($replace), $template);

do {
$template = str_replace(array_keys($replace), array_values($replace), $template, $count);
} while ($count);

return $template;
}

}
Expand All @@ -291,6 +356,7 @@ class AutoloadBuilderException extends \Exception {

const TemplateNotFound = 1;
const InvalidTimestamp = 2;
const TemplateIncludesPhpOpener = 3;

}

Expand Down
34 changes: 32 additions & 2 deletions src/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ private function configure(array $env, \ezcConsoleInput $input) {
$compression,
$input->getOption('all')->value,
$input->getOption('key')->value,
$input->getOption('alias')->value
$input->getOption('alias')->value,
$input->getOption('bootstrap')->value
);
$config->setVariable('PHAR',
$input->getOption('alias')->value ? $input->getOption('alias')->value : basename($output)
Expand Down Expand Up @@ -241,6 +242,12 @@ private function configure(array $env, \ezcConsoleInput $input) {
if ($template = $input->getOption('template')->value) {
$config->setTemplate($template);
}
if ($head = $input->getOption('head')->value) {
$config->setHead($head);
}
if ($tail = $input->getOption('tail')->value) {
$config->setTail($tail);
}
if ($linebreak = $input->getOption('linebreak')->value) {
$config->setLinebreak($linebreak);
}
Expand Down Expand Up @@ -298,10 +305,16 @@ protected function showUsage() {

-b, --basedir Basedir for filepaths
-t, --template Path to code template to use
-H, --head Path to file or code to include at the beginning of the file (must include "<?php" !)
-T, --tail Path to file or code to include at the end of the file ("<?php" will be stripped!)

-o, --output Output file for generated code (default: STDOUT)

-p, --phar Create a phar archive (requires -o )
--bootstrap <file>
Save bootstrap code (stub) also in <file> within the phar file. If -o points to anything
other than a .phar file (thus creating a PharData archive), this is required, defaulting
to 'autoload.php' if omitted (requires -p)
--all Include all files in given directory when creating a phar
--alias Specify explicit internal phar alias filename (default: output filename)
--hash Force given hash algorithm (SHA-1, SHA-256 or SHA-512) (requires -p, conflicts with --key)
Expand Down Expand Up @@ -378,6 +391,13 @@ protected function setupInput() {
array( new \ezcConsoleOptionRule( $input->getOption( 'o' ) ) )
));

$input->registerOption( new \ezcConsoleOption(
'', 'bootstrap', \ezcConsoleInput::TYPE_STRING, NULL, FALSE,
'Filename within the phar, if a PharData is created (output does not end on .phar, but .tar, .tar.gz, tar.bz)',
NULL,
array( new \ezcConsoleOptionRule( $input->getOption( 'p' ) ) )
));

$input->registerOption( new \ezcConsoleOption(
'', 'all', \ezcConsoleInput::TYPE_NONE, NULL, FALSE,
'Add all files from src dir to phar',
Expand Down Expand Up @@ -453,6 +473,16 @@ protected function setupInput() {
'Path to code template to use'
));

$input->registerOption( new \ezcConsoleOption(
'H', 'head', \ezcConsoleInput::TYPE_STRING, NULL, TRUE,
'Path to file or code to include at the beginning of the file (must include "<?php" !)'
));

$input->registerOption( new \ezcConsoleOption(
'T', 'tail', \ezcConsoleInput::TYPE_STRING, NULL, TRUE,
'Path to file or code to include at the end of the file ("<?php" at the beginning will be stripped!)'
));

$input->registerOption( new \ezcConsoleOption(
'', 'follow', \ezcConsoleInput::TYPE_NONE, NULL, FALSE,
'Enables following symbolic links',
Expand Down Expand Up @@ -589,7 +619,7 @@ private function preBootstrap() {
}
if (count($missing)) {
throw new CLIEnvironmentException(
join("\n", $missing),
implode("\n", $missing),
CLIEnvironmentException::ExtensionMissing
);
}
Expand Down
32 changes: 28 additions & 4 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Config {
private $blacklist = array();
private $baseDirectory = NULL;
private $template;
private $head = '';
private $tail = '';
private $linebreak = "\n";
private $indent;
private $lint = FALSE;
Expand All @@ -67,6 +69,7 @@ class Config {
private $pharKey;
private $pharAll = false;
private $pharAliasName = '';
private $pharBootstrapName = '';
private $pharHashAlgorithm;
private $followSymlinks = false;
private $cacheFilename;
Expand All @@ -88,7 +91,7 @@ public function setHomeDirectory($homeDir) {
$this->homeDirectory = $homeDir;
}

public function getHomeDirectory(): string {
public function getHomeDirectory() {
return $this->homeDirectory;
}

Expand All @@ -100,7 +103,7 @@ public function getBaseDirectory() {
$comparator = new PathComparator($this->directories);
return $comparator->getCommonBase();
}
if ($this->outputFile != 'STDOUT') {
if ($this->outputFile !== 'STDOUT') {
return realpath(dirname($this->outputFile) ?: '.');
}
$tmp = $this->getDirectories();
Expand Down Expand Up @@ -229,12 +232,13 @@ public function getOutputFile() {
return $this->outputFile;
}

public function enablePharMode($compression = 'NONE', $all = true, $key = NULL, $alias = NULL) {
public function enablePharMode($compression = 'NONE', $all = true, $key = NULL, $alias = NULL, $bootstrap = NULL) {
$this->pharMode = true;
$this->pharCompression = $compression;
$this->pharAll = (boolean)$all;
$this->pharKey = $key;
$this->pharAliasName = $alias;
$this->pharBootstrapName = $bootstrap;
}

public function isPharMode() {
Expand All @@ -257,6 +261,10 @@ public function getPharAliasName() {
return $this->pharAliasName;
}

public function getPharBootstrapName() {
return $this->pharBootstrapName;
}

public function hasPharHashAlgorithm() {
return $this->pharHashAlgorithm !== null;
}
Expand Down Expand Up @@ -361,6 +369,22 @@ public function getTemplate() {

}

public function setHead($head) {
$this->head = $head;
}

public function getHead() {
return $this->head;
}

public function setTail($tail) {
$this->tail = $tail;
}

public function getTail() {
return $this->tail;
}

public function setTolerantMode($tolerant) {
$this->tolerant = (boolean)$tolerant;
}
Expand Down Expand Up @@ -400,7 +424,7 @@ public function isQuietMode() {
public function getDirectories() {
$list = array();
foreach($this->directories as $dir) {
if (is_file($dir) && basename($dir) == 'composer.json') {
if (is_file($dir) && basename($dir) === 'composer.json') {
foreach(new ComposerIterator(new \SplFileInfo($dir), $this->getHomeDirectory()) as $d) {
$list[] = $d;
}
Expand Down
10 changes: 9 additions & 1 deletion src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function getPharBuilder() {
*
* @param CollectorResult $result
*
* @throws \RuntimeException
* @throws \RuntimeException|\TheSeer\Autoload\AutoloadBuilderException
* @return \TheSeer\Autoload\AutoloadRenderer|\TheSeer\Autoload\StaticRenderer
*/
public function getRenderer(CollectorResult $result) {
Expand Down Expand Up @@ -218,6 +218,14 @@ public function getRenderer(CollectorResult $result) {
$renderer->setVariable($name, $value);
}

if($head = $this->config->getHead()) {
$renderer->setHead($head);
}

if($tail = $this->config->getTail()) {
$renderer->setTail($tail);
}

return $renderer;
}

Expand Down
Loading