Jiliuke

激流客

1.同步生产站点的静态内容数据

例如首页幻灯片,contact-us页面内容等,需要同步的表如下

1
2
3
4
5
6
7
8
cms_block
cms_block_store
cms_page
cms_page_store
sequence_cms_block
sequence_cms_page
staging_update
flag //其中的staging的值必须在staging_update中存在

1.在自定义dom上显示数据loading状态

define 引用 loader 组件并初始化为loader变量,接着设置加载icon,然后调用show显示加载中,调用hide隐藏加载中。

1
2
3
4
5
6
7
8
9
10
11
12
13
define([
...
'loader',
...
],function(...,loader,...){
let overviewContent = $(this).parent().find('.overview-content');

overviewContent.loader({icon: self.options.loadingImg});
overviewContent.loader('show');//显示loader
...
overviewContent.loader('hide');//数据加载完成后隐藏loader

})

参考链接
https://developer.adobe.com/commerce/frontend-core/javascript/jquery-widgets/loader/

2.在Magento2 Js 中再加载js组件

1
2
3
4
5
6
7
define([
...
'mage/apply/main',
...
],function(...,main,...){
main.applyFor('.content-card','{}','swiperOverviewImages');
})

参考链接
https://fishpig.co.uk/magento/tutorials/manually-load-js-component-magento-2/

最新的 XHGUI 用法

https://github.com/michaeldyl520/xhgui

最新的自修改xhgui搭配mongodb用法如下

  1. 在程序入口index.php添加以下代码即可开始分析
    1
    2
    3
    4
    5
    require_once '/path/to/xhgui/vendor/perftools/php-profiler/autoload.php';

    $config = require_once '/path/to/xhgui/config/config.php';
    $profiler = new \Xhgui\Profiler\Profiler($config);
    $profiler->start();
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    <?php
    /**
    * Default configuration for PHP Profiler.
    *
    * config.php
    * To change these, create a file called `config.php` file in the same directory
    * and return an array from there with your overriding settings.
    */

    use Xhgui\Profiler\Profiler;
    use Xhgui\Profiler\ProfilingFlags;

    return array(
    'save.handler' => Profiler::SAVER_STACK,
    'save.handler.stack' => array(
    'savers' => array(
    Profiler::SAVER_UPLOAD
    ),
    'saveAll' => false,
    ),
    'save.handler.upload' => array(
    'url' => 'https://www.xhgui.local/run/import',
    // The timeout option is in seconds and defaults to 3 if unspecified.
    'timeout' => 3,
    // the token must match 'upload.token' config in XHGui
    'token' => '',
    ),
    'profiler.enable' => function () {
    return true;
    },
    'profiler.flags' => array(
    ProfilingFlags::CPU,
    ProfilingFlags::MEMORY,
    ProfilingFlags::NO_BUILTINS,
    ProfilingFlags::NO_SPANS,
    ),
    'profiler.options' => array(),
    'profiler.exclude-env' => array(),
    'profiler.simple_url' => function ($url) {
    return preg_replace('/=\d+/', '', $url);
    },
    'profiler.replace_url' => null,
    );

Composer 小技巧

1. 自定义安装包的方法

adobe 官方已不支持stripe-payment 3.2.8,需要用composer 自定义的方式安装包
composer配置方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
......
"repositories": {
"0":{
"type": "composer",
"url": "https://repo.magento.com/"
},
"1": {
"type": "package",
"package": {
"name": "stripe/module-payments",
"version": "3.2.8",
"dist": {
"url": "https://github.com/stripe/stripe-magento2-releases/raw/master/stripe-magento2-3.2.8.tgz",
"type": "tar"
},
"autoload":{
"files": [
"code/StripeIntegration/Payments/registration.php"
],
"psr-4": {
"StripeIntegration\\Payments\\": "code/StripeIntegration/Payments/"
}
}
}
}
},
......
}

然后终端中在项目目录下使用 composer require stripe/module-payments:3.2.8 安装。

安装代码标准

使用magento2官方的代码标准,如果需要在开发工具中提示代码规范的问题,需要在安装magento2后执行以下shell

1
vendor/bin/phpcs --config-set installed_paths vendor/magento/magento-coding-standard,vendor/phpcompatibility/php-compatibility/PHPCompatibility

PhpStorm 自动格式化的一些配置

自动对齐的问题

