Last active
September 5, 2017 20:57
-
-
Save jasonhofer/d61e77946bac67b2364e2f6d5a1610e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Resolves a path just like realpath(), except the file or directory does not have to exist. | |
* | |
* Works for Unix-based and Windows systems. | |
* | |
* @param string $path | |
* @param string $ds This is only here to make testing easier. | |
* | |
* @return string | |
*/ | |
function normalizePath($path, $ds = DIRECTORY_SEPARATOR) { | |
$path = str_replace(['/', '\\'], $ds, trim($path)); | |
$prefix = ''; | |
if ('\\' === $ds) { | |
preg_match('~^([a-z]:)~i', $path, $m); | |
if ($m[1]) { | |
$prefix = $m[1].$ds; | |
$path = substr($path, 3); | |
} | |
} else { | |
$prefix = $path && $path[0] === $ds ? $ds : ''; | |
} | |
$parts = array_filter(explode($ds, $path)); | |
$out = []; | |
foreach ($parts as $part) { | |
if ('.' === $part) { | |
continue; | |
} | |
if ('..' === $part) { | |
array_pop($out); | |
continue; | |
} | |
$out[] = $part; | |
} | |
return $prefix.implode($ds, $out); | |
} | |
/** | |
* Friendlier command line executing function. | |
* | |
* @param string $cmd | |
* @param array $args | |
* @param string $output | |
* | |
* @return bool | |
*/ | |
function execute($cmd, array $args = [], &$output = null) { | |
$redirect = ''; | |
if (count($args)) { | |
$last = array_pop($args); | |
if (preg_match('~^[12]?>.+$~', (string) $last)) { | |
$redirect = $last; | |
} else { | |
$args[] = $last; | |
} | |
} | |
$escaped = array_map('escapeshellarg', $args); | |
$cmdArgs = trim(implode(' ', $escaped).' '.$redirect); | |
$cmdLine = trim("{$cmd} {$cmdArgs}"); | |
exec($cmdLine, $cmdOutput, $errCode); | |
/** @noinspection CallableParameterUseCaseInTypeContextInspection */ | |
$output = trim(implode(PHP_EOL, $cmdOutput)); | |
return 0 === (int) $errCode; | |
} | |
/** | |
* @param string $filePath | |
* | |
* @return string | |
*/ | |
function stripFileExt($filePath) { | |
return substr($filePath, 0, strrpos($filePath, '.')); | |
} | |
/** | |
* @param string[] $items | |
* @param string $comma | |
* @param string $conjunction | |
* @param bool $useOxfordComma | |
* | |
* @return string | |
*/ | |
function humanizedList(array $items, $comma = ',', $conjunction = 'and', $useOxfordComma = true) { | |
$items = array_filter(array_map('trim', $items)); | |
switch (count($items)) { | |
case 0: return ''; | |
case 1: return reset($items); | |
case 2: return implode(' '.$conjunction.' ', $items); | |
default: | |
$comma .= ' '; | |
$last = array_pop($items); | |
$initial = implode($comma, $items); | |
if ($useOxfordComma) { | |
$initial .= $comma; | |
} | |
return $initial.' '.$conjunction.' '.$last; | |
} | |
} | |
/** | |
* @param string $dir | |
* | |
* @return array | |
*/ | |
function scanFolder($dir) | |
{ | |
$listing = scandir($dir, SCANDIR_SORT_ASCENDING) ?: []; | |
return array_diff($listing, ['.', '..']); | |
} | |
/** | |
* @param string $format | |
* @param array $items | |
* | |
* @return string[] | |
*/ | |
function sprintfEach($format, array $items) | |
{ | |
$formatted = []; | |
foreach ($items as $item) { | |
$formatted[] = vsprintf($format, (array) $item); | |
} | |
return $formatted; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment