public function addChannels(array $channels) { foreach ($channels as $name => $conf) { if (isset($this->channels[$name])) { throw new Error("Conflict in EmailLogService with channel: {$name}"); } Helper::formatArray($conf, ['fields' => ['minLevel' => ['default' => 'error'], 'maxSize' => ['default' => 300], 'autoFlush' => ['default' => true], 'delayS' => ['default' => 600], 'emailTo' => [], 'emailSubject' => $name]]); $this->channels[$name] = ['conf' => $conf]; } }
public function listResources(array $resourceQuery) { Helper::formatArray($resourceQuery, ['fields' => ['where' => ['default' => []], 'orderBy' => ['default' => ['time desc']], 'pageNumber' => ['default' => null], 'pageSize' => ['default' => null]]]); $subQSeq = 0; $amend = []; $qiJoins = []; if (!empty($resourceQuery['where'])) { $amend['where'] = $this->buildQIFilter($resourceQuery['where'], $subQSeq, $qiJoins); } if (!empty($resourceQuery['orderBy'])) { if (!is_array($resourceQuery['orderBy'])) { $resourceQuery['orderBy'] = [$resourceQuery['orderBy']]; } $amend['orderBy'] = $this->buildQIOrderBy($resourceQuery['orderBy'], $qiJoins); } if (isset($resourceQuery['pageNumber'])) { $pageSize = isset($resourceQuery['pageSize']) ? $resourceQuery['pageSize'] : 10; $amend['limit'] = $pageSize; if ($resourceQuery['pageNumber'] > 0) { $amend['offset'] = $resourceQuery['pageNumber'] * $pageSize; } } if (!empty($qiJoins)) { $amend['join'] = $qiJoins; } $query = $this->getBaseResourceQuery(); if (!empty($amend)) { $query = $query->amend($amend); } $rs = $query->queryFetchAll(); $list = []; foreach ($rs as $row) { $list[] = new ResourceReader($this, $row['resource_id'], $row); } return $list; }
/** * @param string $mode * @param array $resValues ['time' => int] * @return array A map of values by field names */ public function toComputed($mode, $resValues) { Helper::formatArray($resValues, ['fields' => ['time' => []]]); $fields = self::toFieldList($mode); $tokens = getdate($resValues['time']); $computed = []; foreach ($fields as $field) { switch ($field) { case 'year': $val = $tokens['year']; break; case 'month': $val = $tokens['mon']; break; case 'day': $val = $tokens['mday']; break; default: throw new Error("Unknown field: {$field}"); } $computed[$field] = $val; } return $computed; }
/** @return \PDO */ public function createDatabaseConnection() { return Helper::openPDO($this->cnInfo); }
/** * # `array{QpNewResource}` * ``` * [ * 'siteId' => int, * 'fieldSet' => string, * 'lang' => string, * 'time' => int, * 'status' => string * ] * ``` * @param array $newResource array{QpNewResource} * @return ResourceWriter */ public static function createPlaceholder(array $newResource) { Helper::formatArray($newResource, ['fields' => ['siteId' => [], 'fieldSet' => [], 'lang' => [], 'time' => [], 'status' => ['default' => 'placeholder'], 'parent' => ['default' => null]]]); // TODO }
public function getContributorType($code) { if (isset($this->cleanedData['contributorTypes'][$code])) { return $this->cleanedData['contributorTypes'][$code]; } $item = $this->getItem('contributorTypes', $code); Helper::formatArray($item, ['contributorTypes' => ['dbCode' => ['default' => $code]]]); $this->cleanedData['contributorTypes'][$code] = $item; return $item; }
/** * # `array{ConnectionInformation}` * ``` * [ * 'dsn' => string (optional: xor dsn, dsnParts), * 'dsnParts' => [ * 'prefix' => string (xor path, code), * 'host' => string, * 'port' => string, * 'database' => string * ] (optional: xor dsn, dsnParts), * 'user' => string (optional), * 'password' => string (optional), * 'pdoOptions' => mixed (optional), * 'pdoAttributes' => [attributeInt => value, ...] (optional), * 'initCommands' => array<string> (optional) * ] * ``` * @param array $cnInfo array{ConnectionInformation} * @return PDO a connection */ public static function openPDO(array $cnInfo) { Helper::formatArray($cnInfo, ['fields' => ['dsn' => [], 'dsnParts' => ['format' => ['fields' => ['prefix' => [], 'host' => [], 'port' => ['default' => null], 'database' => []]]], 'user' => ['default' => null], 'password' => ['default' => null], 'pdoOptions' => ['default' => null], 'pdoAttributes' => ['default' => []], 'initCommands' => ['default' => []]], 'xor' => [['dsn', 'dsnParts']]]); if (isset($cnInfo['dsn'])) { $dsn = $cnInfo['dsn']; } else { $dsnParts = $cnInfo['dsnParts']; $dsn = $dsnParts['prefix'] . ':host=' . $dsnParts['host']; if (isset($dsnParts['port'])) { $dsn .= ';port=' . $dsnParts['port']; } $dsn .= ';dbname=' . $dsnParts['database']; } try { $cn = new PDO($dsn, $cnInfo['user'], isset($cnInfo['user']) ? $cnInfo['password'] : null, $cnInfo['pdoOptions']); foreach ($cnInfo['pdoAttributes'] as $attr => $val) { $cn->setAttribute($attr, $val); } } catch (PDOException $e) { throw new Error('Fail to connect to "' . $dsn . '": ' . $e->getMessage()); } foreach ($cnInfo['initCommands'] as $command) { if ($cn->exec($command) === false) { throw new Error('error on init command "' . $command . '": ' . $cn->errorCode()); } } return $cn; }
<?php use QPress\ApplicationConfiguration; use QPress\LoaderConfiguration; \QPress\Debug::log('Load CMS config'); \QPress\ClassAutoLoader::addNamespace('QpCms', __DIR__ . '/QpCms'); /** @var LoaderConfiguration $loader */ $loader->initializeApplication(function (ApplicationConfiguration $conf, array $parameters) { \QPress\Debug::log('init qp-cms'); \QPress\Helper::formatArray($parameters, ['fields' => ['themeStaticDirectory' => [], 'mediasDirectory' => [], 'themeName' => [], 'pluginsName' => [], 'site' => [], 'db' => []]]); $conf->useComponent(['name' => $parameters['pluginsName']]); $conf->registerService('DataProcessor', 'QpCms\\DataProcessor\\DataProcessorService', ['templateServices' => ['DataReader']]); $conf->registerService('Meta', 'QpCms\\Meta\\MetaService'); $conf->registerCommand('backend', function () { require __DIR__ . '/backend.php'; return 'QpCms\\Backend\\backend'; }); $conf->setProperties(['encoding' => 'UTF-8', 'site' => $parameters['site'], 'db' => $parameters['db'], 'tablePrefix' => isset($parameters['db']['tablePrefix']) ? $parameters['db']['tablePrefix'] : '']); $conf->setThemeAssistant(function (\QPress\ApplicationTool $app) { $site = $app->getProperty('site'); $dbCacheFile = $app->getGlobalProperty('runDataDirectory') . "/cache-{$site}.sqlite"; return new \QpCms\CmsThemeAssistant($app, new \ThanksToQp\TagCache\TagCache(['dsn' => "sqlite:{$dbCacheFile}", 'initCommands' => ['pragma foreign_keys = on']], "qpcms-{$site}", new \QpCms\CmsTagCacheAssistant($app))); }); $conf->addRoutes([['method' => 'POST', 'route' => ['/backend/'], 'action' => ['command' => 'backend']], ['method' => 'GET', 'partialRoute' => ['/static/'], 'action' => ['globalCommand' => 'staticFiles', 'parameters' => ['baseDir' => $parameters['themeStaticDirectory']]]], ['method' => 'GET', 'partialRoute' => ['/medias/'], 'action' => ['globalCommand' => 'staticFiles', 'parameters' => ['baseDir' => $parameters['mediasDirectory']]]], ['method' => 'GET', 'route' => ['category' => '/.*/'], 'trailingSlash' => true, 'action' => ['globalTheme' => $parameters['themeName'], 'template' => 'term', 'variant' => ['type' => 'category']]], ['method' => 'GET', 'route' => ['category' => '/.*/', 'article' => '/.*/'], 'action' => ['globalTheme' => $parameters['themeName'], 'template' => 'article']]])->set404(['method' => 'GET', 'action' => ['globalTheme' => $parameters['themeName'], 'template' => '404']]); $conf->on('APP_READY', function (\QPress\ApplicationTool $app) { \QPress\Debug::log('app ' . $app->getProperty('name') . ': READY'); }); });
/** * @param array $parentRes array{TagCacheResource} * @param string $childFragment * @return array|null If a $childFragment is defined, return the array{TagCacheResource} that match with the $childFragment */ private function insertPathChildren(array $parentRes, $childFragment = null) { $matchingChild = null; $children = $this->assistant->getChildResources($parentRes['code']); Debug::log("[ASSISTANT] children of " . $parentRes['code'] . ": " . var_export($children, true)); Helper::formatArray($children, ['collection' => 'list', 'fields' => ['code' => ['validator' => 'is_string'], 'fragment' => []]]); $this->qi->insert(['insertInto' => 'res_parent_sync', 'values' => ['res_id' => [$parentRes['resId'], PDO::PARAM_INT]]])->execute(); foreach ($children as $child) { $childPath = self::childPath($parentRes['path'], $child['fragment']); $childResId = $this->insertChildResource($child['code'], $childPath, $child['fragment'], $parentRes['resId']); if ($childFragment === $child['fragment']) { $matchingChild = ['resId' => $childResId, 'code' => $child['code'], 'path' => $childPath]; } } if (isset($childFragment) && !isset($matchingChild)) { throw new Error("Missing child \"{$childFragment}\" in resource: " . $parentRes['code']); } return $matchingChild; }