/** * @param integer $id * @return array * @throws \yii\web\NotFoundHttpException */ public function run($id) { /** @var \cookyii\modules\Feed\resources\FeedItem\Model $Model */ $Model = $this->findModel($id); $result = $Model->toArray([], ['picture_300', 'sections', 'meta']); $result['published_at'] = empty($Model->published_at) ? null : Formatter()->asDatetime($Model->published_at, 'dd.MM.yyyy HH:mm'); $result['archived_at'] = empty($Model->archived_at) ? null : Formatter()->asDate($Model->archived_at, 'dd.MM.yyyy'); $result['hash'] = sha1(serialize($result)); return $result; }
/** * @inheritdoc */ public function restoreDump($variant) { $time = strtotime($variant); $date = Formatter()->asDatetime($time, 'dd MMM yyyy HH:mm'); $this->controller->stdout(" > Selected backup {$date}.\n"); $path = implode(DIRECTORY_SEPARATOR, [\Yii::getAlias($this->controller->backupPath, false), str_replace(' ', DIRECTORY_SEPARATOR, $variant)]); $schema = $path . DIRECTORY_SEPARATOR . 'schema.sql'; $schemaConfirm = false; if (!file_exists($schema)) { $this->controller->stdout(" > Schema dump not found.\n"); } elseif (!is_readable($schema)) { $this->controller->stdout(" > Schema dump not readable.\n"); } else { $schemaConfirm = $this->controller->confirm(' > Do you want restore schema?'); if (!$schemaConfirm) { $this->controller->stdout(" > Schema dump skipped.\n"); } } $data = $path . DIRECTORY_SEPARATOR . 'data.sql'; $dataConfirm = false; if (!file_exists($data)) { $this->controller->stdout(" > Data dump not found.\n"); } elseif (!is_readable($data)) { $this->controller->stdout(" > Data dump not readable.\n"); } else { $dataConfirm = $this->controller->confirm(' > Do you want restore data?'); if (!$dataConfirm) { $this->controller->stdout(" > Data dump skipped.\n"); } } if ($schemaConfirm) { $this->controller->stdout(" > Restoring schema... "); $time = microtime(true); $cmd = sprintf('mysql --defaults-extra-file=%s %s %s < %s', $this->controller->getCredentialsFile(), implode(' ', $this->controller->restoreKeys), $this->controller->credentials['database'], $schema); $this->controller->stdout(" > REST SCHEMA ------------------\n"); $this->controller->stdout($cmd . "\n"); $this->controller->stdout(" > ------------------------------\n"); passthru($cmd); $time = sprintf('%.3f', microtime(true) - $time); $this->controller->stdout(" > END (time: {$time}s) ---------------\n"); } if ($dataConfirm) { $this->controller->stdout(" > Restoring data... "); $time = microtime(true); $cmd = sprintf('mysql --defaults-extra-file=%s %s %s < %s', $this->controller->getCredentialsFile(), implode(' ', $this->controller->restoreKeys), $this->controller->credentials['database'], $data); $this->controller->stdout(" > REST DATA --------------------\n"); $this->controller->stdout($cmd . "\n"); $this->controller->stdout(" > ------------------------------\n"); passthru($cmd); $time = sprintf('%.3f', microtime(true) - $time); $this->controller->stdout(" > END (time: {$time}s) ---------------\n"); } }
/** * @param string $slug * @return array * @throws \yii\web\NotFoundHttpException */ public function run($slug) { /* @var $modelClass \cookyii\modules\Feed\resources\FeedSection\Model */ $modelClass = $this->modelClass; $Query = $modelClass::find(); $Model = $Query->bySlug($slug)->one(); if (empty($Model)) { throw new \yii\web\NotFoundHttpException("Object not found: {$slug}"); } $result = $Model->toArray([], ['meta']); $result['published_at'] = empty($result['published_at']) ? null : Formatter()->asDatetime($result['published_at'], 'dd.MM.yyyy HH:mm'); $result['archived_at'] = empty($result['archived_at']) ? null : Formatter()->asDate($result['archived_at'], 'dd.MM.yyyy'); $result['hash'] = sha1(serialize($result)); return $result; }
/** * @return string * @throws \yii\console\Exception */ protected function prepareDump() { if (empty($this->path)) { $path = implode(DIRECTORY_SEPARATOR, [\Yii::getAlias($this->controller->backupPath, false), Formatter()->asDate(time(), 'yyyy-MM-dd'), Formatter()->asTime(time(), 'HH:mm:ss')]); if (!file_exists($path)) { FileHelper::createDirectory($path); } if (!file_exists($path) || !is_dir($path)) { throw new \yii\console\Exception('Backup path not found.'); } if (!is_readable($path)) { throw new \yii\console\Exception('Backup path is not readable.'); } if (!is_writable($path)) { throw new \yii\console\Exception('Backup path is not writable.'); } $this->path = $path; } return $this->path; }
/** * @inheritdoc */ public function fields() { $fields = parent::fields(); unset($fields['password_hash'], $fields['token'], $fields['auth_key']); $fields['created_at_format'] = function (Account $Model) { return Formatter()->asDatetime($Model->created_at); }; $fields['updated_at_format'] = function (Account $Model) { return Formatter()->asDatetime($Model->updated_at); }; $fields['deleted_at_format'] = function (Account $Model) { return Formatter()->asDatetime($Model->deleted_at); }; $fields['activated_at_format'] = function (Account $Model) { return Formatter()->asDatetime($Model->activated_at); }; $fields['avatar'] = [$this, 'getAvatar']; $fields['deleted'] = [$this, 'isDeleted']; $fields['activated'] = [$this, 'isActivated']; return $fields; }
public function testMain() { ob_start(); dump(['test' => rand()]); // это тест! expect('Функция дампа не отдает результат', ob_get_clean())->notEmpty(); $this->assertInstanceOf(yii\log\Dispatcher::class, YiiLog()); $this->assertInstanceOf(yii\db\Connection::class, DB()); $this->assertInstanceOf(yii\base\ErrorHandler::class, ErrorHandler()); $this->assertInstanceOf(yii\caching\Cache::class, Cache()); $this->assertInstanceOf(yii\i18n\Formatter::class, Formatter()); $this->assertInstanceOf(yii\base\View::class, View()); $this->assertInstanceOf(yii\i18n\I18N::class, I18N()); $this->assertInstanceOf(yii\rbac\ManagerInterface::class, AuthManager()); $this->assertInstanceOf(yii\web\AssetManager::class, AssetManager()); $this->assertInstanceOf(yii\web\User::class, User()); $this->assertInstanceOf(yii\base\Request::class, Request()); $this->assertInstanceOf(yii\base\Response::class, Response()); $this->assertInstanceOf(yii\web\Session::class, Session()); $this->assertInstanceOf(yii\web\UrlManager::class, UrlManager()); $this->assertInstanceOf(yii\mail\MailerInterface::class, Mailer()); $this->assertInstanceOf(services\File\Service::class, FileService()); $this->assertInstanceOf(services\Activity\Service::class, ActivityService()); }
/** * @return string */ public function getUpdatedAt() { return Formatter()->asRelativeTime($this->Model->updated_at); }
/** * @param array $backups * @param int $offset * @param int $limit * @return string|false */ protected function selectBackup(array $backups, $offset = 0, $limit = 8) { $total = count($backups); $current_backups = []; if ($offset > 0) { $current_backups[1] = 'Show prev backups'; } else { $current_backups[1] = '----'; } $slice = array_slice($backups, $offset, $limit); if (!empty($slice)) { foreach ($slice as $row) { $current_backups[] = $row; } } if (count($current_backups) + $offset < $total) { $current_backups[0] = 'Show next backups'; } else { $current_backups[0] = '----'; } foreach ($current_backups as $key => $variant) { $time = strtotime($variant); if ($time) { $date = Formatter()->asDatetime($time, 'dd MMM yyyy HH:mm'); $this->stdout(" {$key} => {$date}\n"); } else { $this->stdout(" {$key} => {$variant}\n"); } } $this->stdout("\n"); $selected = $this->select('Select backup to restore', $current_backups); $variant = $current_backups[$selected]; if ($variant === 'Show prev backups') { $this->stdout("\n"); $this->selectBackup($backups, $offset - $limit, $limit); } elseif ($variant === 'Show next backups') { $this->stdout("\n"); $this->selectBackup($backups, $offset + $limit, $limit); } elseif ($variant === '----') { $this->stdout("Exit.\n"); } else { $this->stdout("\n"); return $variant; } return false; }
<?php /** * _general_publishing.php * @author Revin Roman * @link https://rmrevin.com * * @var yii\web\View $this * @var cookyii\widgets\angular\ActiveForm $ActiveForm * @var cookyii\modules\Feed\backend\forms\ItemEditForm $ItemEditForm */ echo $ActiveForm->field($ItemEditForm, 'published_at')->textInput(['ng-datetime-picker' => true, 'placeholder' => Formatter()->asDatetime(time(), 'dd.MM.yyyy HH:mm')]); echo $ActiveForm->field($ItemEditForm, 'archived_at')->textInput(['ng-date-picker' => true, 'ng-date-start' => 'data.published_at', 'placeholder' => Yii::t('cookyii.feed', 'Never')]);
/** * @param array $fields * @param string $attribute * @param array|null $formats */ public static function datetimeFormat(array &$fields, $attribute, $formats = []) { $formats = empty($formats) && $formats !== null ? static::$datetimeFormats : $formats; if (empty($formats)) { $fields[$attribute] = function (\yii\db\BaseActiveRecord $Model) use($attribute) { return $Model->hasAttribute($attribute) ? (string) $Model->getAttribute($attribute) : null; }; } else { $fields[$attribute] = function (\yii\db\BaseActiveRecord $Model) use($attribute, $formats) { $result = []; $value = $Model->hasAttribute($attribute) ? (string) $Model->getAttribute($attribute) : null; $value = empty($value) ? time() : Formatter()->asTimestamp($value); $is_milliseconds = mb_strlen($value, 'utf-8') === 13; $value = $is_milliseconds ? floor($value / 1000) : $value; foreach ($formats as $key => $format) { switch ($format) { case 'raw': $result[$key] = empty($value) ? null : $value; break; case 'relative': $result[$key] = empty($value) ? \Yii::t('yii', '(not set)') : Formatter()->asRelativeTime($value); break; default: $result[$key] = empty($value) ? \Yii::t('yii', '(not set)') : Formatter()->asDatetime($value, $format); break; } } return $result; }; } }
/** * @return string */ protected function getTime() { return Formatter()->asTime(time(), 'HH:mm:ss'); }
<?php /** * _general_publishing.php * @author Revin Roman * @link https://rmrevin.com * * @var yii\web\View $this * @var cookyii\widgets\angular\ActiveForm $ActiveForm * @var cookyii\modules\Feed\backend\forms\SectionEditForm $SectionEditForm */ echo $ActiveForm->field($SectionEditForm, 'published_at')->textInput(['ng-datetime-picker' => true, 'placeholder' => Formatter()->asDatetime(time(), 'dd.MM.yyyy HH:mm')]); echo $ActiveForm->field($SectionEditForm, 'archived_at')->textInput(['ng-date-picker' => true, 'ng-date-start' => 'data.published_at', 'placeholder' => Formatter()->asDate(time() + 86400 * 180, 'dd.MM.yyyy')]);