高版本PhpStorm Function如果有多个变量并换行,会自动两边对齐属性,不符合代码规范,需要在Settings>Editor>Code Style>PHP->Wrapping and Braces->Function declaration parameters中去掉Align when multiline的勾选框且勾选Keep ')' and '{' on one line

自动对齐的问题

强制限定类型的提示问题

Magento2 PHP不需要做强制类型限定参照图去掉以下两个复选框。

强制限定类型的提示问题

eslint 搭法

eslint 搭法
eslint 搭法

phpcs搭法
最新的vendor如果没有包含phpcs等检测工具,我们可以间接使用默认的2.4.5-p1中的phpcs进行项目的检测

自动设置token

为了简化操作,有时候需要postman在请求接口后自动设置token,方法如下
打开postman,在一个请求下面点击Tests,输入以下代码

1
2
var responseData = responseBody;
pm.environment.set("token", responseData.replace(/\"/g,''));

在debian查看SSD寿命

  1. 查看Wear_Leveling_CountSSD_Life_Left
    1
    sudo smartctl -a /dev/sdb
    不同的SSD使用smartctl展示不同

wear leveling count是 磨损均衡计数(WLC)的意思,当前值代表的剩余耐力,以百分比表示,意味着它从100开始减少。

SSD_Life_Left代表着SSD的生命周期还剩下多少

  1. 查看 Percentage Used Endurance Indicator
    1
    sudo smartctl -l devstat /dev/sdb
    最重要和直观Percentage Used Endurance Indicator,单位是百分比,新的是0%

实际应用中会出现需要减少导出的备份体积,然后在本地导入再开发,可以大幅缩短导入等待时间,方法如下

1
sed '/INSERT INTO `tablelog1`/d' xxxx.sql | sed '/INSERT INTO `tablelog2`/d' | sed '/INSERT INTO `tablelog3`/d' | sed '/INSERT INTO `tablelog4`/d' | sed '/INSERT INTO `tablelog5`/d' > reduced.sql

一些有用mysql tips

1
2
3
4
5
6
7
8
#统计所有数据库从大到小排列
select TABLE_SCHEMA,round(SUM(DATA_LENGTH + INDEX_LENGTH)/1024/1024,2) as data from information_schema.tables GROUP BY TABLE_SCHEMA order by data desc;
#统计数据库大小
select round(SUM(DATA_LENGTH + INDEX_LENGTH)/1024/1024,2) as data from information_schema.tables where TABLE_SCHEMA="xxxxxx";
#统计数据表大小
select table_name,round(SUM(DATA_LENGTH + INDEX_LENGTH)/1024/1024,2) as data from information_schema.tables where TABLE_SCHEMA="xxxxxx" GROUP BY table_name order by data desc;
#批量修改域名
UPDATE core_config_data SET value = replace (value,'xxx.xxx.com','xxx.xxx.local') WHERE value LIKE '%xxx.xxx.com%';

需求

magento2 在运行的时候会不断在项目中var/log中记录日志,时间久后就会产生巨大的log日志,所以有必要每天切割日志保存,利于日志分析和减少磁盘开销。

方法 (仅对于linux)

1.首先确定当前的项目名称(magento2)和项目路径(/var/www/magento2),项目执行用户www

2.sudo vim /etc/logrotate.d/magento2
3.拷贝以下内容到/etc/logrotate.d/magento2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 {
daily
missingok
rotate 365
maxage 365
compress
copytruncate
prerotate
# logrotate fails if the .1 file already exists, which only
# ever happens if the last copytruncate failed, or during the
# first run after delaycompress is disabled. When this script
# runs, older logs have already been rotated, so it's safe to
# rotate an unexpected log.1 file into log.2.gz. Sometimes the
# argument already has a .1 suffix, so strip it.
[ -e "${1%.1}.1" ] && sudo -u -g bash -c "gzip --best < \"${1%.1}.1\" > \"${1%.1}.2.gz\"" && rm "${1%.1}.1"
true
endscript
notifempty
create 0640 www www
su www www
lastaction
chown www:www -R /var/www/magento2/var/log/*.log
# fix files with broken permissions
chown www:www -R /var/www/magento2/var/log/*.log.[0-9]*.gz 2>/dev/null || true
endscript
}

4.测试

1
2
3
4
#强制执行
sudo logrotate -f /etc/logrotate.d/magento2
#调试模式
sudo logrotate -d /etc/logrotate.d/magento2
0%