PHP
290
Вклад в тег
class HandlerManager
{
public function register()
{
set_exception_handler([$this, 'exception']);
set_error_handler([$this, 'error']);
register_shutdown_function([$this, 'shutdown']);
}
private function log($error, $code, $file, $line)
{
$error = sprintf('Error %s in file %s[%d]: %s', $code, $file, $line, $error);
return error_log($error);
}
public function exception(\Throwable $e)
{
$this->log($e->getMessage(), $e->getCode(), $e->getFile(), $e->getLine());
// clean the output buffer if one exists
ob_get_level() && ob_clean();
header('Content-Type: text/plain; charset=utf-8', true, 500);
echo $e->getMessage();
exit(1);
}
public function error($severity, $error, $file = '', $line = 0)
{
if (error_reporting() & $severity) {
$this->log($error, $severity, $file, $line);
throw new \ErrorException($error, $severity, $file, $line);
}
// dont execute the PHP error handler
return true;
}
public function shutdown(array $shutdown_errors = [E_PARSE, E_ERROR, E_USER_ERROR])
{
$error = error_get_last();
if ($error && in_array($error['type'], $shutdown_errors)) {
// сlean the output buffer
ob_get_level() && ob_clean();
$this->log($error['message'], $error['type'], $error['file'], $error['line']);
// shutdown now to avoid a "death loop"
exit(1);
}
}
}
$formatter = new \Phpcf\Formatter(new \Phpcf\Options());
$result = $formatter->formatFile('path/to/file.php');
if ($result->getError()) {
throw $result->getError();
} else {
file_put_contents($result->getFile(), $result->getContent());
}
$file = __DIR__ . '/demo.php';
$config = \PhpCsFixer\Config::create()
->setRules([
'@PSR12' => true,
'list_syntax' => ['syntax' => 'short'],
])
->setFinder(new ArrayIterator([new SplFileInfo($file)]));
$resolver = new \PhpCsFixer\Console\ConfigurationResolver(
$config,
[],
__DIR__,
new \PhpCsFixer\ToolInfo()
);
$runner = new \PhpCsFixer\Runner\Runner(
$config->getFinder(),
$resolver->getFixers(),
$resolver->getDiffer(),
null,
new \PhpCsFixer\Error\ErrorsManager(),
$resolver->getLinter(),
$resolver->isDryRun(),
$resolver->getCacheManager(),
$resolver->getDirectory(),
$resolver->shouldStopOnViolation()
);
$runner->fix();
class XmlConverter
{
/**
* Conver array in xml
*/
public function as_array(array $data, $xml = NULL)
{
if (is_null($xml))
{
$xml = simplexml_load_string('<'.key($data).'/>');
$data = current($data);
$return = TRUE;
}
if (is_array($data))
{
foreach ($data as $name => $value)
{
self::from_array($value, is_numeric($name) ? $xml : $xml->addChild($name));
}
}
else
{
$xml->{0} = $data;
}
if ( ! empty($return))
{
return $xml->asXML();
}
}
/**
* Conver xml in array
*/
public function to_array($xml)
{
$tree = NULL;
while($xml->read())
{
switch ($xml->nodeType)
{
case XMLReader::END_ELEMENT:
return $tree;
case XMLReader::ELEMENT:
$node = array(
'tag' => $xml->name,
'value' => $xml->isEmptyElement ? '' : self::to_array($xml)
);
if ($xml->hasAttributes)
{
while ($xml->moveToNextAttribute())
{
$node['attributes'][$xml->name] = $xml->value;
}
}
$tree[] = $node;
break;
case XMLReader::TEXT:
case XMLReader::CDATA:
$tree .= $xml->value;
}
}
return $tree;
}
}
class HttpProxyList
{
/**
* @param array[] список прокси серверов `[host: string, port: int]`
*/
private array $proxyList = [];
public function __construct(array $proxyList)
{
$this->proxyList = $proxyList;
}
protected function checkProxy(string $host, int $port): bool
{
$errorCode = $errorMessage = null;
try {
$handler = fsockopen($host, $port, $errorCode, $errorMessage, 3);
if ($handler !== false) {
fclose($handler);
return true;
}
} catch (Throwable $e) {
error_log("Proxy server '$host:$port' not available: $errorMessage [$errorCode]"):
}
return false;
}
public function getActiveProxy(): ?array
{
foreach ($this->proxyList as $key => $proxy) {
if ($this->checkProxy($proxy['host'], $proxy['port'])) {
return $proxy;
}
unset($this->proxyList[$key]);
}
return null;
}
}