From 3a59b66d7803670cef5f0850a193750ed418b18c Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Tue, 23 May 2017 23:01:31 +0700 Subject: [PATCH 1/3] Removed PollCommand. --- src/PHPCensor/Command/PollCommand.php | 95 --------------------------- src/PHPCensor/Console/Application.php | 2 - 2 files changed, 97 deletions(-) delete mode 100644 src/PHPCensor/Command/PollCommand.php diff --git a/src/PHPCensor/Command/PollCommand.php b/src/PHPCensor/Command/PollCommand.php deleted file mode 100644 index 6901369c..00000000 --- a/src/PHPCensor/Command/PollCommand.php +++ /dev/null @@ -1,95 +0,0 @@ - - */ -class PollCommand extends Command -{ - /** - * @var \Monolog\Logger - */ - protected $logger; - - public function __construct(Logger $logger, $name = null) - { - parent::__construct($name); - $this->logger = $logger; - } - - protected function configure() - { - $this - ->setName('php-censor:poll-github') - ->setDescription('Poll GitHub to check if we need to start a build.'); - } - - /** - * Pulls all pending builds from the database and runs them. - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $parser = new Parser(); - $yaml = file_get_contents(APP_DIR . 'config.yml'); - $this->settings = $parser->parse($yaml); - - $token = $this->settings['php-censor']['github']['token']; - - if (!$token) { - $this->logger->error('No GitHub token found'); - return; - } - - $buildStore = Factory::getStore('Build'); - - $this->logger->addInfo('Finding projects to poll'); - $projectStore = Factory::getStore('Project'); - $result = $projectStore->getWhere(); - $this->logger->addInfo(sprintf('Found %d projects', count($result['items']))); - - foreach ($result['items'] as $project) { - $http = new HttpClient('https://api.github.com'); - $commits = $http->get('/repos/' . $project->getReference() . '/commits', ['access_token' => $token]); - - $last_commit = $commits['body'][0]['sha']; - $last_committer = $commits['body'][0]['commit']['committer']['email']; - $message = $commits['body'][0]['commit']['message']; - - $this->logger->info(sprintf('Last commit to GitHub for %s is %s', $project->getTitle(), $last_commit)); - - if (!$project->getArchived() && ($project->getLastCommit() != $last_commit && $last_commit != "")) { - $this->logger->info('Last commit is different to database, adding new build.'); - - $build = new Build(); - $build->setProjectId($project->getId()); - $build->setCommitId($last_commit); - $build->setStatus(Build::STATUS_PENDING); - $build->setBranch($project->getBranch()); - $build->setCreated(new \DateTime()); - $build->setCommitMessage($message); - - if (!empty($last_committer)) { - $build->setCommitterEmail($last_committer); - } - $buildStore->save($build); - - $project->setLastCommit($last_commit); - $projectStore->save($project); - } - } - - $this->logger->addInfo('Finished processing builds.'); - } -} diff --git a/src/PHPCensor/Console/Application.php b/src/PHPCensor/Console/Application.php index 6b407f92..90480150 100644 --- a/src/PHPCensor/Console/Application.php +++ b/src/PHPCensor/Console/Application.php @@ -7,7 +7,6 @@ use b8\Store\Factory; use PHPCensor\Command\CreateAdminCommand; use PHPCensor\Command\CreateBuildCommand; use PHPCensor\Command\InstallCommand; -use PHPCensor\Command\PollCommand; use PHPCensor\Command\RebuildCommand; use PHPCensor\Command\RebuildQueueCommand; use PHPCensor\Command\RunCommand; @@ -105,7 +104,6 @@ class Application extends BaseApplication $this->add(new RunCommand($loggerConfig->getFor('RunCommand'))); $this->add(new RebuildCommand($loggerConfig->getFor('RunCommand'))); $this->add(new InstallCommand()); - $this->add(new PollCommand($loggerConfig->getFor('PollCommand'))); $this->add(new CreateAdminCommand($userStore)); $this->add(new CreateBuildCommand($projectStore, new BuildService($buildStore))); $this->add(new WorkerCommand($loggerConfig->getFor('WorkerCommand'))); From 5a761dcddb5d4ff2017165e2a1fd9377b9e83806 Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Tue, 23 May 2017 23:06:58 +0700 Subject: [PATCH 2/3] Removed 'using_custom_file' config option. --- src/PHPCensor/Service/BuildService.php | 4 ---- src/PHPCensor/Worker/BuildWorker.php | 15 --------------- 2 files changed, 19 deletions(-) diff --git a/src/PHPCensor/Service/BuildService.php b/src/PHPCensor/Service/BuildService.php index 03ae7e0c..61c80b5a 100644 --- a/src/PHPCensor/Service/BuildService.php +++ b/src/PHPCensor/Service/BuildService.php @@ -173,10 +173,6 @@ class BuildService 'build_id' => $build->getId(), ]; - if ($config->get('using_custom_file')) { - $jobData['config'] = $config->getArray(); - } - $pheanstalk = new Pheanstalk($settings['host']); $pheanstalk->useTube($settings['name']); $pheanstalk->put( diff --git a/src/PHPCensor/Worker/BuildWorker.php b/src/PHPCensor/Worker/BuildWorker.php index 34746300..86e55038 100644 --- a/src/PHPCensor/Worker/BuildWorker.php +++ b/src/PHPCensor/Worker/BuildWorker.php @@ -2,8 +2,6 @@ namespace PHPCensor\Worker; -use b8\Config; -use b8\Database; use b8\Store\Factory; use Monolog\Logger; use Pheanstalk\Job; @@ -95,14 +93,6 @@ class BuildWorker $this->logger->addInfo('Received build #'.$jobData['build_id'].' from Beanstalkd'); - // If the job comes with config data, reset our config and database connections - // and then make sure we kill the worker afterwards: - if (!empty($jobData['config'])) { - $this->logger->addDebug('Using job-specific config.'); - $currentConfig = Config::getInstance()->getArray(); - Database::reset(); - } - try { $build = BuildFactory::getBuildById($jobData['build_id']); } catch (\Exception $ex) { @@ -156,11 +146,6 @@ class BuildWorker // destructor implicitly call flush unset($buildDbLog); - // Reset the config back to how it was prior to running this job: - if (!empty($currentConfig)) { - Database::reset(); - } - // Delete the job when we're done: if (!empty($job)) { $this->pheanstalk->delete($job); From 7e2f63142d87c9de43a14911730d42b5e82f2e2b Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Tue, 23 May 2017 23:17:39 +0700 Subject: [PATCH 3/3] Fixed logging (Now logging autostarting without special loggerconfig.php). Issue #59. --- .gitignore | 1 - app/loggerconfig.example.php | 16 ------- docs/en/README.md | 1 - docs/en/logging.md | 59 -------------------------- src/PHPCensor/Console/Application.php | 10 ++++- src/PHPCensor/Logging/LoggerConfig.php | 17 -------- 6 files changed, 9 insertions(+), 95 deletions(-) delete mode 100644 app/loggerconfig.example.php delete mode 100644 docs/en/logging.md diff --git a/.gitignore b/.gitignore index 6df93349..a75df016 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ /vendor /composer.phar /runtime -/app/loggerconfig.php /app/config.yml /public/assets/vendor diff --git a/app/loggerconfig.example.php b/app/loggerconfig.example.php deleted file mode 100644 index df4e30d4..00000000 --- a/app/loggerconfig.example.php +++ /dev/null @@ -1,16 +0,0 @@ - function() { - return [ - new \Monolog\Handler\StreamHandler(__DIR__ . DIRECTORY_SEPARATOR . 'errors.log', \Monolog\Logger::ERROR), - ]; - }, - /** Loggers for the RunCommand */ - 'RunCommand' => function() { - return [ - new \Monolog\Handler\RotatingFileHandler(__DIR__ . DIRECTORY_SEPARATOR . 'everything', 3, \Monolog\Logger::DEBUG), - ]; - }, -]; diff --git a/docs/en/README.md b/docs/en/README.md index 9e4fbbf4..9f65030e 100644 --- a/docs/en/README.md +++ b/docs/en/README.md @@ -9,7 +9,6 @@ Getting Started * [Run builds using a worker](workers/worker.md) * [Run builds using cronjob](workers/cron.md) * [Adding PHP Censor Support to Your Projects](configuring_project.md) -* [Setting up Logging](logging.md) * Updating PHP Censor (See [README](../../README.md)) * [Configuring PHP Censor](configuring.md) diff --git a/docs/en/logging.md b/docs/en/logging.md deleted file mode 100644 index 46c20773..00000000 --- a/docs/en/logging.md +++ /dev/null @@ -1,59 +0,0 @@ -Setting up Logging -================== - -Basics ------- - -The PHP Censor codebase makes use of the [PSR3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md) logging standard. By default we use [Monolog](https://github.com/Seldaek/monolog) to handle the actual work implementing this standard. - -How to Setup Logging (For people running a PHP Censor instance) ---------------------------------------------------------------- - -The only step required to activate logging is to create a file in the root directory called loggerconfig.php with content like the following: - -```php - function () { - return [ - new \Monolog\Handler\StreamHandler('path/to/log', \Monolog\Logger::ERROR), - ]; - } -]; -``` -This file should return an array of key value pairs. Each key tells PHP Censor which command to attach the logger to (the underscore is a special value which matches all commands). For each command an array of [Monolog](https://github.com/Seldaek/monolog) handlers should be returned. In the example above we've used one that simply writes to the file system but in practise this could be any handler written for monolog. - -Once this file is created all plugins and core PHP Censor functionality should start writing to the configured handlers. - -How to write to the Log (For people creating a new plugin) ----------------------------------------------------------- - -### Using the plugin constructor to get a logger directly - -For plugin creators the simplest way to get hold of an error logger is to add a parameter to the constructor and typehint on 'Psr\Log\LoggerInterface'. The code that loads your plugin will automatically inject the logger when it sees this. For example: -```php -class ExampleLoggingPlugin implements \PHPCensor\Plugin -{ - protected $log; - - public function __construct(Psr\Log\LoggerInterface $log) - { - $this->log = $log; - } - - public function execute() - { - $this->log->notice("You'll notice this in the log"); - } -} -``` - -### Using convenience methods provided by the Builder -Your plugin can also call a couple of messages on the Builder object: - -logSuccess() -logFailure() -log() - -All calls will get piped through to the appropriate logger. diff --git a/src/PHPCensor/Console/Application.php b/src/PHPCensor/Console/Application.php index 90480150..0f4a0083 100644 --- a/src/PHPCensor/Console/Application.php +++ b/src/PHPCensor/Console/Application.php @@ -4,6 +4,8 @@ namespace PHPCensor\Console; use b8\Config; use b8\Store\Factory; +use Monolog\Handler\StreamHandler; +use Monolog\Logger; use PHPCensor\Command\CreateAdminCommand; use PHPCensor\Command\CreateBuildCommand; use PHPCensor\Command\InstallCommand; @@ -40,7 +42,13 @@ class Application extends BaseApplication { parent::__construct($name, $version); - $loggerConfig = LoggerConfig::newFromFile(APP_DIR . 'loggerconfig.php'); + $loggerConfig = new LoggerConfig([ + "_" => function() { + return [ + new StreamHandler(RUNTIME_DIR . 'console.log', Logger::DEBUG), + ]; + } + ]); $applicationConfig = Config::getInstance(); $databaseSettings = $applicationConfig->get('b8.database', []); diff --git a/src/PHPCensor/Logging/LoggerConfig.php b/src/PHPCensor/Logging/LoggerConfig.php index 79f6af30..dd029642 100644 --- a/src/PHPCensor/Logging/LoggerConfig.php +++ b/src/PHPCensor/Logging/LoggerConfig.php @@ -13,23 +13,6 @@ class LoggerConfig private $config; private $cache = []; - /** - * The filepath is expected to return an array which will be - * passed to the normal constructor. - * - * @param string $filePath - * @return LoggerConfig - */ - public static function newFromFile($filePath) - { - if (file_exists($filePath)) { - $configArray = require($filePath); - } else { - $configArray = []; - } - return new self($configArray); - } - /** * Each key of the array is the name of a logger. The value of * each key should be an array or a function that returns an