Initial commit

This commit is contained in:
antonkomarev
2020-07-11 22:55:19 +03:00
commit 64f36e61f4
7 changed files with 170 additions and 0 deletions

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) Anton Komarev <anton@komarev.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

27
README.md Normal file
View File

@@ -0,0 +1,27 @@
# GitHub Profile Views Counter
It counts how many times your profile was viewed.
![Profile View Counter Example](https://user-images.githubusercontent.com/1849174/87232647-40ba1080-c3c9-11ea-9d50-c6778edcd8c7.png)
## Usage
Create your GitHub Profile repository. If you will name a new repository equally to your username — GitHub magic will happen.
Here is my profile: [https://github.com/antonkomarev/antonkomarev](https://github.com/antonkomarev/antonkomarev)
You need to host this app on your server and display it via Markdown syntax:
```markdown
![](https://komarev.com/github-profile-views-counter)
```
Only `public/index.php` should be exposed.
This URL will render SVG image with views counter and will increment it on each view of your profile.
## License
- `GitHub Profile Views Counter` application is open-sourced software licensed under the [MIT license](LICENSE) by [Anton Komarev].
[Anton Komarev]: https://komarev.com

30
public/index.php Normal file
View File

@@ -0,0 +1,30 @@
<?php
/*
* This file is part of GitHub Profile Views Counter.
*
* (c) Anton Komarev <anton@komarev.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
use Komarev\GitHubProfileViewsCounter\CounterImageRendererService;
use Komarev\GitHubProfileViewsCounter\CounterFileRepository;
$basePath = realpath(__DIR__ . '/..');
$resourcesPath = $basePath . '/resources';
$storagePath = $basePath . '/storage';
require_once $basePath . '/src/CounterImageRendererService.php';
require_once $basePath . '/src/CounterFileRepository.php';
$counterRepository = new CounterFileRepository($storagePath, 'views-count');
$counterImageRenderer = new CounterImageRendererService($resourcesPath, 'views-count.svg', $counterRepository);
header('Content-Type: image/svg+xml');
header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
echo $counterImageRenderer->getImage();

11
resources/views-count.svg Normal file
View File

@@ -0,0 +1,11 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="102" height="20">
<g shape-rendering="crispEdges">
<rect width="79" height="20" fill="#555"/>
<rect x="79" width="23" height="20" fill="#007ec6"/>
</g>
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif"
text-rendering="geometricPrecision" font-size="110">
<text x="405" y="140" transform="scale(.1)" textLength="690">Profile views</text>
<text x="895" y="140" transform="scale(.1)" textLength="130">%COUNT%</text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 608 B

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of GitHub Profile Views Counter.
*
* (c) Anton Komarev <anton@komarev.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Komarev\GitHubProfileViewsCounter;
final class CounterFileRepository
{
private string $counterPath;
public function __construct(
string $storagePath,
string $counterFileName
)
{
$this->counterPath = $storagePath . '/' . $counterFileName;
}
public function getCount(): int
{
return file_exists($this->counterPath) ? (int) file_get_contents($this->counterPath) : 0;
}
public function incrementCount(): void
{
file_put_contents($this->counterPath, $this->getCount() + 1);
}
}

View File

@@ -0,0 +1,42 @@
<?php
/*
* This file is part of GitHub Profile Views Counter.
*
* (c) Anton Komarev <anton@komarev.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Komarev\GitHubProfileViewsCounter;
final class CounterImageRendererService
{
private string $sourceImagePath;
private CounterFileRepository $counterRepository;
public function __construct(
string $resourcesPath,
string $sourceImageFileName,
CounterFileRepository $counterRepository
)
{
$this->sourceImagePath = $resourcesPath . '/' . $sourceImageFileName;
$this->counterRepository = $counterRepository;
}
public function getImage(): string
{
$this->counterRepository->incrementCount();
$count = $this->counterRepository->getCount();
$counterImage = file_get_contents($this->sourceImagePath);
$counterImage = str_replace('%COUNT%', $count, $counterImage);
return $counterImage;
}
}

2
storage/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
*
!.gitignore