Page MenuHomeDevCentral

D418.diff
No OneTemporary

D418.diff

diff --git a/app/Assets/Assets.php b/app/Assets/Assets.php
new file mode 100644
--- /dev/null
+++ b/app/Assets/Assets.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace AuthGrove\Assets;
+
+class Assets {
+ const ASSETS_DIRECTORY = 'resources/assets/';
+
+ /**
+ * Gets application root directory
+ *
+ * @return string
+ */
+ static public function getRootDirectory () {
+ // We're in app/Assets, so we need to get twice to the parent directory.
+ return dirname(dirname(__DIR__));
+ }
+
+ static public function getAssetsDirectory () {
+ return static::getRootDirectory() . '/' . static::ASSETS_DIRECTORY;
+ }
+
+ static private function getAssetExtension ($type) {
+ return $type;
+ }
+
+ static public function getAssetFilename ($type, $name) {
+ return
+ static::getAssetsDirectory()
+ .
+ "$type/$name."
+ .
+ static::getAssetExtension($type)
+ ;
+ }
+
+ /**
+ * Checks the resource has a valid format for sanity and to avoid
+ * exploits trying to load an arbitrary resource URL.
+ *
+ * @param $name string The resource name
+ * @return bool true if the resource name is valid; otherwise, false.
+ */
+ static public function isValidAssetName ($name) {
+ return preg_match('/^[A-Za-z0-9\-_]*$/', $name);
+ }
+}
diff --git a/app/Assets/README.md b/app/Assets/README.md
new file mode 100644
--- /dev/null
+++ b/app/Assets/README.md
@@ -0,0 +1,9 @@
+This directory contains a way to serve static assets,
+as a microapplication, without loading a Laravel kernel.
+
+The goal is to speed up development process and avoid to watch with gulp or
+grunt static resouces modifications, but to regenerate them at runtime.
+
+This is intended for front-end development, and not for production.
+
+The entry point could be found at public/stylesheet.php
diff --git a/public/stylesheet.php b/app/Assets/Stylesheet.php
copy from public/stylesheet.php
copy to app/Assets/Stylesheet.php
--- a/public/stylesheet.php
+++ b/app/Assets/Stylesheet.php
@@ -1,55 +1,24 @@
<?php
-class Assets {
- const ASSETS_DIRECTORY = 'resources/assets/';
-
- static public function getAssetsDirectory () {
- return dirname(__DIR__) . "/" . static::ASSETS_DIRECTORY;
- }
-
- static private function getAssetExtension ($type) {
- return $type;
- }
-
- static public function getAssetFilename ($type, $name) {
- return
- static::getAssetsDirectory()
- .
- "$type/$name."
- .
- static::getAssetExtension($type)
- ;
- }
-
- /**
- * Checks the resource has a valid format for sanity and to avoid
- * exploits trying to load an arbitrary resource URL.
- *
- * @param $name string The resource name
- * @return bool true if the resource name is valid; otherwise, false.
- */
- static public function isValidAssetName ($name) {
- return preg_match('/^[A-Za-z0-9\-_]*$/', $name);
- }
-}
+namespace AuthGrove\Assets;
class Stylesheet {
///
/// CSS
///
-
+
static private function printHeaders () {
header("Content-type: text/css");
header("X-Content-Type-Options: nosniff");
}
-
+
static public function compileLess ($file) {
return `plessc $file`;
}
-
+
static public function printLess ($name) {
$file = Assets::getAssetFilename('less', $name);
-
+
if (file_exists($file)) {
static::printHeaders();
echo static::compileLess($file);
@@ -57,41 +26,39 @@
static::print404();
}
}
-
+
///
/// 404
///
-
+
static private function print404Headers () {
header("HTTP/1.0 404 Not Found");
}
-
+
static private function print404 () {
static::print404Headers();
echo "/* 404 - Stylesheet not found. */";
exit;
}
-
+
///
/// Routes
///
-
+
static public function handleRequest () {
$name = $_GET['name'];
-
+
if (!Assets::isValidAssetName($name)) {
die("Invalid resource name: $name");
}
-
+
switch ($_GET['resource']) {
case 'less':
static::printLess($name);
break;
-
+
default;
die("Resource not found.");
}
}
}
-
-Stylesheet::handleRequest();
diff --git a/public/stylesheet.php b/public/stylesheet.php
--- a/public/stylesheet.php
+++ b/public/stylesheet.php
@@ -1,97 +1,26 @@
<?php
-class Assets {
- const ASSETS_DIRECTORY = 'resources/assets/';
-
- static public function getAssetsDirectory () {
- return dirname(__DIR__) . "/" . static::ASSETS_DIRECTORY;
- }
-
- static private function getAssetExtension ($type) {
- return $type;
- }
-
- static public function getAssetFilename ($type, $name) {
- return
- static::getAssetsDirectory()
- .
- "$type/$name."
- .
- static::getAssetExtension($type)
- ;
- }
-
- /**
- * Checks the resource has a valid format for sanity and to avoid
- * exploits trying to load an arbitrary resource URL.
- *
- * @param $name string The resource name
- * @return bool true if the resource name is valid; otherwise, false.
- */
- static public function isValidAssetName ($name) {
- return preg_match('/^[A-Za-z0-9\-_]*$/', $name);
- }
-}
+/*
+|--------------------------------------------------------------------------
+| Auto Loader
+|--------------------------------------------------------------------------
+|
+| Composer provides a convenient, automatically generated class loader for
+| our application. We just need to utilize it! We'll simply require it
+| into the script here so that we don't have to worry about manual
+| loading any of our classes later on. It feels nice to relax.
+|
+*/
-class Stylesheet {
- ///
- /// CSS
- ///
-
- static private function printHeaders () {
- header("Content-type: text/css");
- header("X-Content-Type-Options: nosniff");
- }
-
- static public function compileLess ($file) {
- return `plessc $file`;
- }
-
- static public function printLess ($name) {
- $file = Assets::getAssetFilename('less', $name);
-
- if (file_exists($file)) {
- static::printHeaders();
- echo static::compileLess($file);
- } else {
- static::print404();
- }
- }
-
- ///
- /// 404
- ///
-
- static private function print404Headers () {
- header("HTTP/1.0 404 Not Found");
- }
-
- static private function print404 () {
- static::print404Headers();
- echo "/* 404 - Stylesheet not found. */";
- exit;
- }
-
- ///
- /// Routes
- ///
-
- static public function handleRequest () {
- $name = $_GET['name'];
-
- if (!Assets::isValidAssetName($name)) {
- die("Invalid resource name: $name");
- }
-
- switch ($_GET['resource']) {
- case 'less':
- static::printLess($name);
- break;
-
- default;
- die("Resource not found.");
- }
- }
-}
+require __DIR__.'/../bootstrap/autoload.php';
-Stylesheet::handleRequest();
+/*
+|--------------------------------------------------------------------------
+| Run the micro application
+|--------------------------------------------------------------------------
+|
+| We can now handle the incoming request through Assets and Stylesheet classes.
+|
+*/
+
+AuthGrove\Assets\Stylesheet::handleRequest();

File Metadata

Mime Type
text/plain
Expires
Wed, May 14, 16:22 (17 h, 21 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2654815
Default Alt Text
D418.diff (7 KB)

Event Timeline