package = new Package($this->loadJson($path), dirname(realpath($path)));
$this->logger = new Logger();
}
/**
* set output function to use to output log messages
*
* @param callable|boolean $output callable that receives a single $line argument or boolean echo
*/
public function setOutput($output)
{
$this->logger->setOutput($output);
}
/**
* Get path to target phar file (absolute path or relative to current directory)
*
* @return string
*/
public function getTarget()
{
if ($this->target === null) {
$this->target = $this->package->getShortName() . '.phar';
}
return $this->target;
}
/**
* Set path to target phar file (absolute path or relative to current directory)
*
* If the given path is a directory, the default target (package short name)
* will be appended automatically.
*
* @param string $target
* @return $this
*/
public function setTarget($target)
{
// path is actually a directory => append package name
if (is_dir($target)) {
$this->target = null;
$target = rtrim($target, '/') . '/' . $this->getTarget();
}
$this->target = $target;
return $this;
}
/**
* Get path to main bin (relative to package directory)
*
* @return string
* @throws \UnexpectedValueException
*/
public function getMain()
{
if ($this->main === null) {
foreach ($this->package->getBins() as $path) {
if (!file_exists($this->package->getDirectory() . $path)) {
throw new \UnexpectedValueException('Bin file "' . $path . '" does not exist');
}
$this->main = $path;
break;
}
}
return $this->main;
}
/**
* set path to main bin (relative to package directory)
*
* @param string $main
* @return $this
*/
public function setMain($main)
{
$this->main = $main;
return $this;
}
/**
*
* @return Package
*/
public function getPackageRoot()
{
return $this->package;
}
/**
*
* @return Package[]
*/
public function getPackagesDependencies()
{
$packages = array();
$pathVendor = $this->package->getDirectory() . $this->package->getPathVendor();
// load all installed packages (use installed.json which also includes version instead of composer.lock)
if (is_file($pathVendor . 'composer/installed.json')) {
// file does not exist if there's nothing to be installed
$installed = $this->loadJson($pathVendor . 'composer/installed.json');
// Composer 2.0 format wrapped in additional root key
if (isset($installed['packages'])) {
$installed = $installed['packages'];
}
foreach ($installed as $package) {
$dir = $package['name'] . '/';
if (isset($package['target-dir'])) {
$dir .= trim($package['target-dir'], '/') . '/';
}
$dir = $pathVendor . $dir;
$packages []= new Package($package, $dir);
}
}
return $packages;
}
public function build()
{
$this->log('[' . $this->step . '/' . $this->step.'] Creating phar