49 lines
1.5 KiB
PHP
49 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace NoccyLabs\Lpr;
|
|
|
|
use ArrayIterator;
|
|
|
|
class PrinterList implements \IteratorAggregate
|
|
{
|
|
const PRINTCAP_FILE = "/etc/printcap";
|
|
|
|
/** @var array */
|
|
private static $printers = [];
|
|
|
|
private static function update()
|
|
{
|
|
self::$printers = [];
|
|
if (file_exists(self::PRINTCAP_FILE) && is_readable(self::PRINTCAP_FILE)) {
|
|
$printcap = file(self::PRINTCAP_FILE, FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES);
|
|
foreach ($printcap as $line) {
|
|
if (empty($line) || (strpos(trim($line),"#") === 0)) continue;
|
|
if (strpos($line, "|") === false) continue;
|
|
list ($name, $info) = explode("|", $line, 2);
|
|
$meta = explode(":", $info);
|
|
$props = [
|
|
'name' => $name,
|
|
'description' => array_shift($meta),
|
|
];
|
|
foreach ($meta as $prop) {
|
|
if (strpos($prop, "=") === false) continue;
|
|
list($key,$value) = explode("=",$prop,2);
|
|
$props[$key] = $value;
|
|
}
|
|
self::$printers[$name] = $props;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getIterator()
|
|
{
|
|
if (empty(self::$printers)) self::update();
|
|
return new ArrayIterator(self::$printers);
|
|
}
|
|
|
|
public function getPrinterNames()
|
|
{
|
|
if (empty(self::$printers)) self::update();
|
|
return array_keys(self::$printers);
|
|
}
|
|
} |