diff options
author | Thomas Steur <tsteur@users.noreply.github.com> | 2019-07-23 02:30:13 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-23 02:30:13 +0300 |
commit | a6b67113218903e62ca7b32b006469bbbc813caf (patch) | |
tree | 972ffdc52ee222132e62b293f2410c9b6abf133b | |
parent | 016f83f8e3e7b675bbab5401b5196bbea2fedac3 (diff) |
Determine in file class if the file content is the same (#14687)
* determine in file class if the file content is the same
* trigger event for each updated file
* add comment and test
* remove unneeded file
-rw-r--r-- | plugins/CustomPiwikJs/File.php | 12 | ||||
-rw-r--r-- | plugins/CustomPiwikJs/TrackerUpdater.php | 29 | ||||
-rw-r--r-- | plugins/CustomPiwikJs/tests/Integration/FileTest.php | 11 |
3 files changed, 38 insertions, 14 deletions
diff --git a/plugins/CustomPiwikJs/File.php b/plugins/CustomPiwikJs/File.php index 1f3cb50c83..ffee10ca88 100644 --- a/plugins/CustomPiwikJs/File.php +++ b/plugins/CustomPiwikJs/File.php @@ -41,11 +41,21 @@ class File } } + public function isFileContentSame($content) + { + // we determine if file content is the same in here in case a different "file" implementation needs to check + // whether multiple files are up to date + return $this->getContent() === $content; + } + public function save($content) { - if(false === file_put_contents($this->file, $content, LOCK_EX)) { + if (false === file_put_contents($this->file, $content, LOCK_EX)) { throw new AccessDeniedException(sprintf("Could not write to %s", $this->file)); } + // we need to return an array of files in case some other "File" implementation actually updates multiple files + // eg one file per trusted host + return [$this->getPath()]; } public function getContent() diff --git a/plugins/CustomPiwikJs/TrackerUpdater.php b/plugins/CustomPiwikJs/TrackerUpdater.php index 01e99131c6..c5dabad5cc 100644 --- a/plugins/CustomPiwikJs/TrackerUpdater.php +++ b/plugins/CustomPiwikJs/TrackerUpdater.php @@ -130,15 +130,18 @@ class TrackerUpdater $newContent = $this->getUpdatedTrackerFileContent(); - if ($newContent !== $this->getCurrentTrackerFileContent()) { - $this->toFile->save($newContent); - - /** - * Triggered after the tracker JavaScript content (the content of the piwik.js file) is changed. - * - * @param string $absolutePath The path to the new piwik.js file. - */ - Piwik::postEvent('CustomPiwikJs.piwikJsChanged', [$this->toFile->getPath()]); + if (!$this->toFile->isFileContentSame($newContent)) { + $savedFiles = $this->toFile->save($newContent); + foreach ($savedFiles as $savedFile) { + + /** + * Triggered after the tracker JavaScript content (the content of the piwik.js file) is changed. + * + * @param string $absolutePath The path to the new piwik.js file. + */ + Piwik::postEvent('CustomPiwikJs.piwikJsChanged', [$savedFile]); + } + } // we need to make sure to sync matomo.js / piwik.js @@ -151,9 +154,11 @@ class TrackerUpdater if (Common::stringEndsWith($this->toFile->getName(), $fromFile)) { $alternativeFilename = dirname($this->toFile->getPath()) . DIRECTORY_SEPARATOR . $toFile; $file = $this->toFile->setFile($alternativeFilename); - if ($file->hasWriteAccess() && $file->getContent() !== $newContent) { - $file->save($newContent); - Piwik::postEvent('CustomPiwikJs.piwikJsChanged', [$file->getPath()]); + if ($file->hasWriteAccess() && !$file->isFileContentSame($newContent)) { + $savedFiles = $file->save($newContent); + foreach ($savedFiles as $savedFile) { + Piwik::postEvent('CustomPiwikJs.piwikJsChanged', [$savedFile]); + } } } } diff --git a/plugins/CustomPiwikJs/tests/Integration/FileTest.php b/plugins/CustomPiwikJs/tests/Integration/FileTest.php index cc4bf232b7..da221e0f2e 100644 --- a/plugins/CustomPiwikJs/tests/Integration/FileTest.php +++ b/plugins/CustomPiwikJs/tests/Integration/FileTest.php @@ -180,6 +180,14 @@ class FileTest extends IntegrationTestCase $this->assertSame("// Hello world\nvar fooBar = 'test';", $this->makeFile()->getContent()); } + public function test_isFileContentSame() + { + $this->assertTrue($this->makeFile()->isFileContentSame("// Hello world\nvar fooBar = 'test';")); + $this->assertFalse($this->makeFile()->isFileContentSame("// Hello world\nvar foBar = 'test';")); + $this->assertFalse($this->makeFile()->isFileContentSame("// Hello world\nvar foBar = 'test'")); + $this->assertFalse($this->makeFile()->isFileContentSame("Hello world\nvar foBar = 'test'")); + } + public function test_getContent_returnsNull_IfFileIsNotReadableOrNotExists() { $this->assertNull($this->makeNotReadableFile()->getContent()); @@ -191,8 +199,9 @@ class FileTest extends IntegrationTestCase $this->assertFalse($notExistingFile->hasReadAccess()); $this->assertTrue($notExistingFile->hasWriteAccess()); - $notExistingFile->save('myTestContent'); + $updatedFile = $notExistingFile->save('myTestContent'); + $this->assertEquals([$this->dir . 'notExisTinGFile.js'], $updatedFile); $this->assertEquals('myTestContent', $notExistingFile->getContent()); $this->assertTrue($notExistingFile->hasReadAccess()); $this->assertTrue($notExistingFile->hasWriteAccess()); |