示例#1
0
文件: Uri.php 项目: TuxBoy/Demo-saf
 private function setDefaults()
 {
     if (!$this->controller_name && !$this->feature_name) {
         $this->controller_name = get_class(Application::current());
         $this->feature_name = 'home';
     }
 }
示例#2
0
 public function filesToDatabase()
 {
     foreach (Framework\Application::current()->include_path->getSourceFiles() as $file_path) {
         if (pathinfo($file_path)['file_name'] == 'plugin.php') {
             /** @noinspection PhpIncludeInspection */
             $plugin_configuration = (include $file_path);
             echo '<pre>' . print_r($plugin_configuration, true) . '</pre>';
         }
     }
 }
示例#3
0
 /**
  * Open routes cache file
  *
  * @param $configuration array
  */
 public function __construct($configuration = [])
 {
     if (isset($configuration['exclude'])) {
         $this->exclude = '(' . join('|', $configuration['exclude']) . ')';
     }
     get_class(Application::current());
     $script_name = substr($_SERVER['SCRIPT_NAME'], strrpos($_SERVER['SCRIPT_NAME'], SL) + 1, -4);
     $this->routes_file = getcwd() . SL . $script_name . SL . $script_name . '/cache/routes.php';
     if (file_exists($this->routes_file)) {
         /** @noinspection PhpIncludeInspection */
         include $this->routes_file;
     }
     Namespaces::$router = $this;
     //spl_autoload_register([$this, 'autoload']);
 }
示例#4
0
 /**
  * This is a direct, fast and optimized feature to read an excel file and return it's workseets
  * into a simple PHP array, as fastest as possible, using gnumeric.
  *
  * This enable you to import huge xls files of 10MB and more
  *
  * @param $file_name string
  * @param $errors    string[]
  * @return array three dimensions (worksheet, row, column) array of read data
  */
 public static function fileToArray($file_name, &$errors = [])
 {
     if (substr($file_name, -4) == '.csv') {
         $csv_file = $file_name;
         $count = '';
     } else {
         $csv_file = Application::current()->getTemporaryFilesPath() . SL . uniqid() . '.csv';
         exec('ssconvert ' . DQ . $file_name . DQ . SP . DQ . $csv_file . DQ . ' -S 2>&1 &');
         $count = 0;
     }
     $result = [];
     while (file_exists($csv_file . (strlen($count) ? DOT . $count : ''))) {
         $result[$csv_file . DOT . $count] = self::readCsvFile($csv_file . (strlen($count) ? DOT . $count : ''), $errors);
         $count++;
     }
     return $result;
 }
示例#5
0
 /**
  * @return string main template file path
  */
 public function getMainTemplateFile()
 {
     if (!isset($this->main_template)) {
         $directories = Application::current()->include_path->getSourceDirectories();
         while (current($directories) && !isset($this->main_template)) {
             if (file_exists($main_template = current($directories) . '/main.html')) {
                 $this->main_template = $main_template;
             }
             next($directories);
         }
     }
     return $this->main_template;
 }
示例#6
0
 /**
  * Gets the list of php files that were modifier since $last_time
  *
  * @param $last_time integer scan only files modified since this time
  * @return Reflection_Source[] key is full file path, value is file name
  */
 private function getFilesToCompile($last_time = 0)
 {
     $source_files = Application::current()->include_path->getSourceFiles();
     foreach (scandir(DOT) as $file_name) {
         $source_files[] = $file_name;
     }
     $files = [];
     foreach ($source_files as $file_path) {
         if (substr($file_path, -4) == '.php' && filemtime($file_path) > $last_time) {
             $files[$file_path] = new Reflection_Source($file_path, $this);
         }
     }
     return $files;
 }
示例#7
0
文件: Main.php 项目: TuxBoy/Demo-saf
 /**
  * Start PHP session and remove session id from parameters (if set)
  *
  * @param $get array
  * @param $post array
  */
 private function sessionStart(&$get, &$post)
 {
     if (empty($_SESSION)) {
         ini_set('session.cookie_path', SL . Paths::$script_name);
         session_start();
         if (isset($GLOBALS['X'])) {
             $_SESSION = [];
         }
     }
     $this->setIncludePath($_SESSION, Application::class);
     if (isset($_SESSION['session']) && isset($_SESSION['session']->plugins)) {
         $this->resumeSession();
     } else {
         $this->createSession();
     }
     if (!Application::current()) {
         $_SESSION = [];
         $this->createSession();
     }
     unset($get[session_name()]);
     unset($post[session_name()]);
 }
