Esempio n. 1
0
 /**
  * Return current class path
  */
 public function path()
 {
     $class = get_called_class();
     // Thank you late static binding!
     $path = str_replace('\\', '/', str_replace('\\Controller', '', $class));
     return \Kernel()->config('app.path.root') . '/' . $path;
 }
Esempio n. 2
0
 public function setUp()
 {
     parent::setUp();
     $this->router = \Kernel()->router();
     if ($this->router instanceof \Alloy\Router) {
         $this->router->reset();
     }
 }
Esempio n. 3
0
 public function setUp()
 {
     $kernel = \Kernel();
     $this->kernel = $kernel;
     // Get events container and reset for each test
     $this->events = $kernel->events();
     $this->events->reset();
 }
Esempio n. 4
0
 /**
  * Wrap layout around returned content from primary dispatch
  *
  * @return mixed $content Raw content string, Alloy\Module\Response object, or generic object that implements __toString
  */
 public function wrapLayout($content)
 {
     $kernel = \Kernel();
     $request = $kernel->request();
     $response = $kernel->response();
     $response->contentType('text/html');
     $layoutName = null;
     if ($content instanceof Alloy\View\Template) {
         $layoutName = $content->layout();
     }
     // Use config template if none other specified and request is not Ajax or CLI
     if (null === $layoutName && !$request->isAjax() && !$request->isCli()) {
         $layoutName = $kernel->config('app.layout.template', 'app');
     }
     if ($layoutName && true === $kernel->config('app.layout.enabled', false)) {
         $layout = new \Alloy\View\Template($layoutName, $request->format);
         $layout->path($kernel->config('app.path.layouts'))->format($request->format);
         // Ensure layout exists
         if (false === $layout->exists()) {
             return $content;
         }
         // Pass along set response status and data if we can
         if ($content instanceof Alloy\Module\Response) {
             $layout->status($content->status());
             $layout->errors($content->errors());
         }
         // Pass set title up to layout to override at template level
         if ($content instanceof Alloy\View\Template) {
             // Force render layout so we can pull out variables set in template
             $contentRendered = $content->content();
             $layout->head()->title($content->head()->title());
             $content = $contentRendered;
         }
         $layout->set(array('kernel' => $kernel, 'content' => $content));
         return $layout;
     }
     return $content;
 }
Esempio n. 5
0
<?php

$response = \Kernel()->response();
// No cache and hide potential errors
ini_set('display_errors', 0);
$response->header("Expires", "Mon, 26 Jul 1997 05:00:00 GMT");
$response->header("Last-Modified", gmdate("D, d M Y H:i:s") . "GMT");
$response->header("Cache-Control", "no-cache, must-revalidate");
$response->header("Pragma", "no-cache");
$response->contentType('text/xml');
$response->sendHeaders();
echo $content;
Esempio n. 6
0
 /**
  * Read template file into content string and return it
  *
  * @return string
  */
 public function content($parsePHP = true)
 {
     $this->exists(true);
     $vfile = $this->path() . $this->filePath();
     // Include() and parse PHP code
     if ($parsePHP) {
         ob_start();
         // Extract set variables into local template scope
         extract($this->vars());
         // Localize object instance for easier use in closures
         $view =& $this;
         $kernel = \Kernel();
         include $vfile;
         $templateContent = ob_get_clean();
     } else {
         // Just get raw file contents
         $templateContent = file_get_contents($vfile);
     }
     return $templateContent;
 }
Esempio n. 7
0
 /**
  * Converts object content to string on the fly
  *
  * @return  string
  */
 public function __toString()
 {
     // Exceptions cannot be thrown in __toString method (results in fatal error)
     // We have to catch any that may be thrown and return a string
     try {
         $content = $this->content();
     } catch (\Exception $e) {
         $content = "<strong>" . __CLASS__ . "::__toString ERROR:</strong><br />" . $e->getMessage();
         if (\Kernel()->config('debug')) {
             \Kernel()->dump($e->getTraceAsString());
         }
     }
     return $content;
 }
Esempio n. 8
0
 /**
  * Autoinstall missing tables on exception
  */
 public function autoinstallOnException($content)
 {
     $kernel = \Kernel();
     // Database error
     if ($content instanceof \PDOException || $content instanceof \Spot\Exception_Datasource_Missing) {
         if ($content instanceof \Spot\Exception_Datasource_Missing || '42S02' == $content->getCode() || false !== stripos($content->getMessage(), 'Base table or view not found')) {
             // Table not found - auto-install module to cause Entity migrations
             $ld = $kernel->lastDispatch();
             $content = $kernel->dispatch($ld['module'], 'install');
         }
     }
     return $content;
 }
Esempio n. 9
0
    $cfg = (require $cfgHostFile);
} else {
    // Default config file
    $cfg = (require $cfgPath . '/app.php');
}
// Ensure at least a lib path is set for both alloy and app
if (!isset($cfg['alloy']['path']['lib']) || !isset($cfg['app']['path']['lib'])) {
    throw new \InvalidArgumentException("Configuration must have at least \$cfg['alloy']['path']['lib'] and \$cfg['app']['path']['lib'] set in order to load required classes.");
}
/**
 * Load Kernel
 */
