/** * Constructor asset * * @access public * * @param IView $view * * @result void * @throws \Micro\Base\Exception */ public function __construct(IView $view) { $this->view = $view; if (!$this->sourcePath) { $this->sourcePath = dirname(Autoload::getClassPath(get_class($this))); } $this->hash = md5($this->sourcePath); $this->publishPath = '/' . (($dir = (new Injector())->param('assetsDirName')) ? $dir : 'assets') . '/' . $this->hash; $web = (new KernelInjector())->build()->getWebDir(); if (!file_exists($this->sourcePath)) { throw new Exception('Asset dir not exists: ' . $this->sourcePath); } if (!is_dir($web . $this->publishPath) && (!mkdir($web . $this->publishPath, 0777) && !is_dir($web . $this->publishPath))) { throw new Exception('Could not access to publish dir: ' . $this->publishPath); } FileHelper::recurseCopyIfEdited($this->sourcePath, $web . $this->publishPath, $this->excludes); }
/** * Initialize framework * * @access public * * @param string $appDir Application directory * @param string $microDir Micro directory * @param string $environment Application environment: devel , prod , test * @param bool $debug Debug-mode flag * @param bool $registerLoader Register default autoloader * * @result void */ public function __construct($appDir, $microDir, $environment = 'devel', $debug = true, $registerLoader = true) { $this->appDir = realpath($appDir); $this->microDir = realpath($microDir); $this->webDir = getenv('DOCUMENT_ROOT'); $this->environment = $environment; $this->debug = (bool) $debug; $this->loaded = false; if ($this->debug) { $this->startTime = microtime(true); } if (!$registerLoader) { return; } $this->registerAutoload(['filename' => $this->getMicroDir() . '/base/Autoload.php', 'callable' => ['\\Micro\\base\\Autoload', 'loader']]); Autoload::setAlias('Micro', $this->getMicroDir()); Autoload::setAlias('App', $this->getAppDir()); }
<?php require __DIR__ . '/../vendor/linpax/microphp-framework/base/Autoload.php'; spl_autoload_register(['\\Micro\\Base\\Autoload', 'loader'], true, false); \Micro\Base\Autoload::setAlias('Micro', __DIR__ . '/../vendor/linpax/microphp-framework'); \Micro\Base\Autoload::setAlias('App', __DIR__); \Micro\Base\Autoload::setAlias('Web', getenv('DOCUMENT_ROOT'));
public function testSetAlias() { $path = '/fake/path'; Autoload::setAlias('FakePath1', $path); Autoload::setAlias('FakePath2', []); static::assertEquals(Autoload::getAlias('FakePath1'), $path); static::assertEquals(Autoload::getAlias('FakePath2'), false); }
/** * Constructor asset * * @access public * * @param IView $view * * @result void * @throws \Micro\base\Exception */ public function __construct(IView $view) { $this->view = $view; if (!$this->sourcePath) { $this->sourcePath = dirname(Autoload::getClassPath(get_class($this))); } $this->hash = md5($this->sourcePath); $this->publishPath = '/' . (($dir = $view->container->assetsDirName) ? $dir : 'assets') . '/' . $this->hash; $web = $this->view->container->kernel->getWebDir(); if (!file_exists($this->sourcePath)) { throw new Exception('Asset dir not exists: ' . $this->sourcePath); } if (!file_exists($web . $this->publishPath)) { mkdir($web . $this->publishPath, 0777); } FileHelper::recurseCopyIfEdited($this->sourcePath, $web . $this->publishPath, $this->excludes); }
/** * Get view file * * @access private * * @param string $view view file name * * @return string * @throws Exception */ private function getViewFile($view) { $calledClass = $this->path; // Calculate path to view if (0 === strpos($calledClass, 'App')) { $path = (new KernelInjector())->build()->getAppDir(); } else { $path = Autoload::getAlias('Micro'); } $cl = strtolower(dirname(str_replace('\\', '/', $calledClass))); $cl = substr($cl, strpos($cl, '/')); if ($this->asWidget) { $path .= $cl . '/views/' . $view . '.php'; } else { $className = str_replace('controller', '', strtolower(basename(str_replace('\\', '/', '/' . $this->path)))); $path .= dirname($cl) . '/views/' . $className . '/' . $view . '.php'; } $path = str_replace('//', '/', $path); if (!file_exists($path)) { throw new Exception('View path `' . $path . '` not exists.'); } return $path; }
<?php /** * @link https://github.com/linpax/microphp-framework * @copyright Copyright (c) 2016 Oleg Lunegov * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE */ require __DIR__ . '/../src/base/Autoload.php'; spl_autoload_register(['\\Micro\\Base\\Autoload', 'loader'], true, false); \Micro\Base\Autoload::setAlias('Micro', __DIR__ . '/../src'); \Micro\Base\Autoload::setAlias('Micro\\Tests', __DIR__);