示例#8
0
文件: Tests.php 项目: TuxBoy/Demo-saf
 public function run()
 {
     foreach (Application::current()->include_path->getSourceDirectories() as $directory_name) {
         $this->runDir($directory_name);
     }
 }
示例#9
0
 /**
  * Init self::$default_annotations with cached file content
  */
 private static function initDefaultAnnotations()
 {
     if (!self::$default_annotations) {
         if ($application = Application::current()) {
             $default_annotations_file = Application::current()->getCacheDir() . SL . 'default_annotations.php';
             clearstatcache(true, $default_annotations_file);
             $file_time = file_exists($default_annotations_file) ? filemtime($default_annotations_file) : 0;
             if (!$file_time || $file_time < filemtime(__DIR__ . SL . 'default_annotations.php')) {
                 copy(__DIR__ . SL . 'default_annotations.php', $default_annotations_file);
             }
         } else {
             $default_annotations_file = __DIR__ . SL . 'default_annotations.php';
         }
         /** @noinspection PhpIncludeInspection dynamic */
         include_once $default_annotations_file;
     }
 }
示例#10
0
 /**
  * @param $parameters Parameters
  * @param $form       array
  * @param $files      array
  * @return mixed
  */
 public function run(Parameters $parameters, $form, $files)
 {
     $parameters->unshift(Application::current());
     return View::run($parameters->getObjects(), $form, $files, get_class(Application::current()), 'home');
 }
示例#11
0
 /**
  * Gets built name space for a class name
  *
  * @param $class_name string ie 'SAF\Framework\Module\Class_Name'
  * @return string ie 'Vendor\Application\Built\SAF\Framework\Module\Class_Name'
  */
 public static function builtClassName($class_name)
 {
     $namespace = array_slice(explode(BS, Namespaces::of($class_name)), 1);
     $left = Application::current()->getNamespace();
     $namespace = $left . BS . 'Built' . BS . join(BS, $namespace);
     $built_class = $namespace . BS . Namespaces::shortClassName($class_name);
     return $built_class;
 }