try {
    // Get Kernel with config and host config
    require_once $cfg['alloy']['path']['lib'] . '/Alloy/Kernel.php';
    $kernel = \Kernel($cfg);
    unset($cfg);
    /**
     * Class autoloaders - uses PHP 5.3 SplClassLoader
     */
    $loader = $kernel->loader();
    // Register classes with namespaces
    $loader->registerNamespaces($kernel->config('app.autoload.namespaces', array()));
    // Register a library using the PEAR naming convention
    $loader->registerPrefixes($kernel->config('app.autoload.prefixes', array()));
    // Activate the autoloader
    $loader->register();
    /**
     * Development Mode & Debug Handling
     */
    if ($kernel->config('app.mode.development')) {
Esempio n. 10
0
 /**
  * Autoinstall missing tables on exception
  */
 public function autoinstallOnException($content)
 {
     $kernel = \Kernel();
     // Database error
     if ($content instanceof \PDOException || $content instanceof \Spot\Exception_Datasource_Missing) {
         if ($content instanceof \Spot\Exception_Datasource_Missing || '42S02' == $content->getCode() || false !== stripos($content->getMessage(), 'Base table or view not found')) {
             // Last dispatch attempt
             $ld = $kernel->lastDispatch();
             // Debug trace message
             $mName = is_object($ld['module']) ? get_class($ld['module']) : $ld['module'];
             $kernel->trace("PDO Exception on module '" . $mName . "' when dispatching '" . $ld['action'] . "' Attempting auto-install in Spot plugin at " . __METHOD__ . "", $content);
             // Table not found - auto-install module to cause Entity migrations
             $content = $kernel->dispatch($ld['module'], 'install');
         }
     }
     return $content;
 }
Esempio n. 11
0
 public function setUp()
 {
     $kernel = \Kernel();
     $this->kernel = $kernel;
 }
Esempio n. 12
0
 /**
  * Scaffold module
  */
 public function scaffoldAction(Request $request)
 {
     $kernel = \Kernel();
     /**
      * Variables we expect in CLI request:
      *
      * $name string Name of module
      */
     $name = preg_replace('/[^a-zA-Z0-9_ ]/', '', $kernel->formatUnderscoreWord($request->name));
     $name_table = preg_replace('/\\s+/', '_', strtolower($name));
     // URL-usable name
     $name_url = preg_replace('/\\s+/', '_', $name);
     // Directory path
     $name_dir = preg_replace('/\\s+/', '/', $name);
     // Valid PHP namespace
     $namespace = preg_replace('/\\s+/', '\\', $name);
     // TODO: Make this dynamic and generated (allow user field definitions)
     $fields = array('name' => array('type' => 'string', 'required' => true));
     $field_string = "";
     foreach ($fields as $fieldName => $fieldInfo) {
         // Flattens field definitions for writing in Entity.php file
         // str_replace calls to remove some of the prettyprinting and odd formats var_export does by default
         $field_string .= "'" . $fieldName . "' => " . str_replace(array("array (", "\n", ",)"), array("array(", "", ")"), var_export($fieldInfo, true)) . ",\n";
     }
     // Set tag variables
     $generatorTagNames = compact('name', 'name_table', 'name_sanitized', 'name_url', 'name_dir', 'namespace', 'fields', 'field_string');
     echo PHP_EOL;
     // File paths
     $scaffoldPath = __DIR__ . '/scaffold/';
     $modulePath = $kernel->config('app.path.root') . '/Module/' . $name_dir . '/';
     // Output
     echo 'Generator Module Info' . PHP_EOL;
     echo '-----------------------------------------------------------------------' . PHP_EOL;
     echo 'Name               = ' . $name . PHP_EOL;
     echo 'Namespace          = ' . $namespace . PHP_EOL;
     echo 'Datasource (Table) = ' . $name_table . PHP_EOL;
     echo 'Path               = ' . $modulePath . PHP_EOL;
     echo '-----------------------------------------------------------------------' . PHP_EOL;
     echo PHP_EOL;
     // Variables (in 'generator' namespace):
     // * name
     // * name_table
     // * $fields
     //
     // Other variables
     // * $fields (from Entity::fields())
     // Build tag replecement set for scaffold-generated files
     $generator_tag_start = '{$generator.';
     $generator_tag_end = '}';
     $generatorTags = array();
     foreach ($generatorTagNames as $tag => $val) {
         if (is_array($val)) {
             $val = var_export($val, true);
         }
         $generatorTags[$generator_tag_start . $tag . $generator_tag_end] = $val;
     }
     // Copy files and replace tokens
     $scaffoldFiles = array('Controller.php', 'Entity.php', 'views/indexAction.html.php', 'views/newAction.html.php', 'views/editAction.html.php', 'views/deleteAction.html.php', 'views/viewAction.html.php');
     foreach ($scaffoldFiles as $sFile) {
         $tmpl = file_get_contents($scaffoldPath . $sFile);
         // Replace template tags
         $tmpl = str_replace(array_keys($generatorTags), array_values($generatorTags), $tmpl);
         // Ensure destination directory exists
         $sfDir = dirname($modulePath . $sFile);
         if (!is_dir($sfDir)) {
             mkdir($sfDir, 0755, true);
         }
         // Write file to destination module directory
         $result = file_put_contents($modulePath . $sFile, $tmpl);
         if ($result) {
             echo "+ Generated '" . $sFile . "'";
         } else {
             echo "[ERROR] Unable to generate '" . $sFile . "'";
         }
         echo PHP_EOL;
     }
     echo PHP_EOL;
 }