public function getParentsWithChildren() { $top = ['children' => []]; $insertIn = function (&$root, $parentAlias, $alias, $value) use(&$insertIn) { foreach ($root['children'] as $i => $child) { if ($i == $parentAlias) { $root['children'][$i]['children'][$alias] = $value; } else { if ($result = $insertIn($root['children'][$i], $parentAlias, $alias, $value)) { return $result; } } } return false; }; foreach ($this->models as $index => $model) { $alias = $model['alias']; $value = ['children' => [], 'self' => $this->getModelByAlias($alias)]; if ($parentAlias = $model['matchInfo']['parent'] ?? null) { if ($insertIn($top, $parentAlias, $alias, $value)) { $this->logger->warning("Unable to find parent ({$parentAlias}) for model: {$alias}"); } } else { $top['children'][$alias] = $value; } } return $top['children']; }
protected function str_replace_once($needle, $replace, $haystack, &$pos) { $pos = stripos($haystack, $needle); if ($pos === false) { $this->logger->warn("Cannot find tag {$needle}"); } return $pos !== false ? substr_replace($haystack, $replace, $pos, strlen($needle)) : $haystack; }
protected function compile($args) { $cmd = call_user_func_array('sprintf', $args); if ($this->debugger->enabled()) { $this->loggerEx->debug($cmd); } return $cmd; }
public function write($path, $replacements = [], $info = null) { @mkdir($this->utils->dirname($path), 0777, true); if ($result = file_put_contents($path, $this->readData($replacements))) { $this->logger->info(($info ?? "Written file") . ": " . realpath($path)); return $path; } throw new \Error("Cannot write file: {$path}"); }
public function getUrl(string $url, $options = []) { try { $res = $this->client->request('GET', $url, array_merge($this->defaults, $options)); if ($res->getStatusCode() === 200) { return (string) $res->getBody(); } } catch (\Throwable $e) { $this->logger->warn("unable to load url: {$url}"); } return false; }
/** * Bind listeners defined in plugins, app and Database */ public function bind() { $listeners = $this->cache->get('app-listeners', function () { foreach ($this->resolver->getListeners() as $file) { try { $binding = $this; require_once $file; } catch (\Throwable $e) { $this->logger->warn("Unable to include {$file}: " . $e->getMessage()); } } $listeners = $this->getListeners(); if ($this->database->isConnected()) { /** @var ModelEx $eventModel */ if ($eventModel = $this->resolver->getModel('Event', true)) { try { foreach ($eventModel::all() as $item) { $attrs = $item->attributesToArray(); list($class, $func) = @explode('@', $attrs['handler']); $event = array_merge($attrs, ['event' => $attrs['name'], 'handler' => [sprintf('\\%s', ltrim($class, '\\')), $func ?? 'index']]); $listeners[] = $event; } } catch (\Exception $e) { } } } return $listeners; }, 300); foreach ($listeners as $listener) { $this->dispatcher->listen($listener['event'], $listener['handler'], $listener['priority'] ?? 99, $listener['data'] ?? ''); } }
public function listen($events, $listener, $priority = 0, $data = null) { $wrapper = function ($payload) use($events, $listener, $data) { if (!empty($data) && $payload instanceof Event) { $payload->setData($data); } if (is_array($listener) && count($listener) === 2 && is_string($listener[0])) { $object = $this->injector->make($listener[0]); $listener = [$object, $listener[1]]; } if (is_callable($listener)) { return call_user_func($listener, $payload); } else { $this->logger->warn("Listener is not callable: ", $listener); return false; } }; parent::listen($events, $wrapper, $priority); }
protected function startQueryLog() { $this->logFile = $this->tmpDir->getTempDir('logs') . '/query.log'; @unlink($this->logFile); $this->connection->enableQueryLog(); $this->logger->pushHandler(new StreamHandler($this->logFile, LoggerEx::INFO)); $this->dispatcher->listen(QueryExecuted::class, function (QueryExecuted $query) { $sql = preg_replace_callback('/\\?/', function () use($query, &$index) { return sprintf("'%s'", $query->bindings[$index++ ?? 0]); }, $query->sql); $this->logger->info($sql); }); }
public function newFunction($path, $func) { $code = file_get_contents($path); if (preg_match('/^(.*?)class/s', $code, $matches)) { $brackets = 1 + substr_count($matches[1], '{'); $len = strlen($code); $pos = $len; while ($brackets-- > 0 && $pos !== false) { $pos = strrpos($code, '}', $pos - $len - 1); } if ($pos !== false) { $updated = sprintf("%s\n\n%s\n\t%s", trim(substr($code, 0, $pos)), $func, substr($code, $pos)); if (file_put_contents($path, $updated)) { $this->logger->info("Created new function in {$path}"); return true; } } else { trigger_error("Cannot modify file {$path}. Unable to find class brackets", E_WARNING); } } return false; }
public function signup(UserSignupEvent $event) { $signup = []; $fields = ['ident', 'email', 'contact_email', 'password', 'first_name', 'last_name', 'photo_url', 'tz_offset', 'ip_addr', 'http_referrer', 'http_campaign', 'verified']; $default = function ($field) use($event) { if ($field == 'photo_url' && ($email = $event->email)) { $gravatar = sprintf("http://www.gravatar.com/avatar/%s?d=404", md5(strtolower($email))); if ($headers = get_headers($gravatar, 1)) { if (strpos($headers[0], '200')) { return $gravatar; } } } elseif ($field == 'contact_email') { return $event->email ?? null; } elseif ($field == 'password') { return bin2hex(openssl_random_pseudo_bytes(4)); } elseif ($field == 'http_referrer') { $value = $_COOKIE[Router::HTTP_REFERRER_COOKIE] ?? '/'; return $value !== '/' ? $value : null; } elseif ($field == 'http_campaign') { return $_COOKIE[Router::HTTP_CAMPAIGN_COOKIE] ?? null; } elseif ($field == 'tz_offset') { return $_COOKIE['tz_offset'] ?? 240; } elseif ($field == 'verified') { return 'false'; } return null; }; foreach ($fields as $field) { $signup[$field] = $event->{$field} ?? $default($field); if ($field == 'password') { $signup[$field] = password_hash($signup[$field], PASSWORD_DEFAULT); } } if (filter_var($signup['email'] ?? null, FILTER_VALIDATE_EMAIL) || !empty($signup['ident'])) { try { User::unguard(); $user = new User($signup); $this->modelAutoFill->fillMissing($user); if ($user->save()) { $event->setUser($user); $signupEvent = (new UserSignupEvent($signup))->setUser($user); try { $this->dispatcher->fire(UserSignupEvent::USER_SIGNUP_COMPLETE, $signupEvent); } catch (\Throwable $e) { $this->logger->warn("Plugin error during signup: " . $e->getMessage()); } } } catch (QueryException $e) { $event->setError("EMAIL_IN_USE"); } catch (\Exception $e) { $event->setError($e->getMessage()); } } else { $event->setError('INVALID_DATA'); } if (!$event->getUser()) { //for logging $this->dispatcher->fire(UserSignupEvent::USER_SIGNUP_FAIL, $event); } }