示例#12
0
 /**
  * @param $base_class   string The base name for the class, ie 'SAF\Framework\User'
  * @param $feature_name string The name of the feature, ie 'dataList'
  * @param $suffix       string Class suffix, ie 'Controller', 'View'
  * @param $extension    string File extension, ie 'php', 'html'
  * @param $class_form   boolean true to use 'Feature_Class' naming instead of 'featureClass'
  * @return string[] [$class, $method]
  */
 public static function get($base_class, $feature_name, $suffix, $extension, $class_form = true)
 {
     // ie : $feature_class = 'featureName' transformed into 'Feature_Name'
     $feature_class = Names::methodToClass($feature_name);
     // $feature_what is $feature_class or $feature_name depending on $class_name
     $feature_what = $class_form ? $feature_class : $feature_name;
     $_suffix = $suffix ? '_' . $suffix : '';
     $class_name = $base_class;
     $ext = DOT . $extension;
     $method = 'run';
     $application_classes = Application::current()->getClassesTree();
     // $classes : the controller class name and its parents
     // ['Vendor\Application\Module\Class_Name' => '\Module\Class_Name']
     $classes = [];
     do {
         $classes[$class_name] = substr($class_name, strpos($class_name, BS, strpos($class_name, BS) + 1) + 1);
         if (@class_exists($class_name)) {
             $reflection_class = new Reflection_Class(Builder::className($class_name));
             foreach ($reflection_class->getTraits() as $trait) {
                 $classes[$trait->name] = explode(BS, $trait->name, 3)[2];
             }
             foreach ($reflection_class->getListAnnotation('extends')->values() as $extends) {
                 $classes[$extends] = explode(BS, $extends, 3)[2];
             }
         }
         $class_name = @get_parent_class($class_name);
     } while ($class_name);
     // Looking for specific controller for each application
     $application_class = reset($application_classes);
     do {
         $namespace = Namespaces::of($application_class);
         // for the controller class and its parents
         foreach ($classes as $short_class_name) {
             $class_name = $namespace . BS . $short_class_name;
             $path = strtolower(str_replace(BS, SL, $class_name));
             if (isset($GLOBALS['D'])) {
                 echo '- try A1 ' . $path . SL . $feature_what . $_suffix . $ext . BR;
             }
             if (file_exists($path . SL . $feature_what . $_suffix . $ext)) {
                 $class = $class_name . BS . $feature_what . $_suffix;
                 break 2;
             }
             if (isset($GLOBALS['D'])) {
                 echo '- try A2 ' . $path . SL . strtolower($feature_class) . SL . $feature_what . $_suffix . $ext . BR;
             }
             if (file_exists($path . SL . strtolower($feature_class) . SL . $feature_what . $_suffix . $ext)) {
                 $class = $class_name . BS . $feature_class . BS . $feature_what . $_suffix;
                 break 2;
             }
             if (isset($GLOBALS['D']) && $suffix) {
                 echo '- try A3 ' . $path . SL . strtolower($feature_class) . SL . $suffix . $ext . BR;
             }
             if ($suffix && file_exists($path . SL . strtolower($feature_class) . SL . $suffix . $ext)) {
                 $class = $class_name . BS . $feature_class . BS . $suffix;
                 break 2;
             }
             if (isset($GLOBALS['D'])) {
                 echo '- try A4 ' . Names::classToPath($class_name) . '_' . $feature_what . $_suffix . $ext . BR;
             }
             if (file_exists(Names::classToPath($class_name) . '_' . $feature_what . $_suffix . $ext)) {
                 $class = $class_name . '_' . $feature_what . $_suffix;
                 break 2;
             }
             if (isset($GLOBALS['D']) && $suffix) {
                 echo '- try A5 ' . $path . SL . $suffix . $ext . BR;
             }
             if ($suffix && file_exists($path . SL . $suffix . $ext) && method_exists($class_name . BS . $suffix, 'run' . ucfirst($feature_name))) {
                 $class = $class_name . BS . $suffix;
                 $method = 'run' . ucfirst($feature_name);
                 break 2;
             }
         }
         // next application is the parent one
         $application_class = next($application_classes);
     } while ($application_class);
     // Looking for default controller for each application
     if (empty($class)) {
         reset($application_classes);
         do {
             // looking for default controller
             $path = strtolower(str_replace(BS, SL, $namespace));
             if (isset($GLOBALS['D']) && $suffix) {
                 echo '- try B1 ' . $path . SL . strtolower($feature_class) . SL . $suffix . $ext . BR;
             }
             if ($suffix && file_exists($path . SL . strtolower($feature_class) . SL . $suffix . $ext)) {
                 $class = $namespace . BS . $feature_class . BS . $suffix;
                 break;
             }
             if (isset($GLOBALS['D'])) {
                 echo '- try B2 ' . $path . SL . strtolower($feature_class) . SL . $feature_what . $_suffix . $ext . BR;
             }
             if (file_exists($path . SL . strtolower($feature_class) . SL . $feature_what . $_suffix . $ext)) {
                 $class = $namespace . BS . $feature_class . BS . $feature_what . $_suffix;
                 break;
             }
             if (isset($GLOBALS['D']) && $suffix) {
                 echo '- try B3 ' . $path . SL . 'widget' . SL . strtolower($feature_class) . SL . $suffix . $ext . BR;
             }
             if ($suffix && file_exists($path . SL . 'widget' . SL . strtolower($feature_class) . SL . $suffix . $ext)) {
                 $class = $namespace . BS . 'Widget' . BS . $feature_class . BS . $suffix;
                 break;
             }
             if (isset($GLOBALS['D'])) {
                 echo '- try B4 ' . $path . SL . 'widget' . SL . strtolower($feature_class) . SL . $feature_what . $_suffix . $ext . BR;
             }
             if (file_exists($path . SL . 'widget' . SL . strtolower($feature_class) . SL . $feature_what . $_suffix . $ext)) {
                 $class = $namespace . BS . 'Widget' . BS . $feature_class . BS . $feature_what . $_suffix;
                 break;
             }
             if (isset($GLOBALS['D']) && $suffix) {
                 echo '- try B5 ' . $path . SL . 'webservice' . SL . strtolower($feature_class) . SL . $suffix . $ext . BR;
             }
             if ($suffix && file_exists($path . SL . 'webservice' . SL . strtolower($feature_class) . SL . $suffix . $ext)) {
                 $class = $namespace . BS . 'Webservice' . BS . $feature_class . BS . $suffix;
                 break;
             }
             if (isset($GLOBALS['D'])) {
                 echo '- try B6 ' . $path . SL . 'webservice' . SL . strtolower($feature_class) . SL . $feature_what . $_suffix . $ext . BR;
             }
             if (file_exists($path . SL . 'webservice' . SL . strtolower($feature_class) . SL . $feature_what . $_suffix . $ext)) {
                 $class = $namespace . BS . 'Webservice' . BS . $feature_class . BS . $feature_what . $_suffix;
                 break;
             }
             // next application is the parent one
         } while (next($application_classes));
         // Looking for direct feature call, without using any controller
         static $last_controller_class = '';
         static $last_controller_method = '';
         if (empty($class) && (strpos($suffix, 'View') === false || $last_controller_class !== $base_class && $last_controller_method !== $feature_name)) {
             if (strpos($suffix, 'Controller') !== false) {
                 $last_controller_class = $base_class;
                 $last_controller_method = $feature_name;
             }
             if (@method_exists($base_class, $feature_name)) {
                 $class = $base_class;
                 $method = $feature_name;
             }
         }
         // Looking for default controller for each application
         if (empty($class) && $suffix) {
             reset($application_classes);
             // $suffix == 'Html_View' => $sub = 'View/Html', $suffix = 'View'
             if (strpos($suffix, '_')) {
                 $elements = explode('_', $suffix);
                 $sub = join(SL, array_reverse($elements));
                 $suffix = end($elements);
             } else {
                 $sub = $suffix;
             }
             do {
                 if (isset($GLOBALS['D'])) {
                     echo '- try C2 ' . $path . SL . strtolower($sub) . '/Default_' . $suffix . $ext . BR;
                 }
                 if (file_exists($path . SL . strtolower($sub) . '/Default_' . $suffix . $ext)) {
                     $class = $namespace . BS . str_replace(SL, BS, $sub) . BS . 'Default_' . $suffix;
                     break;
                 }
             } while (next($application_classes));
         }
     }
     $result = [isset($class) ? $class : null, $method];
     if (isset($GLOBALS['D'])) {
         echo '- FOUND ' . join('::', $result) . BR;
     }
     return $result;
 }
