diff --git a/README.md b/README.md index b5edc51a..60a8d4cd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app.json b/app.json index fe524782..0ff2d435 100644 --- a/app.json +++ b/app.json @@ -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, diff --git a/src/controllers/RendererController.php b/src/controllers/RendererController.php index 29f0261a..45a0bc4a 100644 --- a/src/controllers/RendererController.php +++ b/src/controllers/RendererController.php @@ -26,17 +26,14 @@ class RendererController * * @param array $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) { diff --git a/src/models/DatabaseConnection.php b/src/models/DatabaseConnection.php deleted file mode 100644 index d9ab4111..00000000 --- a/src/models/DatabaseConnection.php +++ /dev/null @@ -1,79 +0,0 @@ -conn = pg_connect($conn_string); - } - - /** - * Fetch CSS from PostgreSQL Database - * - * @param string $font Google Font to fetch - * @return array array containing the date and the CSS for displaying the font - */ - public function fetchFontCSS($font) - { - // check connection - if ($this->conn) { - // fetch font from database - $result = pg_query_params($this->conn, 'SELECT fetch_date, css FROM fonts WHERE family = $1', array($font)); - if (!$result) { - return false; - } - $row = pg_fetch_row($result); - if ($row) { - return $row; - } - } - return false; - } - - /** - * Insert font CSS into database - * - * @param string $font Font Family - * @param string $css CSS with Base64 encoding - * @return bool True if successful, false if connection failed - */ - public function insertFontCSS($font, $css) - { - if ($this->conn) { - $entry = array( - "family" => $font, - "css" => $css, - "fetch_date" => date('Y-m-d'), - ); - $result = pg_insert($this->conn, 'fonts', $entry); - if (!$result) { - throw new InvalidArgumentException("Insertion of Google Font to database failed"); - } - return true; - } - return false; - } -} diff --git a/src/models/GoogleFontConverter.php b/src/models/GoogleFontConverter.php index f4881b6c..55f4eff5 100644 --- a/src/models/GoogleFontConverter.php +++ b/src/models/GoogleFontConverter.php @@ -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) { @@ -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); } @@ -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); diff --git a/src/models/RendererModel.php b/src/models/RendererModel.php index 31ee9677..984671d6 100644 --- a/src/models/RendererModel.php +++ b/src/models/RendererModel.php @@ -49,9 +49,6 @@ class RendererModel /** @var string $template Path to template file */ public $template; - /** @var DatabaseConnection $database Database connection */ - private $database; - /** @var array $DEFAULTS */ private $DEFAULTS = array( "font" => "monospace", @@ -72,12 +69,10 @@ class RendererModel * * @param string $template Path to the template file * @param array $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"); @@ -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"]); } /** @@ -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 "\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 "\n"; + return "\n"; } } - // font is not in database or Google Fonts + // font is not found return ""; } } diff --git a/tests/OptionsTest.php b/tests/OptionsTest.php index 6b3759ff..f09624dc 100644 --- a/tests/OptionsTest.php +++ b/tests/OptionsTest.php @@ -8,12 +8,6 @@ final class OptionsTest extends TestCase { - protected static $database; - - public static function setUpBeforeClass(): void - { - self::$database = new DatabaseConnection(); - } /** * Test exception thrown when missing 'lines' parameter @@ -27,7 +21,7 @@ public function testMissingLines(): void "width" => "380", "height" => "50", ); - print_r(new RendererModel("src/templates/main.php", $params, self::$database)); + print_r(new RendererModel("src/templates/main.php", $params)); } /** @@ -39,7 +33,7 @@ public function testValidFont(): void "lines" => "text", "font" => "Open Sans", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals("Open Sans", $model->font); } @@ -52,7 +46,7 @@ public function testValidFontColor(): void "lines" => "text", "color" => "000000", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals("#000000", $model->color); } @@ -65,7 +59,7 @@ public function testInvalidFontColor(): void "lines" => "text", "color" => "00000", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals("#36BCF7", $model->color); } @@ -78,7 +72,7 @@ public function testValidBackgroundColor(): void "lines" => "text", "background" => "00000033", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals("#00000033", $model->background); } @@ -91,7 +85,7 @@ public function testInvalidBackgroundColor(): void "lines" => "text", "background" => "00000", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals("#00000000", $model->background); } @@ -104,7 +98,7 @@ public function testValidFontSize(): void "lines" => "text", "size" => "18", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals(18, $model->size); } @@ -119,7 +113,7 @@ public function testInvalidFontSize(): void "lines" => "text", "size" => "0", ); - print_r(new RendererModel("src/templates/main.php", $params, self::$database)); + print_r(new RendererModel("src/templates/main.php", $params)); } /** @@ -131,7 +125,7 @@ public function testValidHeight(): void "lines" => "text", "height" => "80", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals(80, $model->height); } @@ -146,7 +140,7 @@ public function testInvalidHeight(): void "lines" => "text", "height" => "x", ); - print_r(new RendererModel("src/templates/main.php", $params, self::$database)); + print_r(new RendererModel("src/templates/main.php", $params)); } /** @@ -158,7 +152,7 @@ public function testValidWidth(): void "lines" => "text", "width" => "500", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals(500, $model->width); } @@ -173,7 +167,7 @@ public function testInvalidWidth(): void "lines" => "text", "width" => "-1", ); - print_r(new RendererModel("src/templates/main.php", $params, self::$database)); + print_r(new RendererModel("src/templates/main.php", $params)); } /** @@ -185,7 +179,7 @@ public function testCenterIsTrue(): void "lines" => "text", "center" => "true", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals(true, $model->center); } @@ -198,7 +192,7 @@ public function testCenterIsNotTrue(): void "lines" => "text", "center" => "other", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals(false, $model->center); } @@ -211,7 +205,7 @@ public function testVCenterIsTrue(): void "lines" => "text", "vCenter" => "true", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals(true, $model->vCenter); } @@ -224,7 +218,7 @@ public function testVCenterIsNotTrue(): void "lines" => "text", "vCenter" => "other", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals(false, $model->vCenter); } @@ -237,7 +231,7 @@ public function testValidDuration(): void "lines" => "text", "duration" => "500", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals(500, $model->duration); } @@ -252,7 +246,7 @@ public function testInvalidDuration(): void "lines" => "text", "duration" => "-1", ); - print_r(new RendererModel("src/templates/main.php", $params, self::$database)); + print_r(new RendererModel("src/templates/main.php", $params)); } /** @@ -264,7 +258,7 @@ public function testValidPause(): void "lines" => "text", "pause" => "500", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals(500, $model->pause); } @@ -277,7 +271,7 @@ public function testValidPauseZero(): void "lines" => "text", "pause" => "0", ); - $model = new RendererModel("src/templates/main.php", $params, self::$database); + $model = new RendererModel("src/templates/main.php", $params); $this->assertEquals(0, $model->pause); } @@ -292,6 +286,6 @@ public function testInvalidPause(): void "lines" => "text", "pause" => "-1", ); - print_r(new RendererModel("src/templates/main.php", $params, self::$database)); + print_r(new RendererModel("src/templates/main.php", $params)); } } diff --git a/tests/RendererTest.php b/tests/RendererTest.php index cefecce6..ae22a042 100644 --- a/tests/RendererTest.php +++ b/tests/RendererTest.php @@ -9,13 +9,6 @@ final class RendererTest extends TestCase { - protected static $database; - - public static function setUpBeforeClass(): void - { - self::$database = new DatabaseConnection(); - } - /** * Test normal card render */ @@ -33,7 +26,7 @@ public function testCardRender(): void "width" => "380", "height" => "50", ); - $controller = new RendererController($params, self::$database); + $controller = new RendererController($params); $this->assertEquals(file_get_contents("tests/svg/test_normal.svg"), $controller->render()); } @@ -52,7 +45,7 @@ public function testMultilineCardRender(): void "height" => "200", "multiline" => "true", ); - $controller = new RendererController($params, self::$database); + $controller = new RendererController($params); $this->assertEquals(file_get_contents("tests/svg/test_multiline.svg"), $controller->render()); } @@ -68,7 +61,7 @@ public function testErrorCardRender(): void "width" => "380", "height" => "50", ); - $controller = new RendererController($params, self::$database); + $controller = new RendererController($params); $this->assertEquals(file_get_contents("tests/svg/test_missing_lines.svg"), $controller->render()); } @@ -81,7 +74,7 @@ public function testLoadingGoogleFont(): void "lines" => "text", "font" => "Roboto", ); - $controller = new RendererController($params, self::$database); + $controller = new RendererController($params); $expected = preg_replace("/\/\*(.*?)\*\//", "", file_get_contents("tests/svg/test_fonts.svg")); $actual = preg_replace("/\/\*(.*?)\*\//", "", $controller->render()); $this->assertEquals($expected, $actual); @@ -104,7 +97,7 @@ public function testInvalidGoogleFont(): void "width" => "380", "font" => "Not-A-Font", ); - $controller = new RendererController($params, self::$database); + $controller = new RendererController($params); $expected = str_replace('"monospace"', '"Not-A-Font"', file_get_contents("tests/svg/test_normal.svg")); $this->assertEquals($expected, $controller->render()); } @@ -127,7 +120,7 @@ public function testLineTrimming(): void "width" => "380", "height" => "50", ); - $controller = new RendererController($params, self::$database); + $controller = new RendererController($params); $this->assertEquals(file_get_contents("tests/svg/test_normal.svg"), $controller->render()); } @@ -147,7 +140,7 @@ public function testNormalVerticalAlignment(): void "width" => "380", "height" => "50", ); - $controller = new RendererController($params, self::$database); + $controller = new RendererController($params); $this->assertEquals(file_get_contents("tests/svg/test_normal_vertical_alignment.svg"), $controller->render()); } @@ -169,7 +162,7 @@ public function testPauseParameter(): void "height" => "50", "pause" => "1000", ); - $controller = new RendererController($params, self::$database); + $controller = new RendererController($params); $this->assertEquals(file_get_contents("tests/svg/test_pause.svg"), $controller->render()); } @@ -192,7 +185,7 @@ public function testPauseMultiline(): void "multiline" => "true", "pause" => "1000", ); - $controller = new RendererController($params, self::$database); + $controller = new RendererController($params); $this->assertEquals(file_get_contents("tests/svg/test_pause_multiline.svg"), $controller->render()); } } diff --git a/tests/svg/test_fonts.svg b/tests/svg/test_fonts.svg index 34833589..a560842b 100644 --- a/tests/svg/test_fonts.svg +++ b/tests/svg/test_fonts.svg @@ -6,12 +6,12 @@ width='400px' height='50px'>