Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,6 @@ You can deploy the PHP files on any website server with PHP installed or as a He
3. On the page that comes up, click **"Deploy App"** at the end of the form
4. Once the app is deployed, click **"Manage App"** to go to the dashboard
5. Scroll down to the **Domains** section in the settings to find the URL you will use in place of `readme-typing-svg.demolab.com`
6. [Optional] To use Google fonts or other custom fonts, you will need to configure the database. The login credentials for the database can be found by clicking the PostgreSQL add-on and going to Settings. The following is the definition for the `fonts` table that needs to be created.

```sql
CREATE TABLE fonts (
"family" varchar(50) NOT NULL,
css varchar(1200000) NOT NULL,
fetch_date date NOT NULL,
CONSTRAINT fonts_pkey PRIMARY KEY (family)
);
```

## 🤗 Contributing

Expand Down
1 change: 0 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"description": "⚡ Dynamically generated, customizable SVG that gives the appearance of typing and deleting text. Typing SVGs can be used as a bio on your Github profile readme or repository.",
"repository": "https://github.com/DenverCoder1/readme-typing-svg/",
"keywords": ["github", "dynamic", "readme", "typing", "svg", "profile"],
"addons": ["heroku-postgresql"],
"formation": {
"web": {
"quantity": 1,
Expand Down
7 changes: 2 additions & 5 deletions src/controllers/RendererController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@ class RendererController
*
* @param array<string, string> $params request parameters
*/
public function __construct($params, $database = null)
public function __construct($params)
{
$this->params = $params;

// create new database connection if none was passed
$database = $database ?? new DatabaseConnection();

// set up model and view
try {
// create renderer model
$this->model = new RendererModel(__DIR__ . "/../templates/main.php", $params, $database);
$this->model = new RendererModel(__DIR__ . "/../templates/main.php", $params);
// create renderer view
$this->view = new RendererView($this->model);
} catch (Exception $error) {
Expand Down
79 changes: 0 additions & 79 deletions src/models/DatabaseConnection.php

This file was deleted.

15 changes: 10 additions & 5 deletions src/models/GoogleFontConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ class GoogleFontConverter
* Fetch CSS from Google Fonts
*
* @param string $font Google Font to fetch
* @param string $text Text to display in font
* @return string|false The CSS for displaying the font
*/
public static function fetchFontCSS($font)
public static function fetchFontCSS($font, $text)
{
$url = "https://fonts.googleapis.com/css2?family=" . str_replace(" ", "+", $font);
$url = "https://fonts.googleapis.com/css2?" . http_build_query([
"family" => $font,
"text" => $text,
"display" => "fallback",
]);
try {
// get the CSS for the font
$response = self::curl_get_contents($url);
$response = self::curlGetContents($url);
// find all font files and convert them to base64 Data URIs
return self::encodeFonts($response);
} catch (InvalidArgumentException $error) {
Expand All @@ -37,7 +42,7 @@ private static function encodeFonts($css)
$urls = array_combine($matches[1], $matches[2]);
// go over all links and replace with data URI
foreach ($urls as $url => $fontType) {
$response = self::curl_get_contents($url);
$response = self::curlGetContents($url);
$dataURI = "data:font/{$fontType};base64," . base64_encode($response);
$css = str_replace($url, $dataURI, $css);
}
Expand All @@ -50,7 +55,7 @@ private static function encodeFonts($css)
* @param string $url The URL to fetch
* @return string Response from URL
*/
private static function curl_get_contents($url): string
private static function curlGetContents($url): string
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
Expand Down
33 changes: 9 additions & 24 deletions src/models/RendererModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ class RendererModel
/** @var string $template Path to template file */
public $template;

/** @var DatabaseConnection $database Database connection */
private $database;

/** @var array<string, string> $DEFAULTS */
private $DEFAULTS = array(
"font" => "monospace",
Expand All @@ -72,12 +69,10 @@ class RendererModel
*
* @param string $template Path to the template file
* @param array<string, string> $params request parameters
* @param DatabaseConnection $font_db Database connection
*/
public function __construct($template, $params, $database)
public function __construct($template, $params)
{
$this->template = $template;
$this->database = $database;
$this->lines = $this->checkLines($params["lines"] ?? "");
$this->font = $this->checkFont($params["font"] ?? $this->DEFAULTS["font"]);
$this->color = $this->checkColor($params["color"] ?? $this->DEFAULTS["color"], "color");
Expand All @@ -90,7 +85,7 @@ public function __construct($template, $params, $database)
$this->multiline = $this->checkBoolean($params["multiline"] ?? $this->DEFAULTS["multiline"]);
$this->duration = $this->checkNumberPositive($params["duration"] ?? $this->DEFAULTS["duration"], "duration");
$this->pause = $this->checkNumberNonNegative($params["pause"] ?? $this->DEFAULTS["pause"], "pause");
$this->fontCSS = $this->fetchFontCSS($this->font);
$this->fontCSS = $this->fetchFontCSS($this->font, $params["lines"]);
}

/**
Expand Down Expand Up @@ -184,34 +179,24 @@ private function checkBoolean($bool)
}

/**
* Fetch CSS with Base-64 encoding from database or store new entry if it is missing
* Fetch CSS with Base-64 encoding from Google Fonts
*
* @param string $font Google Font to fetch
* @param string $text Text to display in font
* @return string The CSS for displaying the font
*/
private function fetchFontCSS($font)
private function fetchFontCSS($font, $text)
{
// skip checking if left as default
if ($font != $this->DEFAULTS["font"]) {
// fetch from database
$from_database = $this->database->fetchFontCSS($font);
if ($from_database) {
// return the CSS for displaying the font
$date = $from_database[0];
$css = $from_database[1];
return "<style>\n/* From database {$date} */\n{$css}</style>\n";
}
// fetch and convert from Google Fonts if not found in database
$from_google_fonts = GoogleFontConverter::fetchFontCSS($font);
// fetch and convert from Google Fonts
$from_google_fonts = GoogleFontConverter::fetchFontCSS($font, $text);
if ($from_google_fonts) {
// add font to the database
$this->database->insertFontCSS($font, $from_google_fonts);
// return the CSS for displaying the font
$date = date('Y-m-d');
return "<style>\n/* From Google Fonts {$date} */\n{$from_google_fonts}</style>\n";
return "<style>\n{$from_google_fonts}</style>\n";
}
}
// font is not in database or Google Fonts
// font is not found
return "";
}
}
Loading