mirror of
https://github.com/antonkomarev/github-profile-views-counter.git
synced 2026-08-04 19:15:11 -04:00
Make label and color customizable (#9)
Make label and color customizable
This commit is contained in:
45
README.md
45
README.md
@@ -38,9 +38,40 @@ You need to add counter in README.md file in your profile repository via Markdow
|
||||
|
||||
### Customization
|
||||
|
||||
#### Color
|
||||
|
||||
You can use any valid HEX color or pick from a predefined set of named colors (`blue` is the default).
|
||||
|
||||
| color | demo |
|
||||
| ----- | ---- |
|
||||
| `brightgreen` |  |
|
||||
| `green` |  |
|
||||
| `yellow` |  |
|
||||
| `yellowgreen` |  |
|
||||
| `orange` |  |
|
||||
| `red` |  |
|
||||
| `blue` |  |
|
||||
| `grey` |  |
|
||||
| `lightgray` |  |
|
||||
| `ff69b4` |  |
|
||||
|
||||
**Named color**
|
||||
|
||||
```markdown
|
||||

|
||||
```
|
||||
|
||||
**Hex color**
|
||||
|
||||
```markdown
|
||||

|
||||
```
|
||||
|
||||
> **NOTE**: HEX colors should be used without `#` symbol prefix.
|
||||
|
||||
#### Styles
|
||||
|
||||
The following styles are available (`flat` is the default):
|
||||
The following styles are available (`flat` is the default).
|
||||
|
||||
| style | demo |
|
||||
| ----- | ---- |
|
||||
@@ -52,7 +83,17 @@ The following styles are available (`flat` is the default):
|
||||

|
||||
```
|
||||
|
||||
For example
|
||||
#### Label
|
||||
|
||||
You can overwrite default `Profile views` text with your own label.
|
||||
|
||||

|
||||
|
||||
```markdown
|
||||

|
||||
```
|
||||
|
||||
> **NOTE**: Replace whitespace with `+` character in multi-word labels.
|
||||
|
||||
## Is it safe to place tracking image?
|
||||
|
||||
|
||||
@@ -38,6 +38,8 @@ try {
|
||||
$storagePath = $_ENV['FILE_STORAGE_PATH'];
|
||||
}
|
||||
|
||||
$badgeLabel = $_GET['label'] ?? 'Profile views';
|
||||
$badgeMessageBackgroundFill = $_GET['color'] ?? 'blue';
|
||||
$badgeStyle = $_GET['style'] ?? 'flat';
|
||||
if (!in_array($badgeStyle, ['flat', 'flat-square', 'plastic'])) {
|
||||
$badgeStyle = 'flat';
|
||||
@@ -46,7 +48,7 @@ try {
|
||||
$username = trim($username);
|
||||
|
||||
if ($username === '') {
|
||||
echo $badgeImageRenderer->renderBadgeWithError($badgeStyle, 'Invalid query parameter: username');
|
||||
echo $badgeImageRenderer->renderBadgeWithError($badgeLabel, 'Invalid query parameter: username', $badgeStyle);
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -65,9 +67,9 @@ try {
|
||||
|
||||
$count = $counterRepository->getViewsCountByUsername($username);
|
||||
|
||||
echo $badgeImageRenderer->renderBadgeWithCount($badgeStyle, $count);
|
||||
echo $badgeImageRenderer->renderBadgeWithCount($badgeLabel, $count, $badgeMessageBackgroundFill, $badgeStyle);
|
||||
exit;
|
||||
} catch (Exception $exception) {
|
||||
echo $badgeImageRenderer->renderBadgeWithError($badgeStyle, $exception->getMessage());
|
||||
echo $badgeImageRenderer->renderBadgeWithError($badgeLabel, $exception->getMessage(), $badgeStyle);
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -42,6 +42,8 @@ try {
|
||||
|
||||
$httpUserAgent = $_SERVER['HTTP_USER_AGENT'];
|
||||
|
||||
$badgeLabel = $_GET['label'] ?? 'Profile views';
|
||||
$badgeMessageBackgroundFill = $_GET['color'] ?? 'blue';
|
||||
$badgeStyle = $_GET['style'] ?? 'flat';
|
||||
if (!in_array($badgeStyle, ['flat', 'flat-square', 'plastic'])) {
|
||||
$badgeStyle = 'flat';
|
||||
@@ -50,7 +52,7 @@ try {
|
||||
$username = trim($username);
|
||||
|
||||
if ($username === '') {
|
||||
echo $badgeImageRenderer->renderBadgeWithError($badgeStyle, 'Invalid query parameter: username');
|
||||
echo $badgeImageRenderer->renderBadgeWithError($badgeLabel, 'Invalid query parameter: username', $badgeStyle);
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -78,12 +80,12 @@ try {
|
||||
|
||||
$count = $counterRepository->getViewsCountByUsername($username);
|
||||
|
||||
echo $badgeImageRenderer->renderBadgeWithCount($badgeStyle, $count);
|
||||
echo $badgeImageRenderer->renderBadgeWithCount($badgeLabel, $count, $badgeMessageBackgroundFill, $badgeStyle);
|
||||
exit;
|
||||
} catch (InvalidPathException $exception) {
|
||||
echo $badgeImageRenderer->renderBadgeWithError($badgeStyle, 'Application environment file is missing');
|
||||
echo $badgeImageRenderer->renderBadgeWithError($badgeLabel, 'Application environment file is missing', $badgeStyle);
|
||||
exit;
|
||||
} catch (Exception $exception) {
|
||||
echo $badgeImageRenderer->renderBadgeWithError($badgeStyle, $exception->getMessage());
|
||||
echo $badgeImageRenderer->renderBadgeWithError($badgeLabel, $exception->getMessage(), $badgeStyle);
|
||||
exit;
|
||||
}
|
||||
|
||||
@@ -24,32 +24,34 @@ final class BadgeImageRendererService
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->poser = new Poser([new SvgRender(), new SvgFlatRender(), new SvgFlatSquareRender()]);
|
||||
$this->poser = new Poser([
|
||||
new SvgRender(),
|
||||
new SvgFlatRender(),
|
||||
new SvgFlatSquareRender(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function renderBadgeWithCount($badgeStyle, int $count): string
|
||||
public function renderBadgeWithCount(string $label, int $count, string $messageBackgroundFill, string $badgeStyle): string
|
||||
{
|
||||
$message = (string) $count;
|
||||
|
||||
$messageBackgroundFill = '007ec6';
|
||||
|
||||
return $this->renderBadge($badgeStyle, $message, $messageBackgroundFill);
|
||||
return $this->renderBadge($label, $message, $messageBackgroundFill, $badgeStyle);
|
||||
}
|
||||
|
||||
public function renderBadgeWithError($badgeStyle, string $message): string
|
||||
public function renderBadgeWithError(string $label, string $message, string $badgeStyle): string
|
||||
{
|
||||
$messageBackgroundFill = 'e05d44';
|
||||
$messageBackgroundFill = 'red';
|
||||
|
||||
return $this->renderBadge($badgeStyle, $message, $messageBackgroundFill);
|
||||
}
|
||||
|
||||
private function renderBadge($badgeStyle, string $message, string $messageBackgroundFill): string
|
||||
{
|
||||
return (string) $this->poser->generate('Profile views', $message, $messageBackgroundFill, $badgeStyle);
|
||||
return $this->renderBadge($label, $message, $messageBackgroundFill, $badgeStyle);
|
||||
}
|
||||
|
||||
public function renderPixel(): string
|
||||
{
|
||||
return '<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"/>';
|
||||
}
|
||||
|
||||
private function renderBadge(string $label, string $message, string $messageBackgroundFill, string $badgeStyle): string
|
||||
{
|
||||
return (string) $this->poser->generate($label, $message, $messageBackgroundFill, $badgeStyle);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user