j-stam/magento2-shell
Composer 安装命令:
composer require j-stam/magento2-shell
包简介
Adds magento 1 shell like functionallity
README 文档
README
Creating a new console action can be overkill from time to time. Sometimes we just want to run a simple script on our magento application for example to test some code or run a one time action.
This package allows you to run actions on your magento installation like we did on magento 1.
Install
Install the package
composer require j-stam/magento2-shell:*
Copy the shell folder from this package to your magento 2 root
Usage
Set Area code
The area code defaults to global.
To use a different area code you can set it by overwriting the protected $appAreaCode; in your shell script
The following areas can be set:
\Magento\Framework\App\Area::AREA_GLOBALor'global'\Magento\Framework\App\Area::AREA_FRONTENDor'frontend'\Magento\Framework\App\Area::AREA_ADMINHTMLor'adminhtml'\Magento\Framework\App\Area::AREA_DOCor'doc'\Magento\Framework\App\Area::AREA_CRONTABor'crontab'\Magento\Framework\App\Area::AREA_WEBAPI_RESTor'webapi_rest'\Magento\Framework\App\Area::AREA_WEBAPI_SOAPor'webapi_soap'\Magento\Framework\App\Area::AREA_GRAPHQLor'graphql'
Set Is Secure
Some magento actions for instance removing products are required to be executed in a secure area next to being executed
in the adminhtml area. Use protected function setIsSecureArea($isSecure) to set wether the current area is secure or not.
Instantiate object
- Create a instance:
public function createInstance($type, array $arguments = []) - Get a instance:
public function getInstance($type) - Get the object manager:
public function getObjectManager()
Use dependency injection.
The dependency injection uses the public function getInstance($type) to load your dependency.
If you want to create a new instance, inject a factory for the object you are trying to create and use its create method
or use the createInstance method instead of dependency injection
protected function di( \Magento\Catalog\Model\Product $product ) { $this->product = $product; }
Export data to csv, xml, json
$this->io->writeJson($data, 'filename.json')
Read data from csv, xml, json
$this->io->readJson('filename.json')
Excecute sql querys
You can run sql queries with the standard \Magento\Framework\DB\Adapter\AdapterInterface
$sql = 'SELECT * FROM your_table LIMIT 1'; $result = $this->connection->fetchAll($sql); // or $query = $this->connection->query('SELECT * FROM your_table LIMIT 1'); $result = $query->fetchAll();
Write output to a log file
You can write output to a log file using monologger. To set your log file name overwrite the protected $logFileName.
When this variable is not set, your class name will be converted from PascalCase to lowercase seperated with dashes.
To write output to your log use the protected $logger
$this->logger->error('Your error message'); $this->logger->info('Your info message'); ...
All log files are placed in {magento_root}/var/log/shell/ by default. To specify your own log file path overwrite the protected $logFilePath variable.
Write output to terminal
Available in the shell is the symfony console output class.
This can be used directly by accessing the protected $consoleOutput. Or if you just want to write (a line) you can use the
public functions write($messages, $newLine = true, $options = OutputInterface::OUTPUT_NORMAL) and writeln($message, $options = OutputInterface::OUTPUT_NORMAL)
$this->writeln('Your line'); $this->write('Your message') $this->write(['Your message', 'Your other message'])
Example script
<?php require_once realpath(dirname(__FILE__) . '/../app/bootstrap.php'); class Example extends \Stam\Shell\ShellAbstract { protected $product; /** * Optional function, can be used for dependency injection */ protected function di( \Magento\Catalog\Model\Product $product ) { $this->product = $product; } public function run() { $this->writeln('Hello world!'); $this->logger->debug('Hello world!'); } } $shell = new Example(); $shell->run();
j-stam/magento2-shell 适用场景与选型建议
j-stam/magento2-shell 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.6k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 07 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 j-stam/magento2-shell 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 j-stam/magento2-shell 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 27.6k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-31