Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/wishbase/master'
Browse files Browse the repository at this point in the history
Conflicts:
	packager.php
  • Loading branch information
GerHobbelt committed Mar 23, 2012
2 parents 566023c + 10e154d commit f036a0d
Showing 1 changed file with 55 additions and 43 deletions.
98 changes: 55 additions & 43 deletions packager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
class Packager {

public static function warn($message){
$std_err = fopen('php://stderr', 'w');
fwrite($std_err, $message);
fclose($std_err);
if (PHP_SAPI === 'cli') {
$std_err = fopen('php://stderr', 'w');
fwrite($std_err, $message);
fclose($std_err);
} else {
trigger_error($message, E_USER_WARNING);
}
}

public static function info($message){
Expand All @@ -30,14 +34,14 @@ public function show_warning($message){
public function __construct($package_paths){
foreach ((array)$package_paths as $package_path) $this->parse_manifest($package_path);
}

private function parse_manifest($path){
$pathinfo = pathinfo($path);

if (is_dir($path)){

$package_path = $pathinfo['dirname'] . '/' . $pathinfo['basename'] . '/';

if (file_exists($package_path . 'package.yml')){
$manifest_path = $package_path . 'package.yml';
$manifest_format = 'yaml';
Expand All @@ -54,23 +58,28 @@ private function parse_manifest($path){
$manifest_path = $package_path . $pathinfo['basename'];
$manifest_format = $pathinfo['extension'];
} else {
throw new Exception('Neither directory nor file "' . $path . '" exist.');
self::warn('Neither directory nor file "' . $path . '" exist.');
}

if ($manifest_format == 'json') $manifest = json_decode(file_get_contents($manifest_path), true);
else if ($manifest_format == 'yaml' || $manifest_format == 'yml') $manifest = YAML::decode_file($manifest_path);

if (empty($manifest)) throw new Exception("manifest not found in $package_path, or unable to parse manifest.");

$package_name = $manifest['name'];

if ($this->root == null) $this->root = $package_name;

if (array_has($this->manifests, $package_name)) return;
if (array_has($this->manifests, $package_name)) {
return;
}
if (!isset($manifest['sources']) || !is_array($manifest['sources'])) {
self::warn('No valuable sources defined in package "' . $package_name . '"');
}

$manifest['path'] = $package_path;
$manifest['manifest'] = $manifest_path;

$this->manifests[$package_name] = $manifest;

if (!is_array($manifest['sources'])){
Expand Down Expand Up @@ -101,6 +110,9 @@ private function parse_manifest($path){
if (!$patternUsed) $path = $package_path . $path;

// this is where we "hook" for possible other replacers.
if (!file_exists($path)) {
self::warn('Source file "' . $path . '" for package "' . $package_name . '" does not exist');
}
$source = file_get_contents($path);

$descriptor = array();
Expand All @@ -124,9 +136,9 @@ private function parse_manifest($path){
// "normalization" for requires. Fills up the default package name from requires, if not present.
foreach ($requires as $i => $require)
$requires[$i] = implode('/', $this->parse_name($package_name, $require));

$license = array_get($descriptor, 'license');

$this->packages[$package_name][$file_name] = array_merge($descriptor, array(
'name' => $file_name,
'package' => $package_name,
Expand All @@ -140,18 +152,18 @@ private function parse_manifest($path){

}
}

public function add_package($package_path){
$this->parse_manifest($package_path);
}

public function remove_package($package_name){
unset($this->packages[$package_name]);
unset($this->manifests[$package_name]);
}

// # private UTILITIES

private function parse_name($default, $name){
$exploded = explode('/', $name, 2);
$length = count($exploded);
Expand All @@ -176,7 +188,7 @@ private function bfglob($path, $pattern = '*', $flags = 0, $depth = 0) {
}

// # private HASHES

private function component_to_hash($name){
$pair = $this->parse_name($this->root, $name);
$package = array_get($this->packages, $pair[0]);
Expand All @@ -190,10 +202,10 @@ private function component_to_hash($name){
}
}
}

return null;
}

private function file_to_hash($name){
$pair = $this->parse_name($this->root, $name);
$package = array_get($this->packages, $pair[0]);
Expand All @@ -205,18 +217,18 @@ private function file_to_hash($name){
if ($file == $file_name) return $data;
}
}

return null;
}

public function file_exists($name){
return $this->file_to_hash($name) ? true : false;
}

public function component_exists($name){
return $this->component_to_hash($name) ? true : false;
}

public function package_exists($name){
return array_contains($this->get_packages(), $name);
}
Expand All @@ -239,15 +251,15 @@ public function validate($more_files = array(), $more_components = array(), $mor
}
}
}

foreach ($more_files as $file){
if (!$this->file_exists($file)) show_warning("WARNING: The required file $file could not be found.\n");
}

foreach ($more_components as $component){
if (!$this->component_exists($component)) show_warning("WARNING: The required component $component could not be found.\n");
}

foreach ($more_packages as $package){
if (!$this->package_exists($package)) show_warning("WARNING: The required package $package could not be found.\n");
}
Expand All @@ -259,7 +271,7 @@ public function resolve_files($files = array(), $components = array(), $packages
$more = $this->components_to_files($components);
foreach ($more as $file) array_include($files, $file);
}

foreach ($packages as $package){
$more = $this->get_all_files($package);
foreach ($more as $file) array_include($files, $file);
Expand Down Expand Up @@ -345,7 +357,7 @@ public function write_from_components($file_name, $components = null, $excluded
$full = $this->build_from_components($components, $excluded);
file_put_contents($file_name, $full);
}

// # public FILES

public function get_all_files($of_package = null){
Expand Down Expand Up @@ -385,7 +397,7 @@ public function complete_file($file){
array_include($files, $hash['package/name']);
return $files;
}

public function complete_files($files){
$ordered_files = array();
foreach ($files as $file){
Expand All @@ -394,13 +406,13 @@ public function complete_files($files){
}
return $ordered_files;
}

// # public COMPONENTS

public function component_to_file($component){
return array_get($this->component_to_hash($component), 'package/name');
}

public function components_to_files($components){
$files = array();
foreach ($components as $component){
Expand All @@ -409,9 +421,9 @@ public function components_to_files($components){
}
return $files;
}

// # dynamic getter for PACKAGE properties and FILE properties

public function __call($method, $arguments){
if (strpos($method, 'get_file_') === 0){
$file = array_get($arguments, 0);
Expand All @@ -420,36 +432,36 @@ public function __call($method, $arguments){
$hash = $this->file_to_hash($file);
return array_get($hash, $key);
}

if (strpos($method, 'get_package_') === 0){
$key = substr($method, 12);
$package = array_get($arguments, 0);
$package = array_get($this->manifests, (empty($package)) ? $this->root : $package);
return array_get($package, $key);
}

return null;
}

public function get_packages(){
return array_keys($this->packages);
}

// authors normalization

public function get_package_authors($package = null){
if (empty($package)) $package = $this->root;
$package = array_get($this->manifests, $package);
if (empty($package)) return array();
return $this->normalize_authors(array_get($package, 'authors'), array_get($package, 'author'));
}

public function get_file_authors($file){
$hash = $this->file_to_hash($file);
if (empty($hash)) return array();
return $this->normalize_authors(array_get($hash, 'authors'), array_get($hash, 'author'), $this->get_package_authors());
}

private function normalize_authors($authors = null, $author = null, $default = null){
$use = empty($authors) ? $author : $authors;
if (empty($use) && !empty($default)) return $default;
Expand Down

0 comments on commit f036a0d

Please sign in to comment.