fdevs / cron-bridge
Symfony 2 桥接器,允许您通过配置来配置 crontab 运行控制台命令。
0.1.1
2016-05-14 22:07 UTC
Requires
- php: >=5.4
- fdevs/cron: ~0.1
Requires (Dev)
- symfony/console: ~2.8|~3.0
Suggests
- symfony/console: use with symfony console
This package is auto-updated.
Last update: 2024-08-28 18:43:25 UTC
README
通过 PHP 轻松配置 crontab。
设置和配置
FDevsCron 使用 Composer,请访问 Composer 网站 获取更多信息。
以下简单的命令将安装 cron-bridge
到您的项目中。它还会在您的 composer.json
中添加一个新条目并更新 composer.lock
。
$ composer require fdevs/cron-bridge
FDevsCron 遵循 PSR-4 规范的类名命名约定,这意味着您可以轻松地将
cron
类加载集成到您自己的自动加载器中。
与 Symfony 框架 一起使用
###在内核中启用包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new FDevs\Bridge\Cron\FDevsCronBundle(), // ... ); }
###添加命令
#app/config/config.yml f_devs_cron: commands: swiftmailer: command: 'swiftmailer:spool:sends' bin: executor: '/usr/bin/php' command: 'your_best_command'
默认配置
# Default configuration for extension with alias: "f_devs_cron" f_devs_cron: exporter: key: f_devs_cron # Example: generated mailto: ~ # Example: cron@example.com path: '/usr/local/bin:/usr/bin:/bin' # Example: /usr/local/bin:/usr/bin:/bin executor: php # Example: php console: bin/console # Example: bin/console(symfony 3.0) shell: ~ # Example: /bin/sh commands: # Prototype name: command: ~ # Required, Example: swiftmailer:spool:send minute: '*' # Example: */5 - Every 5 minutes hour: '*' # Example: 8 - 5 minutes past 8am every day day_of_week: '*' # Example: 0 - 5 minutes past 8am every Sunday day: '*' # Example: 1 - 5 minutes past 8am on first of each month month: '*' # Example: 1 - 5 minutes past 8am on first of of January log_file: ~ # Example: %kernel.logs_dir%/%kernel.environment%_cron.log error_file: ~ # Example: %kernel.logs_dir%/%kernel.environment%_error.log params: '' # Example: --color=red # add if use custom executor executor: ~ # Example: /usr/bin/php
###使用控制台命令
$ bin/console fdevs:cron:dump $ bin/console fdevs:cron:replace $ bin/console fdevs:cron:delete
与 控制台组件 一起使用
#!/usr/bin/env php <?php // application.php require __DIR__.'/vendor/autoload.php'; use FDevs\Bridge\Cron\Command\DeleteCommand; use FDevs\Bridge\Cron\Command\DumpCommand; use FDevs\Bridge\Cron\Command\ReplaceCommand; use FDevs\Cron\Cron; use FDevs\Cron\CrontabUpdater; use Symfony\Component\Console\Application; $cron = new Cron() // $cron configuration... $crontabUpdater = new CrontabUpdater('uniquie_key'); $application = new Application(); $application->add(new ReplaceCommand('cron:replace', $cron, $crontabUpdater)); $application->add(new DumpCommand('cron:dump', $cron)); $application->add(new DeleteCommand('cron:delete', $crontabUpdater)); $application->run();
在控制台中使用
$ php application.php cron:replace $ php application.php cron:dump $ php application.php cron:delete
与 依赖注入组件 一起使用
<?php use Symfony\Component\DependencyInjection\ContainerBuilder; use FDevs\Bridge\Cron\DependencyInjection\FDevsCronExtension; use FDevs\Bridge\Cron\DependencyInjection\Compiler\CronJobPass; $container = new ContainerBuilder(); // $container configuration... $container->registerExtension(FDevsCronExtension()); $container->addCompilerPass(new CronJobPass()); $updater = $container->get('f_devs_cron.crontab_updater'); $cron = $container->get('f_devs_cron.cron'); $updater->replace($cron); echo strval($cron);