| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\commands;
- use yii\console\Controller;
- use yii\console\ExitCode;
- /**
- * copyright command
- * @package app\commands
- * @author Syan mzsongyan@gmail.com
- * @date 2022-05-30
- */
- class CopyrightController extends Controller
- {
- /**
- * 批量更新或添加文件版权信息
- * @return int
- * @author Syan mzsongyan@gmail.com
- * @date 2022-05-30
- */
- public function actionUpdate()
- {
- $allFile = $this->allFiles(\Yii::$app->basepath, '*.php', [\Yii::$app->basepath . '/vendor']);
- foreach ($allFile as $file) {
- $this->addCopyright($file);
- }
-
- return ExitCode::OK;
- }
- /**
- *
- * @param mixed $file
- * @return bool
- * @author Syan mzsongyan@gmail.com
- * @date 2022-05-30
- */
- private function addCopyright($file)
- {
- $content = file_get_contents($file);
- if ($content === false) {
- return false;
- }
- $copyright = <<<COPYRIGHT
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- COPYRIGHT;
- $content = preg_replace_callback('/\<\?php(\s+)?(\/\*\*.*?\*\/)?/mis', function ($matches) use ($copyright) {
- $result = '<?php' . PHP_EOL . $copyright;
- if (!isset($matches[2])) {
- $result .= PHP_EOL;
- }
- return $result;
- }, $content);
- file_put_contents($file, $content, LOCK_EX);
- return true;
- }
- /**
- * 根据给出的路径获取所有相关文件
- * @param string $dir
- * @param string $pattern
- * @param array $exclude 排除路径
- * @return array
- * @author Syan mzsongyan@gmail.com
- * @date 2022-05-30
- */
- private function allFiles(string $dir, string $pattern = '*', array $exclude = [])
- {
- $result = [];
- $items = glob($dir . '/' . $pattern, GLOB_BRACE);
- foreach ($items as $item) {
- if (is_file($item)) {
- $result[] = $item;
- }
- }
- $items = glob($dir . '/*', GLOB_ONLYDIR);
- foreach ($items as $item) {
- if (is_dir($item)) {
- if (in_array($item, $exclude)) {
- continue;
- }
- $result = array_merge($result, $this->allFiles($item, $pattern));
- }
- }
- return $result;
- }
- }
|