function css($file, $media = null)
 {
     /* If file is CSS, check if there is a LESS file */
     if (preg_match('/\\.css$/i', $file)) {
         $less = preg_replace('/\\.css$/i', '.less', $file);
         if (is_file(Director::getAbsFile($less))) {
             $file = $less;
         }
     }
     /* If less file, then check/compile it */
     if (preg_match('/\\.less$/i', $file)) {
         $compiler = 'checkedCompile';
         $out = preg_replace('/\\.less$/i', '.css', $file);
         /* Force recompile if ?flush */
         if (isset($_GET['flush'])) {
             $compiler = 'compileFile';
         }
         /* Create instance */
         $less = new lessc();
         /* Automatically compress if in live mode */
         if (DIRECTOR::isLive()) {
             $less->setFormatter("compressed");
         }
         try {
             $less->{$compiler}(Director::getAbsFile($file), Director::getAbsFile($out));
         } catch (Exception $ex) {
             trigger_error("lessphp fatal error: " . $ex->getMessage(), E_USER_ERROR);
         }
         $file = $out;
     }
     /* Return css path */
     return parent::css($file, $media);
 }
 public function css($file, $media = null)
 {
     if (!$this->isURL($file)) {
         if (!Director::fileExists($file)) {
             $file = $this->findRequirementFile($file, "css");
         }
     }
     return parent::css($file, $media);
 }
 public function testExternalUrls()
 {
     $backend = new Requirements_Backend();
     $backend->setCombinedFilesEnabled(true);
     $backend->javascript('http://www.mydomain.com/test.js');
     $backend->javascript('https://www.mysecuredomain.com/test.js');
     $backend->javascript('//scheme-relative.example.com/test.js');
     $backend->css('http://www.mydomain.com/test.css');
     $backend->css('https://www.mysecuredomain.com/test.css');
     $backend->css('//scheme-relative.example.com/test.css');
     $html = $backend->includeInHTML(false, self::$html_template);
     $this->assertTrue(strpos($html, 'http://www.mydomain.com/test.js') !== false, 'Load external javascript URL');
     $this->assertTrue(strpos($html, 'https://www.mysecuredomain.com/test.js') !== false, 'Load external secure javascript URL');
     $this->assertTrue(strpos($html, '//scheme-relative.example.com/test.js') !== false, 'Load external scheme-relative javascript URL');
     $this->assertTrue(strpos($html, 'http://www.mydomain.com/test.css') !== false, 'Load external CSS URL');
     $this->assertTrue(strpos($html, 'https://www.mysecuredomain.com/test.css') !== false, 'Load external secure CSS URL');
     $this->assertTrue(strpos($html, '//scheme-relative.example.com/test.css') !== false, 'Load scheme-relative CSS URL');
 }
예제 #4
0
 function css($file, $media = null)
 {
     /**
      * Only initiate automatically if:
      * - webiste is in dev mode
      * - or a ?flush is called
      */
     if (preg_match('/\\.less$/i', $file) || Director::isDev() || isset($_GET['flush'])) {
         /* If file is CSS, check if there is a LESS file */
         if (preg_match('/\\.css$/i', $file)) {
             $less = preg_replace('/\\.css$/i', '.less', $file);
             if (is_file(Director::getAbsFile($less))) {
                 $file = $less;
             }
         }
         /* If less file exists, then check/compile it */
         if (preg_match('/\\.less$/i', $file)) {
             $out = preg_replace('/\\.less$/i', '.css', $file);
             $css_file = Director::getAbsFile($out);
             $options = array();
             /* Automatically compress if in live mode */
             if (Director::isLive()) {
                 $options['compress'] = true;
             }
             try {
                 /* Force recompile & only write to css if updated */
                 if (isset($_GET['flush']) || !Director::isLive()) {
                     /* Create instance */
                     $parser = new Less_Parser($options);
                     if (!empty(self::$variables)) {
                         $parser->ModifyVars(self::$variables);
                     }
                     /* calculate the LESS file's parent URL */
                     $css_dir = rtrim(Director::baseURL(), '/') . Director::makeRelative(dirname(Director::getAbsFile($file)) . '/');
                     $parser->parseFile(Director::getAbsFile($file), $css_dir);
                     $css = $parser->getCss();
                     if (!is_file($css_file) || md5_file($css_file) != md5($css)) {
                         file_put_contents($css_file, $css);
                     }
                 }
             } catch (Exception $ex) {
                 trigger_error("Less.php fatal error: " . $ex->getMessage(), E_USER_ERROR);
             }
             $file = $out;
         }
     }
     /* Return css path */
     return parent::css($file, $media);
 }
 public function css($file, $media = null)
 {
     $file = $this->collectFile($file);
     parent::css($file, $media);
 }
 function testRequirementsBackend()
 {
     $backend = new Requirements_Backend();
     $backend->javascript(SAPPHIRE_DIR . '/tests/forms/a.js');
     $this->assertTrue(count($backend->get_javascript()) == 1, "There should be only 1 file included in required javascript.");
     $this->assertTrue(in_array(SAPPHIRE_DIR . '/tests/forms/a.js', $backend->get_javascript()), "/test/forms/a.js should be included in required javascript.");
     $backend->javascript(SAPPHIRE_DIR . '/tests/forms/b.js');
     $this->assertTrue(count($backend->get_javascript()) == 2, "There should be 2 files included in required javascript.");
     $backend->block(SAPPHIRE_DIR . '/tests/forms/a.js');
     $this->assertTrue(count($backend->get_javascript()) == 1, "There should be only 1 file included in required javascript.");
     $this->assertFalse(in_array(SAPPHIRE_DIR . '/tests/forms/a.js', $backend->get_javascript()), "/test/forms/a.js should not be included in required javascript after it has been blocked.");
     $this->assertTrue(in_array(SAPPHIRE_DIR . '/tests/forms/b.js', $backend->get_javascript()), "/test/forms/b.js should be included in required javascript.");
     $backend->css(SAPPHIRE_DIR . '/tests/forms/a.css');
     $this->assertTrue(count($backend->get_css()) == 1, "There should be only 1 file included in required css.");
     $this->assertArrayHasKey(SAPPHIRE_DIR . '/tests/forms/a.css', $backend->get_css(), "/tests/forms/a.css should be in required css.");
     $backend->block(SAPPHIRE_DIR . '/tests/forms/a.css');
     $this->assertTrue(count($backend->get_css()) == 0, "There should be nothing in required css after file has been blocked.");
 }
 function css($file, $media = null)
 {
     $isurl = (bool) preg_match('{^\\w+://}', $file);
     return parent::css($isurl ? $file : $this->baseRewrite($file), $media);
 }