示例#13
0
 /**
  * Scans all PHP files into the project (excluding vendor) and store their paths to the cache
  */
 public function update($last_time = 0)
 {
     $files = Application::current()->include_path->getSourceFiles();
     $this->full_class_names = [];
     $this->paths = [];
     foreach ($files as $file_path) {
         if (substr($file_path, -4) == '.php') {
             $buffer = file_get_contents($file_path);
             $short_class = trim(mParse($buffer, LF . 'class' . SP, LF)) ?: trim(mParse($buffer, LF . 'final class' . SP, LF)) ?: trim(mParse($buffer, LF . 'abstract class' . SP, LF)) ?: trim(mParse($buffer, LF . 'final abstract class' . SP, LF));
             if ($short_class) {
                 $type = 'class';
             } else {
                 $short_class = trim(mParse($buffer, LF . 'interface' . SP, LF));
                 if ($short_class) {
                     $type = 'interface';
                 } else {
                     $short_class = trim(mParse($buffer, LF . 'trait' . SP, LF));
                     if ($short_class) {
                         $type = 'trait';
                     }
                 }
             }
             if ($short_class && isset($type)) {
                 if ($i = strpos($short_class, SP)) {
                     $short_class = substr($short_class, 0, $i);
                 }
                 $namespace = trim(mParse($buffer, LF . 'namespace' . SP, LF));
                 if (substr($namespace, -1) == ';') {
                     $namespace = trim(substr($namespace, 0, -1));
                 }
                 $full_class = $namespace . BS . $short_class;
                 if ($type == 'class' && !isset($this->full_class_names[$short_class])) {
                     $this->full_class_names[$short_class] = $full_class;
                 }
                 if (!isset($this->paths[$full_class])) {
                     $this->paths[$full_class] = $file_path;
                 }
             }
         }
     }
     if (!is_dir($this->cache_path)) {
         mkdir($this->cache_path);
     }
     script_put_contents($this->cache_path . SL . 'autoload.php', '<?php' . LF . LF . '$this->full_class_names = ' . var_export($this->full_class_names, true) . ';' . LF . LF . '$this->paths = ' . var_export($this->paths, true) . ';' . LF);
 }
