diff options
Diffstat (limited to 'plugins/CustomPiwikJs/TrackerUpdater.php')
-rw-r--r-- | plugins/CustomPiwikJs/TrackerUpdater.php | 53 |
1 files changed, 49 insertions, 4 deletions
diff --git a/plugins/CustomPiwikJs/TrackerUpdater.php b/plugins/CustomPiwikJs/TrackerUpdater.php index d90d8c166d..add1c0dd20 100644 --- a/plugins/CustomPiwikJs/TrackerUpdater.php +++ b/plugins/CustomPiwikJs/TrackerUpdater.php @@ -8,11 +8,15 @@ namespace Piwik\Plugins\CustomPiwikJs; +use Piwik\Container\StaticContainer; use Piwik\Plugins\CustomPiwikJs\TrackingCode\PiwikJsManipulator; use Piwik\Plugins\CustomPiwikJs\TrackingCode\PluginTrackerFiles; /** - * Updates the Javascript file containing the Tracker. + * Updates the Piwik JavaScript Tracker "piwik.js" in case plugins extend the tracker. + * + * Usage: + * StaticContainer::get('Piwik\Plugins\CustomPiwikJs\TrackerUpdater')->update(); */ class TrackerUpdater { @@ -46,9 +50,35 @@ class TrackerUpdater $toFile = PIWIK_DOCUMENT_ROOT . self::TARGET_PIWIK_JS; } - $this->fromFile = new File($fromFile); - $this->toFile = new File($toFile); - $this->trackerFiles = new PluginTrackerFiles(); + $this->setFromFile($fromFile); + $this->setToFile($toFile); + $this->trackerFiles = StaticContainer::get('Piwik\Plugins\CustomPiwikJs\TrackingCode\PluginTrackerFiles'); + } + + public function setFromFile($fromFile) + { + if (is_string($fromFile)) { + $fromFile = new File($fromFile); + } + $this->fromFile = $fromFile; + } + + public function getFromFile() + { + return $this->fromFile; + } + + public function setToFile($toFile) + { + if (is_string($toFile)) { + $toFile = new File($toFile); + } + $this->toFile = $toFile; + } + + public function getToFile() + { + return $this->toFile; } public function setTrackerFiles(PluginTrackerFiles $trackerFiles) @@ -56,6 +86,12 @@ class TrackerUpdater $this->trackerFiles = $trackerFiles; } + /** + * Checks whether the Piwik JavaScript tracker file "piwik.js" is writable. + * @throws \Exception In case the piwik.js file is not writable. + * + * @api + */ public function checkWillSucceed() { $this->fromFile->checkReadable(); @@ -75,6 +111,15 @@ class TrackerUpdater return $newContent; } + /** + * Updates / re-generates the Piwik JavaScript tracker "piwik.js". + * + * It may not be possible to update the "piwik.js" tracker file if the file is not writable. It won't throw + * an exception in such a case and instead just to nothing. To check if the update would succeed, call + * {@link checkWillSucceed()}. + * + * @api + */ public function update() { if (!$this->toFile->hasWriteAccess() || !$this->fromFile->hasReadAccess()) { |