예제 #8
0
 public function testSuffix()
 {
     $template = '<html><head></head><body><header>My header</header><p>Body</p></body></html>';
     $basePath = $this->getCurrentRelativePath();
     $basePath = 'framework' . substr($basePath, strlen(FRAMEWORK_DIR));
     $backend = new Requirements_Backend();
     $backend->javascript($basePath . '/RequirementsTest_a.js');
     $backend->javascript($basePath . '/RequirementsTest_b.js?foo=bar&bla=blubb');
     $backend->css($basePath . '/RequirementsTest_a.css');
     $backend->css($basePath . '/RequirementsTest_b.css?foo=bar&bla=blubb');
     $backend->set_suffix_requirements(true);
     $html = $backend->includeInHTML(false, $template);
     $this->assertRegexp('/RequirementsTest_a\\.js\\?m=[\\d]*"/', $html);
     $this->assertRegexp('/RequirementsTest_b\\.js\\?m=[\\d]*&amp;foo=bar&amp;bla=blubb"/', $html);
     $this->assertRegexp('/RequirementsTest_a\\.css\\?m=[\\d]*"/', $html);
     $this->assertRegexp('/RequirementsTest_b\\.css\\?m=[\\d]*&amp;foo=bar&amp;bla=blubb"/', $html);
     $backend->set_suffix_requirements(false);
     $html = $backend->includeInHTML(false, $template);
     $this->assertNotContains('RequirementsTest_a.js=', $html);
     $this->assertNotRegexp('/RequirementsTest_a\\.js\\?m=[\\d]*"/', $html);
     $this->assertNotRegexp('/RequirementsTest_b\\.js\\?m=[\\d]*&amp;foo=bar&amp;bla=blubb"/', $html);
     $this->assertNotRegexp('/RequirementsTest_a\\.css\\?m=[\\d]*"/', $html);
     $this->assertNotRegexp('/RequirementsTest_b\\.css\\?m=[\\d]*&amp;foo=bar&amp;bla=blubb"/', $html);
 }
 function css($file, $media = null)
 {
     /* Only initiate if webiste is in dev mode or a ?flush is called */
     if (preg_match('/\\.less$/i', $file) || Director::isDev() || isset($_GET['flush'])) {
         /* If file is CSS, check if there is a LESS file */
         if (preg_match('/\\.css$/i', $file)) {
             $less = preg_replace('/\\.css$/i', '.less', $file);
             if (is_file(Director::getAbsFile($less))) {
                 $file = $less;
             }
         }
         /* If less file exists, then check/compile it */
         if (preg_match('/\\.less$/i', $file)) {
             $out = preg_replace('/\\.less$/i', '.css', $file);
             $css_file = Director::getAbsFile($out);
             $options = array();
             /* Automatically compress if in live mode */
             if (Director::isLive()) {
                 $options['compress'] = true;
             }
             try {
                 /* Force recompile & only write to css if updated */
                 if (isset($_GET['flush']) || !Director::isLive()) {
                     /* Force deleting of all cache files on flush */
                     if (file_exists(self::$cacheDir) && isset($_GET['flush']) && !self::$already_flushed) {
                         $paths = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(self::$cacheDir, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
                         foreach ($paths as $path) {
                             $path->isDir() && !$path->isLink() ? rmdir($path->getPathname()) : unlink($path->getPathname());
                         }
                         /* make sure we only flush once per request and not for each *.less */
                         self::$already_flushed = true;
                     }
                     /* Set cache directory */
                     $options['cache_dir'] = self::$cacheDir;
                     /* Set cache method */
                     $options['cache_method'] = self::$cacheMethod;
                     /* Calculate the LESS file's parent URL */
                     $css_dir = dirname(Director::baseURL() . $file) . '/';
                     /* Generate and return cached file path */
                     $cached_file = self::$cacheDir . '/' . Less_Cache::Get(array(Director::getAbsFile($file) => $css_dir), $options, self::$variables);
                     /* check cache vs. css and overwrite if necessary */
                     if (!is_file($css_file) || md5_file($css_file) != md5_file($cached_file)) {
                         copy($cached_file, $css_file);
                     }
                 }
             } catch (Exception $ex) {
                 trigger_error('Less.php fatal error: ' . $ex->getMessage(), E_USER_ERROR);
             }
             $file = $out;
         }
     }
     /* Return css file path */
     return parent::css($file, $media);
 }
예제 #10
0
 function testRequirementsBackend()
 {
     $basePath = $this->getCurrentRelativePath();
     $backend = new Requirements_Backend();
     $backend->javascript($basePath . '/a.js');
     $this->assertTrue(count($backend->get_javascript()) == 1, "There should be only 1 file included in required javascript.");
     $this->assertTrue(in_array($basePath . '/a.js', $backend->get_javascript()), "a.js should be included in required javascript.");
     $backend->javascript($basePath . '/b.js');
     $this->assertTrue(count($backend->get_javascript()) == 2, "There should be 2 files included in required javascript.");
     $backend->block($basePath . '/a.js');
     $this->assertTrue(count($backend->get_javascript()) == 1, "There should be only 1 file included in required javascript.");
     $this->assertFalse(in_array($basePath . '/a.js', $backend->get_javascript()), "a.js should not be included in required javascript after it has been blocked.");
     $this->assertTrue(in_array($basePath . '/b.js', $backend->get_javascript()), "b.js should be included in required javascript.");
     $backend->css($basePath . '/a.css');
     $this->assertTrue(count($backend->get_css()) == 1, "There should be only 1 file included in required css.");
     $this->assertArrayHasKey($basePath . '/a.css', $backend->get_css(), "a.css should be in required css.");
     $backend->block($basePath . '/a.css');
     $this->assertTrue(count($backend->get_css()) == 0, "There should be nothing in required css after file has been blocked.");
 }
예제 #11
0
 function testRequirementsBackend()
 {
     $backend = new Requirements_Backend();
     $backend->js('tests/phpunit/data/a.js');
     $this->assertCount(1, $backend->get_js(), "There should be only 1 file included in required javascript.");
     $this->assertContains('tests/phpunit/data/a.js', $backend->get_js(), "/tests/phpunit/data/a.js should be included in required javascript.");
     $backend->js('tests/phpunit/data/b.js');
     $this->assertCount(2, $backend->get_js(), "There should be 2 files included in required javascript.");
     $backend->block('tests/phpunit/data/a.js');
     $this->assertCount(1, $backend->get_js(), "There should be only 1 file included in required javascript.");
     $this->assertNotContains('tests/phpunit/data/a.js', $backend->get_js(), "/tests/phpunit/data/a.js should not be included in required javascript after it has been blocked.");
     $this->assertContains('tests/phpunit/data/b.js', $backend->get_js(), "/tests/phpunit/data/b.js should be included in required javascript.");
     $backend->css('tests/phpunit/data/a.css');
     $this->assertCount(1, $backend->get_css(), "There should be only 1 file included in required css.");
     $this->assertArrayHasKey('a.css', $backend->get_css(), "/tests/phpunit/data/a.css should be in required css.");
     $this->assertContains(array('file' => 'tests/phpunit/data/a.css', 'media' => null), $backend->get_css(), "/tests/phpunit/data/a.css should be in required css.");
     $backend->block('tests/phpunit/data/a.css');
     $this->assertCount(0, $backend->get_css(), "There should be nothing in required css after file has been blocked.");
     // Test unblock_all()
     $backend->unblock_all();
     $this->assertCount(2, $backend->get_js(), "There should be only 2 files included in required css.");
     $this->assertCount(1, $backend->get_css(), "There should be only 1 file included in required javascript.");
     // Testing clear()
     $backend->js('tests/phpunit/data/c.css');
     $backend->clear();
     $this->assertCount(0, $backend->get_css(), "There should be nothing in required css after requirements cleared.");
     $this->assertCount(0, $backend->get_js(), "There should be nothing in required js after requirements cleared.");
     // Testing js block by id
     $backend->js('tests/phpunit/data/a.js');
     $backend->block('a.js');
     $this->assertCount(0, $backend->get_js(), "There should be nothing in required js after file has be blocked.");
     // Testing css block by id
     $backend->css('tests/phpunit/data/a.css');
     $backend->block('a.css');
     $this->assertCount(0, $backend->get_css(), "There should be nothing in required css after file has been blocked.");
     // Testing unblock
     $backend->unblock('a.js');
     $this->assertCount(1, $backend->get_js(), "There should be only 1 file included in required javascript.");
 }