示例#14
0
 /**
  * @return string
  */
 public function getLastUpdateFileName()
 {
     return Application::current()->getCacheDir() . SL . self::LAST_UPDATE_FILE;
 }
示例#15
0
文件: Names.php 项目: TuxBoy/Demo-saf
 /**
  * Gets the URI of a class name or object
  *
  * @example class name User : '******'
  * @example User object of id = 1 : 'SAF/Framework/User/1'
  * @param $class_name object|string
  * @return string
  */
 public static function classToUri($class_name)
 {
     // get object id, if object
     if (is_object($class_name)) {
         $id = Dao::getObjectIdentifier($class_name, 'id');
         $class_name = get_class($class_name);
     }
     // link classes : get linked class
     while ((new Reflection_Class($class_name))->getAnnotation('link')->value) {
         $class_name = get_parent_class($class_name);
     }
     // built classes : get object class
     $built_path = Application::current()->getNamespace() . BS . 'Built' . BS;
     while (substr($class_name, 0, strlen($built_path)) == $built_path) {
         $class_name = get_parent_class($class_name);
     }
     // replace \ by /
     return str_replace(BS, SL, $class_name) . (isset($id) ? SL . $id : '');
 }
示例#16
0
 /**
  * Gets replacement class name for a parent class name or a list of traits to implement
  *
  * @param $class_name string can be short or full class name
  * @return string
  */
 private function replacementClassName($class_name)
 {
     if ($this->enabled) {
         $result = isset($this->replacements[$class_name]) ? $this->replacements[$class_name] : $class_name;
         if (is_array($result)) {
             if ($this->build) {
                 $this->compositions[$class_name] = $result;
                 $built_class_name = Class_Builder::builtClassName($class_name);
                 if (file_exists(Application::current()->getCacheDir() . '/compiled/' . str_replace('/', '-', Names::classToPath($built_class_name)))) {
                     $result = $built_class_name;
                 } else {
                     $result = Class_Builder::build($class_name, $result);
                 }
                 $this->replacements[$class_name] = $result;
             } else {
                 $result = $class_name;
             }
         } elseif (!$this->build && self::isBuilt($result)) {
             $result = $class_name;
         }
     } else {
         $result = $class_name;
     }
     return $class_name != $result && !self::isBuilt($result) ? $this->replacementClassName($result) : $result;
 }
示例#17
0
 /**
  * @param $main_controller Main
  */
 public function setMainController(Main $main_controller)
 {
     // AOP compiler needs all plugins to be registered again, in order to build the complete
     // weaver's advices tree
     if (!$this->weaver->hasJoinpoints()) {
         $this->weaver->loadJoinpoints(Application::current()->getCacheDir() . SL . 'weaver.php');
     }
 }
示例#18
0
文件: File.php 项目: TuxBoy/Demo-saf
 /**
  * Gets temporary file name, or write content into a temporary file name and get this name if not
  * set or file does not exist
  *
  * @return string
  */
 private function getTemporaryFileName()
 {
     if (isset($this->content) && (empty($this->temporary_file_name) || !file_exists($this->temporary_file_name))) {
         if (empty($this->temporary_file_name)) {
             $this->temporary_file_name = Application::current()->getTemporaryFilesPath() . SL . uniqid() . '_' . $this->name;
         }
         if (strpos($this->temporary_file_name, SL) !== false) {
             Files::mkdir(lLastParse($this->temporary_file_name, SL));
         }
         file_put_contents($this->temporary_file_name, $this->content);
     }
     return $this->temporary_file_name;
 }