nur-sery/nur_src/ldap/app/LdapSearchApp.php

63 lines
1.5 KiB
PHP
Raw Normal View History

2024-04-04 22:21:20 +04:00
<?php
namespace nur\ldap\app;
use nur\b\IllegalAccessException;
use nur\ldap\io\LdapWriter;
use nur\ldap\io\LdifWriter;
use nur\ldap\io\YamlWriter;
use nur\ldap\LdapSearch;
use nur\ldap\LdapWalker;
class LdapSearchApp extends LdapApplication {
const ARGS = [
"merge" => parent::ARGS,
["-s", "--scope", "args" => 1],
["-b", "--searchbase", "args" => 1],
["-B", "--searchbase-exact", "args" => 1],
["-o", "--output", "args" => "file"],
["group",
["-F", "--format", "args" => 1],
["--ldif", "dest" => "format", "value" => "ldif"],
["--yaml", "dest" => "format", "value" => "yaml"],
],
];
protected $scope;
protected $searchbase, $searchbaseExact;
protected $output;
protected $format = "ldif";
protected $args;
function getWriter(): LdapWriter {
switch ($this->format) {
case "ldif":
case "l":
return new LdifWriter($this->output);
case "yaml":
case "y":
return new YamlWriter($this->output);
}
throw IllegalAccessException::unexpected_state();
}
function main() {
$conn = $this->getConn();
$params = [];
LdapSearch::parse_args($params, $this->args
, $this->searchbase, $this->searchbaseExact
, $this->scope);
/** @var LdapWalker $lo */
$lo = $conn->search(null, $params);
$writer = null;
while ($lo->next($first)) {
if ($first) {
$first = false;
$writer = $this->getWriter();
}
$writer->write($lo);
}
if ($writer !== null) $writer->close();
}
}