mirror of
https://github.com/antonkomarev/github-profile-views-counter.git
synced 2026-03-31 06:24:11 -04:00
Initial commit
This commit is contained in:
21
LICENSE
Normal file
21
LICENSE
Normal 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
27
README.md
Normal file
@@ -0,0 +1,27 @@
|
||||
# GitHub Profile Views Counter
|
||||
|
||||
It counts how many times your profile was viewed.
|
||||
|
||||

|
||||
|
||||
## 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
|
||||

|
||||
```
|
||||
|
||||
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
30
public/index.php
Normal 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
11
resources/views-count.svg
Normal 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 |
37
src/CounterFileRepository.php
Normal file
37
src/CounterFileRepository.php
Normal 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);
|
||||
}
|
||||
}
|
||||
42
src/CounterImageRendererService.php
Normal file
42
src/CounterImageRendererService.php
Normal 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
2
storage/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
Reference in New Issue
Block a user