Refactor PHP. Switch to explicit string literals where possible.

This commit is contained in:
Lars Jung 2015-05-11 12:20:09 +02:00
parent 429a2b7749
commit b340fe3569
3 changed files with 30 additions and 30 deletions

View file

@ -3,8 +3,8 @@
class Archive { class Archive {
private static $SEGMENT_SIZE = 16777216; // 1024 * 1024 * 16 = 16MiB private static $SEGMENT_SIZE = 16777216; // 1024 * 1024 * 16 = 16MiB
private static $TAR_PASSTHRU_CMD = "cd [ROOTDIR] && tar --no-recursion -c -- [DIRS] [FILES]"; private static $TAR_PASSTHRU_CMD = 'cd [ROOTDIR] && tar --no-recursion -c -- [DIRS] [FILES]';
private static $ZIP_PASSTHRU_CMD = "cd [ROOTDIR] && zip - -- [FILES]"; private static $ZIP_PASSTHRU_CMD = 'cd [ROOTDIR] && zip - -- [FILES]';
private $app, $base_path, $dirs, $files; private $app, $base_path, $dirs, $files;
@ -28,22 +28,22 @@ class Archive {
$this->add_hrefs($hrefs); $this->add_hrefs($hrefs);
if (count($this->dirs) === 0 && count($this->files) === 0) { if (count($this->dirs) === 0 && count($this->files) === 0) {
if ($type === "php-tar") { if ($type === 'php-tar') {
$this->add_dir($this->base_path, "/"); $this->add_dir($this->base_path, '/');
} else { } else {
$this->add_dir($this->base_path, "."); $this->add_dir($this->base_path, '.');
} }
} }
if ($type === "php-tar") { if ($type === 'php-tar') {
return $this->php_tar($this->dirs, $this->files); return $this->php_tar($this->dirs, $this->files);
} else if ($type === "shell-tar") { } else if ($type === 'shell-tar') {
return $this->shell_cmd(Archive::$TAR_PASSTHRU_CMD); return $this->shell_cmd(Archive::$TAR_PASSTHRU_CMD);
} else if ($type === "shell-zip") { } else if ($type === 'shell-zip') {
return $this->shell_cmd(Archive::$ZIP_PASSTHRU_CMD); return $this->shell_cmd(Archive::$ZIP_PASSTHRU_CMD);
} }
@ -53,9 +53,9 @@ class Archive {
private function shell_cmd($cmd) { private function shell_cmd($cmd) {
$cmd = str_replace("[ROOTDIR]", escapeshellarg($this->base_path), $cmd); $cmd = str_replace('[ROOTDIR]', escapeshellarg($this->base_path), $cmd);
$cmd = str_replace("[DIRS]", count($this->dirs) ? implode(" ", array_map("escapeshellarg", $this->dirs)) : "", $cmd); $cmd = str_replace('[DIRS]', count($this->dirs) ? implode(' ', array_map('escapeshellarg', $this->dirs)) : '', $cmd);
$cmd = str_replace("[FILES]", count($this->files) ? implode(" ", array_map("escapeshellarg", $this->files)) : "", $cmd); $cmd = str_replace('[FILES]', count($this->files) ? implode(' ', array_map('escapeshellarg', $this->files)) : '', $cmd);
try { try {
Util::passthru_cmd($cmd); Util::passthru_cmd($cmd);
} catch (Exeption $err) { } catch (Exeption $err) {
@ -80,7 +80,7 @@ class Archive {
} }
} }
header("Content-Length: " . $total_size); header('Content-Length: ' . $total_size);
foreach ($dirs as $archived_dir) { foreach ($dirs as $archived_dir) {
@ -154,7 +154,7 @@ class Archive {
foreach ($hrefs as $href) { foreach ($hrefs as $href) {
if (trim($href) === "") { if (trim($href) === '') {
continue; continue;
} }
@ -164,7 +164,7 @@ class Archive {
if ($this->app->is_managed_href($d) && !$this->app->is_hidden($n)) { if ($this->app->is_managed_href($d) && !$this->app->is_hidden($n)) {
$real_file = $this->app->to_path($href); $real_file = $this->app->to_path($href);
$archived_file = preg_replace("!^" . preg_quote(Util::normalize_path($this->base_path, true)) . "!", "", $real_file); $archived_file = preg_replace('!^' . preg_quote(Util::normalize_path($this->base_path, true)) . '!', '', $real_file);
if (is_dir($real_file)) { if (is_dir($real_file)) {
$this->add_dir($real_file, $archived_file); $this->add_dir($real_file, $archived_file);
@ -192,8 +192,8 @@ class Archive {
$files = $this->app->read_dir($real_dir); $files = $this->app->read_dir($real_dir);
foreach ($files as $file) { foreach ($files as $file) {
$real_file = $real_dir . "/" . $file; $real_file = $real_dir . '/' . $file;
$archived_file = $archived_dir . "/" . $file; $archived_file = $archived_dir . '/' . $file;
if (is_dir($real_file)) { if (is_dir($real_file)) {
$this->add_dir($real_file, $archived_file); $this->add_dir($real_file, $archived_file);

View file

@ -2,7 +2,7 @@
class Custom { class Custom {
private static $extensions = ["html", "md"]; private static $extensions = ['html', 'md'];
function __construct($app) { function __construct($app) {
@ -11,10 +11,10 @@ class Custom {
private function read_custom_file($path, $name, &$content, &$type) { private function read_custom_file($path, $name, &$content, &$type) {
$file_prefix = $this->app->get_setup()->get("FILE_PREFIX"); $file_prefix = $this->app->get_setup()->get('FILE_PREFIX');
foreach (Custom::$extensions as $ext) { foreach (Custom::$extensions as $ext) {
$file = "${path}/${file_prefix}.${name}.${ext}"; $file = $path . '/' . $file_prefix . '.' . $name . '.' . $ext;
if (is_readable($file)) { if (is_readable($file)) {
$content = file_get_contents($file); $content = file_get_contents($file);
$type = $ext; $type = $ext;
@ -25,14 +25,14 @@ class Custom {
public function get_customizations($href) { public function get_customizations($href) {
if (!$this->app->query_option("custom.enabled", false)) { if (!$this->app->query_option('custom.enabled', false)) {
return [ return [
"header" => ["content" => null, "type" => null], 'header' => ['content' => null, 'type' => null],
"footer" => ["content" => null, "type" => null] 'footer' => ['content' => null, 'type' => null]
]; ];
} }
$root_path = $this->app->get_setup()->get("FILE_PREFIX"); $root_path = $this->app->get_setup()->get('FILE_PREFIX');
$path = $this->app->to_path($href); $path = $this->app->to_path($href);
$header = null; $header = null;
@ -40,16 +40,16 @@ class Custom {
$footer = null; $footer = null;
$footer_type = null; $footer_type = null;
$this->read_custom_file($path, "header", $header, $header_type); $this->read_custom_file($path, 'header', $header, $header_type);
$this->read_custom_file($path, "footer", $footer, $footer_type); $this->read_custom_file($path, 'footer', $footer, $footer_type);
while ($header === null || $footer === null) { while ($header === null || $footer === null) {
if ($header === null) { if ($header === null) {
$this->read_custom_file($path, "headers", $header, $header_type); $this->read_custom_file($path, 'headers', $header, $header_type);
} }
if ($footer === null) { if ($footer === null) {
$this->read_custom_file($path, "footers", $footer, $footer_type); $this->read_custom_file($path, 'footers', $footer, $footer_type);
} }
if ($path === $root_path) { if ($path === $root_path) {
@ -63,8 +63,8 @@ class Custom {
} }
return [ return [
"header" => ["content" => $header, "type" => $header_type], 'header' => ['content' => $header, 'type' => $header_type],
"footer" => ["content" => $footer, "type" => $footer_type] 'footer' => ['content' => $footer, 'type' => $footer_type]
]; ];
} }
} }

View file

@ -14,7 +14,7 @@ class Search {
$re = Util::wrap_pattern($pattern); $re = Util::wrap_pattern($pattern);
$names = $this->app->read_dir($root); $names = $this->app->read_dir($root);
foreach ($names as $name) { foreach ($names as $name) {
$path = $root . "/" . $name; $path = $root . '/' . $name;
if (preg_match($re, @basename($path))) { if (preg_match($re, @basename($path))) {
$paths[] = $path; $paths[] = $path;
} }