├── .gitignore ├── CONTRIBUTING.md ├── phpcsdocblocks.xml ├── .travis.yml ├── phpcsforbiddenfunctions.xml ├── composer.json ├── src ├── Events │ ├── CommandStarting.php │ └── CommandTerminating.php └── Command.php ├── LICENSE.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | composer.lock 3 | vendor 4 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing # 2 | 3 | Please feel free to contribute by reporting bugs, suggesting features etc... by opening an issue. 4 | 5 | Also please don't hesitate to send any pull requests. -------------------------------------------------------------------------------- /phpcsdocblocks.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | My custom coding standard. 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.6 5 | 6 | before_script: 7 | - travis_retry composer self-update 8 | - travis_retry composer install --no-interaction --prefer-source --dev 9 | 10 | script: 11 | - vendor/bin/phpmd src text codesize,unusedcode,naming 12 | - vendor/bin/phpcs --standard=psr2 src 13 | - vendor/bin/phpcs -n --standard=phpcsdocblocks.xml src 14 | - vendor/bin/phpcs -n --standard=phpcsforbiddenfunctions.xml src 15 | -------------------------------------------------------------------------------- /phpcsforbiddenfunctions.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Forbidden Functions 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bmitch/consoleevents", 3 | "description": "Events for Laravel Console Commands", 4 | "authors": [ 5 | { 6 | "name": "Bill Mitchell", 7 | "email": "wkmitch@gmail.com" 8 | } 9 | ], 10 | "license": "MIT", 11 | "require": { 12 | "php": ">=5.6.4" 13 | }, 14 | "autoload": { 15 | "psr-4": { 16 | "Bmitch\\ConsoleEvents\\": "src/" 17 | } 18 | }, 19 | "require-dev": { 20 | "phpmd/phpmd": "^2.5", 21 | "squizlabs/php_codesniffer": "^2.7" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Events/CommandStarting.php: -------------------------------------------------------------------------------- 1 | command = $command; 30 | $this->input = $input; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Events/CommandTerminating.php: -------------------------------------------------------------------------------- 1 | command = $command; 40 | $this->input = $input; 41 | $this->exitCode = $exitCode; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Bill Mitchell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Command.php: -------------------------------------------------------------------------------- 1 | events = \App::make(Dispatcher::class); 34 | } 35 | 36 | /** 37 | * Runs the command. 38 | * @param InputInterface $input Input Interface. 39 | * @param OutputInterface $output Output Interface. 40 | * @return integer 41 | */ 42 | public function run(InputInterface $input, OutputInterface $output) 43 | { 44 | $this->events->fire( 45 | new Events\CommandStarting($this, $input) 46 | ); 47 | 48 | $this->startTimer(); 49 | $exitCode = parent::run($input, $output); 50 | $this->endTimer(); 51 | 52 | $this->events->fire( 53 | new Events\CommandTerminating($this, $input, $exitCode) 54 | ); 55 | 56 | return $exitCode; 57 | } 58 | 59 | /** 60 | * Sets the start time of the command. 61 | * @return void 62 | */ 63 | protected function startTimer() 64 | { 65 | $this->startTime = microtime(true); 66 | } 67 | 68 | /** 69 | * Sets the end time of the command. 70 | * @return void 71 | */ 72 | protected function endTimer() 73 | { 74 | $this->executionTime = microtime(true) - $this->startTime; 75 | } 76 | 77 | /** 78 | * Get the execution time of the command. 79 | * @return float 80 | */ 81 | public function getExecutionTime() 82 | { 83 | return $this->executionTime; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Console Events for Laravel Commands 2 | [![Build Status](https://travis-ci.org/bmitch/consoleEvents.svg?branch=master)](https://travis-ci.org/bmitch/consoleEvents) 3 | 4 | ## What is it? ## 5 | This package allows you to have events triggered by your Artisan Commands. The events available are: 6 | 7 | `Bmitch\ConsoleEvents\Events\CommandStarting` 8 | Triggered when an Artisan Command is starting. 9 | 10 | `Bmitch\ConsoleEvents\Events\CommandTerminating` 11 | Triggered when an Artisan Command is terminating. 12 | 13 | ## Why use it? ## 14 | The main reason I created this package was for a use case where multiple commands were executed nightly and I wanted an easy way to log when they started and stopped. By hooking into these events it makes it easy. 15 | 16 | ## How to Install ## 17 | 18 | ### Add to composer ### 19 | ``` 20 | composer require bmitch/consoleevents 21 | ``` 22 | 23 | ### Modify commands to extend custom class ### 24 | In any command that you wish to trigger these events simply replace the: 25 | ``` 26 | use Illuminate\Console\Command; 27 | ``` 28 | 29 | with 30 | ``` 31 | use Bmitch\ConsoleEvents\Command; 32 | ``` 33 | 34 | ### Create and Register Listeners ### 35 | Create two listeners within the `app/Listeners` folder like this: 36 | ``` 37 | command->getName(); 55 | Log::info("Command {$name} starting"); 56 | } 57 | } 58 | ``` 59 | ``` 60 | command; 78 | $name = $command->getName(); 79 | 80 | Log::info("Command {$name} stopping", [ 81 | 'commandName' => $name, 82 | 'executionTime' => $command->getExecutionTime(), 83 | 'exitCode' => $commandTerminatingEvent->exitCode, 84 | ]); 85 | } 86 | } 87 | ``` 88 | 89 | Then register it within the `app\Providers\EventServiceProvider.php` class: 90 | 91 | ``` 92 | /** 93 | * The event listener mappings for the application. 94 | * 95 | * @var array 96 | */ 97 | protected $listen = [ 98 | 'Bmitch\ConsoleEvents\Events\CommandStarting' => [ 99 | 'App\Listeners\CommandStartingListener', 100 | ], 101 | 'Bmitch\ConsoleEvents\Events\CommandTerminating' => [ 102 | 'App\Listeners\CommandTerminatingListener', 103 | ], 104 | ]; 105 | ``` 106 | 107 | ## Seeing the results ## 108 | Run your command and check `laravel.log`. You should see an entry that was triggered by the `CommandStartingListener`. 109 | 110 | Something like: 111 | ``` 112 | [2016-12-02 00:16:11] local.INFO: Command foo:bar starting 113 | [2016-12-02 00:16:11] local.INFO: Command foo:bar stopping {"commandName":"foo:bar","executionTime":0.005375862121582,"exitCode":0} 114 | ``` 115 | 116 | ## Additional Methods ## 117 | The `Bmitch\ConsoleEvents\Command` class automatically tracks how long it takes to execute and provides a `getExecutionTime()` method to make it easy to add this data when Logging data. 118 | 119 | ## Contributing ## 120 | Please see [CONTRIBUTING.md](CONTRIBUTING.md) 121 | 122 | ## License ## 123 | 124 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 125 | --------------------------------------------------------------------------------