noalyss-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Noalyss-commit] [noalyss] 03/08: Package_Repository : add a mechanism o


From: dwm
Subject: [Noalyss-commit] [noalyss] 03/08: Package_Repository : add a mechanism of cache
Date: Sat, 1 Jul 2023 11:09:07 -0400 (EDT)

sparkyx pushed a commit to branch master
in repository noalyss.

commit bc11c12efdb75b4854d4141bca589d1919bf4a1c
Author: sparkyx <danydb@noalyss.eu>
AuthorDate: Sun Jun 11 16:00:22 2023 +0200

    Package_Repository : add a mechanism of cache
---
 include/class/package_noalyss.class.php            |  20 +--
 include/class/package_repository.class.php         |  48 +++++-
 unit-test/include/class/package_repositoryTest.php | 175 +++++++++++++++++++++
 3 files changed, 230 insertions(+), 13 deletions(-)

diff --git a/include/class/package_noalyss.class.php 
b/include/class/package_noalyss.class.php
index 025c5f4a9..ea612fa3f 100644
--- a/include/class/package_noalyss.class.php
+++ b/include/class/package_noalyss.class.php
@@ -104,16 +104,18 @@ abstract class Package_Noalyss
 
     function download()
     {
+        if ( ! $this->can_download() ) return false;
         // If install is writable then download 
-        if ( $this->can_download() )
-        {
-            $full=$this->get_path()."/".$this->get_file();
-            $file = file_get_contents(NOALYSS_PACKAGE_REPOSITORY."/".$full);
-            $fh_file=fopen(NOALYSS_HOME."/tmp/".$this->get_file(),"w+");
-            
-            fwrite($fh_file, $file);
-             fclose($fh_file);
-        }
+
+
+        $full=$this->get_path()."/".$this->get_file();
+        $file = file_get_contents(NOALYSS_PACKAGE_REPOSITORY."/".$full);
+        $fh_file=fopen(NOALYSS_HOME."/tmp/".$this->get_file(),"w+");
+
+        fwrite($fh_file, $file);
+        fclose($fh_file);
+        return TRUE;
+
     }
 
     abstract function install();
diff --git a/include/class/package_repository.class.php 
b/include/class/package_repository.class.php
index ba2becce8..89324275b 100644
--- a/include/class/package_repository.class.php
+++ b/include/class/package_repository.class.php
@@ -31,7 +31,8 @@
 class Package_Repository
 {
 
-    private $content;
+    protected $content;
+    static $time_cache_second = 3600; // cache time in second
 
     /**
      * @see package_repository.test.php
@@ -40,17 +41,56 @@ class Package_Repository
     {
         // Check we can resolve the name
         $host=parse_url(NOALYSS_PACKAGE_REPOSITORY,PHP_URL_HOST);
-        
-        if (gethostbyname($host) != $host)
-        {
+        $file=$_ENV['TMP']."/web.xml";
+
+        if ( file_exists($file) ) {
+            $date_time=new \DateTime();
+            $file_tmstamp=filemtime($file);
+
+            $delta=$date_time->getTimestamp() - $file_tmstamp;
+
+            // if file too old , refresh it
+            if ( $delta > self::$time_cache_second ) {
+                $web_repo = 
file_get_contents(NOALYSS_PACKAGE_REPOSITORY."/web.xml");
+                $f_file= fopen($file,"w+");
+                fwrite($f_file,$web_repo);
+                fclose($f_file);
+                $this->setContent($web_repo);
+            } else {
+                $this->setContent(file_get_contents($file));
+            }
+        } elseif( gethostbyname($host) != $host) {
             $content=file_get_contents(NOALYSS_PACKAGE_REPOSITORY."/web.xml");
             $this->content=simplexml_load_string($content);
+            $file=$_ENV['TMP']."/web.xml";
+            $f_file= fopen($file,"w+");
+            fwrite($f_file,$this->content->saveXML());
+            fclose($f_file);
         } else {
             $this->content=NULL;
         }
 
+
+
+    }
+    /**
+     * @return int
+     */
+    public static function getTimeCacheSecond(): int
+    {
+        return self::$time_cache_second;
+    }
+
+    /**
+     * @param int $time_cache_second
+     */
+    public static function setTimeCacheSecond(int $time_cache_second):void
+    {
+        self::$time_cache_second = $time_cache_second;
+
     }
 
+
     public function getContent()
     {
         return $this->content;
diff --git a/unit-test/include/class/package_repositoryTest.php 
b/unit-test/include/class/package_repositoryTest.php
new file mode 100644
index 000000000..f16646e0a
--- /dev/null
+++ b/unit-test/include/class/package_repositoryTest.php
@@ -0,0 +1,175 @@
+<?php
+
+/*
+ * * Copyright (C) 2022 Dany De Bontridder <dany@alchimerys.be>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * 
+ * Author : Dany De Bontridder danydb@noalyss.eu $(DATE)
+ */
+
+/**
+ * @file
+ * @brief noalyss
+ */
+
+use PHPUnit\Framework\TestCase;
+
+require DIRTEST . '/global.php';
+
+/**
+ * @testdox Class Package_RepositoryTest : used for ...
+ * @backupGlobals enabled
+ * @coversDefaultClass
+ */
+class Package_RepositoryTest extends TestCase
+{
+
+    /**
+     * @var Fiche
+     */
+    protected $object;
+    protected $connection;
+
+    /**
+     * Sets up the fixture, for example, opens a network connection.
+     * This method is called before a test method is executed.
+     */
+    protected function setUp(): void
+    {
+
+
+    }
+
+    /**
+     * Tears down the fixture, for example, closes a network connection.
+     * This method is called after a test method is executed.
+     */
+    protected function tearDown(): void
+    {
+        /**
+         * example
+         * if ( ! is_object($this->object->fiche_def)) return;
+         * include_once DIRTEST.'/global.php';
+         * $g_connection=Dossier::connect();
+         * $sql=new ArrayObject();
+         * $sql->append("delete from fiche_detail where f_id in (select f_id 
from fiche where fd_id =\$1 )");
+         * $sql->append("delete from fiche where f_id not in (select f_id from 
fiche_detail where \$1=\$1)");
+         * $sql->append("delete from jnt_fic_attr where fd_id  = \$1 ");
+         * $sql->append("delete from fiche_def where fd_id = \$1");
+         * foreach ($sql as $s) {
+         * $g_connection->exec_sql($s,[$this->object->fiche_def->id]);
+         * }
+         */
+    }
+
+    /**
+     * the setUpBeforeClass() template methods is called before the first test 
of the test case
+     *  class is run
+     */
+    public static function setUpBeforeClass(): void
+    {
+        //        include 'global.php';
+    }
+
+    /**
+     *  tearDownAfterClass() template methods is calleafter the last test of 
the test case class is run,
+     *
+     */
+    static function tearDownAfterClass(): void
+    {
+        //        include 'global.php';
+    }
+    /**
+     * @testdox Test constructor
+     * @covers       Package_Repository::__construct
+     * @backupGlobals enabled
+     */
+    function testConstruct()
+    {
+        $web_xml = $_ENV['TMP'] . "/web.xml";
+        if ( file_exists($web_xml) )        unlink($web_xml);
+
+        $rapav_repository = new Package_Repository();
+        $this->assertFileExists($web_xml, $_ENV['TMP'] . "/web.xml not 
loaded");
+        $this->assertGreaterThan(18000, filesize($web_xml), " web.xml 
truncated");
+
+
+    }
+
+    /**
+     * @testdox Test the cache
+     * @covers       Package_Repository::__construct
+     * @backupGlobals enabled
+     * */
+    function testCache()
+    {
+        $web_xml = $_ENV['TMP'] . "/web.xml";
+        $rapav_repository = new Package_Repository();
+        $this->assertFileExists($web_xml, $_ENV['TMP'] . "/web.xml not 
loaded");
+        clearstatcache(true, $web_xml);
+        $time = filemtime($web_xml);
+        sleep(2);
+        $rapav_repository2 = new Package_Repository();
+        clearstatcache(true, $web_xml);
+        $time2 = filemtime($web_xml);
+        $this->assertEquals($time, $time2, " web.xml not cached");
+
+        Package_Repository::setTimeCacheSecond(5);
+
+        sleep(10);
+
+        $this->assertEquals(Package_Repository::getTimeCacheSecond(), 5, 
"cache not set correctly");
+        $rapav_repository3 = new Package_Repository();
+
+        clearstatcache(true, $web_xml);
+        $time3 = filemtime($web_xml);
+        $this->assertNotEquals($time, $time3, " web.xml not refreshed");
+    }
+
+    /**
+     * @testdox Find a Template
+     * @covers Package_Repository::find_template
+     * @backupGlobals enabled
+     */
+    function testfindTemplate()
+    {
+        $repository=new \Package_Repository();
+        $report=$repository->find_template("mod1");
+        fwrite(STDERR,"[  ".get_class($report)."]");
+        $this->assertTrue(get_class($report)=="SimpleXMLElement","invalid 
return");
+
+        $report=$repository->find_template("FRBIL01XXXXX");
+        $this->assertTrue(empty($report),"unnkown db template must return 
NULL");
+
+    }/**
+     * @testdox Find a Plugin
+     * @covers Package_Repository::find_plugin(
+     * @backupGlobals enabled
+     */
+    function testfindPlugin()
+    {
+        $repository=new \Package_Repository();
+        $report=$repository->find_plugin("NDER");
+        fwrite(STDERR,"[  ".get_class($report)."]");
+        $this->assertTrue(get_class($report)=="SimpleXMLElement","invalid 
return");
+
+        $report=$repository->find_plugin("FRBIL01XXXXX");
+        $this->assertTrue(empty($report),"unnkown Plugin must return NULL");
+
+    }
+
+}
\ No newline at end of file



reply via email to

[Prev in Thread] Current Thread [Next in Thread]